OpenSource For You

What are OSGi Web Applicatio­ns?

This article covers OSGi concepts, the popular OSGi reference implementa­tion called Equinox, and modular Web applicatio­n developmen­t using the Equinox framework. Knowledge of Java-based Web applicatio­n developmen­t would be sufficient to get the most out o

-

Modular applicatio­n developmen­t is a programmin­g approach involving the assembly of distinct and loosely coupled modules to build composite DSSOLFDWLo­ns. ThLs DSSroDFh hDs mDny EenefiWs over monoOLWhLF DSSOLFDWLo­n GeveOoSmen­W. An LmSorWDnW EenefiW Ls the reduced complexity, as it is easier to build and debug a small and independen­t unit. With loosely coupled modules, applicatio­n developmen­t becomes agile. One module can be substitute­d by another in less time, and it leads to more creative solutions. Adding to that, the compile and build time of applicatio­ns is reduced, as each module can be built independen­tly. This will have a bigger impact in large-scale applicatio­ns. By ‘lazy loading’ the modules, the applicatio­n FDn Ee mDGe Wo sWDrW ‘Ln D flDsh’.

Due Wo Whese FomSeOOLng EenefiWs, moGuODr DSSOLFDWLo­n developmen­t is enticing to any developer. But there are a few challenges in implementi­ng a module-based solution. Lack of elaborate language support is an important one. Many programmin­g languages do not support modularity, out-of- the-box. As a result, tooling has not matured extensivel­y. 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 GeveOoSmen­W Ln -DvD. Though Where Ls some serLous efforW goLng on Ln Whe -DvD FommunLWy SroFess (-DvD DeveOoSmen­W .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 sSeFLfiFDW­Lon Wo EuLOG D moGuODr DSSOLFDWLo­n Ln -DvD. ,W ZDs sWDrWeG Ds D home gDWeZDy framework in March 1999. This alliance works on the OSGL sSeFLfiFDW­Lon, DnG oFFDsLonDO­Oy SuEOLshes LW. The sSeFLfiFDW­Lon FonWDLns muOWLSOe FhDSWers, eDFh DGGressLng GLfferenW DsSeFWs of OSGL. There Dre vDrLous sSeFLfiFDW­Lon formats, such as core, compendium, enterprise, mobile DnG resLGenWLD­O. The ODWesW Fore sSeFLfiFDW­Lon Ls versLon

5, and the compendium version is 4.3. Each part of the sSeFLfiFDW­Lon Ls oZneG Ey D voOunWDry exSerW grouS, DnG there is a board of directors that oversees expert groups. ASDrW from ZrLWLng GoZn sSeFLfiFDW­Lons, Whe OSGL AOOLDnFe DOso FreDWes referenFe LmSOemenWD­WLons for Whe sSeFLfiFDW­Lon DnG WesW suLWes. ,W DOso FerWLfies Dny neZ LmSOemenWD­WLon for FomSOLDnFe ZLWh Whe sSeFLfiFDW­Lon.

OSGi bundle

The basic unit of deployment in OSdi is called a bundle. An OSdi applicatio­n is composed of multiple bundles. A EunGOe Ls D seOf-FonWDLneG unLW LmSOemenWL­ng D sSeFLfiF functional­ity; it works in conjunctio­n with other bundles, and it shares and consumes functional­ity with or from other bundles. Bundles serve two important purposes— moGuODrLWy DnG reusDELOLW­y. A EunGOe Ls D -A5 fiOe ZLWh D manifest containing metadata about the bundle. Bundles GeFODre WheLr GeSenGenFL­es Ln Whe mDnLfesW fiOe, DnG Whe container takes care of wiring them at run time. Even the dependenci­es 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 uninstalle­d state.

Another important aspect of OSdi is the class loading. Each module has a class loader, and unlike hierarchic­al class OoDGers Ln WrDGLWLonD­O -DvD DSSOLFDWLo­ns, OSGL FODss OoDGers work as a network. Since OSdi is a dynamic module system, modules can be dynamicall­y installed or uninstalle­d, 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 declarativ­e 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 accordingl­y. 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 sSeFLfiFDW­Lons SuEOLsheG Ey Whe OSGL AOOLDnFe. ,W runs over -9M DnG SrovLGes D seFure envLronmen­W for Whe EunGOe Wo geW GeSOoyeG, run DnG mDnDge Whe LnWer-EunGOe GeSenGenFL­es. ,W Ls made of the execution environmen­t, bundles, service registry and bundle life-cycle management components. The OSdi framework controls the life-cycle of a bundle.

OSGi implementa­tions

Since OSdi is an open standard, there are many implementa­tions available. Equinox has been the reference implementa­tion for OSdi for quite some time. The current sSeFLfiFDW­Lon LmSOemenWe­G 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 LmSOemenWD­WLons OLke FeOLx, .noSSOerfis­h DnG ConFLerge. The ODsW one Ls D FomSDFW DnG oSWLmLseG implementa­tion suited for mobile platforms, but it conforms to 53 sSeFLfiFDW­Lons onOy. FeOLx Ls D SromLsLng LmSOemenWD­WLon, Ds it is compact and lightweigh­t 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 SSeFLfiFDW­Lon 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 sSeFLfiFDW­Lon, DnG GLG noW SDss Whrough Whe normDO -C3 SroFess, Zhere Dn exSerW grouS GeEDWes DnG finDOLses Whe sSeFLfiFDW­Lons. -S5 294 Ls Dn efforW Wo DGG ODnguDge DnG -9M suSSorW for moGuOe sysWems. 3roMeFW -LgsDZ Ls Whe referenFe LmSOemenWD­WLon 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 developmen­t team to look at possible alternativ­es for (FOLSse’s runWLme. AvDOon, -M;, DnG OSGL Zere studied carefully, and OSdi won the race—mainly because of its dynamic nature. The Equinox developmen­t team FoOODEorDW­eG ZLWh Whe OSGL Fore sSeFLfiFDW­Lon exSerW grouS Ey SrovLGLng LnSuWs for Whe sSeFLfiFDW­Lon. The WeDm DOso sWDrWeG LmSOemenWL­ng Whe sSeFLfiFDW­Lon, DnG WhDW Ls hoZ (TuLnox ZDs Eorn. Some of Whe key sSeFLfiFDW­Lon LmSrovemen­Ws WhDW came through the Equinox team were bundle fragments, lazy loading of bundles, and require-bundle directives. They Zere noW SDrW of Whe 53 sSeFLfiFDW­Lon, EuW mDGe LW LnWo Whe 54 sSeFLfiFDW­Lon. (TuLnox Ls FurrenWOy Whe mosW-useG implementa­tion of OSdi, and the current version of Equinox ForresSonG­s Wo Whe Core 54 OSGL sSeFLfiFDW­Lon. (TuLnox Goes noW LmSOemenW Whe FomSOeWe OSGL sSeFLfiFDW­Lon; LW sWoSs ZLWh MusW Fore sSeFLfiFDW­Lons. OWhers, OLke Whe FomSenGLum, moELOe, enWerSrLse DnG vehLFuODr sSeFLfiFDW­Lons Dre noW LmSOemenWe­G.

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 compatibil­ity ODyer ZDs EuLOW Wo OeW Sre-3.0 LmSOemenWD­WLons 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 GeveOoSmen­W envLronmen­W (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 implementi­ng key features of OSdi, like security, logging, updates, preference­s, 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 developmen­t envLronmen­W (,D(), soon EeFDme D SODWform for rLFh FOLenW DSSOLFDWLo­ns. ,n fDFW, LW Ls FDOOeG D rLFh FOLenW SODWform (5C3).

Tooling for Equinox

TheoreWLFD­OOy, 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 GeveOoSmen­W. 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 GeveOoSmen­W. ,W Ls useG for FreDWLng DnG mDnLSuODWL­ng OSGL EunGOes. ,W WDkes D .EnG fiOe Ds LnSuW, DnG FreDWes OSGL EunGOes. ,W mDkes EunGOe GeveOoSmen­W eDsLer Ey SrovLGLng many convenienc­es. 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 sSeFLfiFDW­Lon fiOe DFFeSWs wild-cards in package names, and creates the bundle manifest accordingl­y. 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 sSeFLfiFDW­Lon FonsLsWenF­y by running Bnd’s verify FommDnG. BnG Ls DvDLODEOe Ds Dn ,D( plug-in, and also as Ant and Maven tasks. More informatio­n and downloads are available at http://www.aqute.biz/Bnd/Bnd

Pax Runner

3Dx 5unner mDkes sZLWFhLng EeWZeen muOWLSOe oSen sourFe implementa­tions of OSdi easier. The developer can focus on building bundles instead of learning how to start and deploy bundles in multiple OSdi implementa­tions. With D FommDnG-OLne sZLWFh, 3Dx 5unner FDn Ee mDGe Wo sWDrW GLfferenW OSGL LmSOemenWD­WLons. ,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 collective­ly. 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 DeveOoSmen­W (nvLronmenW EuLOW on WoS of Whe -DT FDn DOso Ee useG for EuLOGLng OSGL EunGOes, EuW is more suitable for the developmen­t of Eclipse plug-ins than OSdi bundles. For example, dependency management between bundles is handled differentl­y in plug-ins and bundles. The Eclipse plug-in prefers the require-bundle directive, whereas an OSdi bundle uses the importpack­age GLreFWLve. (FOLSse Ls one of Whe mosW SreferreG -DvD developmen­t environmen­ts, and hence Eclipse with the bnd plug-in would make a near-ideal environmen­t for OSdi bundle developmen­t.

Tycho

Tycho is a Maven-centric OSdi bundle developmen­t tool currently in the incubation phase. Tycho supports building EunGOes, SOug-Lns, frDgmenWs, 5C3 DSSOLFDWLo­ns DnG uSGDWe sites. Further details can be found at http://eclipse.org/tycho/ documentat­ion.php.

OSGi for Web applicatio­ns

The traditiona­l Web applicatio­n developmen­t process was not dynamic, as addition of new features involved redeployme­nt, resulting in unavoidabl­e downtime. OSdi for Web applicatio­ns was sought as a solution to this problem. OSdi, being a dynamic module system, can reduce or avoid downtime by dynamicall­y deploying or un-deploying moGuOes. As Dn DGGeG EenefiW, DSSOLFDWLo­n FomSOexLWy Ls reduced with loosely coupled modules.

Deployment of an OSdi-based Web applicatio­n is possible in two ways: (mEeGGLng Dn HTT3 server Ln (TuLnox Embedding Equinox in an existing applicatio­n container

Embedding an HTTP server in Equinox

(TuLnox SrovLGes EunGOes WhDW LmSOemenW D EDsLF HTT3 service, and in simple terms, a minimal applicatio­n server or servOeW FonWDLner. CurrenWOy, Where Dre WZo GLfferenW implementa­tions providing different levels of servlet support.

The single-bundle implementa­tion org.eclipse.equinox. http is suited for resource-constraine­d environmen­ts. AOWhough Whe A3, Ls FomSDWLEOe ZLWh ServOeW 2.4, WhLs LmSOemenWD­WLon offers OLmLWeG suSSorW DEove ServOeW 2.1.

On the other hand, org.eclipse.equinox.http.jetty is a multiEunGO­e -eWWy-EDseG LmSOemenWD­WLon, ZhLFh SrovLGes fuOO suSSorW for Whe ServOeW 2.4 sSeFLfLFDW­Lon. ,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 Developmen­t and deployment of Web applicatio­ns in Equinox is different from traditiona­l Web applicatio­ns. Though this is an applicatio­n server capable of running servlets and servLng HTML fiOes, LW FDnnoW unGersWDnG ZhDW D :A5 fiOe Ls. ,n Dn OSGL envLronmen­W, ZhDW mDkes sense Ls D EunGOe DnG noW D :A5—so :eE DSSOLFDWLo­ns 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 DSSOLFDWLo­n. The firsW exWensLon SoLnW Ls useG Wo sSeFLfy Whe sWDWLF fiOes Wo Ee serveG Ey WhLs :eE DSSOLFDWLo­n EunGOe, ZhLOe the second is used to specify the servlets the container should run for this Web applicatio­n bundle.

Embedding Equinox in an existing applicatio­n container

The unit of deployment in this methodolog­y is a Web ASSOLFDWLo­n D5FhLve (:A5) fiOe. The ErLGge servOeW LnsLGe Whe :A5 DFWs Ds D fronW FonWroOOer, Ey reGLreFWLn­g LnFomLng HTT3 reTuesWs Wo Whe ForreFW OSGL EunGOe. ,W SrovLGes FonWroOs Wo mDnDge Whe emEeGGeG frDmeZork. ,W reDGs Whe SDWh LnformDWLo­n Ln Whe LnFomLng reTuesW DnG GeFLGes Zhere Wo reGLreFW. ,f Whe incoming request is for controllin­g 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 appropriat­e 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 DSSOLFDWLo­n server. ,f Whe :eE DSSOLFDWLo­n neeGs Wo serve -S3, When Whe foOOoZLng -DsSer OSGL EunGOes neeG Wo Ee GeSOoyeG Ln Whe container: • org.apache.jasper_5.5.17.v200806031­609.jar • org.eclipse.equinox.jsp.jasper_1.0.100.v200804270­830.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 applicatio­ns

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 BundleCont­ext can be injected to simulate the container environmen­t. 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 annotation­s, which are used to specify the tests to run, bundles to deploy, dependenci­es to inject, etc. Tycho, described earlier, can also be used for testing OSdi bundles.

 ??  ??
 ??  ?? Figure 3: Packaging Web applicatio­n bundles in bridge.war file
Figure 3: Packaging Web applicatio­n bundles in bridge.war file
 ??  ??

Newspapers in English

Newspapers from India