### Eclipse Workspace Patch 1.0 #P org.jboss.tools.maven.sourcelookup.ui Index: src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java (revision 42474) +++ src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java (working copy) @@ -40,6 +40,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 +80,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); if (artifact != null) { IPath sourcePath = JBossSourceContainer.getSourcePath(artifact); if (sourcePath == null || !sourcePath.toFile().exists()) { #P org.jboss.tools.maven.sourcelookup.core Index: META-INF/MANIFEST.MF =================================================================== --- META-INF/MANIFEST.MF (revision 42474) +++ META-INF/MANIFEST.MF (working copy) @@ -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 Index: src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusRepositoryIdentifier.java (working copy) @@ -0,0 +1,147 @@ +package org.jboss.tools.maven.sourcelookup.internal.identification; + +import static org.jboss.tools.maven.sourcelookup.identification.IdentificationUtil.*; + +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 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; + } + +} Index: src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java (revision 42474) +++ src/org/jboss/tools/maven/sourcelookup/containers/JBossSourceContainer.java (working copy) @@ -10,6 +10,7 @@ ************************************************************************************/ package org.jboss.tools.maven.sourcelookup.containers; +import static org.jboss.tools.maven.sourcelookup.identification.IdentificationUtil.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -68,6 +69,8 @@ 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.jboss.tools.maven.sourcelookup.identification.IFileIdentificationManager; +import org.jboss.tools.maven.sourcelookup.internal.identification.FileIdentificationManager; import org.sonatype.nexus.rest.model.NexusArtifact; import org.sonatype.nexus.rest.model.SearchResponse; @@ -79,9 +82,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 +95,9 @@ private List sourceContainers = new ArrayList(); protected static File resolvedFile; private String homePath; - private static List globalRepositories; + private IFileIdentificationManager fileIdentificationManager; + public JBossSourceContainer(ILaunchConfiguration configuration) throws CoreException { IServer server = ServerUtil.getServer(configuration); @@ -116,24 +117,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 getJars() throws CoreException { @@ -272,7 +261,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 +315,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); } - 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 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 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 +367,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())); Index: src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/identification/IdentificationUtil.java (working copy) @@ -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; + } +} Index: src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/internal/identification/MavenPropertiesIdentifier.java (working copy) @@ -0,0 +1,60 @@ +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 { + 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 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; + } +} Index: src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/identification/ArtifactIdentifier.java (working copy) @@ -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 Index: src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/identification/IFileIdentificationManager.java (working copy) @@ -0,0 +1,28 @@ +/************************************************************************************* + * 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; + +/** + * Manager + * + * @author Fred Bricon + * + */ +public interface IFileIdentificationManager { + + ArtifactKey identify(File file) throws CoreException; + +} Index: src/org/jboss/tools/maven/sourcelookup/SourceLookupActivator.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/SourceLookupActivator.java (revision 42474) +++ src/org/jboss/tools/maven/sourcelookup/SourceLookupActivator.java (working copy) @@ -31,6 +31,8 @@ import org.eclipse.ui.IMemento; import org.eclipse.ui.WorkbenchException; import org.eclipse.ui.XMLMemento; +import org.jboss.tools.maven.sourcelookup.identification.IFileIdentificationManager; +import org.jboss.tools.maven.sourcelookup.internal.identification.FileIdentificationManager; import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; Index: src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/internal/identification/NexusIndexIdentifier.java (working copy) @@ -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; + +@SuppressWarnings("restriction") +public class NexusIndexIdentifier implements ArtifactIdentifier { + + private List 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 initGlobalRepositories() { + IRepositoryRegistry repositoryRegistry = MavenPlugin.getRepositoryRegistry(); + List repositories = repositoryRegistry. + getRepositories(IRepositoryRegistry.SCOPE_SETTINGS); + return repositories == null? Collections.emptyList():repositories; + } + +} Index: src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java =================================================================== --- src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java (revision 0) +++ src/org/jboss/tools/maven/sourcelookup/internal/identification/FileIdentificationManager.java (working copy) @@ -0,0 +1,55 @@ +/************************************************************************************* + * 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.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 artifactIdentifiers; + + public FileIdentificationManager() { + initArtifactIdentifiers(); + } + + private void initArtifactIdentifiers() { + //TODO read from extension points? + artifactIdentifiers = new ArrayList(3); + artifactIdentifiers.add(new MavenPropertiesIdentifier()); + artifactIdentifiers.add(new NexusIndexIdentifier()); + artifactIdentifiers.add(new NexusRepositoryIdentifier()); + } + + @Override + public ArtifactKey identify(File file) throws CoreException { + ArtifactKey artifactKey = null; + for (ArtifactIdentifier identifier : artifactIdentifiers) { + artifactKey = identifier.identify(file); + if (artifactKey != null) { + break; + } + } + return null; + } + +}