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?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Li'
  • 3,133
  • 10
  • 34
  • 51
  • My https://github.com/bdelacretaz/OSGi-for-mere-mortals sample demonstrates a simple but complete OSGi-based applications that uses a few servlets. It doesn't use Apache Karaf at its container but should help you get a feel for how this works in general. – Bertrand Delacretaz May 01 '13 at 08:39

6 Answers6

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.

Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
  • Is @Compoment enough? I always thought the Servlet would have to be declared as a \@Service as well. – ilikeorangutans May 01 '13 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 01 '13 at 18:06
  • @ilikeorangutans Yes it's enough. Notice the "provide=Servlet.class" attribute. – Neil Bartlett May 01 '13 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!! – Neil Bartlett May 01 '13 at 18:38
  • @NeilBartlett I was unaware of this attribute. It's not documented on http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html#component . Is it a new enhancement? – ilikeorangutans May 01 '13 at 20:37
  • @NeilBartlett I want to try what you said. But I have some problems: what package/jar does @ Component need? By bndtools, do you mean maven bundle plugin? I need to add Apache Felix Http whiteboard bundle to pom.xml or deploy it to osgi container? Can you give more details? Thanks – Li' May 01 '13 at 22:08
  • @ilikeorangutans You're reading the wrong document, see http://www.aqute.biz/Bnd/Components instead. – Neil Bartlett May 02 '13 at 05:46
  • 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. – Achim Nierbeck May 02 '13 at 06:31
  • @NeilBartlett now that makes much more sense. Thanks! – ilikeorangutans May 02 '13 at 14:00
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

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
3

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

Mina Atia
  • 780
  • 1
  • 4
  • 13
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.

Holly Cummins
  • 10,767
  • 3
  • 23
  • 25
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.

Achim Nierbeck
  • 5,265
  • 2
  • 14
  • 22
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

Victor Bashurov
  • 370
  • 3
  • 12