Author: fbricon
Date: 2012-10-11 06:27:06 -0400 (Thu, 11 Oct 2012)
New Revision: 44425
Added:
trunk/maven/plugins/org.jboss.tools.maven.core/lib/jboss-dmr-1.1.1.Final.jar
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/MavenCentralIdentifier.java
trunk/maven/tests/org.jboss.tools.maven.common.test/resources/groovy-jsr223-2.0.4.jar
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/MavenCentralIdentifierTest.java
Modified:
trunk/maven/plugins/org.jboss.tools.maven.core/.classpath
trunk/maven/plugins/org.jboss.tools.maven.core/META-INF/MANIFEST.MF
trunk/maven/plugins/org.jboss.tools.maven.core/build.properties
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/FileIdentificationManager.java
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/AbstractIdentificationTest.java
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/FileIdentificationManagerTest.java
Log:
JBIDE-12702 query
search.maven.org as a last resort to identify jars
Modified: trunk/maven/plugins/org.jboss.tools.maven.core/.classpath
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.core/.classpath 2012-10-11 09:47:00 UTC (rev
44424)
+++ trunk/maven/plugins/org.jboss.tools.maven.core/.classpath 2012-10-11 10:27:06 UTC (rev
44425)
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
+ <classpathentry exported="true" kind="lib"
path="lib/jboss-dmr-1.1.1.Final.jar"/>
<classpathentry exported="true" kind="lib"
path="lib/nexus-indexer-lucene-rest-api-client.jar"/>
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
Modified: trunk/maven/plugins/org.jboss.tools.maven.core/META-INF/MANIFEST.MF
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.core/META-INF/MANIFEST.MF 2012-10-11
09:47:00 UTC (rev 44424)
+++ trunk/maven/plugins/org.jboss.tools.maven.core/META-INF/MANIFEST.MF 2012-10-11
10:27:06 UTC (rev 44425)
@@ -37,4 +37,5 @@
org.jboss.tools.maven.core.repositories,
org.jboss.tools.maven.core.xpl
Bundle-ClassPath: lib/nexus-indexer-lucene-rest-api-client.jar,
+ lib/jboss-dmr-1.1.1.Final.jar,
.
Modified: trunk/maven/plugins/org.jboss.tools.maven.core/build.properties
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.core/build.properties 2012-10-11 09:47:00
UTC (rev 44424)
+++ trunk/maven/plugins/org.jboss.tools.maven.core/build.properties 2012-10-11 10:27:06
UTC (rev 44425)
@@ -6,4 +6,5 @@
poms/,\
plugin.properties,\
about.html,\
- lib/nexus-indexer-lucene-rest-api-client.jar
+ lib/nexus-indexer-lucene-rest-api-client.jar,\
+ lib/jboss-dmr-1.1.1.Final.jar
Added: trunk/maven/plugins/org.jboss.tools.maven.core/lib/jboss-dmr-1.1.1.Final.jar
===================================================================
(Binary files differ)
Property changes on:
trunk/maven/plugins/org.jboss.tools.maven.core/lib/jboss-dmr-1.1.1.Final.jar
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Modified:
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/FileIdentificationManager.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/FileIdentificationManager.java 2012-10-11
09:47:00 UTC (rev 44424)
+++
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/FileIdentificationManager.java 2012-10-11
10:27:06 UTC (rev 44425)
@@ -47,6 +47,9 @@
addArtifactIdentifier(new MavenPropertiesIdentifier());
addArtifactIdentifier(new NexusIndexIdentifier());
addArtifactIdentifier(new NexusRepositoryIdentifier());
+ //JBIDE-12702 Add Maven Central identifier last. Even though it's faster,
+ //we don't want to pound that public service too hard
+ addArtifactIdentifier(new MavenCentralIdentifier());
}
public synchronized void addArtifactIdentifier(ArtifactIdentifier identifier) {
Added:
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/MavenCentralIdentifier.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/MavenCentralIdentifier.java
(rev 0)
+++
trunk/maven/plugins/org.jboss.tools.maven.core/src/org/jboss/tools/maven/core/internal/identification/MavenCentralIdentifier.java 2012-10-11
10:27:06 UTC (rev 44425)
@@ -0,0 +1,171 @@
+/*************************************************************************************
+ * Copyright (c) 2008-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.core.internal.identification;
+
+import static org.jboss.tools.maven.core.identification.IdentificationUtil.getSHA1;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.UnknownHostException;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.codehaus.plexus.util.IOUtil;
+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.eclipse.osgi.util.NLS;
+import org.jboss.dmr.ModelNode;
+
+public class MavenCentralIdentifier extends AbstractArtifactIdentifier {
+
+
+ private String SHA1_SEARCH_QUERY =
"http://search.maven.org/solrsearch/select?q=1:%22{0}%22&rows=1&wt=json";
+
+ private String NAME_VERSION_SEARCH_QUERY =
"http://search.maven.org/solrsearch/select?q=a:%22{0}%22%20AND%20v:%22{1}%22&rows=2&wt=json";
+
+ @Deprecated
+ public ArtifactKey identify(File file) throws CoreException {
+ return identify(file, null);
+ }
+
+ public ArtifactKey identify(File file, IProgressMonitor monitor) throws CoreException {
+ ArtifactKey key = sha1Search(file, monitor);
+ if (key == null) {
+ System.out.println("Can't identify "+file + " falling back on
name+version search" );
+ key = nameVersionSearch(file, monitor);
+ }
+ return key;
+ }
+
+ private ArtifactKey sha1Search(File file, IProgressMonitor monitor) {
+ if (monitor == null) {
+ monitor = new NullProgressMonitor();
+ }
+ if (monitor.isCanceled()) {
+ return null;
+ }
+ String sha1;
+ try {
+ sha1 = getSHA1(file);
+ } catch (Exception e) {
+ return null;
+ }
+ String searchUrl = NLS.bind(SHA1_SEARCH_QUERY, sha1);
+ return find(searchUrl);
+ }
+
+ private ArtifactKey nameVersionSearch(File file, IProgressMonitor monitor) {
+
+ JarFile jar = null;
+ Manifest manifest = null;
+ try {
+ jar = new JarFile(file);
+ manifest = jar.getManifest();
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } finally {
+ if (jar != null) {
+ try {
+ jar.close();
+ } catch (Exception e) {
+ //Ignore
+ }
+ }
+ }
+ if (manifest == null) {
+ return null;
+ }
+
+ String version =
manifest.getMainAttributes().getValue("Implementation-Version");
+ String name = manifest.getMainAttributes().getValue("Bundle-SymbolicName");
+ if (version == null || version.trim().isEmpty() || name == null ||
name.trim().isEmpty()) {
+ return null;
+ }
+ if (monitor == null) {
+ monitor = new NullProgressMonitor();
+ }
+ if (monitor.isCanceled()) {
+ return null;
+ }
+
+ String searchUrl = NLS.bind(NAME_VERSION_SEARCH_QUERY, name, version);
+ return find(searchUrl);
+ }
+
+
+
+ private ArtifactKey find(String searchUrl) {
+ HttpURLConnection connection = null;//TODO use eclipse connections to handle proxies
+ InputStream is = null;
+ try {
+ connection = (HttpURLConnection) new URL(searchUrl).openConnection();
+ connection.setConnectTimeout(5*1000);
+ connection.connect();
+ int status = connection.getResponseCode();
+ switch (status) {
+ case 200:
+ case 201: {
+ is = connection.getInputStream();
+ ModelNode modelNode = ModelNode.fromJSONStream(is);
+ if (modelNode.isDefined()) {
+ return extractKey(modelNode);
+ }
+ }
+ default:
+
+ }
+ } catch (UnknownHostException uhe) {
+ System.err.println("Can't connect to search.maven.org:"+
uhe.getMessage());
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ IOUtil.close(is);
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ return null;
+ }
+
+ private ArtifactKey extractKey(ModelNode modelNode) {
+ ModelNode response = modelNode.get("response");
+ if (response != null) {
+ int num = response.get("numFound").asInt();
+ if (num > 0) {
+ ModelNode docs = response.get("docs");
+ if (docs.isDefined()) {
+ String a = null, g = null, v = null;
+ for (ModelNode n : docs.asList()) {
+ if (n.hasDefined("a")
+ && n.hasDefined("g")
+ && n.hasDefined("v")) {
+ a = n.get("a").asString();
+ g = n.get("g").asString();
+ v = n.get("v").asString();
+ if (a != null && g != null && v != null) {
+ return new ArtifactKey(g, a, v, null);
+ }
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+}
Added:
trunk/maven/tests/org.jboss.tools.maven.common.test/resources/groovy-jsr223-2.0.4.jar
===================================================================
(Binary files differ)
Property changes on:
trunk/maven/tests/org.jboss.tools.maven.common.test/resources/groovy-jsr223-2.0.4.jar
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Modified:
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/AbstractIdentificationTest.java
===================================================================
---
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/AbstractIdentificationTest.java 2012-10-11
09:47:00 UTC (rev 44424)
+++
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/AbstractIdentificationTest.java 2012-10-11
10:27:06 UTC (rev 44425)
@@ -1,3 +1,13 @@
+/*************************************************************************************
+ * Copyright (c) 2008-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.core.identification;
import java.io.File;
@@ -10,12 +20,14 @@
protected File arquillian;//Has maven properties
protected File junit;//Has no maven properties
protected File jansi;//Has multiple maven properties
+ protected File groovy_jsr223;//only has manifest info
@Before
public void initFiles() {
junit = new File("resources/junit_4_10.jar");
arquillian = new File("resources/arquillian-core-spi.jar");
jansi = new File("resources/jansi-1.6.jar");
+ groovy_jsr223 = new File("resources/groovy-jsr223-2.0.4.jar");
}
@After
@@ -23,6 +35,7 @@
junit = null;
arquillian = null;
jansi = null;
+ groovy_jsr223 = null;
}
Modified:
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/FileIdentificationManagerTest.java
===================================================================
---
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/FileIdentificationManagerTest.java 2012-10-11
09:47:00 UTC (rev 44424)
+++
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/FileIdentificationManagerTest.java 2012-10-11
10:27:06 UTC (rev 44425)
@@ -1,6 +1,17 @@
+/*************************************************************************************
+ * Copyright (c) 2008-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.core.identification;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.io.File;
import java.util.Collections;
@@ -47,13 +58,13 @@
new ArtifactIdentifier() {
@Override
public ArtifactKey identify(File file) throws CoreException {
- return new ArtifactKey("junit", "junit", "4.10",
null);
+ return identify(file, null);
}
@Override
public ArtifactKey identify(File file, IProgressMonitor monitor) throws CoreException
{
// TODO Auto-generated method stub
- return null;
+ return new ArtifactKey("junit", "junit", "4.10",
null);
}
}
);
Added:
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/MavenCentralIdentifierTest.java
===================================================================
---
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/MavenCentralIdentifierTest.java
(rev 0)
+++
trunk/maven/tests/org.jboss.tools.maven.common.test/src/org/jboss/tools/maven/core/identification/MavenCentralIdentifierTest.java 2012-10-11
10:27:06 UTC (rev 44425)
@@ -0,0 +1,35 @@
+/*************************************************************************************
+ * Copyright (c) 2008-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.core.identification;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+import org.jboss.tools.maven.core.internal.identification.MavenCentralIdentifier;
+import org.junit.Test;
+
+public class MavenCentralIdentifierTest extends AbstractIdentificationTest {
+
+ @Test
+ public void testIdentify() throws Exception {
+ MavenCentralIdentifier identifier = new MavenCentralIdentifier();
+ ArtifactKey key;
+
+ key = identifier.identify(groovy_jsr223, null);
+ assertNotNull("groovy-jsr223 was not identifed", key);
+ assertEquals("groovy-jsr223", key.getArtifactId());
+ assertEquals("org.codehaus.groovy", key.getGroupId());
+ assertEquals("2.0.4", key.getVersion());
+ }
+
+
+}