[jbosstools-commits] JBoss Tools SVN: r42693 - in trunk/maven/plugins: org.jboss.tools.maven.sourcelookup.core/META-INF and 7 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Tue Jul 24 12:31:18 EDT 2012


Author: fbricon
Date: 2012-07-24 12:31:18 -0400 (Tue, 24 Jul 2012)
New Revision: 42693

Added:
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/.settings/org.eclipse.m2e.core.prefs
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/
   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
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/.settings/org.eclipse.m2e.core.prefs
Modified:
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/META-INF/MANIFEST.MF
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java
   trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java
Log:
JBIDE-12314 : Extract file identification API for the Maven Source Lookup Plugin - part 1

Added: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/.settings/org.eclipse.m2e.core.prefs
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/.settings/org.eclipse.m2e.core.prefs	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/.settings/org.eclipse.m2e.core.prefs	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1

Modified: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/META-INF/MANIFEST.MF
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/META-INF/MANIFEST.MF	2012-07-24 16:02:08 UTC (rev 42692)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/META-INF/MANIFEST.MF	2012-07-24 16:31:18 UTC (rev 42693)
@@ -20,6 +20,8 @@
 Bundle-Vendor: %BundleVendor
 Bundle-Localization: plugin
 Export-Package: org.jboss.tools.maven.sourcelookup,
- org.jboss.tools.maven.sourcelookup.containers
+ org.jboss.tools.maven.sourcelookup.containers,
+ org.jboss.tools.maven.sourcelookup.identification,
+ org.jboss.tools.maven.sourcelookup.internal.identification
 Bundle-ClassPath: .,
  lib/nexus-indexer-lucene-rest-api-client.jar

Modified: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java	2012-07-24 16:02:08 UTC (rev 42692)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -10,34 +10,24 @@
  ************************************************************************************/
 package org.jboss.tools.maven.sourcelookup.containers;
 
+import static org.jboss.tools.maven.sourcelookup.identification.IdentificationUtil.getSourcesClassifier;
+
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLEncoder;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Properties;
-import java.util.Set;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.Unmarshaller;
-
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
@@ -49,13 +39,6 @@
 import org.eclipse.m2e.core.MavenPlugin;
 import org.eclipse.m2e.core.embedder.ArtifactKey;
 import org.eclipse.m2e.core.embedder.IMaven;
-import org.eclipse.m2e.core.internal.index.IIndex;
-import org.eclipse.m2e.core.internal.index.IndexManager;
-import org.eclipse.m2e.core.internal.index.IndexedArtifactFile;
-import org.eclipse.m2e.core.internal.index.nexus.NexusIndex;
-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.eclipse.wst.server.core.IServer;
 import org.eclipse.wst.server.core.ServerUtil;
 import org.jboss.ide.eclipse.as.core.server.IJBossServerRuntime;
@@ -66,10 +49,9 @@
 import org.jboss.ide.eclipse.as.core.util.IJBossRuntimeResourceConstants;
 import org.jboss.ide.eclipse.as.core.util.IJBossToolingConstants;
 import org.jboss.ide.eclipse.as.core.util.ServerConverter;
-import org.jboss.tools.maven.sourcelookup.NexusRepository;
 import org.jboss.tools.maven.sourcelookup.SourceLookupActivator;
