diff options
Diffstat (limited to 'org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/ExtensionReader.java')
-rw-r--r-- | org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/ExtensionReader.java | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/ExtensionReader.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/ExtensionReader.java new file mode 100644 index 00000000..c0c542e6 --- /dev/null +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/ExtensionReader.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (c) 2008-2010 Sonatype, Inc. + * 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: + * Sonatype, Inc. - initial API and implementation + *******************************************************************************/ + +package org.eclipse.m2e.core.internal; + +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.osgi.framework.Bundle; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IContributor; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; + +import org.eclipse.m2e.core.archetype.ArchetypeCatalogFactory; +import org.eclipse.m2e.core.core.IMavenConstants; +import org.eclipse.m2e.core.core.MavenLogger; +import org.eclipse.m2e.core.project.IMavenProjectChangedListener; + + +/** + * Extension reader + * + * @author Eugene Kuleshov + */ +public class ExtensionReader { + + public static final String EXTENSION_ARCHETYPES = IMavenConstants.PLUGIN_ID + ".archetypeCatalogs"; //$NON-NLS-1$ + + public static final String EXTENSION_PROJECT_CHANGED_EVENT_LISTENERS = IMavenConstants.PLUGIN_ID + ".mavenProjectChangedListeners"; //$NON-NLS-1$ + + private static final String ELEMENT_LOCAL_ARCHETYPE = "local"; //$NON-NLS-1$ + + private static final String ELEMENT_REMOTE_ARCHETYPE = "remote"; //$NON-NLS-1$ + + private static final String ATTR_NAME = "name"; //$NON-NLS-1$ + + private static final String ATTR_URL = "url"; //$NON-NLS-1$ + + private static final String ATTR_DESCRIPTION = "description"; //$NON-NLS-1$ + + private static final String ELEMENT_LISTENER = "listener"; //$NON-NLS-1$ + + public static List<ArchetypeCatalogFactory> readArchetypeExtensions() { + List<ArchetypeCatalogFactory> archetypeCatalogs = new ArrayList<ArchetypeCatalogFactory>(); + + IExtensionRegistry registry = Platform.getExtensionRegistry(); + IExtensionPoint archetypesExtensionPoint = registry.getExtensionPoint(EXTENSION_ARCHETYPES); + if(archetypesExtensionPoint != null) { + IExtension[] archetypesExtensions = archetypesExtensionPoint.getExtensions(); + for(IExtension extension : archetypesExtensions) { + IConfigurationElement[] elements = extension.getConfigurationElements(); + IContributor contributor = extension.getContributor(); + for(IConfigurationElement element : elements) { + ArchetypeCatalogFactory factory = readArchetypeCatalogs(element, contributor); + // archetypeManager.addArchetypeCatalogFactory(factory); + archetypeCatalogs.add(factory); + } + } + } + return archetypeCatalogs; + } + + private static ArchetypeCatalogFactory readArchetypeCatalogs(IConfigurationElement element, IContributor contributor) { + if(ELEMENT_LOCAL_ARCHETYPE.equals(element.getName())) { + String name = element.getAttribute(ATTR_NAME); + if(name != null) { + Bundle[] bundles = Platform.getBundles(contributor.getName(), null); + URL catalogUrl = null; + for(int i = 0; i < bundles.length; i++ ) { + Bundle bundle = bundles[i]; + catalogUrl = bundle.getEntry(name); + if(catalogUrl != null) { + String description = element.getAttribute(ATTR_DESCRIPTION); + String url = catalogUrl.toString(); + // XXX ARCHETYPE-161: RemoteCatalogArchetypeDataSource don't allow to download arbitrary urls + return new ArchetypeCatalogFactory.RemoteCatalogFactory(url.substring(0, url.lastIndexOf("/")), //$NON-NLS-1$ + description, false); + } + } + MavenLogger.log("Unable to find Archetype catalog " + name + " in " + contributor.getName(), null); + } + } else if(ELEMENT_REMOTE_ARCHETYPE.equals(element.getName())) { + String url = element.getAttribute(ATTR_URL); + if(url != null) { + String description = element.getAttribute(ATTR_DESCRIPTION); + return new ArchetypeCatalogFactory.RemoteCatalogFactory(url, description, false); + } + } + return null; + } + + public static List<IMavenProjectChangedListener> readProjectChangedEventListenerExtentions() { + ArrayList<IMavenProjectChangedListener> listeners = new ArrayList<IMavenProjectChangedListener>(); + + IExtensionRegistry registry = Platform.getExtensionRegistry(); + IExtensionPoint mappingsExtensionPoint = registry.getExtensionPoint(EXTENSION_PROJECT_CHANGED_EVENT_LISTENERS); + if(mappingsExtensionPoint != null) { + IExtension[] mappingsExtensions = mappingsExtensionPoint.getExtensions(); + for(IExtension extension : mappingsExtensions) { + IConfigurationElement[] elements = extension.getConfigurationElements(); + for(IConfigurationElement element : elements) { + if(element.getName().equals(ELEMENT_LISTENER)) { + try { + listeners.add( (IMavenProjectChangedListener) element.createExecutableExtension("class") ); //$NON-NLS-1$ + } catch(CoreException ex) { + MavenLogger.log(ex); + } + } + } + } + } + + return listeners; + } + +} |