What are OSGi Web Applications?
This article covers OSGi concepts, the popular OSGi reference implementation called Equinox, and modular Web application development using the Equinox framework. Knowledge of Java-based Web application development would be sufficient to get the most out o
Modular application development is a programming approach involving the assembly of distinct and loosely coupled modules to build composite DSSOLFDWLons. ThLs DSSroDFh hDs mDny EenefiWs over monoOLWhLF DSSOLFDWLon GeveOoSmenW. An LmSorWDnW EenefiW Ls the reduced complexity, as it is easier to build and debug a small and independent unit. With loosely coupled modules, application development becomes agile. One module can be substituted by another in less time, and it leads to more creative solutions. Adding to that, the compile and build time of applications is reduced, as each module can be built independently. This will have a bigger impact in large-scale applications. By ‘lazy loading’ the modules, the application FDn Ee mDGe Wo sWDrW ‘Ln D flDsh’.
Due Wo Whese FomSeOOLng EenefiWs, moGuODr DSSOLFDWLon development is enticing to any developer. But there are a few challenges in implementing a module-based solution. Lack of elaborate language support is an important one. Many programming languages do not support modularity, out-of- the-box. As a result, tooling has not matured extensively. This has led to low adoption rates and community support. OSdi fiOOs Whese gDSs Ln Whe -DvD DrenD, DnG henFe LW hDs EeFome Whe de facto sWDnGDrG for moGuODr GeveOoSmenW Ln -DvD. Though Where Ls some serLous efforW goLng on Ln Whe -DvD FommunLWy SroFess (-DvD DeveOoSmenW .LW) LWseOf Wo DGG suSSorW for modularity, it is still in the nascent stages.
The history and the current state of OSGi
OSGL Ls D GynDmLF moGuOe sysWem Ln -DvD. ,W Ls Whe ouWFome of an alliance formed between technology innovators who LnWenGeG Wo FreDWe Dn oSen sSeFLfiFDWLon Wo EuLOG D moGuODr DSSOLFDWLon Ln -DvD. ,W ZDs sWDrWeG Ds D home gDWeZDy framework in March 1999. This alliance works on the OSGL sSeFLfiFDWLon, DnG oFFDsLonDOOy SuEOLshes LW. The sSeFLfiFDWLon FonWDLns muOWLSOe FhDSWers, eDFh DGGressLng GLfferenW DsSeFWs of OSGL. There Dre vDrLous sSeFLfiFDWLon formats, such as core, compendium, enterprise, mobile DnG resLGenWLDO. The ODWesW Fore sSeFLfiFDWLon Ls versLon
5, and the compendium version is 4.3. Each part of the sSeFLfiFDWLon Ls oZneG Ey D voOunWDry exSerW grouS, DnG there is a board of directors that oversees expert groups. ASDrW from ZrLWLng GoZn sSeFLfiFDWLons, Whe OSGL AOOLDnFe DOso FreDWes referenFe LmSOemenWDWLons for Whe sSeFLfiFDWLon DnG WesW suLWes. ,W DOso FerWLfies Dny neZ LmSOemenWDWLon for FomSOLDnFe ZLWh Whe sSeFLfiFDWLon.
OSGi bundle
The basic unit of deployment in OSdi is called a bundle. An OSdi application is composed of multiple bundles. A EunGOe Ls D seOf-FonWDLneG unLW LmSOemenWLng D sSeFLfiF functionality; it works in conjunction with other bundles, and it shares and consumes functionality with or from other bundles. Bundles serve two important purposes— moGuODrLWy DnG reusDELOLWy. A EunGOe Ls D -A5 fiOe ZLWh D manifest containing metadata about the bundle. Bundles GeFODre WheLr GeSenGenFLes Ln Whe mDnLfesW fiOe, DnG Whe container takes care of wiring them at run time. Even the dependencies could be versioned, and multiple versions can Fo-exLsW. ,n OSGL, every FODss Ls EunGOe-SrLvDWe, DnG Whrough its manifest, a bundle can specify which package it shares using the export directive; other modules can use it by an import directive. The container takes care of delegating the class loading requests between bundles. Each bundle has a lifecycle, which starts with the installed state, and ends once it passes the uninstalled state.
Another important aspect of OSdi is the class loading. Each module has a class loader, and unlike hierarchical class OoDGers Ln WrDGLWLonDO -DvD DSSOLFDWLons, OSGL FODss OoDGers work as a network. Since OSdi is a dynamic module system, modules can be dynamically installed or uninstalled, and the container takes care of notifying the dependent bundles. The OSdi container passes the bundle’s context to all the bundles, which is useful to query and manipulate the state of the container and the bundles running.
OSdi also has a concept of declarative services, similar to Web services. Any bundle can expose any number of services, and other bundles may consume them. OSdi services are registered in a service tracker, and bundles can look up the tracker and consume services accordingly. The difference between OSdi services and Web services is the absence of the network in the former. Since OSdi services and their consumers run in the same container, there is no network layer involved.
The OSGI framework
The OSdi framework has been developed as per the sSeFLfiFDWLons SuEOLsheG Ey Whe OSGL AOOLDnFe. ,W runs over -9M DnG SrovLGes D seFure envLronmenW for Whe EunGOe Wo geW GeSOoyeG, run DnG mDnDge Whe LnWer-EunGOe GeSenGenFLes. ,W Ls made of the execution environment, bundles, service registry and bundle life-cycle management components. The OSdi framework controls the life-cycle of a bundle.
OSGi implementations
Since OSdi is an open standard, there are many implementations available. Equinox has been the reference implementation for OSdi for quite some time. The current sSeFLfiFDWLon LmSOemenWeG Ey OSGL Ls Whe Core 54 versLon. ,W also enjoys support from the Eclipse Foundation, as the Eclipse plug-in system is built on top of Equinox. There are several SoSuODr oSen sourFe LmSOemenWDWLons OLke FeOLx, .noSSOerfish DnG ConFLerge. The ODsW one Ls D FomSDFW DnG oSWLmLseG implementation suited for mobile platforms, but it conforms to 53 sSeFLfiFDWLons onOy. FeOLx Ls D SromLsLng LmSOemenWDWLon, Ds it is compact and lightweight when compared to Equinox.
Modularity support in Java
,nLWLDOOy, Whe -DvD ODnguDge ZDs noW GesLgneG for moGuODrLWy— but attempts were made later to include modularity. Some of Whe noWDEOe -DvD SSeFLfiFDWLon 5eTuesWs (-S5) for ErLngLng moGuODrLWy Dre -S5 277, 291 DnG 294. -S5 277 Gefines D sWDWLF moGuOe sysWem Ln -DvD, Ds oSSoseG Wo OSGL’s GynDmLF moGuOe sysWem. -S5 291 GefineG D GynDmLF moGuOe sysWem, EuW LW ZDs MusW D referenFe Wo Whe OSGL 54 sSeFLfiFDWLon, DnG GLG noW SDss Whrough Whe normDO -C3 SroFess, Zhere Dn exSerW grouS GeEDWes DnG finDOLses Whe sSeFLfiFDWLons. -S5 294 Ls Dn efforW Wo DGG ODnguDge DnG -9M suSSorW for moGuOe sysWems. 3roMeFW -LgsDZ Ls Whe referenFe LmSOemenWDWLon for -S5 294 from OSen-D.. An eDrOy-DFFess -2S( 8 reOeDse ZLWh 3roMeFW -LgsDZ Ls DvDLODEOe from Whe OSen-D. ZeEsLWe, DnG fuOOEOoZn -LgsDZ mDy feDWure Ln fuWure reOeDses.
Rise of Equinox
When Eclipse’s own plug-in system was facing immense challenges in keeping up to what was required of it, a total revDmS ZDs FDOOeG for. 3roMeFW (TuLnox ZDs sWDrWeG Ey Whe Eclipse development team to look at possible alternatives for (FOLSse’s runWLme. AvDOon, -M;, DnG OSGL Zere studied carefully, and OSdi won the race—mainly because of its dynamic nature. The Equinox development team FoOODEorDWeG ZLWh Whe OSGL Fore sSeFLfiFDWLon exSerW grouS Ey SrovLGLng LnSuWs for Whe sSeFLfiFDWLon. The WeDm DOso sWDrWeG LmSOemenWLng Whe sSeFLfiFDWLon, DnG WhDW Ls hoZ (TuLnox ZDs Eorn. Some of Whe key sSeFLfiFDWLon LmSrovemenWs WhDW came through the Equinox team were bundle fragments, lazy loading of bundles, and require-bundle directives. They Zere noW SDrW of Whe 53 sSeFLfiFDWLon, EuW mDGe LW LnWo Whe 54 sSeFLfiFDWLon. (TuLnox Ls FurrenWOy Whe mosW-useG implementation of OSdi, and the current version of Equinox ForresSonGs Wo Whe Core 54 OSGL sSeFLfiFDWLon. (TuLnox Goes noW LmSOemenW Whe FomSOeWe OSGL sSeFLfiFDWLon; LW sWoSs ZLWh MusW Fore sSeFLfiFDWLons. OWhers, OLke Whe FomSenGLum, moELOe, enWerSrLse DnG vehLFuODr sSeFLfiFDWLons Dre noW LmSOemenWeG.
How Eclipse is built around Equinox
Eclipse can be considered as a group of bundles running on Whe (TuLnox OSGL runWLme. 3rLor Wo versLon 3.0, (FOLSse ZDs
using its own component runtime. The unit of deployment ZDs D SOug-Ln (MusW D -A5 fiOe, Ds Dny OSGL EunGOe). (DFh plug-in provided extension points, using which other bundles FouOG exWenG Whe EehDvLour of Whe orLgLnDO moGuOe. 3Ougins used plugin.xml fiOe for GeFODrLng exWensLon SoLnWs DnG other metadata. After Equinox adoption, a compatibility ODyer ZDs EuLOW Wo OeW Sre-3.0 LmSOemenWDWLons FonWLnue Wo Zork ZLWhouW reZrLWLng. Some (FOLSse-sSeFLfiF meWDGDWD remained in plugin.xml, and others like bundle name, version, exSorW-SDFkDge, eWF, moveG Wo Whe EunGOe mDnLfesW fiOe. The SOug-Ln GeveOoSmenW envLronmenW (3D() suSSorW ZDs DGGeG in Eclipse to let developers extend Eclipse with their own SOug-Ln. ,n Whe plug-ins folder of Eclipse, there would be D -A5 ZLWh Whe nDme org.eclipse.osgi.<version_number> and this is the Equinox runtime that runs Eclipse and every other plug-in built by developers. There are many optional and mandatory bundles implementing key features of OSdi, like security, logging, updates, preferences, etc. The Eclipse ZorkEenFh Ls EuLOW usLng Whe SWDnGDrG :LGgeW TooOkLW (S:T) DnG -FDFe, ZhLFh SrovLGes D nDWLve Oook DnG feeO. :LWh Dn extensible plug-in mechanism and a rich cross-platform look and feel, Eclipse, which started as an integrated development envLronmenW (,D(), soon EeFDme D SODWform for rLFh FOLenW DSSOLFDWLons. ,n fDFW, LW Ls FDOOeG D rLFh FOLenW SODWform (5C3).
Tooling for Equinox
TheoreWLFDOOy, one ZouOG noW neeG more WhDn Whe -D. DnG D text editor to develop an Equinox bundle—but that would not be a productive approach. By selecting the right tools, any sWDnGDrG ,D( OLke (FOLSse or 1eWBeDns FDn Ee eTuLSSeG for (TuLnox GeveOoSmenW. Some of Whem Dre BnG, 3Dx 5unner, 3Dx (xDm DnG (FOLSse 3D( LWseOf.
Bnd
Bnd, as the creator claims, is the Swiss army knife of OSdi GeveOoSmenW. ,W Ls useG for FreDWLng DnG mDnLSuODWLng OSGL EunGOes. ,W WDkes D .EnG fiOe Ds LnSuW, DnG FreDWes OSGL EunGOes. ,W mDkes EunGOe GeveOoSmenW eDsLer Ey SrovLGLng many conveniences. For example, a bundle can export or import multiple packages, and this has to be declared in the mDnLfesW fiOe. ,f hDnG-FoGLng Whe mDnLfesW, Whe GeveOoSer hDs to list all the packages, but the .bnd sSeFLfiFDWLon fiOe DFFeSWs wild-cards in package names, and creates the bundle manifest accordingly. Bnd can also do dependency analysis and update the manifest. Bnd can build bundles from sources available on Whe fiOe sysWem, or oWher -A5 fiOes, Ds sSeFLfieG Ln Whe .bnd fiOe. The EunGOe -A5 FDn Ee verLfieG for sSeFLfiFDWLon FonsLsWenFy by running Bnd’s verify FommDnG. BnG Ls DvDLODEOe Ds Dn ,D( plug-in, and also as Ant and Maven tasks. More information and downloads are available at http://www.aqute.biz/Bnd/Bnd
Pax Runner
3Dx 5unner mDkes sZLWFhLng EeWZeen muOWLSOe oSen sourFe implementations of OSdi easier. The developer can focus on building bundles instead of learning how to start and deploy bundles in multiple OSdi implementations. With D FommDnG-OLne sZLWFh, 3Dx 5unner FDn Ee mDGe Wo sWDrW GLfferenW OSGL LmSOemenWDWLons. ,W DOso DOOoZs OoDGLng EunGOes from muOWLSOe sourFes OLke Whe U5L, fLOe sysWem, Zip file, Maven repository, etc. Bundles can be grouped as profiles, and can be deployed or un-deployed collectively. 3Dx 5unner Ls DvDLODEOe Ds D sWDnGDOone WooO, DnG DOso Ds Dn Eclipse plug-in. More details at http:// team. ops4j. org/ wiki/ display/paxrunner/Pax+Runner
Eclipse PDE
The (FOLSse 3Oug-Ln DeveOoSmenW (nvLronmenW EuLOW on WoS of Whe -DT FDn DOso Ee useG for EuLOGLng OSGL EunGOes, EuW is more suitable for the development of Eclipse plug-ins than OSdi bundles. For example, dependency management between bundles is handled differently in plug-ins and bundles. The Eclipse plug-in prefers the require-bundle directive, whereas an OSdi bundle uses the importpackage GLreFWLve. (FOLSse Ls one of Whe mosW SreferreG -DvD development environments, and hence Eclipse with the bnd plug-in would make a near-ideal environment for OSdi bundle development.
Tycho
Tycho is a Maven-centric OSdi bundle development tool currently in the incubation phase. Tycho supports building EunGOes, SOug-Lns, frDgmenWs, 5C3 DSSOLFDWLons DnG uSGDWe sites. Further details can be found at http://eclipse.org/tycho/ documentation.php.
OSGi for Web applications
The traditional Web application development process was not dynamic, as addition of new features involved redeployment, resulting in unavoidable downtime. OSdi for Web applications was sought as a solution to this problem. OSdi, being a dynamic module system, can reduce or avoid downtime by dynamically deploying or un-deploying moGuOes. As Dn DGGeG EenefiW, DSSOLFDWLon FomSOexLWy Ls reduced with loosely coupled modules.
Deployment of an OSdi-based Web application is possible in two ways: (mEeGGLng Dn HTT3 server Ln (TuLnox Embedding Equinox in an existing application container
Embedding an HTTP server in Equinox
(TuLnox SrovLGes EunGOes WhDW LmSOemenW D EDsLF HTT3 service, and in simple terms, a minimal application server or servOeW FonWDLner. CurrenWOy, Where Dre WZo GLfferenW implementations providing different levels of servlet support.
The single-bundle implementation org.eclipse.equinox. http is suited for resource-constrained environments. AOWhough Whe A3, Ls FomSDWLEOe ZLWh ServOeW 2.4, WhLs LmSOemenWDWLon offers OLmLWeG suSSorW DEove ServOeW 2.1.
On the other hand, org.eclipse.equinox.http.jetty is a multiEunGOe -eWWy-EDseG LmSOemenWDWLon, ZhLFh SrovLGes fuOO suSSorW for Whe ServOeW 2.4 sSeFLfLFDWLon. ,W Ls mDGe uS of the following bundles: • org.eclipse.equinox.http.jetty • org.eclipse.equinox.http.servlet • org.mortbay.jetty • org.apache.commons.logging • javax.servlet • org.eclipse.equinox.http.registry Development and deployment of Web applications in Equinox is different from traditional Web applications. Though this is an application server capable of running servlets and servLng HTML fiOes, LW FDnnoW unGersWDnG ZhDW D :A5 fiOe Ls. ,n Dn OSGL envLronmenW, ZhDW mDkes sense Ls D EunGOe DnG noW D :A5—so :eE DSSOLFDWLons shouOG Ee SDFkDgeG Ds D EunGOe. ThLs EunGOe shouOG exWenG HTT3 servLFe-sSeFLfiF exWensLon points ( org.eclipse.equinox.http.registry.resources and org. eclipse.equinox.http.registry.servlets) Wo Ee DEOe Wo DFW Ds D :eE DSSOLFDWLon. The firsW exWensLon SoLnW Ls useG Wo sSeFLfy Whe sWDWLF fiOes Wo Ee serveG Ey WhLs :eE DSSOLFDWLon EunGOe, ZhLOe the second is used to specify the servlets the container should run for this Web application bundle.
Embedding Equinox in an existing application container
The unit of deployment in this methodology is a Web ASSOLFDWLon D5FhLve (:A5) fiOe. The ErLGge servOeW LnsLGe Whe :A5 DFWs Ds D fronW FonWroOOer, Ey reGLreFWLng LnFomLng HTT3 reTuesWs Wo Whe ForreFW OSGL EunGOe. ,W SrovLGes FonWroOs Wo mDnDge Whe emEeGGeG frDmeZork. ,W reDGs Whe SDWh LnformDWLon Ln Whe LnFomLng reTuesW DnG GeFLGes Zhere Wo reGLreFW. ,f Whe incoming request is for controlling the framework, then it starts or sWoSs Whe frDmeZork EDseG on Whe reTuesW. ,f Whe reTuesW Ls for some oWher HTML fiOe or servOeW Ln Whe OSGL :eE DSS, LW redirects the request to the appropriate resource.
-usW Ds Ln Whe FDse of emEeGGLng HTT3 server Ln (TuLnox, :eE OSGL EunGOes exWenGLng HTT3 sSeFLfiF exWensLon SoLnWs Dre SDFkDgeG LnsLGe :A5s DnG GeSOoyeG Wo Whe DSSOLFDWLon server. ,f Whe :eE DSSOLFDWLon neeGs Wo serve -S3, When Whe foOOoZLng -DsSer OSGL EunGOes neeG Wo Ee GeSOoyeG Ln Whe container: • org.apache.jasper_5.5.17.v200806031609.jar • org.eclipse.equinox.jsp.jasper_1.0.100.v200804270830.jar org.eclipse.equinox.jsp.jasper. registry_1.0.0.v20080427-0830.jar As a best practice, instead of deploying multiple OSdi bundles, all the bundles are placed inside bridge.war.
A sDmSOe :A5 SDFkDge sWruFWure Ls shoZn Ln FLgure 3.
Testing OSGi applications
UnLW WesWLng of OSGL EunGOes FDn Ee Gone Ey ZrLWLng -UnLW WesW cases. Mock object frameworks like Mockito can be used to FreDWe moFk oEMeFWs Ln -UnLW WesW FDses. DeSenGenFy LnMeFWLon frameworks like Blueprint can be used to inject objects. Even container objects like BundleContext can be injected to simulate the container environment. Though this form of testing can uncover some defects, the absence of a real container in all these tests will allow many defects through. A well-accepted solution to this problem is a tool from the 3Dx 5unner fDmLOy FDOOeG 3Dx (xDm. 3Dx (xDm ZrDSs Whe test cases as an OSdi service bundle, and deploys the bundle Ln Whe OSGL runWLme usLng 3Dx 5unner. 3Dx (xDm Lnvokes Whe WesWs DnG reForGs Whe resuOWs. 3Dx (xDm Ls GrLven fuOOy Ey annotations, which are used to specify the tests to run, bundles to deploy, dependencies to inject, etc. Tycho, described earlier, can also be used for testing OSdi bundles.