-import org.sonatype.nexus.rest.model.NexusArtifact;
-import org.sonatype.nexus.rest.model.SearchResponse;
+import org.jboss.tools.maven.sourcelookup.identification.IFileIdentificationManager;
+import org.jboss.tools.maven.sourcelookup.internal.identification.FileIdentificationManager;
 
 /**
  * 
@@ -79,9 +61,6 @@
 public class JBossSourceContainer extends AbstractSourceContainer {
 
 	private static final String PATH_SEPARATOR = "/";
-	private static final String CLASSIFIER_SOURCES = "sources"; //$NON-NLS-1$
-	private static final String CLASSIFIER_TESTS = "tests"; //$NON-NLS-1$
-	private static final String CLASSIFIER_TESTSOURCES = "test-sources"; //$NON-NLS-1$
 	public static final String TYPE_ID = "org.jboss.tools.maven.sourcelookup.containerType"; //$NON-NLS-1$
 
 	public static final String EAP = "EAP"; //$NON-NLS-1$
@@ -95,8 +74,9 @@
 	private List<ISourceContainer> sourceContainers = new ArrayList<ISourceContainer>();
 	protected static File resolvedFile;
 	private String homePath;
-	private static List<IRepository> globalRepositories;
 
+	private IFileIdentificationManager fileIdentificationManager;
+	
 	public JBossSourceContainer(ILaunchConfiguration configuration)
 			throws CoreException {
 		IServer server = ServerUtil.getServer(configuration);
@@ -116,24 +96,12 @@
 					SourceLookupActivator.PLUGIN_ID, "Invalid configuration");
 			throw new CoreException(status);
 		}
-		initialize();
+		fileIdentificationManager = new FileIdentificationManager();
 	}
 
-	private static void initialize() throws CoreException {
-		IRepositoryRegistry repositoryRegistry = MavenPlugin
-				.getRepositoryRegistry();
-		globalRepositories = repositoryRegistry
-				.getRepositories(IRepositoryRegistry.SCOPE_SETTINGS);
-		MavenPlugin.getMaven().reloadSettings();
-	}
-
 	public JBossSourceContainer(String homePath) {
 		this.homePath = homePath;
-		try {
-			initialize();
-		} catch (CoreException e) {
-			SourceLookupActivator.log(e);
-		}
+		fileIdentificationManager = new FileIdentificationManager();
 	}
 
 	private List<File> getJars() throws CoreException {
@@ -152,20 +120,20 @@
 		if (JBossServerType.AS7.equals(type)) {
 			getAS7xJars();
 		}
-		if (JBossServerType.AS.equals(type)) {
+		else if (JBossServerType.AS.equals(type)) {
 			if (IJBossToolingConstants.V6_0.equals(version)
 					|| IJBossToolingConstants.V6_1.equals(version)) {
 				getAS6xJars();
 			}
-			if (IJBossToolingConstants.V5_0.equals(version)
+			else if (IJBossToolingConstants.V5_0.equals(version)
 					|| IJBossToolingConstants.V5_1.equals(version)) {
 				getAS5xJars();
 			}
 		}
-		if (JBossServerType.EAP6.equals(type)) {
+		else if (JBossServerType.EAP6.equals(type)) {
 			getAS7xJars();
 		}
-		if (JBossServerType.SOAP.equals(type) || JBossServerType.EAP.equals(type) || EPP.equals(type)
+		else if (JBossServerType.SOAP.equals(type) || JBossServerType.EAP.equals(type) || EPP.equals(type)
 				|| JBossServerType.SOAP_STD.equals(type) || JBossServerType.EWP.equals(type)
 				|| JBossServerType.EAP_STD.equals(type)) {
 
@@ -272,7 +240,7 @@
 				className = className.replace("\\", PATH_SEPARATOR);
 				ZipEntry entry = jar.getEntry(className);//$NON-NLS-1$
 				if (entry != null) {
-					ArtifactKey artifact = getArtifact(file, jar);
+					ArtifactKey artifact = getArtifact(file);
 					if (artifact != null) {
 						IPath sourcePath = getSourcePath(artifact);
 						if (sourcePath == null) {
@@ -326,224 +294,10 @@
 		return objects;
 	}
 
-	public static ArtifactKey getArtifact(File file, ZipFile jar)
-			throws CoreException, IOException {
-		ArtifactKey artifact = getArtifactFromM2eIndex(file);
-		if (artifact == null) {
-			artifact = getArtifactFromMetaInf(jar);
-		}
-		if (artifact == null) {
-			artifact = getArtifactFromJBossNexusRepository(file);
-		}
-		return artifact;
+	public ArtifactKey getArtifact(File file) throws CoreException {
+		return fileIdentificationManager.identify(file, new NullProgressMonitor());
 	}
 
-	private static ArtifactKey getArtifactFromJBossNexusRepository(String sha1,
-			NexusRepository nexusRepository) {
-		if (sha1 == null || nexusRepository == null
-				|| nexusRepository.getUrl() == null) {
-			return null;
-		}
-		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?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.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;
-	}
-
-	private static ArtifactKey getArtifactFromJBossNexusRepository(File file) {
-		String sha1;
-		try {
-			sha1 = getSHA1(file);
-		} catch (Exception e) {
-			return null;
-		}
-		Set<NexusRepository> nexusRepositories = SourceLookupActivator
-				.getNexusRepositories();
-		for (NexusRepository repository : nexusRepositories) {
-			if (!repository.isEnabled()) {
-				continue;
-			}
-			ArtifactKey key = getArtifactFromJBossNexusRepository(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) {
-					return key;
-				}
-			}
-		}
-		return null;
-	}
-
-	private static ArtifactKey getSourcesArtifactFromJBossNexusRepository(ArtifactKey key,
-			NexusRepository nexusRepository) {
-		if (key == null || nexusRepository == null
-				|| nexusRepository.getUrl() == null) {
-			return null;
-		}
-		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;
-	}
-	private static String getSHA1(File file) throws IOException,
-			NoSuchAlgorithmException {
-		MessageDigest md = MessageDigest.getInstance("SHA1");
-		InputStream inputStream = new FileInputStream(file);
-		byte[] bytes = new byte[16 * 1024];
-		int count = 0;
-		while ((count = inputStream.read(bytes)) != -1) {
-			md.update(bytes, 0, count);
-		}
-		byte[] digestBytes = md.digest();
-		StringBuffer sb = new StringBuffer("");
-		for (int i = 0; i < digestBytes.length; i++) {
-			sb.append(Integer.toString((digestBytes[i] & 0xff) + 0x100, 16)
-					.substring(1));
-		}
-		return sb.toString();
-	}
-
-	protected static ArtifactKey getArtifactFromMetaInf(ZipFile jar)
-			throws IOException {
-		ZipEntry mavenEntry = jar.getEntry("META-INF/maven");//$NON-NLS-1$
-		if (mavenEntry == null) {
-			return null;
-		}
-		String entryName = mavenEntry.getName();
-		Enumeration<? extends ZipEntry> zipEntries = jar.entries();
-		ArtifactKey artifact = null;
-		while (zipEntries.hasMoreElements()) {
-			ZipEntry zipEntry = zipEntries.nextElement();
-			if (zipEntry.getName().endsWith("pom.properties")
-					&& zipEntry.getName().startsWith(entryName)) {
-				Properties props = new Properties();
-				props.load(jar.getInputStream(zipEntry));
-				String groupId = props.getProperty("groupId");
-				String artifactId = props.getProperty("artifactId");
-				String version = props.getProperty("version");
-				String classifier = props.getProperty("classifier");
-				if (groupId != null && artifactId != null && version != null) {
-					artifact = new ArtifactKey(groupId, artifactId, version,
-							classifier);
-					return artifact;
-				}
-			}
-		}
-		return artifact;
-	}
-
-	protected static ArtifactKey getArtifactFromM2eIndex(File file)
-			throws CoreException {
-		IndexManager indexManager = MavenPlugin.getIndexManager();
-		IIndex index = indexManager.getAllIndexes();
-		IndexedArtifactFile info = null;
-		try {
-			info = index.identify(file);
-		} catch (Throwable e) {
-			// ignore
-		}
-		ArtifactKey artifact = null;
-		if (info != null) {
-			artifact = info.getArtifactKey();
-			if (artifact != null) {
-				return artifact;
-			}
-		}
-		if (indexManager instanceof NexusIndexManager) {
-			NexusIndexManager nexusIndexManager = (NexusIndexManager) indexManager;
-			if (globalRepositories == null) {
-				initialize();
-			}
-			for (IRepository repository : globalRepositories) {
-				NexusIndex nexusIndex = nexusIndexManager.getIndex(repository);
-				if (nexusIndex != null) {
-					try {
-						info = nexusIndex.identify(file);
-					} catch (Throwable t) {
-						// ignore
-					}
-					if (info != null) {
-						artifact = info.getArtifactKey();
-						if (artifact != null) {
-							return artifact;
-						}
-					}
-				}
-			}
-		}
-		return artifact;
-	}
-
 	public static Job downloadArtifact(File file, ArtifactKey artifact) {
 		final ArtifactKey sourcesArtifact = new ArtifactKey(
 				artifact.getGroupId(), artifact.getArtifactId(),
@@ -592,11 +346,6 @@
 		return resolved;
 	}
 
-	static String getSourcesClassifier(String baseClassifier) {
-		return CLASSIFIER_TESTS.equals(baseClassifier) ? CLASSIFIER_TESTSOURCES
-				: CLASSIFIER_SOURCES;
-	}
-
 	public static IPath getSourcePath(ArtifactKey a) {
 		File file = getAttachedArtifactFile(a,
 				getSourcesClassifier(a.getClassifier()));

Added: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,22 @@
+/*************************************************************************************
+ * 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.sourcelookup.identification;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+
+public interface ArtifactIdentifier {
+	
+	ArtifactKey identify(File file) throws CoreException;
+
+}
\ No newline at end of file

Added: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,29 @@
+/*************************************************************************************
+ * 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.sourcelookup.identification;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+
+/**
+ * Manager 
+ * 
+ * @author Fred Bricon
+ *
+ */
+public interface IFileIdentificationManager {
+
+	ArtifactKey identify(File file, IProgressMonitor monitor) throws CoreException;
+	
+}

