-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixes redhat-developer/vscode-java#1664 - Download maven-central artifact's sources on-demand Signed-off-by: Fred Bricon <fbricon@gmail.com> [rgrunber@redhat.com]: Fix testDynamicSourceLookups() - Move testcase from HoverHandlerTest to InvisibleProjectBuildSupportTest - Set up the external library after project import to mimic approach in other invisible projects - Create IMavenArtifactIdentifier to be implemented by MavenPropertiesIdentifier and MavenCentralIdentifier Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
- Loading branch information
Showing
17 changed files
with
512 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...e.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IMavenArtifactIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal.managers; | ||
|
||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.m2e.core.embedder.ArtifactKey; | ||
|
||
public interface IMavenArtifactIdentifier { | ||
|
||
public ArtifactKey identify(IPath path, IProgressMonitor monitor); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ISourceDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal.managers; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.jdt.core.IClassFile; | ||
import org.eclipse.jdt.core.IClasspathEntry; | ||
|
||
/** | ||
* Service to automatically discover and attach sources to unknown class files. | ||
* | ||
* @author Fred Bricon | ||
* | ||
*/ | ||
public interface ISourceDownloader { | ||
|
||
/** | ||
* Discovers and attaches sources to the given {@link IClassFile}'s parent | ||
* {@link IClasspathEntry}, if it's a jar file. | ||
* | ||
* @param classFile | ||
* the file to identify and search sources for | ||
* @param monitor | ||
* a progress monitor | ||
* @throws CoreException | ||
*/ | ||
public void discoverSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...pse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenCentralIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2008-2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal.managers; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.ProxySelector; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpClient.Version; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
|
||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.core.runtime.NullProgressMonitor; | ||
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; | ||
import org.eclipse.jdt.ls.internal.gradle.checksums.HashProvider; | ||
import org.eclipse.m2e.core.embedder.ArtifactKey; | ||
import org.eclipse.osgi.util.NLS; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
/** | ||
* @author Fred Bricon | ||
* | ||
* Partially copied from | ||
* https://github.com/jbosstools/jbosstools-central/blob/24e907a07cbf81b9b7a9cafa69bd38b2271878eb/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/MavenCentralIdentifier.java | ||
* | ||
*/ | ||
public class MavenCentralIdentifier implements IMavenArtifactIdentifier { | ||
|
||
private static final String SHA1_SEARCH_QUERY = "https://search.maven.org/solrsearch/select?q=1:%22{0}%22&rows=1&wt=json"; | ||
|
||
private HashProvider hashProvider = new HashProvider(HashProvider.SHA1); | ||
|
||
@Override | ||
public ArtifactKey identify(IPath path, IProgressMonitor monitor) { | ||
if (path == null) { | ||
return null; | ||
} | ||
return identify(path.toFile(), monitor); | ||
} | ||
|
||
private ArtifactKey identify(File file, IProgressMonitor monitor) { | ||
if (file == null || !file.isFile() || !file.canRead()) { | ||
return null; | ||
} | ||
String sha1 = null; | ||
try { | ||
sha1 = hashProvider.getChecksum(file); | ||
} catch (IOException e) { | ||
JavaLanguageServerPlugin.logError("Failed to compute SHA1 checksum for " + file + " : " + e.getMessage()); | ||
return null; | ||
} | ||
return identifySha1(sha1, monitor); | ||
} | ||
|
||
private ArtifactKey identifySha1(String sha1, IProgressMonitor monitor) { | ||
if (sha1 == null || sha1.isBlank()) { | ||
return null; | ||
} | ||
String searchUrl = NLS.bind(SHA1_SEARCH_QUERY, sha1); | ||
try { | ||
return find(searchUrl, monitor); | ||
} catch (Exception e) { | ||
JavaLanguageServerPlugin.logError("Failed to identify " + sha1 + " with Maven Central : " + e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
private ArtifactKey find(String searchUrl, IProgressMonitor monitor) throws IOException, InterruptedException { | ||
Duration timeout = Duration.ofSeconds(10); | ||
HttpClient client = HttpClient.newBuilder() | ||
.connectTimeout(timeout) | ||
.proxy(ProxySelector.getDefault()) | ||
.version(Version.HTTP_2) | ||
.build(); | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.timeout(timeout) | ||
.uri(URI.create(searchUrl)) | ||
.GET() | ||
.build(); | ||
|
||
if (monitor == null) { | ||
monitor = new NullProgressMonitor(); | ||
} | ||
if (monitor.isCanceled()) { | ||
return null; | ||
} | ||
|
||
//TODO implement request cancellation, according to monitor status | ||
HttpResponse<String> response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
JsonElement jsonElement = new JsonParser().parse(response.body()); | ||
if (jsonElement != null && jsonElement.isJsonObject()) { | ||
return extractKey(jsonElement.getAsJsonObject()); | ||
} | ||
return null; | ||
} | ||
|
||
private ArtifactKey extractKey(JsonObject modelNode) { | ||
JsonObject response = modelNode.getAsJsonObject("response"); | ||
if (response != null) { | ||
int num = response.get("numFound").getAsInt(); | ||
if (num > 0) { | ||
JsonArray docs = response.getAsJsonArray("docs"); | ||
String a = null, g = null, v = null; | ||
for (JsonElement d : docs) { | ||
JsonObject o = d.getAsJsonObject(); | ||
a = o.get("a").getAsString(); | ||
g = o.get("g").getAsString(); | ||
v = o.get("v").getAsString(); | ||
if (a != null && g != null && v != null) { | ||
return new ArtifactKey(g, a, v, null); | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.