You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Charles Moulliard <cm...@gmail.com> on 2011/04/13 12:23:36 UTC

Camel running into OSGI container - Lookup

When camel is deployed on OSGI container, it can automatically have
access to the OSGI Service Registry to find spring beans created and
deployed from other bundles. This lookup strategy has been coded into
the following method of the class OsgiServiceRegistry

    public Object lookup(String name) {
        Object service = serviceCacheMap.get(name);
        if (service == null) {
            // First Lookup using the name which can be a Class name, bean id
            ServiceReference sr = bundleContext.getServiceReference(name);

            if (sr != null) {
                // Need to keep the track of Service
                // and call ungetService when the camel context is closed
                serviceReferenceQueue.add(sr);
                service = bundleContext.getService(sr);
                if (service != null) {
                    serviceCacheMap.put(name, service);
                }
            }
        }
        return service;
    }

This lookup method is called when we would like to find a spring bean
referenced like that

<bean ref="referenceToBeRetrieved"/>

The "referenceToBeRetrieved" is not the id of a spring bean registered
but corresponds to the interface name "InterfaceExposedAsOSGIService"
exposed as an osgi service

<osgi:service id="serviceId" ref="beanId"
interface="InterfaceExposedAsOSGIService"/>

So, to find an OSGI Service on Karaf, Equinox, ... you must use the
following syntax

<bean ref="org.apache.camel.example.services.MyService"/>

This is not beautiful but is required as we can only lookup to find
the Services using the name of the class or name of the class + Filter
(= additional property link to the service).

When a Spring bean is defined, Spring adds the following property
"org.springframework.osgi.bean" with id name of the bean. We could use
this filter property to find our bean registered but that means that
we have to retrieve all the services registered and find the one
containing the property.

http://www.osgi.org/javadoc/r4v42/org/osgi/framework/BundleContext.html#getServiceReferences(java.lang.String,
java.lang.String)

What do you think that we must do ?
(1) Modify the camel-core-osgi code to also perform a lookup using the
filter without any class name
(2) Keep the code like now but update documentation to prevent users

Regards,

Charles Moulliard

Sr. Principal Solution Architect - FuseSource
Apache Committer

Blog : http://cmoulliard.blogspot.com
Twitter : http://twitter.com/cmoulliard
Linkedin : http://www.linkedin.com/in/charlesmoulliard
Skype: cmoulliard