Search in sources :

Example 6 with BundleReference

use of org.osgi.framework.BundleReference in project aries by apache.

the class Util method fixContextClassloader.

public static void fixContextClassloader(String cls, String method, Class<?> clsArg, ClassLoader bundleLoader) {
    BundleReference br = getBundleReference(bundleLoader);
    if (br == null) {
        return;
    }
    final ClassLoader cl = findContextClassloader(br.getBundle(), cls, method, clsArg);
    if (cl != null) {
        BaseActivator.activator.log(Level.FINE, "Temporarily setting Thread Context Classloader to: " + cl);
        AccessController.doPrivileged(new PrivilegedAction<Void>() {

            @Override
            public Void run() {
                Thread.currentThread().setContextClassLoader(cl);
                return null;
            }
        });
    } else {
        BaseActivator.activator.log(Level.FINE, "No classloader found for " + cls + ":" + method + "(" + clsArg + ")");
    }
}
Also used : BundleReference(org.osgi.framework.BundleReference)

Example 7 with BundleReference

use of org.osgi.framework.BundleReference in project linuxtools by eclipse.

the class CheckDevhelp method testHelpTopic.

@Test
public void testHelpTopic() throws IOException {
    // We need to have a devhelp directory with contents to test.
    // Copy over the needed devhelp/html contents in this test plug-in,
    // test1.devhelp2 and index.html, to the workspace.
    ClassLoader cl = getClass().getClassLoader();
    Bundle bundle = null;
    if (cl instanceof BundleReference) {
        bundle = ((BundleReference) cl).getBundle();
    }
    IWorkspace ws = ResourcesPlugin.getWorkspace();
    IPath wslocpath = ws.getRoot().getLocation();
    // $NON-NLS-1$
    IPath outfilepath = wslocpath.append("devhelp/html/test1");
    File outfiledir = outfilepath.toFile();
    assertTrue(outfiledir.mkdirs());
    // $NON-NLS-1$
    outfilepath = outfilepath.append("test1.devhelp2");
    File outfile = outfilepath.toFile();
    // $NON-NLS-1$
    IPath outfilepath2 = wslocpath.append("devhelp/html/test1/index.html");
    File outfile2 = outfilepath2.toFile();
    outfile.createNewFile();
    outfile2.createNewFile();
    try (InputStream in = FileLocator.openStream(bundle, new Path("devhelp/html/test1/test1.devhelp2"), false)) {
        // $NON-NLS-1$
        Files.copy(in, Paths.get(outfile.toURI()), StandardCopyOption.REPLACE_EXISTING);
    }
    try (InputStream in2 = FileLocator.openStream(bundle, new Path("devhelp/html/test1/index.html"), false)) {
        // $NON-NLS-1$
        Files.copy(in2, Paths.get(outfile2.toURI()), StandardCopyOption.REPLACE_EXISTING);
    }
    // $NON-NLS-1$
    IPath x = wslocpath.append("devhelp/html");
    IPreferenceStore ps = DevHelpPlugin.getDefault().getPreferenceStore();
    ps.setValue(PreferenceConstants.DEVHELP_DIRECTORY, x.toOSString());
    DevHelpToc toc = new DevHelpToc();
    ITopic[] topics = toc.getTopics();
    // Verify we have the test1 topic
    assertTrue(topics.length > 0);
    ITopic topic = topics[0];
    // $NON-NLS-1$
    assertTrue(topic.getLabel().startsWith("test1"));
    ITopic[] subtopics = topic.getSubtopics();
    // Verify it has 4 or more sub-topics
    assertTrue(subtopics.length > 3);
    IUAElement[] elements = topic.getChildren();
    assertTrue(elements.length > 3);
    String href = topic.getHref();
    // Verify the topic href is the index.html file and that the topic is
    // enabled
    assertEquals(href, // $NON-NLS-1$
    "/org.eclipse.linuxtools.cdt.libhover.devhelp/test1/index.html");
    assertTrue(topic.isEnabled(null));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ITopic(org.eclipse.help.ITopic) IPath(org.eclipse.core.runtime.IPath) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) BundleReference(org.osgi.framework.BundleReference) IUAElement(org.eclipse.help.IUAElement) IWorkspace(org.eclipse.core.resources.IWorkspace) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) File(java.io.File) DevHelpToc(org.eclipse.linuxtools.internal.cdt.libhover.devhelp.DevHelpToc) Test(org.junit.Test)

