How to make servlet using OSGi

hello everyone..
Here I'll tell you how to make  Servlet  using Osgi framework.
Steps:-
1. Make new plug-ins project give the name as SimpleServlet.
 Make sure to select Equinox as the target OSGI framework on the first page of the wizard. Also make sure you’ve got an activator (that option is on by default).
 In menifest Eddition -> Dependencies.
import these packages.

* javax.servlet


* javax.servlet.http


* org.osgi.framework


* org.osgi.service.http


* org.osgi.util.tracker

2.make a new class (SimpleServlet)


package simpleservlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet  extends HttpServlet{
    private static final long serialVersionUID = 1L;

      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("Hello world!!");
      }
}

3.Now make a 2nd  new class for Servlet Service Tracker. (HttpServiceTracker)

package simpleservlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;
public class HttpServiceTracker  extends ServiceTracker{
   
     
      public HttpServiceTracker(BundleContext context) {
          super(context, HttpService.class.getName(), null);
        // TODO Auto-generated constructor stub
    }

    public Object addingService(ServiceReference reference) {
        HttpService httpService = (HttpService) super.addingService(reference);
        if (httpService == null)
          return null;

        try {
          System.out.println("Registering servlet at /simple");
          httpService.registerServlet("/simple", new SimpleServlet(), null, null);
        } catch (Exception e) {
          e.printStackTrace();
        }

        return httpService;
      }

      public void removedService(ServiceReference reference, Object service) {
        HttpService httpService = (HttpService) service;

        System.out.println("Unregistering /simple");
        httpService.unregister("/simple");

        super.removedService(reference, service);
      }

    }

4. Now Come to activator. which is main part of the OSGi.
Edit your Activator like This-
package simpleservlet;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {
    private HttpServiceTracker serviceTracker;
    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        serviceTracker = new HttpServiceTracker(context);
           serviceTracker.open();
    }

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;
         serviceTracker.close();
         serviceTracker = null;
    }

}

4.run configuration..
Make a New Osgi framework run. then go to tab  arguments -> VM arguments write another line

-Dorg.osgi.service.http.port=8080
now Apply and run.
5. go to your chrome and type
http://localhost:8080/simple.
it will print "Hello world"
 and tutorial for this artical  is available at http://youtu.be/nr0kNxjP-w0
Thank you!!


                                                                                                    Anshul Madan
                                                                         CASCADEMIC Solutins Pvt.Ltd

Comments

Popular posts from this blog

TOUCH SENSING SOFTWARE

LoRa and its suitability for IoT