You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Guido Spadotto <gs...@soluta.net> on 2010/02/22 12:50:55 UTC

ServiceReference.isAssignableTo

Hi all,
  below you'll find a snippet of code (whose maintainer I am not) that 
should safely cast the service
associated with a ServiceReference to a given interface.

if (sref[i].isAssignableTo(this.bundleContext.getBundle(), 
IMyInterface.class.getName())) {
                 log.debug("Service Reference [" + i + "] is assignable 
to IMyInterface");
                 return (IMyInterface) 
this.bundleContext.getService(sref[i]);
             }

But, looking at the OSGi API docs, I think that 
ServiceReference.isAssignableTo does NOT mean
that you can cast the associated Service to the "assignable" class.

Indeed, I get lots of ClassCastExceptions.

Should I instead do something like:
boolean isCastable = 
IMyInterface.class.isAssignableFrom(this.bundleContext.getService(sref[i]).getClass());

or is there an established, "best-practice" way of performing such a task?

I am pushing towards adopting Declarative Services, but it seems that 
they're not an option
for the code maintainer at the moment.

Thank you,
   Guido

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


Re: ServiceReference.isAssignableTo

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 2/22/10 8:15 PM, Guido Spadotto wrote:
> On 22/02/2010 13.02, Richard S. Hall wrote:
>> It is not clear to me why you are doing this at all. Are you using 
>> AllServiceListeners or something? If you just looking up a service 
>> via getServiceReferences() or a normal ServiceListener, the framework 
>> will only give you references with which your bundle is compatible.
>>
>> -> richard
>>
> Hi Richard,
>  the code is not under my control.
>
> Anyway, I think that the ultimate goal of it is:
> "Given a reference to a bundle, retrieve all its ServiceReferences
> and select those ones that represent instances of a given Interface".
>
> It would be very easy to do this with DS, but - as I said - I'm not in 
> control of that code.
> This is why I asked for the correct way of determining if the service 
> a service reference
> refers to is indeed an implementation of a given interface.
>
> I know it's pretty convoluted, but I have to deal with it.

Well, the isAssignableTo() method is used to determine if the passed in 
bundle and the registering bundle use the same version of the class 
passed in as a string. So technically, it is doing the right thing...

However, if it is just doing this on arbitrary bundles, not know if the 
provider is even remotely of the correct type, then I suppose it is 
possible you are getting the wrong answer since we try to make guesses 
when the provider has no access at all to the class.

For some tests, you could get the objectClass service property and print 
it out and see if it includes the desired interface and whether or not 
isAssignableTo() is returning true or not.

-> richard
>
> Guido

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


Re: ServiceReference.isAssignableTo

Posted by Guido Spadotto <gs...@soluta.net>.
On 22/02/2010 13.02, Richard S. Hall wrote:
> It is not clear to me why you are doing this at all. Are you using 
> AllServiceListeners or something? If you just looking up a service via 
> getServiceReferences() or a normal ServiceListener, the framework will 
> only give you references with which your bundle is compatible.
>
> -> richard
>
Hi Richard,
  the code is not under my control.

Anyway, I think that the ultimate goal of it is:
"Given a reference to a bundle, retrieve all its ServiceReferences
and select those ones that represent instances of a given Interface".

It would be very easy to do this with DS, but - as I said - I'm not in 
control of that code.
This is why I asked for the correct way of determining if the service a 
service reference
refers to is indeed an implementation of a given interface.

I know it's pretty convoluted, but I have to deal with it.

Guido

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


Re: ServiceReference.isAssignableTo

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 2/22/10 7:50 PM, Guido Spadotto wrote:
> Hi all,
>  below you'll find a snippet of code (whose maintainer I am not) that 
> should safely cast the service
> associated with a ServiceReference to a given interface.
>
> if (sref[i].isAssignableTo(this.bundleContext.getBundle(), 
> IMyInterface.class.getName())) {
>                 log.debug("Service Reference [" + i + "] is assignable 
> to IMyInterface");
>                 return (IMyInterface) 
> this.bundleContext.getService(sref[i]);
>             }
>
> But, looking at the OSGi API docs, I think that 
> ServiceReference.isAssignableTo does NOT mean
> that you can cast the associated Service to the "assignable" class.
>
> Indeed, I get lots of ClassCastExceptions.
>
> Should I instead do something like:
> boolean isCastable = 
> IMyInterface.class.isAssignableFrom(this.bundleContext.getService(sref[i]).getClass()); 
>
>
> or is there an established, "best-practice" way of performing such a 
> task?
>
> I am pushing towards adopting Declarative Services, but it seems that 
> they're not an option
> for the code maintainer at the moment.

It is not clear to me why you are doing this at all. Are you using 
AllServiceListeners or something? If you just looking up a service via 
getServiceReferences() or a normal ServiceListener, the framework will 
only give you references with which your bundle is compatible.

-> richard

>
> Thank you,
>   Guido
>
> ---------------------------------------------------------------------
> 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