felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicolas Lalevée <nicolas.lale...@hibnet.org>
Subject OSGi-aware resource loading
Date Sun, 01 Sep 2013 15:31:36 GMT
Hi,

Maybe my issue has already been addressed several time, so here is the actual question: how
can I get the bundle/classloader which *owns* the URL which I looked up through classloader.getResource(file)
?

If it's not clear, here is my context.

I am experimenting a build system where some Ant build files and Ant task would be managed
like as OSGi bundles. So I can do a modularisation of build files and jars of Ant Tasks.

At some point a build file (within a bundle) will have to load some other Ant script (through
the bundle wiring). For that I simply get the classloader of the current classloader and do
a classloader.getResource("/path/other/build.xml"). So far so good.
But when running that other build file, I would need its classloader to do some other import
of build.xml file. But I only have the resolved URL, not the bundle which is containing the
resolved script. Which java code, it's simple, from the resolved class I can get its classloader.
But I cannot do that for a script which as been resolved as an URL.

Here is what I have manage to do so far but I find it not pretty:

    URL buildUrl = currentClassLoader.getResource(buildFile);
    ClassLoader buildClassLoader = null;
    for (Bundle bundle : allBundles) {
        BundleWiring wiring = bundle.adapt(BundleWiring.class);
        int i = buildFile.lastIndexOf('/');
        String path = buildFile.substring(0, i);
        String name = buildFile.substring(i + 1);
        List<URL> entries = wiring.findEntries(path, name, 0);
        if (!entries.isEmpty() && containsUrls(entries, buildUrl)) {
            buildClassLoader = wiring.getClassLoader();
            break;
        }
    }
    if (buildClassLoader == null) {
        throw new RuntimeException("WTF! Unable to find the classloader of the build file
" + buildFile);
    }

It is working but it doesn't sound nice. Is there an API I didn't found which allows to look
for a resource and its bundle or classloader ? I would prefer an OSGi API, but if it's a Felix
one I don't mind.

Nicolas


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Mime
View raw message