Search in sources :

Example 1 with ClasspathManager

use of org.eclipse.osgi.internal.loader.classpath.ClasspathManager in project rt.equinox.framework by eclipse.

the class EclipseLazyStarter method postFindLocalClass.

@Override
public void postFindLocalClass(String name, Class<?> clazz, ClasspathManager manager) throws ClassNotFoundException {
    if (initiatingClassName.get() != name)
        return;
    initiatingClassName.set(null);
    Deque<ClasspathManager> stack = activationStack.get();
    if (stack == null || stack.isEmpty())
        return;
    // if we have a stack we must clear it even if (clazz == null)
    List<ClasspathManager> managers = new ArrayList<>(stack);
    stack.clear();
    if (clazz == null)
        return;
    for (ClasspathManager managerElement : managers) {
        if (errors.get(managerElement) != null) {
            if (container.getConfiguration().throwErrorOnFailedStart)
                throw errors.get(managerElement);
            continue;
        }
        // The bundle must be started.
        // Note that another thread may already be starting this bundle;
        // In this case we will timeout after a default of 5 seconds and record the BundleException
        long startTime = System.currentTimeMillis();
        Module m = managerElement.getGeneration().getRevision().getRevisions().getModule();
        try {
            // do not persist the start of this bundle
            secureAction.start(m, StartOptions.LAZY_TRIGGER);
        } catch (BundleException e) {
            Bundle bundle = managerElement.getGeneration().getRevision().getBundle();
            if (e.getType() == BundleException.STATECHANGE_ERROR) {
                String message = NLS.bind(Msg.ECLIPSE_CLASSLOADER_CONCURRENT_STARTUP, new Object[] { Thread.currentThread(), name, m.getStateChangeOwner(), bundle, new Long(System.currentTimeMillis() - startTime) });
                container.getLogServices().log(EquinoxContainer.NAME, FrameworkLogEntry.WARNING, message, e);
                continue;
            }
            String message = NLS.bind(Msg.ECLIPSE_CLASSLOADER_ACTIVATION, bundle.getSymbolicName(), Long.toString(bundle.getBundleId()));
            ClassNotFoundException error = new ClassNotFoundException(message, e);
            errors.put(managerElement, error);
            if (container.getConfiguration().throwErrorOnFailedStart) {
                container.getLogServices().log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, message, e, null);
                throw error;
            }
            container.getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, bundle, new BundleException(message, e));
        }
    }
}
Also used : ClasspathManager(org.eclipse.osgi.internal.loader.classpath.ClasspathManager) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) Module(org.eclipse.osgi.container.Module)

Example 2 with ClasspathManager

use of org.eclipse.osgi.internal.loader.classpath.ClasspathManager in project rt.equinox.framework by eclipse.

the class EquinoxBundle method getResource.

@Override
public URL getResource(String name) {
    try {
        equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
    } catch (SecurityException e) {
        return null;
    }
    checkValid();
    if (isFragment()) {
        return null;
    }
    ModuleClassLoader classLoader = getModuleClassLoader(false);
    if (classLoader != null) {
        return classLoader.getResource(name);
    }
    return new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null).findLocalResource(name);
}
Also used : ClasspathManager(org.eclipse.osgi.internal.loader.classpath.ClasspathManager) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader)

Example 3 with ClasspathManager

use of org.eclipse.osgi.internal.loader.classpath.ClasspathManager in project rt.equinox.framework by eclipse.

the class EquinoxBundle method getResources.

@Override
public Enumeration<URL> getResources(String name) throws IOException {
    try {
        equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
    } catch (SecurityException e) {
        return null;
    }
    checkValid();
    if (isFragment()) {
        return null;
    }
    ModuleClassLoader classLoader = getModuleClassLoader(false);
    Enumeration<URL> result = null;
    if (classLoader != null) {
        result = classLoader.getResources(name);
    } else {
        result = new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null).findLocalResources(name);
    }
    return result != null && result.hasMoreElements() ? result : null;
}
Also used : ClasspathManager(org.eclipse.osgi.internal.loader.classpath.ClasspathManager) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) URL(java.net.URL)

Example 4 with ClasspathManager

use of org.eclipse.osgi.internal.loader.classpath.ClasspathManager in project rt.equinox.framework by eclipse.

the class EclipseLazyStarter method preFindLocalClass.

