Hi,
On Sun, Dec 22, 2013 at 2:27 AM, <[email protected]> wrote:
> Hello, i am trying to OSGify a non-OSGi library which is relying on loading
> resources from root of itself, resources which are not in any package, using
> something like
> Thread.currentThread().getContextClassLoader().getResourceAsStream("nifty.vs");
>
> I know that i could perform setContextClassloader() in a thread, but which
> classloader to choose? I need one, which would be able to load resources
> from root of any bundle, but is there any pre-made solutions for that?
> Currently i am using own classloader, which is iterating over all bundles
> from the system until it finds a match for resource, but i wonder if there
> any better solutions.
I think a better solution would be to
1. register a BundleTracker in your bundle's activator
2. whenever a new bundle is registered scan the bundle for *.vs files
3. maintain a mapping of vs files ( File name -> Bundle )
4. query the mapping of vs files when needed and load them using the
bundle you have retained in the mapping
5. clean up the mapping whenever a bundle is stopped
You could scan the entries using a call to bundle.findEntries('/',
'*.vs', false); ( all .vs files in the bundle's root directory, not
recursing ) and load the .vs files on demand using
bundle.getResource("nifty.vs"). The mapping from step 4 can be
registered as an OSGi service.
For some code examples, you can see the javax.activation bundle from
Sling [1], it uses the same basic approach, with the exception of
having a single file name it cares for.
As a bonus, if the bundles containing *.vs files are under your
control, you could make the lookups more efficient by recording their
location inside the MANIFEST.MF, e.g.
Vs-Files: nifty.vs, other.vs
to minimize the runtime cost of inspecting bundles.
Cheers,
Robert
[1]:
http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/javax.activation/
>
> Thanks.
>
> ---------------------------------------------------------------------
> 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]