Search in sources :

Example 1 with ExtensionActivator

use of aQute.bnd.service.extension.ExtensionActivator in project bnd by bndtools.

the class Workspace method addExtensions.

/**
	 * Add any extensions listed
	 * 
	 * @param list
	 */
@Override
protected void addExtensions(Set<Object> list) {
    //
    // <bsn>; version=<range>
    //
    Parameters extensions = getMergedParameters(EXTENSION);
    Map<DownloadBlocker, Attrs> blockers = new HashMap<DownloadBlocker, Attrs>();
    for (Entry<String, Attrs> i : extensions.entrySet()) {
        String bsn = removeDuplicateMarker(i.getKey());
        String stringRange = i.getValue().get(VERSION_ATTRIBUTE);
        logger.debug("Adding extension {}-{}", bsn, stringRange);
        if (stringRange == null)
            stringRange = Version.LOWEST.toString();
        else if (!VersionRange.isVersionRange(stringRange)) {
            error("Invalid version range %s on extension %s", stringRange, bsn);
            continue;
        }
        try {
            SortedSet<ResourceDescriptor> matches = resourceRepositoryImpl.find(null, bsn, new VersionRange(stringRange));
            if (matches.isEmpty()) {
                error("Extension %s;version=%s not found in base repo", bsn, stringRange);
                continue;
            }
            DownloadBlocker blocker = new DownloadBlocker(this);
            blockers.put(blocker, i.getValue());
            resourceRepositoryImpl.getResource(matches.last().id, blocker);
        } catch (Exception e) {
            error("Failed to load extension %s-%s, %s", bsn, stringRange, e);
        }
    }
    logger.debug("Found extensions {}", blockers);
    for (Entry<DownloadBlocker, Attrs> blocker : blockers.entrySet()) {
        try {
            String reason = blocker.getKey().getReason();
            if (reason != null) {
                error("Extension load failed: %s", reason);
                continue;
            }
            @SuppressWarnings("resource") URLClassLoader cl = new URLClassLoader(new URL[] { blocker.getKey().getFile().toURI().toURL() }, getClass().getClassLoader());
            Enumeration<URL> manifests = cl.getResources("META-INF/MANIFEST.MF");
            while (manifests.hasMoreElements()) {
                try (InputStream is = manifests.nextElement().openStream()) {
                    Manifest m = new Manifest(is);
                    Parameters activators = new Parameters(m.getMainAttributes().getValue("Extension-Activator"), this);
                    for (Entry<String, Attrs> e : activators.entrySet()) {
                        try {
                            Class<?> c = cl.loadClass(e.getKey());
                            ExtensionActivator extensionActivator = (ExtensionActivator) c.getConstructor().newInstance();
                            customize(extensionActivator, blocker.getValue());
                            List<?> plugins = extensionActivator.activate(this, blocker.getValue());
                            list.add(extensionActivator);
                            if (plugins != null)
                                for (Object plugin : plugins) {
                                    list.add(plugin);
                                }
                        } catch (ClassNotFoundException cnfe) {
                            error("Loading extension %s, extension activator missing: %s (ignored)", blocker, e.getKey());
                        }
                    }
                }
            }
        } catch (Exception e) {
            error("failed to install extension %s due to %s", blocker, e);
        }
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) Attrs(aQute.bnd.header.Attrs) VersionRange(aQute.bnd.version.VersionRange) Manifest(java.util.jar.Manifest) IOException(java.io.IOException) TimeLimitExceededException(javax.naming.TimeLimitExceededException) URL(java.net.URL) ExtensionActivator(aQute.bnd.service.extension.ExtensionActivator) URLClassLoader(java.net.URLClassLoader) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Aggregations

Attrs (aQute.bnd.header.Attrs)1 Parameters (aQute.bnd.header.Parameters)1 ExtensionActivator (aQute.bnd.service.extension.ExtensionActivator)1 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)1 VersionRange (aQute.bnd.version.VersionRange)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JarInputStream (java.util.jar.JarInputStream)1 Manifest (java.util.jar.Manifest)1 TimeLimitExceededException (javax.naming.TimeLimitExceededException)1