From 53e31991bf8057029ca0cc2a7d656bb43438d393 Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Mon, 31 Oct 2016 20:23:51 -0400 Subject: [PATCH] Cache projects --- .../java/DefaultJavaProjectFinder.java | 5 +- .../java/IJavaProjectFinderStrategy.java | 54 +++++++++---------- ...rojectWithClasspathFileFinderStrategy.java | 25 +++++---- .../java/MavenProjectFinderStrategy.java | 27 +++++----- 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/DefaultJavaProjectFinder.java b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/DefaultJavaProjectFinder.java index 5e30bee0e..8503c514d 100644 --- a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/DefaultJavaProjectFinder.java +++ b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/DefaultJavaProjectFinder.java @@ -2,6 +2,7 @@ package org.springframework.ide.vscode.commons.languageserver.java; import org.springframework.ide.vscode.commons.java.IJavaProject; import org.springframework.ide.vscode.commons.languageserver.util.IDocument; +import org.springframework.ide.vscode.commons.util.Log; public class DefaultJavaProjectFinder implements JavaProjectFinder { @@ -18,8 +19,8 @@ public class DefaultJavaProjectFinder implements JavaProjectFinder { if (project != null) { return project; } - } catch (Throwable t) { - // Log perhaps? + } catch (Exception e) { + Log.log(e); } } return null; diff --git a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/IJavaProjectFinderStrategy.java b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/IJavaProjectFinderStrategy.java index 5ab571f76..a5af54dfa 100644 --- a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/IJavaProjectFinderStrategy.java +++ b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/IJavaProjectFinderStrategy.java @@ -1,27 +1,27 @@ -/******************************************************************************* - * Copyright (c) 2016 Pivotal, Inc. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Pivotal, Inc. - initial API and implementation - *******************************************************************************/ -package org.springframework.ide.vscode.commons.languageserver.java; - -import org.springframework.ide.vscode.commons.java.IJavaProject; -import org.springframework.ide.vscode.commons.languageserver.util.IDocument; - -/** - * Strategy foe finding Java project for a document - * - * @author Alex Boyko - * - */ -@FunctionalInterface -public interface IJavaProjectFinderStrategy { - - IJavaProject find(IDocument document); - -} +/******************************************************************************* + * Copyright (c) 2016 Pivotal, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.vscode.commons.languageserver.java; + +import org.springframework.ide.vscode.commons.java.IJavaProject; +import org.springframework.ide.vscode.commons.languageserver.util.IDocument; + +/** + * Strategy foe finding Java project for a document + * + * @author Alex Boyko + * + */ +@FunctionalInterface +public interface IJavaProjectFinderStrategy { + + IJavaProject find(IDocument document) throws Exception; + +} diff --git a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/JavaProjectWithClasspathFileFinderStrategy.java b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/JavaProjectWithClasspathFileFinderStrategy.java index 89fa77d8d..8603ba5d3 100644 --- a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/JavaProjectWithClasspathFileFinderStrategy.java +++ b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/JavaProjectWithClasspathFileFinderStrategy.java @@ -13,6 +13,7 @@ package org.springframework.ide.vscode.commons.languageserver.java; import java.io.File; import java.net.URI; import java.net.URISyntaxException; +import java.util.concurrent.ExecutionException; import org.springframework.ide.vscode.commons.languageserver.util.IDocument; import org.springframework.ide.vscode.commons.maven.MavenCore; @@ -20,22 +21,26 @@ import org.springframework.ide.vscode.commons.maven.java.classpathfile.JavaProje import org.springframework.ide.vscode.commons.util.FileUtils; import org.springframework.ide.vscode.commons.util.StringUtil; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + public class JavaProjectWithClasspathFileFinderStrategy implements IJavaProjectFinderStrategy { + public Cache cache = CacheBuilder.newBuilder().build(); + @Override - public JavaProjectWithClasspathFile find(IDocument d) { + public JavaProjectWithClasspathFile find(IDocument d) throws ExecutionException, URISyntaxException { String uriStr = d.getUri(); if (StringUtil.hasText(uriStr)) { - try { - URI uri = new URI(uriStr); - //TODO: This only work with File uri. Should it work with others too? - File file = new File(uri).getAbsoluteFile(); - File cpFile = FileUtils.findFile(file, MavenCore.CLASSPATH_TXT); - if (cpFile!=null) { + URI uri = new URI(uriStr); + // TODO: This only work with File uri. Should it work with others + // too? + File file = new File(uri).getAbsoluteFile(); + File cpFile = FileUtils.findFile(file, MavenCore.CLASSPATH_TXT); + if (cpFile != null) { + return cache.get(cpFile, () -> { return new JavaProjectWithClasspathFile(cpFile); - } - } catch (URISyntaxException | IllegalArgumentException e) { - //garbage data. Ignore it. + }); } } return null; diff --git a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/MavenProjectFinderStrategy.java b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/MavenProjectFinderStrategy.java index 3822d369c..8f5c85429 100644 --- a/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/MavenProjectFinderStrategy.java +++ b/vscode-extensions/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/java/MavenProjectFinderStrategy.java @@ -13,6 +13,7 @@ package org.springframework.ide.vscode.commons.languageserver.java; import java.io.File; import java.net.URI; import java.net.URISyntaxException; +import java.util.concurrent.ExecutionException; import org.springframework.ide.vscode.commons.languageserver.util.IDocument; import org.springframework.ide.vscode.commons.maven.MavenCore; @@ -20,6 +21,9 @@ import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject; import org.springframework.ide.vscode.commons.util.FileUtils; import org.springframework.ide.vscode.commons.util.StringUtil; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + /** * Finds Maven Project based * @@ -28,22 +32,21 @@ import org.springframework.ide.vscode.commons.util.StringUtil; */ public class MavenProjectFinderStrategy implements IJavaProjectFinderStrategy { + public Cache cache = CacheBuilder.newBuilder().build(); + @Override - public MavenJavaProject find(IDocument d) { + public MavenJavaProject find(IDocument d) throws ExecutionException, URISyntaxException { String uriStr = d.getUri(); if (StringUtil.hasText(uriStr)) { - try { - URI uri = new URI(uriStr); - //TODO: This only work with File uri. Should it work with others too? - File file = new File(uri).getAbsoluteFile(); - File pomFile = FileUtils.findFile(file, MavenCore.POM_XML); - if (pomFile!=null) { + URI uri = new URI(uriStr); + // TODO: This only work with File uri. Should it work with others + // too? + File file = new File(uri).getAbsoluteFile(); + File pomFile = FileUtils.findFile(file, MavenCore.POM_XML); + if (pomFile != null) { + return cache.get(pomFile, () -> { return new MavenJavaProject(pomFile); - } - } catch (URISyntaxException | IllegalArgumentException e) { - //garbage data. Ignore it. - } catch (Exception e) { - //TODO: Erroneous Pom file. Ignore it? Log? + }); } } return null;