Intro to OSGi and Eclipse Virgo


Published on

Presentation I gave on OSGi with Eclipse Virgo and OSGi Blueprint

Published in: Technology, Education
1 Comment
  • Very Rare details about virgo server.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Analagous (not completely) to web application containers in that there is a specification (OSGI R4) and various implementations of the specification. Sun is also trying to build it’s own Java Module system as JSR-277, and there is a draft circulating to provide OSGI Bundles within the Java Module System when it is deployed. See for more details on this proposal, and for details on JSR-277.
  • This was created for us by a maven plugin (we’ll see later in this chapter)… Bundle-ManifestVersion – Which OSGi container rules to follow. 2 = OSGi Specification release 4 1 = OSGi version 3 or earlierBundle-Name – Short, human readable nameBundle-SymbolicName – Unique, machine-usable name for the bundle used by other bundles and the container to refer to the bundleBundle-Version – The version of the bundle that is being deployedBundle-Activator – A listener class that may be bound to the lifecycle of the bundle (start, stop events)Bundle-Vendor – A human readable name for the vendor who created the bundleImport-Package – A set of packages that are required for dependency management
  • Services provide a way to register a Java POJO within the OSGi container. The services can be then referenced by name in the OSGi container by any other module that requests the package the service is exposed within.
  • The maven-bundle-plugin works to build standard MANIFEST.MF files. You can use the output of this plugin in Felix, Knopflerfish and Equinox.We’ll see in an upcoming chapter that Spring-DM gives us an even bigger advantage.sample Maven configuration for the plugin: <build> <plugins> … <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <version>1.4.3</version> <configuration> <instructions> <Export-Package>,,</Export-Package> <Export-Service>;version="1.0"</Export-Service> <Bundle-SymbolicName>maven-osgi-demo-services</Bundle-SymbolicName> <Bundle-Activator></Bundle-Activator> <Import-Package>*</Import-Package> </instructions> </configuration> </plugin> </plugins> </build>
  • Undeploying also destroys application contexts. Undeploying the extender undeploys all application contexts.
  • This behavior can be overridden in the manifest.
  • Parameter descriptions:ref = the bean to lookup in the Spring Contextid= the OSGi service name to export (and that can be consumed by another Spring-DM bundle as a Spring Bean)interface = the service interface to advertise to the OSGi frameworkNote: If a bean implements multiple business interfaces, you can use an alternative form:<osgi:service id="theServiceId" ref="theBeanid"> <osgi:interfaces> <value>com.gordondickens.bean.InterfaceA</value> <value>com.gordondickens.bean.interfaceB</value> </osgi:interfaces></osgi:service><s
  • Note: if you consume a OSGi Service that is not created by a Spring Dynamic Module, you will get the service based on the default OSGi service registry lookup rules (see the OSGi specification section 5 for details at
  • Note – the same multi-interface wiring applies here too:<osgi:reference id="exampleBeanService"> <osgi:interfaces> <value>com.gordondickens.demo.bean.ExampleBean</value> <value>com.gordondickens.demo.bean.Example2Bean</value> </osgi:interfaces></osgi:references>
  • The bean-name attribute on the osgi:reference tag allows you to refer to the bean not by an OSGi Service ID, but by the actual bean ID in the Spring context. If the Spring Bean exampleBean was exposed to OSGi as exampleBeanService, a consuming context could request it using a bean-name rather than an id, and therefore refer to Spring names, not OSGi names.Note that this mechanism matches on Services that expose the ExampleBean interface and that have a property named org.springframework.osgi.bean.nameset to exampleBean.
  • Note: the bundles in the repository must be standard OSGi header-based bundles, NOT ones with Import-Bundle: and Import-Library tags. This means that the Spring-DM server is holding onto pure OSGi bundles, and allowing you to deploy your applications based on Spring-DM headers in the deployment directory.You may then use Import-Bundle, which imports all packages from a Bundle, or Import-Library, which imports a series of bundles (example: Spring Framework library)
  • Sample Manifest:Manifest-Version: 1.0Bundle-Name: DemoSpringDMBundle BundleBundle-ManifestVersion: 2Bundle-SymbolicName: DemoSpringDMBundleBundle-Version: 1.0.0Import-Library: org.springframework.spring;version="[2.5.6.A,2.5.6.A]"Import-Bundle:;version="[3.1.200.v20071203,3.1.200.v20071203]"Import-Package: org.osgi.framework;version="[1.4.0,1.4.0]"Bundle-Activator: com.gordondickens.demo.BeanRunner
  • For information on the Spring-DM Server, visit
  • Intro to OSGi and Eclipse Virgo

    1. 1. Introduction to OSGi & Eclipse VirgoGordon technophile.gordondickens.comGitHub: 1
    2. 2. >OSGi Terminology OSGI container Bundle Service Spring Dynamic Modules Spring dm Server 2
    3. 3. OSGI Container A low-footprint engine that manages OSGi components Can be embedded into applications to provide modular components Three major OSGi Containers: ◦ Equinox  The reference implementation for the framework. The container built into Eclipse.  Used by default with Spring dm Server  Spring dm Server moving to Eclipse Virgo project ◦ Knoplerfish ◦ Apache Felix 3
    4. 4. The OSGi Bundle JAR with extra Config Info ◦ Classes and resources that deliver functions to consumers ◦ Can “export” services & packages ◦ Can “import” services & packages 4
    5. 5. More on Bundles (bundles for dummies?) Bundle is a JAR Requires a Manifest that describes the bundle ◦ META-INF/MANIFEST.MF You configure the Manifest with ◦ A machine-readable ID ◦ Packages required ◦ Which packages are exportable/private ◦ Services interfaces to expose ◦ Requirements - such as the minimum JDK 5
    6. 6. Sample META- INF/MANIFEST.MFManifest-Version: 1.0Export-Package: com.gordondickens.utils,org.osgi.framework”Bundle-Name: Maven OSGi Demo – ServicesBuild-Jdk: 1.6.0Bundle-Version: 1.0.0Bundle-ManifestVersion: 2Bundle-Activator: maven-osgi-demo-servicesImport-Package: org.osgi.framework;version="1.3" 6
    7. 7. MANIFEST.MF Keywords Bundle-ManifestVersion ◦ Which OSGi container rules to follow.  2 = OSGi Specification release 4  1 = OSGi version 3 or earlier Bundle-Name ◦ Short, human readable name Bundle-SymbolicName ◦ Unique, machine-usable name ◦ Used by other bundles & container to refer to the bundle Bundle-Version ◦ Version of the bundle Bundle-Activator ◦ Listener class ◦ May be bound to bundle lifecycle (start, stop events) Bundle-Vendor ◦ Human readable name for the vendor who created the bundle Import-Package ◦ Set of packages required for dependency management 7
    8. 8. Dependencies Bundles are versioned using the header Bundle-Version ◦ Matches Maven version numbers (underscores converted to periods) Bundles can require other Bundles to function ◦ This is configured with the Import- Package header ◦ Required versions can be specified using the version= keyword Versions can be a specific version or range 8
    9. 9. Bundle Lifecycle 9
    10. 10. What can you do with a bundle? Bundles just expose packages or services Service to be used Service ◦ Does not automatically hide impl classes Service ◦ Package exporting should only be for utility packages (plumbing). Bundle◦ You only want to expose “services” 10
    11. 11. Services Register POJOs to container as a service ◦ Source bundle registers the service interface ◦ Target bundle asks the container registry for a reference to the service Services are managed by Virgo ◦ Services are “published” to the container registry ◦ Clients lookup and consume the services 11
    12. 12. Preparing the Manifest You could do that by hand, but… ◦ Format the MANIFEST.MF file EXACTLY per spec (ending each line on 72 characters, etc) Instead,Virgo provides the Bundlor build tool 12
    13. 13. Bundlor Input: • Generates: ◦ jar – ◦  Import-Template jar The BUNDLOR 13
    14. 14. What is Blueprint? A framework that ◦ Exposes & consumes services from Beans ◦ Dynamically publishes App Contexts & beans ◦ Handles dependencies ◦ Can pause bundles while locating dependencies (during updates, etc) ◦ Simplifies defining OSGi bundles ◦ The basis for the Eclipse Virgo, a fully OSGi-compliant platform based on Equinox ◦ Originally from Spring Dynamic Modules 14
    15. 15. The Blueprint Extender Extender start up ◦ Looks for Blueprint enabled Bundles ◦ Loads their application contexts automatically ◦ Publishes application contexts ◦ Exports beans as Services ◦ Injects any consumed services from other Bundles automatically When a bundle comes online ◦ Detects & publishes the Application Context ◦ Inject beans from other required app contexts Replaces Bundle Activator 15
    16. 16. Template.mfThe Bundlor uses the template.mfto generate the OSGi required manifest.mffileWhich header(s) to use for dependency resolution: Import-Package - Core OSGi ◦ Used for every external package that the bundle depends on Require-Bundle - Core OSGi ◦ Bundle must be found to start this bundle ◦ Used specifying the bundle and bundles version ◦ Brittle, only searched if not found in Import-Package Import-Bundle - Virgo ◦ Like Import-Package but all packages within an entire bundle ◦ Used specifying the bundle and bundles version. Not always a best practice, too broad brush. Import-Library - Virgo ◦ Imports all packages of a specific package, from a library in the Virgo repository. ◦ Packages can be resolved across multiple bundles ◦ Preferred approach for expressing dependency on the Spring Framework. Import-Template - Bundlor ◦ Used to import packages by version range. ◦ Used when a transitive dependencies not found "your.package (0.0.0)" or "Import of package xyz doesnt specify a version”. ◦ Can use wildcards and setup custom patterns for matching. In Summary, you need to Import Packages! Bundlor assists with directives to save you time instead of listing out all your packages in the standard OSGi Import-Package header. 16
    17. 17. Dependency Management If a Bundle requires another bundle ◦ Load the other bundle ◦ Damping - Suspends any threads attempting to access the imported packages ◦ Once loaded, threads are resumed 17
    18. 18. Using Blueprint Configuring your context The blueprint: namespace Exporting beans as OSGi Services into Registry Importing beans from OSGi Registry 18
    19. 19. Configuring your Context Good practice: ◦ Separate Spring configuration from Blueprint imports/exports  bundle-context.xml  bundle-context-osgi.xml Create2 context files in META- INF/spring ◦ All *.xml files in this dir are processed 19
    20. 20. Blueprint Services Blueprint enables services to be published and consumed using descriptions written in XML Eclipse Virgo supports Blueprint The XML descriptions reside in files with extension .xml in the bundle’s META-INF/spring sub-directory. 20
    21. 21. Spring DM Services To publish a service <blueprint:service> and specify ◦ implementation class ◦ interface class(es) ◦ at bundle start, an instance is published to the OSGi service registry under the interface ◦ impl class constructed like any other bean To consume a service <blueprint:reference> ◦ service may be passed into other beans using DI 21
    22. 22. Expose the Spring Bean as an OSGi Service  Use the osgi: namespace<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="" xmlns:xsi= xsi:schemaLocation="” default-activation="eager"><serviceref="exampleBean" interface="com.gordondickens.demo.bean.ExampleBean"/></blueprint> 22
    23. 23. Consuming the Service Use the blueprint: namespace: ◦ Service Beans ◦ Valid OSGi Service beans Can be injected into other Beans using ◦ XML injection ◦ annotation-driven injection 23
    24. 24. Example: Consuming an OSGi Service  Use the <reference>tag in the blueprint: namespace.<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns= xmlns:xsi="" xsi:schemaLocation="" default-activation="eager"><reference id="exampleBeanService”/></blueprint> 24
    25. 25. Alternative: use bean-name Exposing<bean id="exampleBean” class="com.gordondickens.demo.bean.internal.ExampleBeanImpl" /><service id="exampleBeanService” ref="exampleBean" interface="com.gordondickens.demo.bean.ExampleBean"/> Consuming <reference id="exampleBean” interface="com.gordondickens.demo.bean.ExampleBean" /> 25
    26. 26. Service Damping Proxies automatically created services ◦ the actual service object may come and go at runtime If service disappears, any proxies will wait for the service to re-appear. This effect is known as damping. 26
    27. 27. Blueprint Bundle Life When a bundle is started: ◦ builds application contexts as per the XML ◦ creates proxies for specified services ◦ publishes specified services to the registry When a bundle is stopped: ◦ retracts any services ◦ closes bundle application context ◦ turns off damping of a service proxy ◦ proxy’s app context is being closed 27
    28. 28. Eclipse Virgo OSGi Server based on Equinox Provides a PAR file archive Provides PLAN file for grouped or scoped bundles Provides Import-Bundle: tag to import all packages of a given Bundle Bundle Repository used to manage artifacts required by other projects Can be configured to automatically download required bundles from a Maven repository 28
    29. 29. Virgo Dependency Mgmt Import-Bundle: ◦ Import-Package is fine grained ◦ Import-Bundle imports all Packages from a Bundle Import-Library: ◦ Imports a Blueprint configured Library ◦ A package of Bundles ◦ Example: the Spring 3.x Framework ◦ Can use wildcards 29
    30. 30. Virgo Fragments A bundle attached to a host bundle Treated as part of the host Can only add to the host ◦ Configuration ◦ Classes ◦ Resources Can not have ◦ activator ◦ its own class loader Designated in with Fragment-Host ◦ Fragment-Host points to symbolic name of host 30
    31. 31. Virgo Feature - Slices  Fragments for web apps  Uses manifest values  In myapp/web/ ◦ Slice-Host ◦ Slice-ContextPathSlice-Host:com.gordondickenssolutions.runtime.webapp;version="[1.1.0,1.1.0]"Slice-ContextPath: /myapp 31
    32. 32. Virgo Feature - PAR Files Platform Archive – like a jar/ear/war Multiple bundles packaged together Contains ◦ application name ◦ version ◦ symbolic name ◦ description The modules within are scoped together Cannot be shared accidentally by other apps The scope forms a boundary for automatic propagation of load time weaving and bundle refresh The modules of a PAR have their exported packages imported by the synthetic context bundle, used for thread context class loading The PAR file is visible to management interfaces The PAR file can be undeployed and redeployed as a unit PARs physically contain the included artifacts 32
    33. 33. Virgo Feature -Plan Files Recommended over PARs Deployment strategy for a Virgo applications Similar to PARs describing a collection of bundles to load together as an application Simple XML file ◦ defines a collection of artifacts The syntax of a plan file ◦ outer <plan/> tag defines a name and version, as well as scoping and atomicity requirements ◦ Inside the tag is at least one artifact tag with  type  name  version 33
    34. 34. Virgo Plan Files Deploys the artifacts in the order listed Can contain other plans (nesting) Easily share content between plans Update individual parts of a plan without repackaging (re-JAR) Copy the bundles to the repository the $DMS_HOME/repository/usr directory 34
    35. 35. Virgo Plan File Example<?xml version="1.0" encoding="UTF-8"?><plan name="greenpages.db.plan" version="2.4.3.RELEASE” scoped="false" atomic="true” xmlns= xmlns:xsi= xsi:schemaLocation=""><attributes><attribute name="web.context.path" value="greenpages"/></attributes><!-- Database properties file (minus file extension) to be deployed --><artifact type="configuration" name="greenpages.db.config"/><artifact type="bundle" name="greenpages" version="[2.4, 2.5)"/><artifact type="bundle" name="greenpages.db" version="[2.4, 2.5)"/><artifact type="bundle" name="greenpages.jpa" version="[2.4, 2.5)"/><artifact type="bundle" name="greenpages.web" version="[2.4, 2.5)"><property name="header:Web-ContextPath" value="${web.context.path}"/></artifact></plan> 35
    36. 36. Connecting to OSGi Platform bin/ –shell [or] ssh -p 2402 admin@localhost 36
    37. 37. Admin Console http://localhost:8080/admin 37
    38. 38. Terminology Review Bundle Service Extender Fragment Damping Slices 38
    39. 39. Summary Virgo makes OSGi easier ◦ Registers with the OSGi container ◦ Exposes Beans as Services ◦ Consumes Services as Beans ◦ Simplfies MANIFEST.MF creation 39