users@glassfish.java.net

Re: running servlet in felix OSGi

From: Sahoo <sanjeeb.sahoo_at_oracle.com>
Date: Wed, 07 Sep 2011 15:45:27 +0530

Hi Shai,

Yes, that's documented in section #9.1 of our document [1]. You will
also find many examples referenced in that document.

I have copied users_at_glassfish.java.net so that the information is useful
to other users of GlassFish. Pl. copy your questions to that alias.

Thanks,
Sahoo

[1] http://glassfish.java.net/public/GF-OSGi-Features.pdf



On Wednesday 07 September 2011 03:36 PM, Shai Amar wrote:
> Thanks!
> You have no idea how you helped :)
> Thanks again.
>
> BTW
> Is this fact mentioned anywhere?
> If it is not, I'm sure other people would love to know about it.
>
> Regards
> Shai Amar
>
> On Wed, Sep 7, 2011 at 12:40 PM, Sahoo <sanjeeb.sahoo_at_oracle.com
> <mailto:sanjeeb.sahoo_at_oracle.com>> wrote:
>
> Yes, access http://localhost:8080/osgi/simple . All servlets
> registered using OSGi Http Service listen at context path /osgi.
>
> Thanks,
> Sahoo
>
> On Wednesday 07 September 2011 03:00 PM, Shai Amar wrote:
>> Hi Sahoo
>> Thanks for your reply.
>> I've download, installed and configure Glassfish 3.1.1
>> I also configure the OSGi web console.
>>
>> I've installed my web bundle successfully, but when I'm trying to
>> activate the URL bundle (http://localhost:8080/simple) it tells
>> me /HTTP status 404 //The requested resource () is not available./
>>
>> Am I missing something here?
>> Can you help me with this?
>>
>> Regards
>> Shai Amar
>>
>> On Wed, Sep 7, 2011 at 7:33 AM, Sahoo <sanjeeb.sahoo_at_oracle.com
>> <mailto:sanjeeb.sahoo_at_oracle.com>> wrote:
>>
>> GlassFish 3.1 didn't come preinstalled with osgi-http service
>> implementation. Pl. use GlassFish 3.1.1 which comes
>> preinstalled with osgi-http implementation.
>>
>> Thanks,
>> Sahoo
>>
>> On Tuesday 06 September 2011 09:11 PM, Shai Amar wrote:
>>
>> Hi
>>
>> I'm trying to run a simple servlet in Felix OSGi under
>> Glassfish 3.1
>> application server
>>
>> I've created 3 files:
>>
>> 1. *BackAppsOSGiSevletPOC*
>> 2. *ServletServiceTracker *
>> 3. *ServletActivator*
>>
>>
>> The files are specified below.
>>
>> Then, after I've installed the module, I've tried to
>> activate the servlet
>> via the request *http://localhost:8080/simple* and got
>> nothing.
>> I've tried to check and recheck what I've done and came
>> out empty.
>>
>> Can anyone tell me what I'm missing here?
>>
>>
>> *BackAppsOSGiSevletPOC *with the following code:
>>
>> -------------------------------------------------------------------------------------------------------------------------------------------
>> *package com.krynnlance.backapps.client.servlet;*
>> *
>> *
>> *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 BackAppsOSGiSevletPOC extends HttpServlet*
>> *{*
>> *
>> *
>> * private static final long serialVersionUID = 1L;*
>> * *
>> * /***
>> * * @see HttpServlet#doGet(HttpServletRequest request,
>> HttpServletResponse
>> response)*
>> * */*
>> * protected void doGet(HttpServletRequest request,
>> HttpServletResponse
>> response) throws ServletException, IOException *
>> * {*
>> * response.setContentType("text/plain");*
>> * response.getWriter().write("This is the Servlet module
>> [GET request]...");
>> *
>> * }*
>> *
>> *
>> *}*
>> -------------------------------------------------------------------------------------------------------------------------------------------
>>
>> *ServletServiceTracker* with the following code:
>>
>> -------------------------------------------------------------------------------------------------------------------------------------------
>> *package com.krynnlance.backapps.client.servlet;*
>> *
>> *
>> *import org.osgi.framework.BundleContext;*
>> *import org.osgi.framework.ServiceReference;*
>> *import org.osgi.service.http.HttpService;*
>> *import org.osgi.util.tracker.ServiceTracker;*
>> *
>> *
>> *public class ServletServiceTracker extends ServiceTracker*
>> *{*
>> * *
>> * public ServletServiceTracker(BundleContext context)*
>> * {*
>> * /***
>> * * The service tracker will be registered for a specific
>> class - *
>> * * in our case that's org.osgi.service.http.HttpService*
>> * */*
>> * super(context, HttpService.class.getName(), null);*
>> * }*
>> * *
>> * /***
>> * * When the service tracker is asked to return the
>> HttpService, *
>> * * we can register our servlet*
>> * */*
>> * 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
>> BackAppsOSGiSevletPOC(), null,
>> null);*
>> * }*
>> * catch (Exception e) *
>> * {*
>> * e.printStackTrace();*
>> * }*
>> * *
>> * return httpService;*
>> * }*
>> * *
>> * /***
>> * * Likewise, we must make sure to unregister the servlet
>> when the *
>> * * service tracker is asked to shut down the HttpService*
>> * */*
>> * public void removedService(ServiceReference reference,
>> Object service)*
>> * {*
>> * HttpService httpService = (HttpService)service;*
>> * *
>> * System.out.println("Unregistering /simple");*
>> * httpService.unregister("/simple");*
>> * *
>> * super.removedService(reference, service);*
>> * }*
>> *}*
>> -------------------------------------------------------------------------------------------------------------------------------------------
>>
>> *ServletActivator *with the following code:
>>
>> -------------------------------------------------------------------------------------------------------------------------------------------
>> *package com.krynnlance.backapps.client.servlet;*
>> *
>> *
>> *import org.osgi.framework.BundleActivator;*
>> *import org.osgi.framework.BundleContext;*
>> *
>> *
>> *public class ServletActivator implements BundleActivator*
>> *{*
>> * private ServletServiceTracker servletServiceTracker;*
>> * *
>> * *
>> * @Override*
>> * public void start(BundleContext ctx) throws Exception *
>> * {*
>> * servletServiceTracker = new ServletServiceTracker(ctx);*
>> * servletServiceTracker.open();*
>> * }*
>> *
>> *
>> * @Override*
>> * public void stop(BundleContext ctx) throws Exception *
>> * {*
>> * servletServiceTracker.close();*
>> * servletServiceTracker = null;*
>> * }*
>> *
>> *
>> *}*
>>
>> -------------------------------------------------------------------------------------------------------------------------------------------
>>
>> And the *pom.xml* woth the following code:
>>
>> -------------------------------------------------------------------------------------------------------------------------------------------
>> *<project xmlns="http://maven.apache.org/POM/4.0.0"
>> xmlns:xsi="
>> http://www.w3.org/2001/XMLSchema-instance"*
>> * xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
>> http://maven.apache.org/maven-v4_0_0.xsd">*
>> * *
>> *<!-- PARENT POM.XML -->*
>> *<parent>*
>> *<artifactId>backapps</artifactId>*
>> *<groupId>com.krynnlance</groupId>*
>> *<version>0.0.1</version>*
>> *</parent>*
>> *<modelVersion>4.0.0</modelVersion>*
>> *<groupId>com.krynnlance</groupId>*
>> *<artifactId>BackAppsOSGiSevletPOC</artifactId>*
>> *<packaging>bundle</packaging>*
>> *<version>1.0</version>*
>> *<name>BackApps OSGi Servlet Proof of Concept</name>*
>> *<description></description>*
>> *<organization>*
>> *<name>Krynnlance LTD</name>*
>> *<url>http://www.krynnlance.com</url>*
>> *</organization>*
>> *<dependencies>*
>> *
>> *
>> *<!-- JUNIT DEPENDENCY FOR TESTING -->*
>> *<dependency>*
>> *<groupId>junit</groupId>*
>> *<artifactId>junit</artifactId>*
>> *<version>3.8.2</version>*
>> *<scope>test</scope>*
>> *</dependency>*
>> * *
>> *<!-- Logging -->*
>> *<!--<dependency>*
>> *<groupId>log4j</groupId>*
>> *<artifactId>log4j</artifactId>*
>> *<version>1.2.16</version>*
>> *</dependency>-->*
>> * *
>> *<!-- This JAR suppose to be in the war package -->*
>> *<!-- BackApps Beans Dependencies -->*
>> *<dependency>*
>> *<groupId>com.krynnlance.backapps.common</groupId>*
>> *<artifactId>BackAppsCommon</artifactId>*
>> *<version>1.0</version>*
>> *</dependency>*
>> * *
>> *<!-- SERVLET -->*
>> *<dependency>*
>> *<groupId>org.glassfish</groupId>*
>> *<artifactId>javax.servlet</artifactId>*
>> *<version>3.1</version>*
>> *</dependency>*
>> * *
>> *<!-- OSGi modules dependencies -->*
>> * *
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>felix</artifactId>*
>> *<version>3.2.2</version>*
>> *</dependency>*
>> * *
>> *<!-- HTTP SERVICE -->*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.api</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.base</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.bridge</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.bundle</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.jetty</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.proxy</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<dependency>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>org.apache.felix.http.whiteboard</artifactId>*
>> *<version>2.0.4</version>*
>> *</dependency>*
>> *<!-- END OF HTTP SERVICE -->*
>> * *
>> *</dependencies>*
>> *<build>*
>> *<resources>*
>> *<resource>*
>> *<filtering>false</filtering>*
>> *<directory>src/main/resources</directory>*
>> *</resource>*
>> *<resource>*
>> *<filtering>false</filtering>*
>> *<directory>src/main/java</directory>*
>> *<includes>*
>> *<include>**</include>*
>> *</includes>*
>> *<excludes>*
>> *<exclude>**/*.java</exclude>*
>> *</excludes>*
>> *</resource>*
>> *</resources>*
>> *<testResources>*
>> *<testResource>*
>> *<filtering>false</filtering>*
>> *<directory>src/test/java</directory>*
>> *<includes>*
>> *<include>**</include>*
>> *</includes>*
>> *<excludes>*
>> *<exclude>**/*.java</exclude>*
>> *</excludes>*
>> *</testResource>*
>> *</testResources>*
>> *<plugins>*
>> *<plugin>*
>> *<inherited>true</inherited>*
>> *<groupId>org.apache.maven.plugins</groupId>*
>> *<artifactId>maven-compiler-plugin</artifactId>*
>> *<configuration>*
>> *<source>1.5</source>*
>> *<target>1.5</target>*
>> *<optimize>true</optimize>*
>> *<debug>true</debug>*
>> *</configuration>*
>> *</plugin>*
>> *<plugin>*
>> *<groupId>org.apache.felix</groupId>*
>> *<artifactId>maven-bundle-plugin</artifactId>*
>> *<extensions>true</extensions>*
>> *<configuration>*
>> *<instructions>*
>> *
>> <!--<Export-Package>com.krynnlance.backapps.common.logging</Export-Package>-->
>> *
>> *
>> <Bundle-Activator>com.krynnlance.backapps.client.servlet.ServletActivator</Bundle-Activator>
>> *
>> *</instructions>*
>> *</configuration>*
>> *</plugin>*
>> *<plugin>*
>> *<groupId>org.mortbay.jetty</groupId>*
>> *<artifactId>maven-jetty-plugin</artifactId>*
>> *</plugin> *
>> *</plugins>*
>> *</build>*
>> *<properties>*
>> *<jetty.version>6.1.4</jetty.version>*
>> *</properties>*
>> *</project>*
>> -------------------------------------------------------------------------------------------------------------------------------------------
>>
>>
>>
>>
>>
>> --
>> Regards
>>
>> Shai Amar
>>
>> Technologies manager, Founder
>> Krynnlance
>> www.krynnlance.com <http://www.krynnlance.com/>
>> www.backapps.com <http://www.backapps.com/>
>>
>
>
>
>
> --
> Regards
>
> Shai Amar
>
> Technologies manager, Founder
> Krynnlance
> www.krynnlance.com <http://www.krynnlance.com/>
> www.backapps.com <http://www.backapps.com/>
>