On 9/2/13 04:13 , Nicolas Lalevée wrote:
Le 1 sept. 2013 à 22:28, Richard S. Hall <[email protected]> a écrit :

I'm pretty sure this isn't possible for resources, like it is for classes.

The OSGi spec doesn't mandate the format of bundle resource URLs, which is what 
you would need if you wanted to determine from which bundle a looked up 
resource comes.

Not sure about other frameworks, but this is fairly easy to determine from a 
resource URL in the Felix framework, since this host is the bundle id + 
revision id.
I can do an if(felix) in my code to optimize. In the long term can I rely on 
this URL scheme ?

Well, I don't think we are going to guarantee it since it is explicitly opaque and non-spec'ed, but it has been that way for a very long time.

-> richard


Nicolas


-> richard

On 9/1/13 11:31 , Nicolas Lalevée wrote:
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: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to