Service Factory... [message #88197] |
Wed, 16 May 2007 09:01 |
Christian Campo Messages: 597 Registered: July 2009 |
Senior Member |
|
|
Hi,
I am trying to define a Service Factory for OSGi Services. That means
each and every time a component tries to get access to an OSGi Service,
I like to control the process of creating a specific instance for it.
Second condition is that the component that requests the OSGi Service
should be unaware of the use of a factory and last not least I like to
do that either by API and also by using Declarative Services.
API wise I found a solution like this:
I register a class that implements ServiceFactory in the Bundle Context.
Activator:
context.registerService(ICustomerSearch.class.getName(), new
ServiceBuilder(), null);
If my Component now access the the ICustomerSearch like this:
ServiceReference ref =
context.getBundleContext().getServiceReference(ICustomerSear ch.class.getName());
Object search = context.getBundleContext().getService(ref);
The result of the last statement is that in the ServiceBuilder the
method getService is called:
public Object getService(Bundle bundle, ServiceRegistration reg)
where I can perfectly create service for each call or cache instances or
whatever.
Doing the same step by step declarative does not work. If I disable the
registerService in the Activator, leave the component unchanged and
register the ServiceBuilder in XML like this:
<component name="xx.CustomerService2" immediate="true">
<implementation class="xx.ServiceBuilder"/>
<service>
<provide interface= "xx.ICustomerSearch"/>
</service>
</component>
The call to getService in the component fails because ServiceBuilder
does not implement the instance ICustomerSearch.
I am also aware that there is a factory pattern in DS. To use it you had
the say:
<component name="xx.CustomerService2" factory="xx.ServiceBuilder">
<implementation class="xx.ServiceBuilder"/>
<service>
<provide interface= "xx.ICustomerSearch"/>
</service>
</component>
However that registers the ServiceBuilder as ComponentFactory, the spec
says that I can retrieve the ComponentFactory for a specific service
(which I can filter with the properties) and the call the newInstance
method which would give me a new ICustomerSearch instance. However that
would just the way the Component retrieves an OSGi service instance. The
Component must remain unchanged by the fact that I use a factory.
Another issue that I want to use dependency injection. So I like to
inject a instance that is created by a regular definition as well as if
it is created by a factory. The definition for the dependency inject
(that replaces the lookup above) looks like this:
<component name="xx.Component" immediate="true">
<implementation class="xx.Component"/>
<reference name="ref.customersearch"
interface="xx.ICustomerSearch"
bind="bind"
unbind="unbind"
/>
<service>
<provide interface= "xx.ICustomerSearch"/>
</service>
</component>
This works if the ICustomerSearch is created by API or my a regular
definition (that specifies an implementing class). If define
ICustomerSearch with DS as a factory, nothing happens --> the component
is not instances, not activated and not bound to an instance of
ICustomerSearch.
Does that all make sense ? Can somebody help me ?
thanks a lot in advance
christian campo
|
|
|
Powered by
FUDForum. Page generated in 0.03121 seconds