Added: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,62 @@
+/*************************************************************************************
+ * 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.sourcelookup.identification;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class IdentificationUtil {
+
+	private static final String CLASSIFIER_SOURCES = "sources"; //$NON-NLS-1$
+	private static final String CLASSIFIER_TESTS = "tests"; //$NON-NLS-1$
+	private static final String CLASSIFIER_TESTSOURCES = "test-sources"; //$NON-NLS-1$
+
+	private IdentificationUtil() {}
+	
+	public static String getSHA1(File file) throws IOException,
+			NoSuchAlgorithmException {
+		
+		InputStream inputStream = null;
+		StringBuilder sb = new StringBuilder();
+		try {
+			MessageDigest md = MessageDigest.getInstance("SHA1");
+			inputStream = new FileInputStream(file);
+			byte[] bytes = new byte[16 * 1024];
+			int count = 0;
+			while ((count = inputStream.read(bytes)) != -1) {
+				md.update(bytes, 0, count);
+			}
+			byte[] digestBytes = md.digest();
+			for (int i = 0; i < digestBytes.length; i++) {
+				sb.append(Integer.toString((digestBytes[i] & 0xff) + 0x100, 16)
+						.substring(1));
+			}
+		} finally {
+			if (inputStream != null) {
+				try {
+					inputStream.close();
+				} catch (Exception e){
+				  //ignore
+				}
+			}
+		}
+		return sb.toString();
+	}
+	
+	public static String getSourcesClassifier(String baseClassifier) {
+		return CLASSIFIER_TESTS.equals(baseClassifier) ? CLASSIFIER_TESTSOURCES
+				: CLASSIFIER_SOURCES;
+	}
+}

Added: 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	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,62 @@
+/*************************************************************************************
+ * 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.sourcelookup.internal.identification;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+import org.jboss.tools.maven.sourcelookup.identification.ArtifactIdentifier;
+import org.jboss.tools.maven.sourcelookup.identification.IFileIdentificationManager;
+
+/**
+ * 
+ * @author Fred Bricon
+ *
+ */
+public class FileIdentificationManager implements IFileIdentificationManager {
+
+	private List<ArtifactIdentifier> artifactIdentifiers;
+	
+	public FileIdentificationManager() {
+		initArtifactIdentifiers();
+	}
+	
+	private void initArtifactIdentifiers() {
+		//TODO read from extension points?
+		artifactIdentifiers = new ArrayList<ArtifactIdentifier>(3);
+		artifactIdentifiers.add(new MavenPropertiesIdentifier());
+		artifactIdentifiers.add(new NexusIndexIdentifier());
+		artifactIdentifiers.add(new NexusRepositoryIdentifier());
+	}
+
+	@Override
+	public ArtifactKey identify(File file, IProgressMonitor monitor) throws CoreException {
+		ArtifactKey artifactKey = null;
+		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");
+				break;
+			}
+		}
+		return artifactKey;
+	}
+	
+}

