Cache projects

This commit is contained in:
BoykoAlex
2016-10-31 20:23:51 -04:00
parent bc06755259
commit 53e31991bf
4 changed files with 60 additions and 51 deletions

View File

@@ -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.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument; import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
import org.springframework.ide.vscode.commons.util.Log;
public class DefaultJavaProjectFinder implements JavaProjectFinder { public class DefaultJavaProjectFinder implements JavaProjectFinder {
@@ -18,8 +19,8 @@ public class DefaultJavaProjectFinder implements JavaProjectFinder {
if (project != null) { if (project != null) {
return project; return project;
} }
} catch (Throwable t) { } catch (Exception e) {
// Log perhaps? Log.log(e);
} }
} }
return null; return null;

View File

@@ -22,6 +22,6 @@ import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
@FunctionalInterface @FunctionalInterface
public interface IJavaProjectFinderStrategy { public interface IJavaProjectFinderStrategy {
IJavaProject find(IDocument document); IJavaProject find(IDocument document) throws Exception;
} }

View File

@@ -13,6 +13,7 @@ package org.springframework.ide.vscode.commons.languageserver.java;
import java.io.File; import java.io.File;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument; import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
import org.springframework.ide.vscode.commons.maven.MavenCore; 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.FileUtils;
import org.springframework.ide.vscode.commons.util.StringUtil; 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 class JavaProjectWithClasspathFileFinderStrategy implements IJavaProjectFinderStrategy {
public Cache<File, JavaProjectWithClasspathFile> cache = CacheBuilder.newBuilder().build();
@Override @Override
public JavaProjectWithClasspathFile find(IDocument d) { public JavaProjectWithClasspathFile find(IDocument d) throws ExecutionException, URISyntaxException {
String uriStr = d.getUri(); String uriStr = d.getUri();
if (StringUtil.hasText(uriStr)) { if (StringUtil.hasText(uriStr)) {
try { URI uri = new URI(uriStr);
URI uri = new URI(uriStr); // TODO: This only work with File uri. Should it work with others
//TODO: This only work with File uri. Should it work with others too? // too?
File file = new File(uri).getAbsoluteFile(); File file = new File(uri).getAbsoluteFile();
File cpFile = FileUtils.findFile(file, MavenCore.CLASSPATH_TXT); File cpFile = FileUtils.findFile(file, MavenCore.CLASSPATH_TXT);
if (cpFile!=null) { if (cpFile != null) {
return cache.get(cpFile, () -> {
return new JavaProjectWithClasspathFile(cpFile); return new JavaProjectWithClasspathFile(cpFile);
} });
} catch (URISyntaxException | IllegalArgumentException e) {
//garbage data. Ignore it.
} }
} }
return null; return null;

View File

@@ -13,6 +13,7 @@ package org.springframework.ide.vscode.commons.languageserver.java;
import java.io.File; import java.io.File;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument; import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
import org.springframework.ide.vscode.commons.maven.MavenCore; 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.FileUtils;
import org.springframework.ide.vscode.commons.util.StringUtil; import org.springframework.ide.vscode.commons.util.StringUtil;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
/** /**
* Finds Maven Project based * Finds Maven Project based
* *
@@ -28,22 +32,21 @@ import org.springframework.ide.vscode.commons.util.StringUtil;
*/ */
public class MavenProjectFinderStrategy implements IJavaProjectFinderStrategy { public class MavenProjectFinderStrategy implements IJavaProjectFinderStrategy {
public Cache<File, MavenJavaProject> cache = CacheBuilder.newBuilder().build();
@Override @Override
public MavenJavaProject find(IDocument d) { public MavenJavaProject find(IDocument d) throws ExecutionException, URISyntaxException {
String uriStr = d.getUri(); String uriStr = d.getUri();
if (StringUtil.hasText(uriStr)) { if (StringUtil.hasText(uriStr)) {
try { URI uri = new URI(uriStr);
URI uri = new URI(uriStr); // TODO: This only work with File uri. Should it work with others
//TODO: This only work with File uri. Should it work with others too? // too?
File file = new File(uri).getAbsoluteFile(); File file = new File(uri).getAbsoluteFile();
File pomFile = FileUtils.findFile(file, MavenCore.POM_XML); File pomFile = FileUtils.findFile(file, MavenCore.POM_XML);
if (pomFile!=null) { if (pomFile != null) {
return cache.get(pomFile, () -> {
return new MavenJavaProject(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; return null;