Example 8 with BundleReference

use of org.osgi.framework.BundleReference in project logging-log4j2 by apache.

the class BundleContextSelector method getContext.

@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext, final URI configLocation) {
    if (currentContext) {
        final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
        if (ctx != null) {
            return ctx;
        }
        return getDefault();
    }
    // it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut
    if (loader instanceof BundleReference) {
        return locateContext(((BundleReference) loader).getBundle(), configLocation);
    }
    final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
    if (callerClass != null) {
        return locateContext(FrameworkUtil.getBundle(callerClass), configLocation);
    }
    final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
    return lc == null ? getDefault() : lc;
}
Also used : BundleReference(org.osgi.framework.BundleReference) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 9 with BundleReference

use of org.osgi.framework.BundleReference in project aries by apache.

the class Util method serviceLoaderLoad.

public static <C, S> ServiceLoader<S> serviceLoaderLoad(Class<S> service, Class<C> caller) {
    if (BaseActivator.activator == null) {
        // The system is not yet initialized. We can't do anything.
        return null;
    }
    ClassLoader bundleLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

        @Override
        public ClassLoader run() {
            return caller.getClassLoader();
        }
    });
    if (!(bundleLoader instanceof BundleReference)) {
        BaseActivator.activator.log(Level.FINE, "Classloader of consuming bundle doesn't implement BundleReference: " + bundleLoader);
        return ServiceLoader.load(service);
    }
    BundleReference bundleReference = (BundleReference) bundleLoader;
    final ClassLoader bundleClassloader = findContextClassloader(bundleReference.getBundle(), ServiceLoader.class.getName(), "load", service);
    if (bundleClassloader == null) {
        return ServiceLoader.load(service);
    }
    Thread thread = Thread.currentThread();
    return AccessController.doPrivileged(new PrivilegedAction<ServiceLoader<S>>() {

        @Override
        public ServiceLoader<S> run() {
            ClassLoader contextClassLoader = thread.getContextClassLoader();
            try {
                thread.setContextClassLoader(bundleClassloader);
                return ServiceLoader.load(service);
            } finally {
                thread.setContextClassLoader(contextClassLoader);
            }
        }
    });
}
Also used : ServiceLoader(java.util.ServiceLoader) BundleReference(org.osgi.framework.BundleReference)

Example 10 with BundleReference

use of org.osgi.framework.BundleReference in project aries by apache.

the class Util method serviceLoaderLoad.

public static <C, S> ServiceLoader<S> serviceLoaderLoad(Class<S> service, ClassLoader specifiedClassLoader, Class<C> caller) {
    if (BaseActivator.activator == null) {
        // The system is not yet initialized. We can't do anything.
        return null;
    }
    ClassLoader bundleLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

        @Override
        public ClassLoader run() {
            return caller.getClassLoader();
        }
    });
    if (!(bundleLoader instanceof BundleReference)) {
        BaseActivator.activator.log(Level.FINE, "Classloader of consuming bundle doesn't implement BundleReference: " + bundleLoader);
        return ServiceLoader.load(service, specifiedClassLoader);
    }
    BundleReference bundleReference = (BundleReference) bundleLoader;
    final ClassLoader bundleClassloader = findContextClassloader(bundleReference.getBundle(), ServiceLoader.class.getName(), "load", service);
    if (bundleClassloader == null) {
        return ServiceLoader.load(service, specifiedClassLoader);
    }
    return ServiceLoader.load(service, new WrapperCL(specifiedClassLoader, bundleClassloader));
}
Also used : ServiceLoader(java.util.ServiceLoader) BundleReference(org.osgi.framework.BundleReference)

Aggregations

BundleReference (org.osgi.framework.BundleReference)11 Bundle (org.osgi.framework.Bundle)6 File (java.io.File)2 InputStream (java.io.InputStream)2 ServiceLoader (java.util.ServiceLoader)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 IWorkspace (org.eclipse.core.resources.IWorkspace)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 Test (org.junit.Test)2 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Preference (org.apache.felix.ipojo.extender.internal.queue.pref.Preference)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ITopic (org.eclipse.help.ITopic)1 IUAElement (org.eclipse.help.IUAElement)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 FunctionInfo (org.eclipse.linuxtools.cdt.libhover.FunctionInfo)1 LibHoverInfo (org.eclipse.linuxtools.cdt.libhover.LibHoverInfo)1