Engineering Full Stack Apps with Java and JavaScript
OSGi stands for Open Service Gateway initiative. The OSGi specification describes means to achieve better modularity when using Java. Applications or components come in the form of bundles, and these bundles can be remotely installed, started, stopped, updated, and uninstalled without requiring a reboot. Though the original focus of OSGi was initially on service gateways (as the name suggest), the OSGi specification have evolved beyond that and is now used in wide range of applications, and Eclipse IDE is one popular example. Modular architecture reduces complexity, allows for better changeability, parallel development, improved testability, reuse and flexibility, and thus reducing time and expenses for development and maintenance. Read more about OSGi specific benefits @ http://www.osgi.org/Technology/WhyOSGi.
Three key aspects of OSGi are bundles, life cycle, and services, and these are provided through an OSGi container. A bundle is a jar, and bundles allow developers to package code into deployable units and even to control which package needs to imported, exported, and their access levels. Multiple versions of a bundle can be installed into OSGi at the same time. Technically, each bundle is loaded into its own class space. Life cycle management allows starting, stopping, updating, or removing bundles and downloading of management policies, remotely and via an API. Services can be provided by one bundle to another bundle dynamically: services can become available and unavailable at any time. The service registry allows bundles to detect the addition of new services, or the removal of services, and adapt accordingly.
OSGi-based applications run within an OSGi container and an ODGi container is an implementation of the OSGi core framework specification. There are many open source OSGi containers like Apache Felix, Eclipse Equinox and Knopflerfish, and these three are open source implementations of the OSGi Release 4 core framework specification. The Eclipse IDE is built upon Equinox. Rather than downloading the core (e.g. Felix, equinox) and using it directly, you can use an OSGi runtime environment that wraps an OSGi core and provide developers with a simple interface to manage their OSGi applications. Apache Karaf and Eclipse Virgo are examples of such OSGi runtime environments and these two contains Felix and Equinox cores respectively by default.
Blueprint is a dependency injection framework specification for OSGi created based on Spring Dynamic Modules. Spring DM is almost similar and can even be considered as an implementation of the Blueprint specification. Blueprint support the dynamic nature of OSGi services by describing how the components get instantiated and wired together using an extender pattern: an extender bundle monitors the state of bundles in the framework and performs actions on behalf of those bundles based on their state. Technically, Blueprint defines the assembly of various components using xml files known as Blueprint XML files. An OSGi bundle is considered to be a Blueprint bundle when it contains one or more Blueprint XML files under the predefined directory location OSGI-INF/blueprint or as specified explicitly in the Bundle-Blueprint manifest header.
OPS4J Pax Exam is a useful tool while working with OSGi, especially while testing. Pax Exam lets you control the OSGi framework, a test framework (e.g. JUnit, TestNG6) and your test system at the same time. Test system is the system under test. Technically, Pax Exam has a test driver and a test container. The driver launches the OSGi framework and the test system. It builds on-the-fly bundles from your test cases and injects them into the container. The driver is a plain old Java application and does not itself require an OSGi framework. Pax Exam 3.x generalizes the in-container testing approach to cover not only OSGi, but also Java EE and CDI components and applications.
Integration and migration are two important concerns whenever we adopt a new technology. We can easily integrate OSGi with existing non OSGi bundles (java archives) or migrate existing java archives easily as OSGi bundles. We can convert a normal java archive file to a basic OSGi bundle by adding some OSGi specific headers to the java archive manifest file, to identify the bundle, its version, its imported dependencies, and any exported packages the bundle provides. We can code for any additional feature than OSGi provide as and when required.
In this note, we have quickly touched upon OSGi and many products related to OSGi like Apache Felix, Apache Karaf, Eclipse Equinox, Eclipse Virgo, Blueprint and Pax Exam, so that you will at least know what they are if you come across their names. We will see them in detail in further tutorials. Also, note that implementing OSGi will not automatically make your application modular and provide all the benefits of modular architecture we saw initially. OSGi is a helping hand that makes it easy for us to create well defined modules and it is our responsibility to understand and use it well.
http://en.wikipedia.org/wiki/OSGi
http://www.osgi.org/Specifications/HomePage
http://www.osgi.org/Technology/WhyOSGi
http://en.wikipedia.org/wiki/Apache_Felix
http://www.eclipse.org/equinox
https://www.eclipse.org/equinox/framework
http://en.wikipedia.org/wiki/Equinox_(OSGi)
http://en.wikipedia.org/wiki/Knopflerfish
https://aries.apache.org/modules/blueprint.html
https://ops4j1.jira.com/wiki/display/PAXEXAM3/Pax+Exam
Book: Instant OSGi Starter by Johan Edstrom and Jamie Goodyear.
Book: Creating Flexible Applications with OSGi and Spring by Craig Walls
Comments
Technically, each bundle is
Technically, each bundle is loaded into its own class space.