Integrate commons-maven to eliminate classpath text file based solution

This commit is contained in:
Alex Boyko
2016-10-25 17:15:25 -04:00
parent ea6079bea2
commit 5ea5b5cb28
47 changed files with 1315 additions and 1002 deletions

View File

@@ -1,79 +1,84 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>commons-language-server</artifactId>
<name>commons-language-server</name>
<description>Shared utilities for building vscode language servers in Java</description>
<parent>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-java</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Java implementation of VS Code language server protocol -->
<dependency>
<groupId>io.typefox.lsapi</groupId>
<artifactId>io.typefox.lsapi</artifactId>
<version>${lsapi-version}</version>
</dependency>
<dependency>
<groupId>io.typefox.lsapi</groupId>
<artifactId>io.typefox.lsapi.services</artifactId>
<version>${lsapi-version}</version>
</dependency>
<dependency>
<groupId>io.typefox.lsapi</groupId>
<artifactId>io.typefox.lsapi.annotations</artifactId>
<version>${lsapi-version}</version>
</dependency>
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>language-server-test-harness</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>commons-language-server</artifactId>
<name>commons-language-server</name>
<description>Shared utilities for building vscode language servers in Java</description>
<parent>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-java</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>commons-maven</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Java implementation of VS Code language server protocol -->
<dependency>
<groupId>io.typefox.lsapi</groupId>
<artifactId>io.typefox.lsapi</artifactId>
<version>${lsapi-version}</version>
</dependency>
<dependency>
<groupId>io.typefox.lsapi</groupId>
<artifactId>io.typefox.lsapi.services</artifactId>
<version>${lsapi-version}</version>
</dependency>
<dependency>
<groupId>io.typefox.lsapi</groupId>
<artifactId>io.typefox.lsapi.annotations</artifactId>
<version>${lsapi-version}</version>
</dependency>
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.springframework.ide.vscode</groupId>
<artifactId>language-server-test-harness</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,49 +1,24 @@
package org.springframework.ide.vscode.commons.languageserver.java;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
import org.springframework.ide.vscode.commons.util.StringUtil;
public class DefaultJavaProjectFinder implements JavaProjectFinder {
private final String CLASSPATH_FILE_NAME;
public DefaultJavaProjectFinder(String classpathFileName) {
this.CLASSPATH_FILE_NAME = classpathFileName;
}
private File findClasspathFile(File file) {
if (file!=null && file.exists()) {
File cpFile = new File(file, CLASSPATH_FILE_NAME);
if (cpFile.isFile()) {
return cpFile;
} else {
return findClasspathFile(file.getParentFile());
}
}
return null;
}
@Override
public IJavaProject find(IDocument d) {
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 = findClasspathFile(file);
if (cpFile!=null) {
return new JavaProjectWithClasspathFile(cpFile);
}
} catch (URISyntaxException | IllegalArgumentException e) {
//garbage data. Ignore it.
}
}
return null;
}
}
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;
public class DefaultJavaProjectFinder implements JavaProjectFinder {
private final IJavaProjectFinderStrategy[] STRATEGIES = new IJavaProjectFinderStrategy[] {
new JavaProjectWithClasspathFileFinderStrategy(),
new MavenProjectFinderStrategy()
};
@Override
public IJavaProject find(IDocument d) {
for (IJavaProjectFinderStrategy strategy : STRATEGIES) {
try {
return strategy.find(d);
} catch (Throwable t) {
// Log perhaps?
}
}
return null;
}
}

View File

@@ -0,0 +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);
}

View File

@@ -1,10 +1,10 @@
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;
@FunctionalInterface
public interface JavaProjectFinder {
JavaProjectFinder DEFAULT = new DefaultJavaProjectFinder("classpath.txt");
IJavaProject find(IDocument doc);
}
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;
@FunctionalInterface
public interface JavaProjectFinder {
JavaProjectFinder DEFAULT = new DefaultJavaProjectFinder();
IJavaProject find(IDocument doc);
}

View File

@@ -1,75 +0,0 @@
package org.springframework.ide.vscode.commons.languageserver.java;
import java.io.File;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.util.HtmlSnippet;
public class JavaProjectWithClasspathFile implements IJavaProject {
private File cpFile;
public JavaProjectWithClasspathFile(File cpFile) {
this.cpFile = cpFile;
}
@Override
public String getElementName() {
return cpFile.getParentFile().getName();
}
@Override
public HtmlSnippet getJavaDoc() {
return null;
}
@Override
public boolean exists() {
return cpFile.exists();
}
@Override
public IType findType(String fqName) {
//TODO: implement
return null;
}
@Override
public IClasspath getClasspath() {
return () -> cpFile.toPath();
}
@Override
public String toString() {
return "JavaProjectWithClasspathFile("+cpFile+")";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cpFile == null) ? 0 : cpFile.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
JavaProjectWithClasspathFile other = (JavaProjectWithClasspathFile) obj;
if (cpFile == null) {
if (other.cpFile != null)
return false;
} else if (!cpFile.equals(other.cpFile))
return false;
return true;
}
}

View File

@@ -0,0 +1,44 @@
/*******************************************************************************
* 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 java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
import org.springframework.ide.vscode.commons.maven.MavenCore;
import org.springframework.ide.vscode.commons.maven.java.classpathfile.JavaProjectWithClasspathFile;
import org.springframework.ide.vscode.commons.util.FileUtils;
import org.springframework.ide.vscode.commons.util.StringUtil;
public class JavaProjectWithClasspathFileFinderStrategy implements IJavaProjectFinderStrategy {
@Override
public JavaProjectWithClasspathFile find(IDocument d) {
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) {
return new JavaProjectWithClasspathFile(cpFile);
}
} catch (URISyntaxException | IllegalArgumentException e) {
//garbage data. Ignore it.
}
}
return null;
}
}

View File

@@ -0,0 +1,52 @@
/*******************************************************************************
* 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 java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
import org.springframework.ide.vscode.commons.maven.MavenCore;
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;
/**
* Finds Maven Project based
*
* @author Alex Boyko
*
*/
public class MavenProjectFinderStrategy implements IJavaProjectFinderStrategy {
@Override
public MavenJavaProject find(IDocument d) {
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) {
return new MavenJavaProject(pomFile);
}
} catch (URISyntaxException | IllegalArgumentException e) {
//garbage data. Ignore it.
} catch (Exception e) {
//TODO: Erroneous Pom file. Ignore it? Log?
}
}
return null;
}
}