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.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;

View File

@@ -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;
}

View File

@@ -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<File, JavaProjectWithClasspathFile> 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;

View File

@@ -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<File, MavenJavaProject> 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;