The high cost of productivity in Java OSGi

How hard should it be to build a simple web page that says ‘Hello World’?

If you are using Java and OSGi, it seems that this is a non-trivial exercise – at least if you want to use tools for making your life easier..but, when you are through the pain, the end result seems to be quite good, although the key decider will be whether there is some longer-term benefit to be had by going through such a relatively complex process.

My goal was to build an OSGi-enabled ‘Hello World’ web application that allowed me to conduct the whole dev lifecycle within my Eclipse environment. Should have been simple, but it turned out trying to persuade Eclipse to put the right (non-Java resource files) in the right place in JAR files, and getting the right bundles included to use them, was a challenge. Also, when things didn’t work as expected, trying to resolve apparently relevant error messages was a huge waste of time.

In particular, the only tools I am using at this point in time are:

  • Eclipse with Bndtools plugin
  • Git
  • An empty bundle repository, apart from that provided by default bndtools

So, the specific challenges were:

Web Application Bundles (WABs) + Servlets

  • To use the embedded jetty bundle (i.e., Java http server) to serve servlets, you need to include a web.xml in the jar file, in the WEB-INF folder. Turns out to do that, you need to put the WEB-INF/web.xml in the same src directory of your servlet java file, and manually add ‘-wab: src/<servlet src dir>’ to the bnd.bnd source. This tells bndtools to generate a JAR structure compatible with web applications.
  • The key pointer to this was in the bdntools docs:
  • Once that was done, eclipse generated the correct jar structure.

Including the right bundle dependencies

  • There are a number of resources to describe how to build a servlet with OSGi. Having started with the example in the ‘Enterprise OSGi in Action’ book (which precedes bndtools, and saves time by using an Apache Aries build but which includes a lot of unnecessary bundles), I decided to go back to basics and start from the beginning. I found the link below:
  • Basically, just use the ‘Apache Felix 4 with Web Console and Gogo’ option (provided within bndtools, and included as part of the default bndtools bundle repository), and add Apache Felix Whiteboard. It took a bit of effort to get this working: in the end I had to download the latest Felix jar download for all its components, and add them to my local bdntools repo.
  • The standard bndtools Felix components did not seem to work, and I think some of this may be due to old versions of the Felix servlet and jetty implementation in the bndtools repo (javax.servlet v2.5 vs 3.0, jetty 2.2 vs 2.3) conflicting with some newer bundles which support the Felix whiteboard functionality. So to use the whiteboard functionality, ensure you have the latest Felix bundles in your local bndtools repo. (Download from Felix website and add them via Eclipse Repositories view to the ‘Local’ repository.)

Error messages which (again) were not really what the problem was.

  • When I first built the application, the jetty server said ‘not found’ for the servlet URL I was building, and this message appeared in the log:
    • NO JSP Support for /fancyfoods.web, did not find org.apache.jasper.servlet.JspServlet
  • This was followed by a message suggesting that the server *had* processed the web.xml file.
  • After exploring what this error was (through judicious googling) it turns out Felix does not include JSP support by default, but this is not relevant to implementing the servlet example. In fact, this error was still displayed even after I got the application working correctly via the methods described above.

In conclusion: the learning curve has been high, and there’s a lot of feeling in the dark just because of the sheer volume of material out there one needs to know to become expert in this. But once environment/configuration issues have been sorted out, in principle, productivity should improve a lot.

However, I am not done yet: I suspect there will be much more challenges ahead to get blueprint and JPA working in my Eclipse environment. 

 

Advertisements
The high cost of productivity in Java OSGi

3 thoughts on “The high cost of productivity in Java OSGi

  1. We’re working on the OSGi enRoute project which consists for a very large part of documenting how you do these kind of things. This is still very much work in progress because it is a lot of work. However, we’re trying to create a no-compromise OSGi chain that allows you to make Web apps the real OSGi way instead of trying to push Java EE through an OSGi hole.

    Which, as a matter of fact, I think you’re also trying to do 🙂 So I made a small example how trivial it is to make a servlet run in OSGi: https://github.com/osgi/osgi.enroute.examples Look at the osgi.enroute.examples.servlet project. Ok, maybe a few more lines of code/setup than in Smalltalk but this is really component oriented and modular.

    Like

    1. Peter – great comment, thanks for that. I wasn’t too familiar with OSGi enroute, so I found this link which is quite insightful as to your thinking:
      http://www.infoq.com/interviews/osgi-en-route

      Enroute seems like an excellent concept, and frankly, from an enterprise perspective, is a minimum requirement before OSGi is likely to be aggressively used in an enterprise scenario. Plus, I can see regulators being supportive of it as demonstrating compliance with development controls.

      I tried the demo. It built without a hitch (I had to install gradle), but then I came across 3 errors which needed to be resolved before I was able to run the demo within Eclipse:

      * The default Java version in the enroute.bnd assumed it to be 1.8; as luck would have it, I had Java 1.7. By the time I had discovered the place to fix the problem, I had installed 1.8 from the Oracle Java site, and so in the end I just left it at 1.8.
      * There were a lot of errors in the Eclipse project, which looked like bndtools wasn’t picking up any bundles from its repository. But only one repository was showing – the examples one. None of the other repositories were visible in Bndtools ‘Repositories’ view, and of course the missing packages were not in that repository. On a hunch I checked to see if BndTools had a newer version, and sure enough it had. When I installed it, the correct repositories appeared and all my compile errors were resolved, except for…
      * In the servlet compilation, I got a compile error complaining that Annotations only work on Java source > 1.5. I don’t know what was telling it to assume source v1.5, but Eclipse offered to correct it automatically. I gratefully accepted the offer, and lo, everything built and ran perfectly…

      Given I am using a basic Mac OS X environment, I am pretty impressed at how easily all this stuff came together. I am looking forward to seeing more from Enroute, and anything to help reduce the complexity of managing environments containing many interdependent components.

      Like

  2. Darragh,
    We’re working on the OSGi enRoute project which consists for a very large part of documenting how you do these kind of things. This is still very much work in progress because it is a lot of work. However, we’re trying to create a no-compromise OSGi chain that allows you to make Web apps the real OSGi way instead of trying to push Java EE through an OSGi hole.

    Which, as a matter of fact, I think you’re also trying to do 🙂 So I made a small example how trivial it is to make a servlet run in OSGi: https://github.com/osgi/osgi.enroute.examples Look at the osgi.enroute.examples.servlet project. Ok, maybe a few more lines of code/setup than in Smalltalk but this is really component oriented and modular.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s