[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [equinox-dev] Registering two services in the same bundle
|
David,
While Thomas's suggestion is absolutely correct, I think you need to step back from the problem a little and ask if this is the right approach.
If your consumer bundle needs the Interface1 service then it should simply bind to an instance of Interface1. If it also needs the Interface2 service then it should just bind to Interface2. The consumer should not *require* both services to be offered by the same object, because this will severely limit the ability of other producers to offer an alternative implementation for those services. In other words, you are coupling your consumer to an implementation detail of the producer which it shouldn't care about.
On the other hand, if your API is such that these interfaces really should be published and consumed as a unit, then maybe you should just merge them into one interface, or declare a new Interface3 that extends both Interface1 and Interface2.
Neil
On Mon, Nov 16, 2009 at 3:38 PM, David Conde
<dconde@xxxxxxxx> wrote:
Hi Thomas,
Thank you for your answer,
So once I get ServiceReference = X getServiceReferences
using the filter that you told me "(&(objectClass=Interface1)(objectClass=Interface2))",
I have to use two castings for getting two different services like Object1 =
(Interface1) BundleContext.getReference (X) and Object2= (Interface2)
BundleContext.getReference(X), and then use two instances of the same object
for using two different interfaces implemented by this same object, am I right?
Is there any other mechanism for using the same object for two different
interfaces, because really the object provides two different services (2
different interfaces)
Thank you in advance
David
You can use the
registerService(java.lang.String[] clazzes,java.lang.Object service,
java.util.Dictionary properties) to register your service object under multiple
service interfaces. This will allow you to control the registration with a
single ServiceRegistration object. But in order to get a ServiceReference that
is guaranteed to implement both service interfaces then you must use the
getServiceReferences(String clazz, String filter) with a null clazz and a
filter that looks something like this:
"(&(objectClass=Interface1)(objectClass=Interface2))"
This will ensure that the ServiceReference objects returned implement both
Interface1 and Interface2. This should allow you to cast the Object returned
from getService(ServiceReference) to both Interface1 and Interface2 in your
service using client bundle. But for client code I recommend you use an
injection mechanism (like Declarative Services, Spring DM or Blueprint) if
possible so you don't have to worry about tracking the services yourself.
Tom
"David Conde" ---11/16/2009 02:34:18 AM---Hi,

From:
|

"David Conde"
<dconde@xxxxxxxx>
|

To:
|

"'Equinox development mailing list'"
<equinox-dev@xxxxxxxxxxx>
|

Date:
|

11/16/2009 02:34 AM
|

Subject:
|

[equinox-dev] Registering two services in the
same bundle
|
Hi,
I would like to know what I have to do
for registering two different services in a same bundle. I mean, I have two
create a service object which implements two different interfaces (registering
two different services). I thought about using registerService(java.lang.String[]
clazzes,java.lang.Object service,
java.util.Dictionary
properties) by including Interface1.class.getName() and
Interface2.class.getName() in String array called clazzes.
However, I found as a problem
that if I want to use this object service I have to get two service objects,
one for Interface1 and another one for Interface2 by doing
BundleContext.getServiceReference(Interface1) &
BundleContext.getServiceReference(Interface2), am I missing anything?Does it
exist another way to do that?
Thanks in advance _______________________________________________
equinox-dev mailing list
equinox-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/equinox-dev
_______________________________________________
equinox-dev mailing list
equinox-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/equinox-dev