You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Daniel Spicar <ds...@apache.org> on 2012/01/18 15:33:48 UTC

Programatically create package dependencies for a bundle

Hi,

I have an issue with package dependencies.

We have a service that allows other components to register scala-based
templates at runtime (typically when the component activates). These
templates are then processed, compiled, and loaded dynamically. However
this process adds new package dependencies to the bundle (e.g. on scala,
but may also be other packages exported in the osgi environment). I can
solve the problem by using dynamic imports for the bundle. But I would
prefer to do this programatically, such that a user of the service does not
needs to declare dynamic imports or declare additional runtime package
dependencies manaully.

Can this be done somehow in felix and how?

I looked at dependency admin and wire admin. But I am not sure which (if
any) is the right approach.

Best,
Daniel

Re: Programatically create package dependencies for a bundle

Posted by Daniel Spicar <da...@trialox.org>.
Ok, thank you all for your advice.

Re: Programatically create package dependencies for a bundle

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 1/18/12 10:29 , Daniel Spicar wrote:
> On 18 January 2012 16:11, Richard S. Hall<he...@ungoverned.org>  wrote:
>
>> I agree it isn't the best, since all service clients would need to add the
>> dynamic import. Perhaps you need to think about restructuring your service.
>> Since the clients must not have a direct dependency on the new packages
>> (since they were compiled without it), you could look into push the
>> dependencies out into the service and provide a "bigger" service that hides
>> the new dependencies, then only the service bundle would need the dynamic
>> import.
>>
>>   ->  richard
>>
> We did have the setup with the big service hiding these dependencies
> before. But this caused 'problematic' dependencies. Then the big service
> depends on packages imported in the templates. Whenever one of these was
> changed, and a refresh has been issued, the big service was refreshed and
> along with it all its clients. In our case we have lots of clients and this
> caused a cascade of restarting bundles and ended with nearly every bundle
> being restarted. That's the reason for this effort. The idea was to get rid
> of these dependencies and make the clients depend on what the use, such
> that the affected services would correcpond with their actualy requirements.
>

Then I think dynamic imports are about as good as it is going to get, 
since there really isn't a way to programmatically add imports to bundles.

-> richard


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


Re: Programatically create package dependencies for a bundle

Posted by Daniel Spicar <da...@trialox.org>.
On 18 January 2012 16:11, Richard S. Hall <he...@ungoverned.org> wrote:

>
> I agree it isn't the best, since all service clients would need to add the
> dynamic import. Perhaps you need to think about restructuring your service.
> Since the clients must not have a direct dependency on the new packages
> (since they were compiled without it), you could look into push the
> dependencies out into the service and provide a "bigger" service that hides
> the new dependencies, then only the service bundle would need the dynamic
> import.
>
>  -> richard
>

We did have the setup with the big service hiding these dependencies
before. But this caused 'problematic' dependencies. Then the big service
depends on packages imported in the templates. Whenever one of these was
changed, and a refresh has been issued, the big service was refreshed and
along with it all its clients. In our case we have lots of clients and this
caused a cascade of restarting bundles and ended with nearly every bundle
being restarted. That's the reason for this effort. The idea was to get rid
of these dependencies and make the clients depend on what the use, such
that the affected services would correcpond with their actualy requirements.

Re: Programatically create package dependencies for a bundle

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 1/18/12 09:33 , Daniel Spicar wrote:
> Hi,
>
> I have an issue with package dependencies.
>
> We have a service that allows other components to register scala-based
> templates at runtime (typically when the component activates). These
> templates are then processed, compiled, and loaded dynamically. However
> this process adds new package dependencies to the bundle (e.g. on scala,
> but may also be other packages exported in the osgi environment). I can
> solve the problem by using dynamic imports for the bundle. But I would
> prefer to do this programatically, such that a user of the service does not
> needs to declare dynamic imports or declare additional runtime package
> dependencies manaully.
>
> Can this be done somehow in felix and how?
>
> I looked at dependency admin and wire admin. But I am not sure which (if
> any) is the right approach.

These have nothing to do with what you want.

Technically, there are three ways to add imports:

 1. Dynamic imports
 2. Fragments
 3. Byte-code weaving hooks.

None of them do exactly what you want, but probably dynamic imports are 
the easiest, especially if the packages are all in some common prefix so 
you could just dynamically import it (e.g., org.foo.*).

I agree it isn't the best, since all service clients would need to add 
the dynamic import. Perhaps you need to think about restructuring your 
service. Since the clients must not have a direct dependency on the new 
packages (since they were compiled without it), you could look into push 
the dependencies out into the service and provide a "bigger" service 
that hides the new dependencies, then only the service bundle would need 
the dynamic import.

