1
votes

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?

1
What version of Karaf are you using? Do you see any error in the logs when you start your bundle?Alessandro Da Rugna
Thank you for your reply, i am using karaf vesion 4.2.0 and try to inject dependencies in the servlet application, i checked the karaf logs there are no errors mentioned for bundles. Did i miss anything in the above steps ?santhosh.K
If you see no errors I think your bundle is not starting. Is there a bundle publishing com.test.test.jsonstore.service.JsonClientStore service?Alessandro Da Rugna

1 Answers

0
votes

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.