Search code examples
urlclasspathosgiequinox

OSGi/Equinox: How do I convert a resource URI into a bundle name?


After looking up a resource on the classpath, I got this URL:

bundleresource://23.fwk1186515174/com/google/inject/Injector.class

How can I find out in which bundle provided the resource?

[EDIT] I'm trying to debug a problem where I have duplicate classes on the classpath. Here is the code I'm using:

private void debugClassPath() {
    String resource = "com/google/inject/Injector.class";
    try {
        Enumeration<URL> urls = getClass().getClassLoader().getResources( resource );

        while( urls.hasMoreElements() ) {
            System.out.println(urls.nextElement());
        }

        System.out.println("---");

        urls = XtextRunner.class.getClassLoader().getResources( resource );

        while( urls.hasMoreElements() ) {
            System.out.println(urls.nextElement());
        }
    } catch( IOException e ) {
        e.printStackTrace();
    }
}

That gives me several URLs for com.google.inject.Injector and I want to figure out which bundles add them to the classpath.


Solution

  • OSGi urls guarantee the hierarchy. So just replace com/google/inject/Injector.class with META-INF/MANIFEST.MF and read the resource as a manifest (or text file). The information in there tells you which bundle you're looking at.

    Duplicate classes can ONLY happen with split packages which is considered bad (very bad imho) practice. In OSGi, split packages require Require-Bundle with re-export or Bundle-Classpath. Life is a lot easier without these ...