Author: fbricon
Date: 2012-08-24 09:22:27 -0400 (Fri, 24 Aug 2012)
New Revision: 43221
Added:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/AbstractArtifactIdentifier.java
Modified:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/NexusRepository.java
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java
Log:
JBIDE-8973 : don't discard identified file if sources are missing
Modified:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/NexusRepository.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/NexusRepository.java 2012-08-24
13:15:06 UTC (rev 43220)
+++
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/NexusRepository.java 2012-08-24
13:22:27 UTC (rev 43221)
@@ -10,6 +10,12 @@
************************************************************************************/
package org.jboss.tools.maven.sourcelookup;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import org.eclipse.core.runtime.Assert;
+
+
/**
*
* @author snjeza
@@ -20,7 +26,8 @@
private String url;
private boolean enabled;
-
+ private static final String PATH_SEPARATOR = "/";
+
public NexusRepository() {
super();
}
@@ -61,4 +68,44 @@
return "NexusRepository [name=" + name + ", url=" + url + ",
enabled="
+ enabled + "]";
}
+
+ public String getSearchUrl(String sha1) throws UnsupportedEncodingException {
+ // "https://repository.jboss.org/nexus/service/local/data_index?sha1=";
+ Assert.isNotNull(sha1);
+ StringBuilder searchUrl = getBaseUrl()
+ .append("service/local/data_index?sha1=")
+ .append(URLEncoder.encode(sha1, "UTF-8"));
+ return searchUrl.toString();
+ }
+
+ public String getSearchUrl(String groupId, String artifactId, String version, String
classifier) throws UnsupportedEncodingException {
+ Assert.isNotNull(artifactId);
+ StringBuilder searchUrl = getBaseUrl().append("service/local/data_index?");
+ searchUrl.append("a=").append(URLEncoder.encode(artifactId,
"UTF-8")).append("&");
+ if (groupId != null) {
+ searchUrl.append("g=").append(URLEncoder.encode(groupId,
"UTF-8")).append("&");
+ }
+ if (version != null) {
+ searchUrl.append("v=").append(URLEncoder.encode(version,
"UTF-8")).append("&");
+ }
+ if (classifier != null) {
+ searchUrl.append("c=").append(URLEncoder.encode(classifier,
"UTF-8"));
+ }
+ return searchUrl.toString();
+ }
+
+ private StringBuilder getBaseUrl() {
+ StringBuilder sb = new StringBuilder();
+ String base = getUrl();
+ sb.append(base);
+ if (!base.endsWith(PATH_SEPARATOR)) {
+ sb.append(PATH_SEPARATOR);
+ }
+ return sb;
+ }
+
+
+
+
+
}
Added:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/AbstractArtifactIdentifier.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/AbstractArtifactIdentifier.java
(rev 0)
+++
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/AbstractArtifactIdentifier.java 2012-08-24
13:22:27 UTC (rev 43221)
@@ -0,0 +1,40 @@
+/*************************************************************************************
+ * Copyright (c) 2012 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 v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss by Red Hat - Initial implementation.
+ ************************************************************************************/
+package org.jboss.tools.maven.sourcelookup.internal.identification;
+
+import org.jboss.tools.maven.sourcelookup.identification.ArtifactIdentifier;
+
+abstract class AbstractArtifactIdentifier implements ArtifactIdentifier {
+
+ private String name;
+
+ AbstractArtifactIdentifier() {
+ }
+
+ AbstractArtifactIdentifier(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ if (name == null) {
+ name = getClass().getSimpleName();
+ }
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String toString() {
+ return (name == null)? super.toString():name;
+ }
+}
Modified:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java 2012-08-24
13:15:06 UTC (rev 43220)
+++
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java 2012-08-24
13:22:27 UTC (rev 43221)
@@ -14,8 +14,10 @@
import java.util.ArrayList;
import java.util.List;
+import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.jboss.tools.maven.sourcelookup.identification.ArtifactIdentifier;
import org.jboss.tools.maven.sourcelookup.identification.IFileIdentificationManager;
@@ -33,30 +35,60 @@
initArtifactIdentifiers();
}
- private void initArtifactIdentifiers() {
+ protected void initArtifactIdentifiers() {
//TODO read from extension points?
- artifactIdentifiers = new ArrayList<ArtifactIdentifier>(3);
- artifactIdentifiers.add(new MavenPropertiesIdentifier());
- artifactIdentifiers.add(new NexusIndexIdentifier());
- artifactIdentifiers.add(new NexusRepositoryIdentifier());
+ addArtifactIdentifier(new MavenPropertiesIdentifier());
+ addArtifactIdentifier(new NexusIndexIdentifier());
+ addArtifactIdentifier(new NexusRepositoryIdentifier());
}
+ public synchronized void addArtifactIdentifier(ArtifactIdentifier identifier) {
+ Assert.isNotNull(identifier, "Artifact identifier can not be null");
+ if (artifactIdentifiers == null) {
+ artifactIdentifiers = new ArrayList<ArtifactIdentifier>();
+ }
+ artifactIdentifiers.add(identifier);
+ //System.err.println("Added "+ identifier);
+ }
+
+ public synchronized void removeArtifactIdentifier(ArtifactIdentifier identifier) {
+ if (identifier != null) {
+ getArtifactIdentifiers().remove(identifier);
+ }
+ }
+
+ protected List<ArtifactIdentifier> getArtifactIdentifiers() {
+ if (artifactIdentifiers == null) {
+ initArtifactIdentifiers();
+ }
+ return artifactIdentifiers;
+ }
+
@Override
public ArtifactKey identify(File file, IProgressMonitor monitor) throws CoreException {
+ if (file == null) {
+ return null;
+ }
+ if (monitor == null) {
+ monitor = new NullProgressMonitor();
+ }
ArtifactKey artifactKey = null;
- long start = System.currentTimeMillis();
+ //long start = System.currentTimeMillis();
for (ArtifactIdentifier identifier : artifactIdentifiers) {
if (monitor.isCanceled()) {
return null;
}
artifactKey = identifier.identify(file);
if (artifactKey != null) {
- long stop = System.currentTimeMillis();
- System.err.println(file.getName() + " identified as " + artifactKey +
" in " + (stop-start) + " ms");
+ //long stop = System.currentTimeMillis();
+ //System.err.println(file.getName() + " identified as " + artifactKey +
" in " + (stop-start) + " ms");
break;
}
}
+ //if (artifactKey == null)
+ //System.err.println("Could not identify "+file);
return artifactKey;
}
+
}
Modified:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java 2012-08-24
13:15:06 UTC (rev 43220)
+++
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java 2012-08-24
13:22:27 UTC (rev 43221)
@@ -1,3 +1,13 @@
+/*************************************************************************************
+ * Copyright (c) 2012 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 v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss by Red Hat - Initial implementation.
+ ************************************************************************************/
package org.jboss.tools.maven.sourcelookup.internal.identification;
import java.io.File;
@@ -10,27 +20,25 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.m2e.core.embedder.ArtifactKey;
-import org.jboss.tools.maven.sourcelookup.identification.ArtifactIdentifier;
-public class MavenPropertiesIdentifier implements ArtifactIdentifier {
+public class MavenPropertiesIdentifier extends AbstractArtifactIdentifier {
+ public MavenPropertiesIdentifier() {
+ super("Maven Properties identifier");
+ }
+
@Override
public ArtifactKey identify(File file) throws CoreException {
ZipFile jar;
try {
-// try {
-// Random r = new Random();
-// Thread.sleep(r.nextInt(10)*1000);
-// } catch (InterruptedException e) {
-// }
jar = new ZipFile(file);
return getArtifactFromMetaInf(jar);
} catch (ZipException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
+ //System.err.println(getName() + " could not identify "+file);
return null;
}
Modified:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java 2012-08-24
13:15:06 UTC (rev 43220)
+++
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java 2012-08-24
13:22:27 UTC (rev 43221)
@@ -1,3 +1,13 @@
+/*************************************************************************************
+ * Copyright (c) 2012 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 v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss by Red Hat - Initial implementation.
+ ************************************************************************************/
package org.jboss.tools.maven.sourcelookup.internal.identification;
import java.io.File;
@@ -14,14 +24,14 @@
import org.eclipse.m2e.core.internal.index.nexus.NexusIndexManager;
import org.eclipse.m2e.core.repository.IRepository;
import org.eclipse.m2e.core.repository.IRepositoryRegistry;
-import org.jboss.tools.maven.sourcelookup.identification.ArtifactIdentifier;
@SuppressWarnings("restriction")
-public class NexusIndexIdentifier implements ArtifactIdentifier {
+public class NexusIndexIdentifier extends AbstractArtifactIdentifier {
private List<IRepository> globalRepositories;
-
+
public NexusIndexIdentifier() {
+ super("Nexus Index identifier");
globalRepositories = initGlobalRepositories();
}
@@ -61,6 +71,7 @@
}
}
}
+ //System.err.println(getName() + " could not identify "+file);
return artifact;
}
Modified:
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java 2012-08-24
13:15:06 UTC (rev 43220)
+++
trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java 2012-08-24
13:22:27 UTC (rev 43221)
@@ -1,12 +1,21 @@
+/*************************************************************************************
+ * Copyright (c) 2012 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 v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss by Red Hat - Initial implementation.
+ ************************************************************************************/
package org.jboss.tools.maven.sourcelookup.internal.identification;
import static
org.jboss.tools.maven.sourcelookup.identification.IdentificationUtil.getSHA1;
-import static
org.jboss.tools.maven.sourcelookup.identification.IdentificationUtil.getSourcesClassifier;
import java.io.File;
+import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
-import java.net.URLEncoder;
import java.util.Set;
import javax.xml.bind.JAXBContext;
@@ -16,20 +25,21 @@
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.jboss.tools.maven.sourcelookup.NexusRepository;
import org.jboss.tools.maven.sourcelookup.SourceLookupActivator;
-import org.jboss.tools.maven.sourcelookup.identification.ArtifactIdentifier;
import org.sonatype.nexus.rest.model.NexusArtifact;
import org.sonatype.nexus.rest.model.SearchResponse;
-public class NexusRepositoryIdentifier implements ArtifactIdentifier {
+public class NexusRepositoryIdentifier extends AbstractArtifactIdentifier {
- private static final String PATH_SEPARATOR = "/";
-
+ public NexusRepositoryIdentifier() {
+ super("Nexus repository identifier");
+ }
+
@Override
public ArtifactKey identify(File file) throws CoreException {
return getArtifactFromRemoteNexusRepository(file);
}
- private static ArtifactKey getArtifactFromRemoteNexusRepository(File file) {
+ private ArtifactKey getArtifactFromRemoteNexusRepository(File file) {
String sha1;
try {
sha1 = getSHA1(file);
@@ -42,55 +52,52 @@
if (!repository.isEnabled()) {
continue;
}
- ArtifactKey key = getArtifactFromRemoteNexusRepository(sha1, repository);
- if (key != null) {
- ArtifactKey sourcesArtifact = new ArtifactKey(
- key.getGroupId(), key.getArtifactId(),
- key.getVersion(),
- getSourcesClassifier(key.getClassifier()));
- ArtifactKey resolvedKey = getSourcesArtifactFromJBossNexusRepository(sourcesArtifact,
repository);
- if (resolvedKey != null) {
+ try {
+ ArtifactKey key =
searchArtifactFromRemoteNexusRepository(repository.getSearchUrl(sha1));
+ if (key != null) {
+ /*
+ Searching for sources here makes no sense here :
+ ex : guice.jar (from seam 2.3) is found in jboss nexus via a SHA1 search but
subsequent search
+ of its sources on this repo returns null => guice.jar can never be identified in
that case!
+ Source resolution / Missing sources should be dealt upstream from here
+
+ String searchSourcesUrl = repository.getSearchUrl(key.getArtifactId(),
+ key.getGroupId(),
+ key.getVersion(),
+ getSourcesClassifier(key.getClassifier()));
+
+ ArtifactKey resolvedKey =
searchArtifactFromRemoteNexusRepository(searchSourcesUrl);
+
+ if (resolvedKey != null) {
+ return key;
+ }
+ */
return key;
}
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
}
}
+ System.err.println(getName()+ "Couldn't find match for SHA1="+sha1);
return null;
}
- private static ArtifactKey getArtifactFromRemoteNexusRepository(String sha1,
- NexusRepository nexusRepository) {
- if (sha1 == null || nexusRepository == null || nexusRepository.getUrl() == null) {
+ private static ArtifactKey searchArtifactFromRemoteNexusRepository(String searchUrl) {
+ if (searchUrl == null) {
return null;
}
- HttpURLConnection connection = null;
+ HttpURLConnection connection = null;//TODO use eclipse connections to handle proxies
try {
- String base = nexusRepository.getUrl();
- if (!base.endsWith(PATH_SEPARATOR)) {
- base = base + PATH_SEPARATOR;
- }
- // String url =
- // "https://repository.jboss.org/nexus/service/local/data_index?sha1=";
- String url = base + "service/local/data_index?sha1=";
- url = url + URLEncoder.encode(sha1, "UTF-8");
JAXBContext context = JAXBContext.newInstance(SearchResponse.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
- connection = (HttpURLConnection) new URL(url).openConnection();
+ connection = (HttpURLConnection) new URL(searchUrl).openConnection();
connection.connect();
Object object = unmarshaller.unmarshal(connection.getInputStream());
if (object instanceof SearchResponse) {
- SearchResponse searchResponse = (SearchResponse) object;
- for (NexusArtifact nexusArtifact : searchResponse.getData()) {
- String groupId = nexusArtifact.getGroupId();
- String artifactId = nexusArtifact.getArtifactId();
- String version = nexusArtifact.getVersion();
- String classifier = nexusArtifact.getClassifier();
- ArtifactKey artifact = new ArtifactKey(groupId, artifactId,
- version, classifier);
- return artifact;
- }
+ return extractArtifactKey((SearchResponse)object);
}
} catch (Exception e) {
- return null;
+ e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
@@ -99,49 +106,16 @@
return null;
}
- private static ArtifactKey getSourcesArtifactFromJBossNexusRepository(ArtifactKey key,
- NexusRepository nexusRepository) {
- if (key == null || nexusRepository == null
- || nexusRepository.getUrl() == null) {
- return null;
+ private static ArtifactKey extractArtifactKey(SearchResponse searchResponse) {
+ for (NexusArtifact nexusArtifact : searchResponse.getData()) {
+ String groupId = nexusArtifact.getGroupId();
+ String artifactId = nexusArtifact.getArtifactId();
+ String version = nexusArtifact.getVersion();
+ String classifier = nexusArtifact.getClassifier();
+ ArtifactKey artifact = new ArtifactKey(groupId, artifactId,
+ version, classifier);
+ return artifact;
}
- HttpURLConnection connection = null;
- try {
- String base = nexusRepository.getUrl();
- if (!base.endsWith(PATH_SEPARATOR)) {
- base = base + PATH_SEPARATOR;
- }
- // String url =
- //
"https://repository.jboss.org/nexus/service/local/data_index?g=groupId&a=artifactId&v=version&c=classifier";
- String url = base + "service/local/data_index?";
- url= url + "g=" + URLEncoder.encode(key.getGroupId(), "UTF-8") +
"&";
- url= url + "a=" + URLEncoder.encode(key.getArtifactId(), "UTF-8")
+ "&";
- url= url + "v=" + URLEncoder.encode(key.getVersion(), "UTF-8") +
"&";
- url= url + "c=" + URLEncoder.encode(key.getClassifier(),
"UTF-8");
- JAXBContext context = JAXBContext.newInstance(SearchResponse.class);
- Unmarshaller unmarshaller = context.createUnmarshaller();
- connection = (HttpURLConnection) new URL(url).openConnection();
- connection.connect();
- Object object = unmarshaller.unmarshal(connection.getInputStream());
- if (object instanceof SearchResponse) {
- SearchResponse searchResponse = (SearchResponse) object;
- for (NexusArtifact nexusArtifact : searchResponse.getData()) {
- String groupId = nexusArtifact.getGroupId();
- String artifactId = nexusArtifact.getArtifactId();
- String version = nexusArtifact.getVersion();
- String classifier = nexusArtifact.getClassifier();
- ArtifactKey artifact = new ArtifactKey(groupId, artifactId,
- version, classifier);
- return artifact;
- }
- }
- } catch (Exception e) {
- return null;
- } finally {
- if (connection != null) {
- connection.disconnect();
- }
- }
return null;
}