@Override
public void preFindLocalClass(String name, ClasspathManager manager) throws ClassNotFoundException {
    if (initiatingClassName.get() == null) {
        initiatingClassName.set(name);
    }
    ModuleRevision revision = manager.getGeneration().getRevision();
    Module module = revision.getRevisions().getModule();
    // been initialized (though it may have been destroyed) so just return the class.
    if (alreadyActive.contains(module.getState()))
        return;
    // The bundle is not active and does not require activation, just return the class
    if (!shouldActivateFor(name, module, revision, manager))
        return;
    Deque<ClasspathManager> stack = activationStack.get();
    if (stack == null) {
        stack = new ArrayDeque<>(6);
        activationStack.set(stack);
    }
    // the initiating class has been defined (see postFindLocalClass)
    if (!stack.contains(manager)) {
        // only add the manager if it has not been added already
        stack.addFirst(manager);
    }
}
Also used : ClasspathManager(org.eclipse.osgi.internal.loader.classpath.ClasspathManager) Module(org.eclipse.osgi.container.Module) ModuleRevision(org.eclipse.osgi.container.ModuleRevision)

Example 5 with ClasspathManager

use of org.eclipse.osgi.internal.loader.classpath.ClasspathManager in project knime-core by knime.

the class EclipseUtil method findClasses.

/**
 * Searches the given class loader for classes that match the given class filter. This method is capable of
 * searching through Eclipse plug-ins but it will not recursivly search dependencies.
 *
 * @param filter a filter for classes
 * @param classLoader the class loader that should be used for searching
 * @return a collection with matching classes
 * @throws IOException if an I/O error occurs while scanning the class path
 * @since 2.12
 */
public static Collection<Class<?>> findClasses(final ClassFilter filter, final ClassLoader classLoader) throws IOException {
    List<URL> classPathUrls = new ArrayList<>();
    if (classLoader instanceof ModuleClassLoader) {
        ModuleClassLoader cl = (ModuleClassLoader) classLoader;
        ClasspathManager cpm = cl.getClasspathManager();
        for (ClasspathEntry e : cpm.getHostClasspathEntries()) {
            BundleFile bf = e.getBundleFile();
            classPathUrls.add(bf.getEntry("").getLocalURL());
        }
    } else if (classLoader instanceof URLClassLoader) {
        URLClassLoader cl = (URLClassLoader) classLoader;
        for (URL u : cl.getURLs()) {
            classPathUrls.add(u);
        }
    } else {
        FileLocator.toFileURL(classLoader.getResource(""));
    }
    // filter classpath for nested entries
    for (Iterator<URL> it = classPathUrls.iterator(); it.hasNext(); ) {
        URL url1 = it.next();
        for (URL url2 : classPathUrls) {
            if ((url1 != url2) && url2.getPath().startsWith(url1.getPath())) {
                it.remove();
                break;
            }
        }
    }
    List<Class<?>> classes = new ArrayList<>();
    for (URL url : classPathUrls) {
        if ("file".equals(url.getProtocol())) {
            String path = url.getPath();
            // path = path.replaceFirst(Pattern.quote(classPath) + "$", "");
            collectInDirectory(new File(path), "", filter, classes, classLoader);
        } else if ("jar".equals(url.getProtocol())) {
            String path = url.getPath().replaceFirst("^file:", "").replaceFirst("\\!.+$", "");
            collectInJar(new JarFile(path), filter, classes, classLoader);
        } else {
            throw new IllegalStateException("Cannot read from protocol '" + url.getProtocol() + "'");
        }
    }
    return classes;
}
Also used : ClasspathManager(org.eclipse.osgi.internal.loader.classpath.ClasspathManager) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) ClasspathEntry(org.eclipse.osgi.internal.loader.classpath.ClasspathEntry) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) JarFile(java.util.jar.JarFile) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) File(java.io.File)

Aggregations

ClasspathManager (org.eclipse.osgi.internal.loader.classpath.ClasspathManager)6 ModuleClassLoader (org.eclipse.osgi.internal.loader.ModuleClassLoader)4 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 File (java.io.File)2 JarFile (java.util.jar.JarFile)2 Module (org.eclipse.osgi.container.Module)2 URLClassLoader (java.net.URLClassLoader)1 ModuleRevision (org.eclipse.osgi.container.ModuleRevision)1 ClasspathEntry (org.eclipse.osgi.internal.loader.classpath.ClasspathEntry)1 Generation (org.eclipse.osgi.storage.BundleInfo.Generation)1 BundleEntry (org.eclipse.osgi.storage.bundlefile.BundleEntry)1 BundleFile (org.eclipse.osgi.storage.bundlefile.BundleFile)1 Bundle (org.osgi.framework.Bundle)1 BundleException (org.osgi.framework.BundleException)1