You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by bokie <jm...@gmail.com> on 2012/11/10 14:18:44 UTC
DependencyManager - ResourceDependency
Hi,
There must be something about the ResourceDependency which I don't
understand because nothing happens in the class that depends on a specific
resource.
I have embedded a text file with my bundle (/res/file.txt).
In my bundle Activator, which extends DependencyActivatorBase I have added
the dependency with the DependencyManager in the init method:
public void init(final BundleContext context, final DependencyManager
dependencyManager) throws Exception {
dependencyManager.add(
createComponent()
.setImplementation(ResDependent.instance())
.add(createResourceDependency()
.setResource(context.getBundle().getEntry("/res/file.txt"))
.setRequired(true)
)
);
}
If I set required = true, ResDependent.start does not get called.
If I set required = false, ResDependent.start gets called but nothing else
happens.
What am I missing here?
Thanks,
Bokie
--
View this message in context: http://apache-felix.18485.n6.nabble.com/DependencyManager-ResourceDependency-tp5000335.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: DependencyManager - ResourceDependency
Posted by Marcel Offermans <ma...@luminis.nl>.
Hello Bokie,
On Nov 11, 2012, at 13:11 PM, bokie <jm...@gmail.com> wrote:
> 1. If my ResourceProvider provides two resources; /res/file1 and /res/file2,
> the "added" callback of the impl class that I've defined as having a
> dependency on /res/file1 gets called twice even though I've
> setFilter("(path=/res/file1)") - seems like I'm not dealing properly with
> the filter mechanism somewhere.
The provider you implement should deal with the filter. I think the test case had some code for that as well. The reason to let the provider deal with the filters is that the provider should be capable of doing this in an optimal manner.
> 2. What would you suggest is the best policy to handle resources in a more
> "bundle private" manner - it seems that the DependencyManager -
> ResourceDependency is great for cases where you want to say: Hey, anyone
> who's interested in resource A, here you go, I'm providing it.
There is a different design pattern you could use, the extender pattern. Let's say you have bundles that have resources that are used for a very specific purpose (maybe they're exposed via a web server). What you could do is define a new manifest header for these bundles, let's call it:
X-WebResources: res/
Now, with the dependency manager, you could make a bundle that listens to all bundles that have this header. You can use the bundle dependency for that:
manager.add(createComponent()
.setImplementation(WebResourceHandler.class)
.add(createBundleDependency()
.setStateMask(Bundle.ACTIVE)
.setFilter("(X-WebResources=*)")
.setCallbacks("add", "remove")));
This will make a component that listens to all bundles that are ACTIVE (so if you stop a bundle its resources will go away) and it will only consider bundles that have a manifest header X-WebResources (the filter will work against the whole manifest, so you can filter on any entries you like).
class WebResourceHandler {
public void add(Bundle b) { ... }
public void remove(Bundle b) { ... }
}
In the add/remove you would actually extract the resources from the bundle somehow and do something useful with them.
If your resources are always in a bundle, this extender pattern is probably the easiest solution. If the resources can be anywhere, the resource abstraction that the dependency manager offers is the more flexible solution.
> 3. Is there another way of getting the BundleContext injected besides
> defining a field like this (perhaps method injection) that I am not aware
> of:
> private volatile BundleContext context;
No, there currently only is field injection. You can however specify the name of the field, should you have multiple fields of the type BundleContext.
Greetings, Marcel
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: DependencyManager - ResourceDependency
Posted by bokie <jm...@gmail.com>.
Hi Marcel,
Thanks for input, I was somewhat able to get it to work with the test case
you pointed me to.
I do have some questions though:
1. If my ResourceProvider provides two resources; /res/file1 and /res/file2,
the "added" callback of the impl class that I've defined as having a
dependency on /res/file1 gets called twice even though I've
setFilter("(path=/res/file1)") - seems like I'm not dealing properly with
the filter mechanism somewhere.
2. What would you suggest is the best policy to handle resources in a more
"bundle private" manner - it seems that the DependencyManager -
ResourceDependency is great for cases where you want to say: Hey, anyone
who's interested in resource A, here you go, I'm providing it.
3. Is there another way of getting the BundleContext injected besides
defining a field like this (perhaps method injection) that I am not aware
of:
private volatile BundleContext context;
Thank again and regards,
Bokie
--
View this message in context: http://apache-felix.18485.n6.nabble.com/DependencyManager-ResourceDependency-tp5000335p5000340.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: DependencyManager - ResourceDependency
Posted by Marcel Offermans <ma...@luminis.nl>.
Hello Bokie,
On Nov 10, 2012, at 14:18 PM, bokie <jm...@gmail.com> wrote:
> There must be something about the ResourceDependency which I don't
> understand because nothing happens in the class that depends on a specific
> resource.
>
> I have embedded a text file with my bundle (/res/file.txt).
> In my bundle Activator, which extends DependencyActivatorBase I have added
> the dependency with the DependencyManager in the init method:
>
> public void init(final BundleContext context, final DependencyManager
> dependencyManager) throws Exception {
> dependencyManager.add(
> createComponent()
> .setImplementation(ResDependent.instance())
> .add(createResourceDependency()
> .setResource(context.getBundle().getEntry("/res/file.txt"))
> .setRequired(true)
> )
> );
> }
>
> If I set required = true, ResDependent.start does not get called.
> If I set required = false, ResDependent.start gets called but nothing else
> happens.
>
> What am I missing here?
A resource in the context of the dependency manager is an abstract thing that is identified by a URL. So a resource can be anything that can be represented by a URL. That means resources can come from (a folder in) a bundle, the filesystem, some remote location or anywhere else you can point a URL.
What you are missing is that the dependency manager currently does not come with any implementation of a resource provider. Therefore, whenever you define a resource dependency (like in your example) you will find it's not there.
What you need to provide yourself, is a resource provider that "serves" all resources in your bundle. Now you're not completely on your own here, as there are tests that you can use as examples. Take a look at:
in: test/src/main/java
org.apache.felix.dm.impl.ResourceAdapterTest
It contains a nested static class called ResourceProvider. It is not exactly what you want, because this implementation serves resources from a static list (with one item for this test) but it should be enough to get you going.
Hope this helps.
Greetings, Marcel
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org