Introduction of OSGI
OSGi stands for “Open Services Gateway initiative”. The OSGi technology is a set of specifications that define a dynamic component system for Java. These specifications enable a development model where applications are (dynamically) composed of many different (reusable) components.
Formed in 1999, the OSGi alliance made its first steps into the Java market within the embedded device segment. Years later it further extended its reach to Java desktop projects, providing a major foundation for the modularity and extensibility of the Eclipse open-source IDE project. Now, as the service’s era is dawning, OSGi is stepping into its latest Java frontier: the server side.
Like many other alliances in the tech industry, OSGi’s best practices and development policies are dictated by consensus among its many member companies. And in the case of OSGi’s server-side presence, numerous prominent Java vendors have already started shifting their SOA product lines around OSGi blueprints, one such case being BEA’s own microService Architecture (mSA), which relies on OSGi for its backplane components.
OSGi Key Concepts
With much of OSGi’s principles successfully applied to everything from smart phones to IDEs (Integrated Development Environments), it can be difficult to sum up every OSGi aspect in a few sentences; however, the following key concepts should be enough to give you a feel for OSGi’s overall place in the Java ecosystem:
- The environment and framework: OSGi is often categorized as a Java framework. However, don’t let this often overused word in the Java world cause you to pigeonhole OSGi as one of the hundreds of frameworks now available to ease everything from Java Web development to Java testing. OSGi crosses this boundary, effectively providing an ad-hoc environment that facilitates modularizing an application into smaller and more manageable pieces.
- The JVM companion: Since OSGi’s roots are in the embedded market, it should come as no surprise that OSGi’s primary focus is on boosting the capabilities of the lowest common denominator in Java: The Java Virtual Machine. While the JVM has more than a decade of engineering behind it, for certain tasks—such as system services and dynamic loading—it has fallen shy of the expected curve in some vertical industries, giving way to initiatives like OSGi to fill in feature gaps.
- The Java packaging bundle: Of course, no environment or framework is exempt from having its own packaging model, so just as you may have become accustomed to dealing with WAR (Web Archives) and EAR (Enterprise Archives) while working with Java EE applications, or the more general purpose JAR (Java Archives) on your Java projects, so too OSGi has its own packaging model, which goes by the name of a “bundle.” For now, don’t worry too much about what makes up a bundle—we will get to that in a little while—just be aware that the term “bundle” practically goes hand in hand with OSGi.
Creating struts example plugin project
- Dowload http://prdownloads.sourceforge.net/sse-examples/org.eclipse.equinox.struts.examples-proj.zip?download
- Make an empty project of plugin type in eclipse and then put above downloaded zip file following contents at following location of this new project.
- src contents into project src folder.
- \META-INF\manifest.mf file into the project Meta-INF folder
- .web folder at the project root
- Add all jar files in web/lib in java build libraries.
- Add web/WEB-INF/classes at the java build class path
- build.properties file located at the root of the project should have following contents:
output.. = bin/
bin.includes = META-INF/,\
source.. = src/
- Now your project should look like this:
You would not be able to compile this project up till now successfully. This is because OSGI web project always needs org.eclipse.equinox.http.helper plugin which unfortunately eclipse installation doesn’t have. Unfortunately this plugin jar is not available as separate download so you need to create a plugin project and get its source code from eclipse source code repository. The detail of how to make this project is mention at this blog
- After creating this plugin project you would need to add its dependency in the build path of the struts project. Now you can compile whole project successfully without any errors.
Running the project in the OSGI container
When you run this application first time, you would be presented following screen:
At this screen check org.eclipse.equinox.struts.example as show in red circle in above diagram. Then click Run button. This would start OSGI container with struts plugin already installed in it. You would need to type ss at OSGI prompt as shown below to see your plugin in the list:
You will see that org.eclipse.equinox.struts.examples in the list with status active. Congratulation you have successfully deployed the struts OSGI application. Now you can browse this struts example application using browser. The default port is 80 of the jetty server that is embedded in equinox, so you would need to type following URL to run this application:
Follow ups :
Here is my second post on how to deploy this application as standalone app for distribution