Announcement Announcement Module
No announcement yet.
Injecting a reference to ServiceReference Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting a reference to ServiceReference

    This is not really a spring AP bug - its more of an equinox bug. Am hoping that someone from the spring team has contacts inside the equinox team and can get this moving on the fast track..

    If I inject ServiceReference and BundleContext into a bean, and try to resolve it to a service interface at runtime using something like:

    EchoService test = (EchoService) bundleContext.getService(serviceReference);

    - it fails with the following error:

    java.lang.ClassCastException: ServiceReferenceDelegate
    at org.eclipse.osgi.framework.internal.core.BundleCon textImpl.getService(
    at b3.impl.ClientServiceImpl.getEchoService(ClientSer
    at b3.impl.ClientServiceImpl$ java:23)

    The problem is in the org.eclipse.osgi codebase, where it assumes that the incoming object will be their custom implementation..

    class BundleContextImpl:
    public Object getService(org.osgi.framework.ServiceReference reference) {
    ServiceRegistrationImpl registration = ((ServiceReferenceImpl) reference).registration;

    This apart, what are the recommendations in order to achieve the following:

    Bundle B1 : EchoService (interface and implementation)
    Bundle B2: ClientBean (injected with reference to above service)

    Should ClientBean hold a reference to
    (1) the EchoService interface, or
    (2) the BundleContext & ServiceReference objects?

    The idea is to be able to update B1 (i.e. the EchoService implementation) with minimal impact to B2. Can B2 have compile time dependency on B1 (say we go with #2 above, but cast the output of bundleContext.getService() to EchoService) and still enjoy the hot-deploy capabilities promised by Spring AP?

  • #2
    Injecting a reference to ServiceReference


    thanks for reporting the issues. The cast to the Equinox class is unfortunate since it's a private class (as far as I know) and thus cannot be used by the client code. The use of a ServiceDelegate in Spring-DM is required since the proxy has a target class that changes and which requires the service reference to be constantly updated. To avoid this on the client side, the delegate is used to do the handling internally.
    I've raised an issue at:

    As a work around you can use Spring-DM for retrieving the OSGi the service for your - instead of using a service reference inject the original object managed by Spring-DM. That is, inside your configuration, define a property of the service type instead of ServiceReference and keep the same injection configuration.

    Costin Leau


    • #3
      Injecting a reference to ServiceReference


      I've just provided a fix for this problem in Spring-DM (see the aforementioned issue).
      The upcoming release of Spring-DM will contain the fix (not sure about the Platform release, probably beta4+).

      Costin Leau