Hi,

Am Dienstag, den 06.05.2008, 12:14 -0700 schrieb jaredmac:
> Is it true that each component in declarative services is inherently a
> singleton? That is to say, if I have something like this:
> 
> <component name="foo" immediate="true">
>     <implementation class="com.pkg.internal.FooImpl"/>
>     <service>
>         <provide interface="com.pkg.api.Foo"/>
>     </service>
> </component>
> 
> then there will only ever be one instance of FooImpl?

For this exact declaration, there will only be a single instance created
(and registered as a service) when the containing bundle is started.
When the containing bundle is stopped, the service is unregistered and
the instance dropped.

When the bundle is started again, a new instance is created and
registered as a service.

So, it is actually more precise to say, that at any moment in time,
there is only a single instance of the component existing, which is also
registered as Foo service.

>  My use case is that I
> want the application client to be able to say to Felix: give me a new
> instance of all implementors of "Foo". In my case, Foo implementations have
> state, and the application doesn't want to reuse an existing Foo.

What you want is probably a Foo factory: Your application asks the Foo
factory for instances of the Foo interface implementations.

> 
> I know that I can get multiple implementors of an interface from one bundle
> to another by doing this:
> 
> <component name="foo-consumer">
>     ...
>     <reference name="contributor"
>                interface="com.pkg.api.Foo"
>                bind="addFoo"
>                unbind="removeFoo"
>                cardinality="0..n"
>                policy="dynamic" />
> </component>
> 
> but that will give me a list each of the Foo implementors, where again each
> one is instantiated only one.

Yes, the reference element just instructs the Declarative Services
runtime to provide registered services to the foo-consumer. This is
exactly the same as accessing these services through the BundleContext.

> 
> I guess my question could be rephrased as: can an OSGi client request that a
> new instance of a particular service provider be created, as opposed to
> getting the existing instance?

Not as per the OSGi framework specification. You might of course - as I
said above - create a FooFactory and register that factory as a service.
Your application would then get the FooFactory service and ask that
FooFactory for Foo instances.

Hope this helps.

Regards
Felix


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to