javaapache-karafblueprint-osgiaries

How to inject apache karaf bundles as a service in the web application using aries blue print?


I have servlet web application and want to inject apache karaf bundles as a service in the web application using aries blueprint.

These are the Steps followed for injecting the bundles:

1) added reference tag with id and interface values in the blueprint.xml sample code is here

<reference id="jsonStore" interface="com.test.test.jsonstore.service.JsonClientStore" />

2) added bean tag with ref attribute as reference id, of bundles what we are injecting in the blueprint.xml file. sample code is here

<bean id="echo" class="com.test.test.core.jsonrequest.JsonServlet">
     <property name="jsonStore" ref="jsonStore"/>
   </bean>

3) blueprint.xml file location as context-param tag the web.xml. sample code is here

<context-param>
      <param-name>blueprintLocation</param-name>
      <param-value>OSGI-INF/blueprint/blueprint.xml</param-value>
    </context-param>

4) added listner class in the web.xml. sample code is here

<listener>
      <listener-class>org.apache.aries.blueprint.web.BlueprintContextListener</listener-class>
    </listener>

5) @Inject annotation for injecting the particular bundle in the servlet class. sample code is here

@Inject(ref="jsonStore")
    JsonClientStore jsonStore = null;

The following link is for reference documentation http://aries.apache.org/modules/blueprintweb.html

Still the bundles are not injected please some one can help on this ?

how to inject these karaf bundles as a service to the servlet application?


Solution

  • I solved the above problem without using aries blueprint.

    The following method i added in the servlet to get injected bundles, i will send the bundle name as serviceName parameter to the below method, this will send back the required service of injected bundle, by using that i will use the methods present in that service.

    private <T> T getService(Class<T> serviceName) {
            Bundle bundle = FrameworkUtil.getBundle(serviceName);
            if ( bundle == null ) {
                return null;
            }       
            BundleContext bundleContext = bundle.getBundleContext();
            ServiceReference serviceRef = bundleContext.getServiceReference(serviceName);  
            if (serviceRef == null)          
                return null;
            return (T) bundleContext.getService(serviceRef);
        }
    

    This code solved my problem.