-> richard

>
> Best,
> Daniel
>

Re: Programatically create package dependencies for a bundle

Posted by Arjun Panday <ar...@alcatel-lucent.com>.
Hi Daniel,

I had a similar issue recently when using javassist within OSGi.
Instead of loading the generated class directly from the bundle 
classloader, I created an additional custom ClassLoader which "merges" 
two bundle classloaders; the classloader of the component and a "parent" 
classloader which has a view on the extra packages (scala and so on), 
typically the bundle that generates the code at runtime.

Note: In my case I could not use ResolverHook because I was not trying 
to modify at runtime an existing class loaded by a bundle but rather 
generate a brand new class from scratch.

I don't know if it applies in your case but here's what it looks like:
("overDefineClass" is just here to work around the fact that defineClass 
is protected)

static class ProxyClassLoader extends ClassLoader {
       private ClassLoader _parent;
       public ProxyClassLoader(ClassLoader cl1, ClassLoader cl2) {
         super(cl1);
         if (cl2 != null) _parent = cl2;
       }
       public Class overDefineClass(String name, byte[] b, int o, int l) {
         return super.defineClass(name, b, o, l);
       }
       public Class<?> loadClass(String name) throws 
ClassNotFoundException {
         try {
           return super.loadClass(name);
         } catch(ClassNotFoundException e) {
           if (_parent != null) try {
             return _parent.loadClass(name);
           } catch(ClassNotFoundException e2) {
             return ClassLoader.getSystemClassLoader().loadClass(name);
           }
           else
             throw e;
         }
       }
   }


-arjun



On 01/18/2012 03:33 PM, Daniel Spicar wrote:
> Hi,
>
> I have an issue with package dependencies.
>
> We have a service that allows other components to register scala-based
> templates at runtime (typically when the component activates). These
> templates are then processed, compiled, and loaded dynamically. However
> this process adds new package dependencies to the bundle (e.g. on scala,
> but may also be other packages exported in the osgi environment). I can
> solve the problem by using dynamic imports for the bundle. But I would
> prefer to do this programatically, such that a user of the service does not
> needs to declare dynamic imports or declare additional runtime package
> dependencies manaully.
>
> Can this be done somehow in felix and how?
>
> I looked at dependency admin and wire admin. But I am not sure which (if
> any) is the right approach.
>
> Best,
> Daniel


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


Re: Programatically create package dependencies for a bundle

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 1/18/12 10:02 , Felix Meschberger wrote:
> Hi,
>
> I think ResolverHooks can be used for this.
>
> AFAICT this is supported in the latest Felix framework release.

Byte-code weaving hooks, actually, but I definitely wouldn't recommend 
using them. They are low-level and besides, they are only triggered on 
class loads from the bundle itself, so if they are not a good way to add 
run-time dependencies since if the bundle has already loaded all of its 
classes, the hooks will never be triggered again.

-> richard

>
> Regards
> Felix
>
> Am 18.01.2012 um 15:33 schrieb Daniel Spicar:
>
>> Hi,
>>
>> I have an issue with package dependencies.
>>
>> We have a service that allows other components to register scala-based
>> templates at runtime (typically when the component activates). These
>> templates are then processed, compiled, and loaded dynamically. However
>> this process adds new package dependencies to the bundle (e.g. on scala,
>> but may also be other packages exported in the osgi environment). I can
>> solve the problem by using dynamic imports for the bundle. But I would
>> prefer to do this programatically, such that a user of the service does not
>> needs to declare dynamic imports or declare additional runtime package
>> dependencies manaully.
>>
>> Can this be done somehow in felix and how?
>>
>> I looked at dependency admin and wire admin. But I am not sure which (if
>> any) is the right approach.
>>
>> Best,
>> Daniel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>

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


Re: Programatically create package dependencies for a bundle

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

I think ResolverHooks can be used for this.

AFAICT this is supported in the latest Felix framework release.

Regards
Felix

Am 18.01.2012 um 15:33 schrieb Daniel Spicar:

> Hi,
> 
> I have an issue with package dependencies.
> 
> We have a service that allows other components to register scala-based
> templates at runtime (typically when the component activates). These
> templates are then processed, compiled, and loaded dynamically. However
> this process adds new package dependencies to the bundle (e.g. on scala,
> but may also be other packages exported in the osgi environment). I can
> solve the problem by using dynamic imports for the bundle. But I would
> prefer to do this programatically, such that a user of the service does not
> needs to declare dynamic imports or declare additional runtime package
> dependencies manaully.
> 
> Can this be done somehow in felix and how?
> 
> I looked at dependency admin and wire admin. But I am not sure which (if
> any) is the right approach.
> 
> Best,
> Daniel


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