Enterprise OSGi runtime setup with Apache Aries Blueprint
Z Jacek Laskowski - Wiki Projektanta Java EE
After the seminar where I presented Enterprise OSGi and Contexts and Dependency Injection for the Java EE platform at University of Zielona Gora the idea to read a few books about OSGi awaiting on my bookshelf crossed my mind. I turned my attention to OSGi and Equinox: Creating Highly Modular Java Systems. While not Enterprise OSGi-oriented, the book provided many tips and tricks about OSGi, and therefore I made the decision to give them a whirl with Eclipse Equinox (as the OSGi framework) and Apache Aries (as the Enterprise OSGi framework). The idea is to find out the pros and cons of developing modular, enterprise applications with Java EE, CDI and Enterprise OSGi. They're often crossing each other domains claiming easier development of modular, enterprise applications.
I set out my Enterprise OSGi journey with setting up an environment to host Blueprint bundles I'm going to develop while studying the specification.
The article assumes online connection (for a few bundles to be downloaded) and Equinox Stable Build: 3.7RC3. While downloading bundles makes the final OSGified product tailor-made and slim, I decided to pull down "a complete set of all bundles and launchers produced by the Equinox project" - equinox-SDK-3.7RC3.zip.
I use rlwarp for better command line management - history and simple editing. It's not necessary for the endeavor, but helps enormously on command line.
The option -configuration <location> is for placing the OSGi configuration in another place than the default plugins/configuration. Consult The Eclipse runtime options.
jacek:~/apps/equinox-SDK-3.7RC3 $ rlwrap -H ~/equinox-aries.rlwrap java -jar plugins/org.eclipse.osgi_3.7.0.v20110524.jar -configuration ~/equinox-aries.configuration -console osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.7.0.v20110524
At the time when the need to install bundles off maven repositories surfaced, along came the latest version of Pax URL 1.3.2 that's exactly for the purpose - installing OSGi bundles that are in maven repos.
osgi> install http://repo1.maven.org/maven2/org/ops4j/pax/url/pax-url-mvn/1.3.2/pax-url-mvn-1.3.2.jar Bundle id is 1 osgi> start 1 osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.7.0.v20110524 1 ACTIVE org.ops4j.pax.url.mvn_1.3.2
For Apache Aries OSGi bundles you may fancy using mvn:[repository_url!]groupId/artifactId[/[version]/[type]] (as described at OPS4J Pax URL documentation). I've built Apache Aries locally from the sources so the bundles are in the local repository of mine.
To run the Apache Aries Blueprint Sample I had to build the modules: Apache Aries Blueprint API, Apache Aries Blueprint Core, Apache Aries Proxy, and Apache Aries Util.
Due to an issue with the org.apache.aries.blueprint.api bundle that merely exports the version 0.0.0 of org.osgi.service.blueprint, but the version 1.0.0 is required, the org.apache.aries.blueprint bundle is used instead.
osgi> install mvn:org.apache.aries.blueprint/org.apache.aries.blueprint/0.3.2-SNAPSHOT Bundle id is 2 osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.7.0.v20110524 1 ACTIVE org.ops4j.pax.url.mvn_1.3.2 2 INSTALLED org.apache.aries.blueprint_0.3.2.SNAPSHOT
Don't start the bundle 2 as the necessary imports are not available yet.
osgi> install file:plugins/org.eclipse.osgi.services_3.3.0.v20110513.jar Bundle id is 3 osgi> install mvn:org.apache.aries.proxy/org.apache.aries.proxy.api/0.4-SNAPSHOT Bundle id is 4 osgi> install mvn:org.apache.aries.proxy/org.apache.aries.proxy.impl/0.4-SNAPSHOT Bundle id is 5 osgi> install mvn:org.apache.aries/org.apache.aries.util/0.4-SNAPSHOT Bundle id is 6 osgi> install file:plugins/org.eclipse.equinox.cm_1.0.300.v20110502.jar Bundle id is 7
The following OPS4J Pax Logging bundles are described at http://www.ops4j.org/projects/pax/logging/.
osgi> install mvn:http://repo2.maven.org/maven2/!org.ops4j.pax.logging/pax-logging-api/1.6.2 Bundle id is 8 osgi> install mvn:http://repo2.maven.org/maven2/!org.ops4j.pax.logging/pax-logging-service/1.6.2 Bundle id is 9 osgi> install mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.sample/0.3.2-SNAPSHOT Bundle id is 10 osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.7.0.v20110524 1 ACTIVE org.ops4j.pax.url.mvn_1.3.2 2 INSTALLED org.apache.aries.blueprint_0.3.2.SNAPSHOT 3 INSTALLED org.eclipse.osgi.services_3.3.0.v20110513 4 INSTALLED org.apache.aries.proxy.api_0.4.0.SNAPSHOT 5 INSTALLED org.apache.aries.proxy.impl_0.4.0.SNAPSHOT 6 INSTALLED org.apache.aries.util_0.4.0.SNAPSHOT 7 INSTALLED org.eclipse.equinox.cm_1.0.300.v20110502 8 INSTALLED org.ops4j.pax.logging.pax-logging-api_1.6.2 9 INSTALLED org.ops4j.pax.logging.pax-logging-service_1.6.2 10 INSTALLED org.apache.aries.blueprint.sample_0.3.2.SNAPSHOT osgi> start 10 Bundle start osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.7.0.v20110524 1 ACTIVE org.ops4j.pax.url.mvn_1.3.2 2 RESOLVED org.apache.aries.blueprint_0.3.2.SNAPSHOT 3 RESOLVED org.eclipse.osgi.services_3.3.0.v20110513 4 RESOLVED org.apache.aries.proxy.api_0.4.0.SNAPSHOT 5 RESOLVED org.apache.aries.proxy.impl_0.4.0.SNAPSHOT 6 RESOLVED org.apache.aries.util_0.4.0.SNAPSHOT 7 RESOLVED org.eclipse.equinox.cm_1.0.300.v20110502 8 RESOLVED org.ops4j.pax.logging.pax-logging-api_1.6.2 9 RESOLVED org.ops4j.pax.logging.pax-logging-service_1.6.2 10 ACTIVE org.apache.aries.blueprint.sample_0.3.2.SNAPSHOT
If you need to have an insight into the way the Blueprint works, start the bundles org.ops4j.pax.logging.pax-logging-* to turn DEBUG messages on that will be printed out when bundles and services change their state.
osgi> start 9 osgi> [Framework Event Dispatcher] DEBUG org.ops4j.pax.logging.pax-logging-service - BundleEvent STARTED osgi> start 8 [OSGi Console] INFO org.ops4j.pax.logging.internal.Activator - Enabling SLF4J API support. [OSGi Console] INFO org.ops4j.pax.logging.internal.Activator - Enabling Jakarta Commons Logging API support. [OSGi Console] INFO org.ops4j.pax.logging.internal.Activator - Enabling Log4J API support. [OSGi Console] INFO org.ops4j.pax.logging.internal.Activator - Enabling Avalon Logger API support. [OSGi Console] INFO org.ops4j.pax.logging.internal.Activator - Enabling JULI Logger API support. [Framework Event Dispatcher] DEBUG org.ops4j.pax.logging.pax-logging-api - BundleEvent STARTED osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.7.0.v20110524 1 ACTIVE org.ops4j.pax.url.mvn_1.3.2 2 RESOLVED org.apache.aries.blueprint_0.3.2.SNAPSHOT 3 RESOLVED org.eclipse.osgi.services_3.3.0.v20110513 4 RESOLVED org.apache.aries.proxy.api_0.4.0.SNAPSHOT 5 RESOLVED org.apache.aries.proxy.impl_0.4.0.SNAPSHOT 6 RESOLVED org.apache.aries.util_0.4.0.SNAPSHOT 7 RESOLVED org.eclipse.equinox.cm_1.0.300.v20110502 8 ACTIVE org.ops4j.pax.logging.pax-logging-api_1.6.2 9 ACTIVE org.ops4j.pax.logging.pax-logging-service_1.6.2 10 ACTIVE org.apache.aries.blueprint.sample_0.3.2.SNAPSHOT
Note the difference between the way the sample works with and without Apache Aries Blueprint.
osgi> start 2
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Starting blueprint extender...
[OSGi Console] DEBUG org.apache.aries.blueprint - ServiceEvent REGISTERED
[OSGi Console] INFO org.apache.aries.blueprint.container.BlueprintExtender - No quiesce support is available, so blueprint components
will not participate in quiesce operations
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Blueprint extender started
[Framework Event Dispatcher] DEBUG org.apache.aries.blueprint - BundleEvent STARTED
osgi> start 3
osgi> start 5
[OSGi Console] DEBUG org.apache.aries.proxy.impl - ServiceEvent REGISTERED
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Found ProxyManager service, starting to process blueprint bundles
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.eclipse.osgi for blueprint application
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - No blueprint application found in bundle org.eclipse.osgi
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.ops4j.pax.url.mvn for blueprint application
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - No blueprint application found in bundle org.ops4j.pax.url.mvn
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.apache.aries.blueprint for blueprint application
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Found blueprint application in bundle org.apache.aries.blueprint with paths:
[bundleentry://2.fwk788223665/OSGI-INF/blueprint/blueprint-cm.xml, bundleentry://2.fwk788223665/OSGI-INF/blueprint/blueprint-ext.xml]
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scheduling creation of blueprint bundle org.apache.aries.blueprint
asynchronously
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.eclipse.osgi.services for blueprint application
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Running blueprint container for bundle
org.apache.aries.blueprint in state Unknown
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - No blueprint application found in bundle org.eclipse.osgi.services
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.ops4j.pax.logging.pax-logging-api for blueprint application
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - No blueprint application found in bundle org.ops4j.pax.logging.pax-logging-api
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.ops4j.pax.logging.pax-logging-service for
blueprint application
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - No blueprint application found in bundle
org.ops4j.pax.logging.pax-logging-service
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.apache.aries.blueprint.sample for blueprint application
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Found blueprint application in bundle org.apache.aries.blueprint.sample
with paths: [bundleentry://10.fwk788223665/OSGI-INF/blueprint/config.xml]
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Grace-period directive: false
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scheduling creation of blueprint bundle
org.apache.aries.blueprint.sample asynchronously
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint.container.BlueprintEventDispatcher - Sending blueprint container event
BlueprintEvent[type=CREATING] for bundle org.apache.aries.blueprint
[Blueprint Extender: 2] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Running blueprint container for bundle
org.apache.aries.blueprint.sample in state Unknown
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Scanning bundle org.apache.aries.proxy.impl for blueprint application
[Blueprint Extender: 2] DEBUG org.apache.aries.blueprint.container.BlueprintEventDispatcher - Sending blueprint container event
BlueprintEvent[type=CREATING] for bundle org.apache.aries.blueprint.sample
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - No blueprint application found in bundle org.apache.aries.proxy.impl
...
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Instantiating components:
[blueprintContainer, blueprintBundle, blueprintBundleContext, blueprintConverter]
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint - ServiceEvent REGISTERED
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint.container.BlueprintEventDispatcher - Sending blueprint container event
BlueprintEvent[type=CREATED] for bundle org.apache.aries.blueprint
[Blueprint Extender: 1] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Running blueprint container for bundle
org.apache.aries.blueprint in state Created
osgi> start 7
[OSGi Console] DEBUG org.eclipse.equinox.cm - ServiceEvent REGISTERED
[OSGi Console] DEBUG org.eclipse.equinox.cm - ServiceEvent REGISTERED
[OSGi Console] DEBUG org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe - Tracking reference
{org.osgi.service.cm.ConfigurationAdmin}={service.id=38} for OSGi service (objectClass=org.osgi.service.cm.ConfigurationAdmin)
[OSGi Console] DEBUG org.apache.aries.blueprint.container.ReferenceRecipe - Binding reference #recipe-3 to
{org.osgi.service.cm.ConfigurationAdmin}={service.id=38}
...
[Blueprint Extender: 2] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Running blueprint container for bundle
org.apache.aries.blueprint.sample in state Created
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.7.0.v20110524
1 ACTIVE org.ops4j.pax.url.mvn_1.3.2
2 ACTIVE org.apache.aries.blueprint_0.3.2.SNAPSHOT
3 ACTIVE org.eclipse.osgi.services_3.3.0.v20110513
4 RESOLVED org.apache.aries.proxy.api_0.4.0.SNAPSHOT
5 ACTIVE org.apache.aries.proxy.impl_0.4.0.SNAPSHOT
6 RESOLVED org.apache.aries.util_0.4.0.SNAPSHOT
7 ACTIVE org.eclipse.equinox.cm_1.0.300.v20110502
8 ACTIVE org.ops4j.pax.logging.pax-logging-api_1.6.2
9 ACTIVE org.ops4j.pax.logging.pax-logging-service_1.6.2
10 ACTIVE org.apache.aries.blueprint.sample_0.3.2.SNAPSHOT
osgi> stop 10
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintExtender - Destroying BlueprintContainer for bundle org.apache.aries.blueprint.sample
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintEventDispatcher - Sending blueprint container event
BlueprintEvent[type=DESTROYING] for bundle org.apache.aries.blueprint.sample
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.container.ServiceRecipe - Unregistering service .component-2
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.container.ServiceRecipe - Unregistering service .component-1
[OSGi Console] DEBUG org.apache.aries.blueprint.container.ServiceRecipe - Calling listeners for service unregistration
Service unregistration notification: 5 -1 1854861687: Hello FooBar org.eclipse.osgi.framework.internal.core.BundleContextImpl@6335605a
[a list element, 5] PLN Fri Apr 17 00:00:00 CEST 2009 {osgi.service.blueprint.compname=foo, key=value}
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Instantiating component defaultRunnable
[OSGi Console] DEBUG org.apache.aries.blueprint.compendium.cm.CmManagedProperties - Removing bean for bean=container-managed /
pid=blueprint-sample-managed.container
[OSGi Console] DEBUG org.apache.aries.blueprint.compendium.cm.CmManagedProperties - Removing bean for bean=component-managed /
pid=blueprint-sample-managed.component
[OSGi Console] DEBUG org.apache.aries.blueprint.compendium.cm.CmManagedProperties - Removing bean for bean=none-managed /
pid=blueprint-sample-managed.none
======== Destroying Foo =========
[OSGi Console] DEBUG org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder - Destroying CmPropertyPlaceholder
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.sample - ServiceEvent UNREGISTERING
[OSGi Console] DEBUG org.apache.aries.blueprint.container.ReferenceRecipe - Unbinding reference ref2
[OSGi Console] DEBUG org.apache.aries.blueprint.container.ReferenceRecipe - Unbinding reference refWithDefault
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintEventDispatcher - Sending blueprint container event
BlueprintEvent[type=DESTROYED] for bundle org.apache.aries.blueprint.sample
[OSGi Console] DEBUG org.apache.aries.blueprint.container.BlueprintContainerImpl - Blueprint container destroyed:
org.eclipse.osgi.framework.internal.core.BundleContextImpl@6335605a
Bundle stopThe environment is ready for hosting Blueprint bundles as Apache Aries Blueprint Sample proved.