Added: 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	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,72 @@
+package org.jboss.tools.maven.sourcelookup.internal.identification;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Properties;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+
+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 {
+
+	@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();
+		}
+		return null;
+	}
+
+	protected static ArtifactKey getArtifactFromMetaInf(ZipFile jar) throws IOException {
+		ZipEntry mavenEntry = jar.getEntry("META-INF/maven");//$NON-NLS-1$
+		if (mavenEntry == null) {
+			return null;
+		}
+		String entryName = mavenEntry.getName();
+		Enumeration<? extends ZipEntry> zipEntries = jar.entries();
+		ArtifactKey artifact = null;
+		
+		
+		while (zipEntries.hasMoreElements()) {
+			ZipEntry zipEntry = zipEntries.nextElement();
+			if (zipEntry.getName().endsWith("pom.properties")
+					&& zipEntry.getName().startsWith(entryName)) {
+				if (artifact != null) {
+					//org.fusesource.jansi:jansi:1.6 is an OSGi bundle containing several maven pom files.
+					//The first properties being found is wrong.
+					//So for the moment we bail but should try to look at the MANIFEST.MF
+					return null;
+				}
+				Properties props = new Properties();
+				props.load(jar.getInputStream(zipEntry));
+				String groupId = props.getProperty("groupId");
+				String artifactId = props.getProperty("artifactId");
+				String version = props.getProperty("version");
+				String classifier = props.getProperty("classifier");
+				if (groupId != null && artifactId != null && version != null) {
+					artifact = new ArtifactKey(groupId, artifactId, version,
+							classifier);
+				}
+			}
+		}
+		
+		return artifact;
+	}
+}

