10

I want to create and deploy a web service to OSGi container. For example, publish the service to the address:

http://localhost:8080/testservice. 

The service generate HTML response in a servlet.

I have searched a lot and got:

public class HelloWorldServlet extends HttpServlet {
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Hola</title>");
    out.println("</head>");
    out.println("<body bgcolor=\"white\">");
    out.println("</body>");
    out.println("</html>");
  }
}

The tool I need to use:

  1. maven to create the project

  2. Fuse ESB karaf as OSGi container

The question is that I do not know how to use Maven to create and implement such web service, like:

  • how to specify webapp/web.xml

  • how to specify pom.xml: dependencies, package type, plugin

  • how to register service: implement BundlActivator or configure Spring xml file

Can anyone help me with this? Is there a detailed tutorial for newbie?

1

6 Answers 6

7

If you use bndtools, create a Declarative Services project and add this annotation to your servlet:

 @Component(provide = Servlet.class, properties = {"alias=/hello"})
 public class HelloWorldServlet extends HttpServlet { ... }

Then create a bnd run descriptor with 'Apache Felix 4 with Web Console and Gogo', just add the Apache Felix Http whiteboard bundle and you're good to go. You can find your servlet at http://localhost:8080/hello

How it works. The @Component annotation makes your class a service (a Servlet service in this case due to the provide attribute). This is registered with the service property 'alias'. The Apache Felix Http Whiteboard bundle picks up these services and registers them as servlets. I do not think it can get any simpler than this.

9
  • Is @Compoment enough? I always thought the Servlet would have to be declared as a \@Service as well. May 1, 2013 at 15:16
  • I need to use FUSE ESB Enterprise. Can not switch to Apache Felix. Is Apache Felix similar to Apache Karaf?
    – Li'
    May 1, 2013 at 18:06
  • @ilikeorangutans Yes it's enough. Notice the "provide=Servlet.class" attribute. May 1, 2013 at 18:35
  • 1
    @Li' Both FUSE ESB and Karaf are built on top of Apache Felix. So you don't have to switch at all. Anyway all this works on ANY framework such as Equinox and Knopflerfish as well. Welcome to OSGi, we take reusability and portability seriously!! May 1, 2013 at 18:38
  • 1
    FUSE ESB and Karaf use Pax Web as the underlying implementation of the HTTPService, still the answer of Peter is correct also for this implementation of the HTTPService. Pax Web though does give you a couple of more features which aren't available with the std. HTTPService, e.g. WhiteBoard Extender which also is capable of beeing used for Filters, JSPs and other Resources. Not to mention that you're able to deploy std. wars in a Karaf. May 2, 2013 at 6:31
6

I would like to follow up on the answer of Peter Kriens. With @Component annotations available in the OSGi specification, the example could look like this:

@Component(service = Servlet.class, property = { "osgi.http.whiteboard.servlet.pattern = /hello" })
public class HelloWorldServlet extends HttpServlet { ... }

The @Component annotation is imported from org.osgi.service.component and the property that specifies the implemented service has changed its name to service.

Despite its name, property can hold multiple properties for example

@Component(service = ..., property = { "a=b", "c=d" })

or you could use properties to specify one or more properties files like so:

@Component(service = ..., properties = { "OSGI-INF/servlet.properties" } )

The above has been tested with the HttpService that comes with Apache Felix. The documentation of the Apache Felix HTTP Service can be found here: http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html

3

Check this, may be can help you Create a servlet that accesses an OSGi service

1

You may find the following tutorial helpful: http://www.javabeat.net/2011/11/writing-an-osgi-web-application/. It's based on chapter two of Enterprise OSGi in Action. Chapter eight also has a discussion of how to use build tools like maven to get the right bundle structure, and http://coding.alasdair.info/2011/01/creating-web-application-bundle-using.html also has really helpful maven instructions.

At a high level, your best route is probably to take advantage of something like Apache Aries or Eclipse Gemini to allow you to run a WAB (a web bundle). A WAB is structured almost exactly like a WAR, except that the manifest has OSGi metadata in it. Your servlet class itself would be identical to the non-OSGi case. The framework will handle discovering and launching your servlet.

1

To answer your question, since Karaf (FUSE ESB) uses Pax Web as it's default Web-Container take a look at Pax Web for more details how it works and probably best for you at the more than 100 integration tests of Pax Web to give you an Idea on how to use it. There are also samples available to show you how to use either std. Http-Service, through Whiteboard-Extender or as WAR/WAB.

0

I guess you need a servlet bridge to access the service. Your service should be implemented as an OSGI bundle; servlet bridge has to have embedded OSGI framework. Follow this sample for details: http://vbashur.blogspot.kr/2014/07/osgi-servlet-bridge-sample.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.