diff options
Diffstat (limited to 'bundles/org.eclipse.e4.tools.compat/src/org/eclipse/e4/tools/compat/internal/ReflectionContributionFactory.java')
-rw-r--r-- | bundles/org.eclipse.e4.tools.compat/src/org/eclipse/e4/tools/compat/internal/ReflectionContributionFactory.java | 186 |
1 files changed, 0 insertions, 186 deletions
diff --git a/bundles/org.eclipse.e4.tools.compat/src/org/eclipse/e4/tools/compat/internal/ReflectionContributionFactory.java b/bundles/org.eclipse.e4.tools.compat/src/org/eclipse/e4/tools/compat/internal/ReflectionContributionFactory.java deleted file mode 100644 index 5b61577c..00000000 --- a/bundles/org.eclipse.e4.tools.compat/src/org/eclipse/e4/tools/compat/internal/ReflectionContributionFactory.java +++ /dev/null @@ -1,186 +0,0 @@ -package org.eclipse.e4.tools.compat.internal; - -import java.util.HashMap; -import java.util.Map; - -import javax.inject.Inject; - -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IConfigurationElement; -import org.eclipse.core.runtime.IExtensionRegistry; -import org.eclipse.core.runtime.InvalidRegistryObjectException; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; -import org.eclipse.e4.core.contexts.IEclipseContext; -import org.eclipse.e4.core.di.InjectionException; -import org.eclipse.e4.core.services.contributions.IContributionFactory; -import org.eclipse.e4.core.services.contributions.IContributionFactorySpi; -import org.eclipse.e4.core.services.log.Logger; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.osgi.service.packageadmin.PackageAdmin; - -/** - * Create the contribution factory. - */ -@SuppressWarnings("restriction") -public class ReflectionContributionFactory implements IContributionFactory { - - private IExtensionRegistry registry; - private Map<String, Object> languages; - - @Inject - private PackageAdmin packageAdmin; - - @Inject - private Logger logger; - - /** - * Create a reflection factory. - * - * @param registry - * to read languages. - */ - @Inject - public ReflectionContributionFactory(IExtensionRegistry registry) { - this.registry = registry; - processLanguages(); - } - - public Object create(String uriString, IEclipseContext context, IEclipseContext staticContext) { - return doCreate(uriString, context, staticContext); - } - - public Object create(String uriString, IEclipseContext context) { - return doCreate(uriString, context, null); - } - - private Object doCreate(String uriString, IEclipseContext context, IEclipseContext staticContext) { - if (uriString == null) { - return null; - } - URI uri = URI.createURI(uriString); - Bundle bundle = getBundle(uri); - Object contribution; - if (bundle != null) { - contribution = createFromBundle(bundle, context, staticContext, uri); - } else { - contribution = null; - logger.error("Unable to retrive the bundle from the URI: " //$NON-NLS-1$ - + uriString); - } - return contribution; - } - - protected Object createFromBundle(Bundle bundle, IEclipseContext context, - IEclipseContext staticContext, URI uri) { - Object contribution; - if (uri.segmentCount() > 3) { - String prefix = uri.segment(2); - IContributionFactorySpi factory = (IContributionFactorySpi) languages.get(prefix); - StringBuffer resource = new StringBuffer(uri.segment(3)); - for (int i = 4; i < uri.segmentCount(); i++) { - resource.append('/'); - resource.append(uri.segment(i)); - } - contribution = factory.create(bundle, resource.toString(), context); - } else { - String clazz = uri.segment(2); - try { - Class<?> targetClass = bundle.loadClass(clazz); - if (staticContext == null) - contribution = ContextInjectionFactory.make(targetClass, context); - else - contribution = ContextInjectionFactory - .make(targetClass, context, staticContext); - - if (contribution == null) { - String message = "Unable to load class '" + clazz + "' from bundle '" //$NON-NLS-1$ //$NON-NLS-2$ - + bundle.getBundleId() + "'"; //$NON-NLS-1$ - logger.error(message); - } - } catch (ClassNotFoundException e) { - contribution = null; - String message = "Unable to load class '" + clazz + "' from bundle '" //$NON-NLS-1$ //$NON-NLS-2$ - + bundle.getBundleId() + "'"; //$NON-NLS-1$ - logger.error(e,message); - } catch (InjectionException e) { - contribution = null; - String message = "Unable to create class '" + clazz + "' from bundle '" //$NON-NLS-1$ //$NON-NLS-2$ - + bundle.getBundleId() + "'"; //$NON-NLS-1$ - logger.error(e, message); - } - } - return contribution; - } - - protected void processLanguages() { - languages = new HashMap<String, Object>(); - String extId = "org.eclipse.e4.languages"; //$NON-NLS-1$ - IConfigurationElement[] languageElements = registry.getConfigurationElementsFor(extId); - for (int i = 0; i < languageElements.length; i++) { - IConfigurationElement languageElement = languageElements[i]; - try { - languages.put(languageElement.getAttribute("name"), //$NON-NLS-1$ - languageElement.createExecutableExtension("contributionFactory")); //$NON-NLS-1$ - } catch (InvalidRegistryObjectException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - - - protected Bundle getBundle(URI platformURI) { - return getBundleForName(platformURI.segments[1]); - } - - public Bundle getBundle(String uriString) { - return getBundle(new URI(uriString)); - } - - public Bundle getBundleForName(String bundlename) { - if( packageAdmin == null ) { - Bundle bundle = FrameworkUtil.getBundle(getClass()); - BundleContext context = bundle.getBundleContext(); - ServiceReference reference = context.getServiceReference(PackageAdmin.class.getName()); - packageAdmin = (PackageAdmin) context.getService(reference); - } - - Bundle[] bundles = packageAdmin.getBundles(bundlename, null); - if (bundles == null) - return null; - // Return the first bundle that is not installed or uninstalled - for (int i = 0; i < bundles.length; i++) { - if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { - return bundles[i]; - } - } - return null; - } - - static class URI { - String[] segments; - String uri; - - URI(String uriString) { - segments = uriString.substring(uriString.indexOf('/')+1).split("/"); - } - - public String segment(int i) { - return segments[i]; - } - - public int segmentCount() { - return segments.length; - } - - static URI createURI(String uriString) { - return new URI(uriString); - } - } -} |