Added: 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	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,74 @@
+package org.jboss.tools.maven.sourcelookup.internal.identification;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.m2e.core.MavenPlugin;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+import org.eclipse.m2e.core.internal.index.IIndex;
+import org.eclipse.m2e.core.internal.index.IndexManager;
+import org.eclipse.m2e.core.internal.index.IndexedArtifactFile;
+import org.eclipse.m2e.core.internal.index.nexus.NexusIndex;
+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;
+
+ at SuppressWarnings("restriction")
+public class NexusIndexIdentifier implements ArtifactIdentifier {
+
+	private List<IRepository> globalRepositories;
+
+	public NexusIndexIdentifier() {
+		globalRepositories = initGlobalRepositories();
+	}
+	
+	@Override
+	public ArtifactKey identify(File file) throws CoreException {
+		IndexManager indexManager = MavenPlugin.getIndexManager();
+		IIndex index = indexManager.getAllIndexes();
+		IndexedArtifactFile info = null;
+		try {
+			info = index.identify(file);
+		} catch (Throwable e) {
+			// ignore
+		}
+		ArtifactKey artifact = null;
+		if (info != null) {
+			artifact = info.getArtifactKey();
+			if (artifact != null) {
+				return artifact;
+			}
+		}
+		if (indexManager instanceof NexusIndexManager) {
+			NexusIndexManager nexusIndexManager = (NexusIndexManager) indexManager;
+			for (IRepository repository : globalRepositories) {
+				NexusIndex nexusIndex = nexusIndexManager.getIndex(repository);
+				if (nexusIndex != null) {
+					try {
+						info = nexusIndex.identify(file);
+					} catch (Throwable t) {
+						// ignore
+					}
+					if (info != null) {
+						artifact = info.getArtifactKey();
+						if (artifact != null) {
+							return artifact;
+						}
+					}
+				}
+			}
+		}
+		return artifact;
+	}
+
+	private List<IRepository> initGlobalRepositories() {
+		IRepositoryRegistry repositoryRegistry = MavenPlugin.getRepositoryRegistry();
+		List<IRepository> repositories = repositoryRegistry.
+				getRepositories(IRepositoryRegistry.SCOPE_SETTINGS);
+		return repositories == null? Collections.<IRepository>emptyList():repositories;
+	}
+
+}

