Announcement Announcement Module
Collapse
No announcement yet.
Injecting a reference to ServiceReference Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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: org.springframework.osgi.service.importer.support. ServiceReferenceDelegate
    at org.eclipse.osgi.framework.internal.core.BundleCon textImpl.getService(BundleContextImpl.java:890)
    at b3.impl.ClientServiceImpl.getEchoService(ClientSer viceImpl.java:33)
    at b3.impl.ClientServiceImpl$1.run(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

    Hi,

    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: http://jira.springframework.org/browse/OSGI-484

    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.

    Thanks,
    Costin Leau

    Comment


    • #3
      Injecting a reference to ServiceReference

      Hi,

      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+).

      Cheers,
      Costin Leau

      Comment

      Working...
      X