>true, and have to fix this later but as you can see my
HelloWorldServlet-Bundle is actually the last to be started, so the
HttpService should already be available, shouldn't it?

Not necessarily. The story with Http Bridge stuff is bit convoluted. The
bridge bundle Activator registers a DispatcherServlet. The Felix proxy
servlet registers a tracker for that servlet and upon its registration it
triggers a call which leads to registration of HttpService. In brief the
HttpService would be registered in asynchronous manner and *might* not be
present when your activator gets called

>i rewrote the EmbeddedServletActivator to use a ServiceTracker

The way you are using the tracker would not work. If you immediately call
the getService in activator you are effectively having the same logic which
does not used tracker. Instead you should use tracker as mentioned in [1].
Below is your code modified to use tracker (sort of example). The main
change here is that you register the servlet in 'addingService' callback in
tracker.

public class EmbeddedServletActivator implements BundleActivator{
    public static final String ALIAS = "/hello";
    private HttpTracker tracker;

    public void start(BundleContext context)
            throws Exception
    {
        ServiceReference sRef =
context.getServiceReference(HttpService.class.getName());
        if (sRef != null)
        {
            HttpService service = (HttpService)context.getService(sRef);
            service.registerServlet("/hello", new HelloWorldServlet(),
null, null);
        } else {
            System.out.println("no HttpService found to register Servlet
for /hello");
        }

        tracker = new HttpTracker(context);
        tracker.open();

    }

    public void stop(BundleContext context)
            throws Exception
    {
        ServiceReference sRef =
context.getServiceReference(HttpService.class.getName());
        if (sRef != null)
        {
            HttpService service = (HttpService)context.getService(sRef);
            try {
                service.unregister("/hello");
            } catch (IllegalArgumentException e) {
                System.out.println("/hello wasn't registered in
HttpService");
            }
        }
        else {
            System.out.println("not HttpService found to unregister Servlet
for /hello");
        }

        if(tracker != null){
            tracker.close();
        }
    }

    private class HttpTracker extends ServiceTracker {

        public HttpTracker(BundleContext context) {
            super(context, HttpService.class.getName(), null);
        }

        public Object addingService(ServiceReference reference) {
            HttpService service = (HttpService)
super.addingService(reference);
            try {
                service.registerServlet("/hello", new HelloWorldServlet(),
null, null);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (NamespaceException e) {
                e.printStackTrace();
            }
            System.out.println("Registered Servlet for /hello");
            return service;
        }

        public void remove(ServiceReference reference) {
            ((HttpService)getService()).unregister("/hello");
            super.remove(reference);
        }
    }
}


regards
Chetan
[1]  http://www.aqute.biz/Snippets/Tracker

Chetan Mehrotra


On Thu, Mar 14, 2013 at 7:34 PM, Felix Meschberger <fmesc...@adobe.com>wrote:

> Hi
>
> Could it be that the Http Service API exists twice in your framework ? And
> thus your sample bundle and the Web Console and bridge bundles use
> different API wirings ?
>
> You can cross check in the web console to which bundle a specific package
> is wired by looking at the details of a bundle and checking the imports
> (which contains the wire sources)
>
> Regards
> Felix
>
> Am 14.03.2013 um 14:35 schrieb Stephan Schröder:
>
> >> Can you verify the HttpService is available at all in the framework ?
> >> (You might try to access the web console)
> > The web console seems to be available. Requesting "
> http://localhost:8080/system/console/bundles"; leads to:
> > 'A username and password are being requested by http://localhost:8080.
> The site says: "OSGi Management Console"'
> > So i assume the HttpService is available.
> >
> > i rewrote the EmbeddedServletActivator to use a ServiceTracker
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> >    @Override
> >    public void start(BundleContext context) throws Exception
> >    {
> >        httpServiceTracker = new ServiceTracker(context,
> HttpService.class.getName(), null);
> >        httpServiceTracker.open();
> >
> >        System.out.println("Looking for HttpService");
> >        final HttpService httpService =
> (HttpService)httpServiceTracker.getService();
> >
> >        if(httpService!=null) {
> >            System.out.println("Found HttpSerice");
> >            httpService.registerServlet(ALIAS, new HelloWorldServlet(),
> null, null);
> >            System.out.println("registered "+ALIAS+" with HttpService");
> >        }else{
> >            System.out.println("httpServiceTracker.getService() returned
> null");
> >        }
> >    }
> > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> > which lead to
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> > Starting bundle [com.mobenga.HelloWorldServletEmbedded]
> > Looking for HttpService
> > httpServiceTracker.getService() returned null
> > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> > so no change there.
> >
> > /Stephan
> >
> >
> > Gesendet: Donnerstag, 14. März 2013 um 13:46 Uhr
> > Von: "Felix Meschberger" <fmesc...@adobe.com>
> > An: "users@felix.apache.org" <users@felix.apache.org>
> > Betreff: Re: No HttpService available
> > Hi
> >
> > Can you verify the HttpService is available at all in the framework ?
> (You might try to access the web console)
> >
> > If not, you might have an issue to not properly setup the Http Service
> bridge: This requires wiring from the servlet which is actually deployed in
> the servlet container. See [1] and the linked examples for details.
> >
> > Regards
> > Felix
> >
> > [1]
> http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html#using-the-servlet-bridge
> >
> > Am 14.03.2013 um 12:50 schrieb Stephan Schröder:
> >
> >> Hi,
> >>
> >> i want to write a HelloWorld-Servlet using Felixes HttpServie.
> >> The setup is to embedd Felix and the HelloWorldServlet in a war-file
> and deploy this on Tomcat6.
> >>
> >> The BundleActivator for the HelloWorldServletBundle checks whether a
> HttpServie is available a prints out an error message if that's not the
> case:
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> >>
> >> @Override
> >> public void start(BundleContext context) throws Exception
> >> {
> >> ServiceReference sRef =
> context.getServiceReference(HttpService.class.getName());
> >> if (sRef != null)
> >> {
> >> HttpService service = (HttpService) context.getService(sRef);
> >> service.registerServlet(ALIAS, new HelloWorldServlet(), null, null);
> >> }else{
> >> System.out.println("no HttpService found to register Servlet for
> "+ALIAS);
> >> }
> >> }
> >> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> >>
> >> and sadly this is exectly what happens when deploying the war-file on
> Tomcat6:
> >>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> >> Connected to server
> >> [2013-03-14 12:34:42,775] Artifact felix-bridge:war exploded: Artifact
> is being deployed, please wait...
> >> starting OSGi
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.http.bridge-2.2.0.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.gogo.runtime_0.10.0.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.webconsole-3.1.8.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.http.base-2.2.0.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.bundlerepository-1.6.6.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.gogo.shell_0.10.0.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.http.api-2.2.0.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/systemBundles/org.apache.felix.gogo.command_0.12.0.jar]
> >> Installing bundle
> [jndi:/localhost/WEB-INF/applicationBundles/HelloWorldServletEmbedded-1.0-SNAPSHOT.jar]
> >> Starting bundle [org.apache.felix.http.bridge]
> >> [INFO] Started bridged http service
> >> Starting bundle [org.apache.felix.gogo.runtime]
> >> Starting bundle [org.apache.felix.webconsole]
> >> Starting bundle [org.apache.felix.http.base]
> >> Starting bundle [org.apache.felix.bundlerepository]
> >> Starting bundle [org.apache.felix.gogo.shell]
> >> Starting bundle [org.apache.felix.http.api]
> >> Starting bundle [org.apache.felix.gogo.command]
> >> Starting bundle [com.mobenga.HelloWorldServletEmbedded]
> >> no HttpService found to register Servlet for /hello <---no HttpService!
> >> OSGi startet
> >> [2013-03-14 12:34:43,176] Artifact felix-bridge:war exploded: Artifact
> is deployed successfully
> >> ____________________________
> >> Welcome to Apache Felix Gogo
> >>
> >> g! Mar 14, 2013 12:34:52 PM org.apache.catalina.startup.HostConfig
> deployDirectory
> >> INFO: Deploying web application directory manager
> >> lb
> >> START LEVEL 1
> >> ID|State |Level|Name
> >> 0|Active | 0|System Bundle (4.0.2)
> >> 1|Active | 1|Apache Felix Http Bridge (2.2.0)
> >> 2|Active | 1|Apache Felix Gogo Runtime (0.10.0)
> >> 3|Active | 1|Apache Felix Web Management Console (3.1.8)
> >> 4|Active | 1|Apache Felix Http Base (2.2.0)
> >> 5|Active | 1|Apache Felix Bundle Repository (1.6.6)
> >> 6|Active | 1|Apache Felix Gogo Shell (0.10.0)
> >> 7|Active | 1|Apache Felix Http Api (2.2.0)
> >> 8|Active | 1|Apache Felix Gogo Command (0.12.0)
> >> 9|Active | 1|HelloWorldServletEmbedded (1.0.0.SNAPSHOT)
> >> g!
> >> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> >>
> >> Because all bundles are active i assume i forgot some needed bundle or
> configuration?
> >> (I assume i don't need an embedded Jetty because this runs on top of
> Tomcat!?)
> >>
> >> If you need more information here is the whole maven project as a zip:
> >>
> https://docs.google.com/file/d/0BwYlZDH_xJugamxJQ2QzaWJULVU/edit?usp=sharing[https://docs.google.com/file/d/0BwYlZDH_xJugamxJQ2QzaWJULVU/edit?usp=sharing]
> >>
> >> Regards,
> >> Stephan
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
> >> For additional commands, e-mail: users-h...@felix.apache.org
> >>
> >
> >
> > --
> > Felix Meschberger | Principal Scientist | Adobe
> >
> >
> >
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
> > For additional commands, e-mail: users-h...@felix.apache.org
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
> > For additional commands, e-mail: users-h...@felix.apache.org
> >
>
>
> --
> Felix Meschberger | Principal Scientist | Adobe
>
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
> For additional commands, e-mail: users-h...@felix.apache.org
>
>

Reply via email to