Added: 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	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.core/src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,148 @@
+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.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.Set;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Unmarshaller;
+
+import org.eclipse.core.runtime.CoreException;
+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 {
+
+	private static final String PATH_SEPARATOR = "/";
+
+	@Override
+	public ArtifactKey identify(File file) throws CoreException {
+		return getArtifactFromRemoteNexusRepository(file);
+	}
+
+	private static ArtifactKey getArtifactFromRemoteNexusRepository(File file) {
+		String sha1;
+		try {
+			sha1 = getSHA1(file);
+		} catch (Exception e) {
+			return null;
+		}
+		Set<NexusRepository> nexusRepositories = SourceLookupActivator
+				.getNexusRepositories();
+		for (NexusRepository repository : nexusRepositories) {
+			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) {
+					return key;
+				}
+			}
+		}
+		return null;
+	}
+
+	private static ArtifactKey getArtifactFromRemoteNexusRepository(String sha1,
+			NexusRepository nexusRepository) {
+		if (sha1 == null || nexusRepository == null	|| nexusRepository.getUrl() == null) {
+			return null;
+		}
+		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?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.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;
+	}
+
+	private static ArtifactKey getSourcesArtifactFromJBossNexusRepository(ArtifactKey key,
+			NexusRepository nexusRepository) {
+		if (key == null || nexusRepository == null
+				|| nexusRepository.getUrl() == null) {
+			return null;
+		}
+		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;
+	}
+	
+}

Added: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/.settings/org.eclipse.m2e.core.prefs
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/.settings/org.eclipse.m2e.core.prefs	                        (rev 0)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/.settings/org.eclipse.m2e.core.prefs	2012-07-24 16:31:18 UTC (rev 42693)
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1

Modified: trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java
===================================================================
--- trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java	2012-07-24 16:02:08 UTC (rev 42692)
+++ trunk/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java	2012-07-24 16:31:18 UTC (rev 42693)
@@ -11,7 +11,6 @@
 package org.jboss.tools.maven.sourcelookup.ui.actions;
 
 import java.io.File;
-import java.util.zip.ZipFile;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.ResourcesPlugin;
@@ -40,6 +39,8 @@
 import org.eclipse.ui.IEditorPart;
 import org.jboss.tools.maven.sourcelookup.SourceLookupActivator;
 import org.jboss.tools.maven.sourcelookup.containers.JBossSourceContainer;
+import org.jboss.tools.maven.sourcelookup.identification.IFileIdentificationManager;
+import org.jboss.tools.maven.sourcelookup.internal.identification.FileIdentificationManager;
 import org.jboss.tools.maven.sourcelookup.ui.SourceLookupUIActivator;
 
 /**
@@ -78,8 +79,8 @@
 						if (fragment.isArchive()) {
 							IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
 							File file = iFile == null || iFile.getLocation() == null ? fragment.getPath().toFile() : iFile.getLocation().toFile();
-							ZipFile jar = new ZipFile(file);
-							final ArtifactKey artifact = JBossSourceContainer.getArtifact(file, jar);
+							IFileIdentificationManager identificationManager = new FileIdentificationManager();
+							final ArtifactKey artifact = identificationManager.identify(file, new NullProgressMonitor());
 							if (artifact != null) {
 								IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
 								if (sourcePath == null || !sourcePath.toFile().exists()) {



More information about the jbosstools-commits mailing list