Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Introduction To OSGi
1. Introduction to OSGi
Chris Custine
ccustine@apache.org
Denver Java User Group - November 12, 2008
1
2. Introduction - Chris Custine
Apache ServiceMix - Committer
Open Source ESB
JBI 1.0 Implementation
ServiceMix 4 - OSGi Based (Apache Felix)
Apache Directory Server - Committer and PMC Member
LDAP v3 Certified
100% Java
Soon to be OSGi, but currently used in some OSGi services
Organic Element - Open Source Consulting and Software
Development
2
4. What is OSGi?
OSGi - Formerly Open Services Gateway Initiative
Now just OSGi due to generalization of market
OSGi Alliance - Consortium of Vendors
4
5. What Does the OSGi Alliance Do?
Defines OSGi Service Platform specification
Multiple groups define the specifications
Core Platform Expert Group (CPEG) – Core framework
Mobile Expert Group (MEG) – Mobile
Vehicle Expert Group (VEG) – Automotive
Enterprise Expert Group (EEG) – Enterprise
Residential Expert Group (REG) – Home Automation
5
6. Where did OSGi come from?
Originally aimed at Home Automation market
Home Gateway device to monitor and interact with devices
Multi vendor devices connected throughout the home
6
7. Where did OSGi come from?
Requirement for standard plugin model for Gateway device
GUI Model for administration of devices
Services available to devices
Data storage
Conversion
Network
Software management and deployment
Software updates
Security
Services only available to same device/vendor
Only allow updates from properly signed jar
“Hello, this is your refrigerator... you need to buy milk”
7
8. How we got here
Original use case (Home Automation) faltered around 2000
OSGi used in niche areas (automotive, mobile) for several
years
Eclipse 3.0 released June 2004 - First Eclipse with OSGi
Gradual adoption and recognition for OSGi after Eclipse
Currently undergoing widespread adoption
8
9. OSGi is...
About building systems from smaller components
Components working together
Managing components
9
10. Why OSGi?
Convergence of mobile and embedded devices creates
huge manageability problems
Portability - Hardware, OS
Management - Massive pushes for updates and fixes
Maintenance - Dynamic library management and
dependencies causing unmanageable hacks like
Class.forName()
10
11. OSGi Relationship with Java
Always focused on Java
Bundles may contain native libraries used via JNI
Example: Eclipse uses native libraries for SWT GUI calls
RFC-119 for distributed OSGi allows communication with
external systems (possibly not Java)
Script language support from bundle providers
11
12. OSGi Service Platform
Two distinct specifications
Core Specification - Core framework, packaging,
modularity, security, API (interfaces), etc.
Service Compendium - Additional services (some optional),
utilities, logging, event system, configuration admin, etc.
12
13. OSGi Architecture and Layers
Service
Lifecycle
Security
Bundles
Module
Execution Environment
Hardware / OS
13
14. Java Execution Environment
Managing the runtime environment
Service
What do I require to run? Lifecycle
Security
Bundles
JRE Level Module
Execution Environment
GUI Requirements Hardware / OS
Matching application requirements with the platform
14
15. Module Layer
Packaging and deployment
standards Service
Lifecycle
Security
Specify constraints on
Bundles
Module
dependencies and requirements Execution Environment
Hardware / OS
Visibility between components
15
16. Lifecycle Layer
Fine grained events for
application and component Service
lifecycle
Lifecycle
Security
Bundles
Module
Installing, starting, stopping Execution Environment
Standard API for registering,
Hardware / OS
listening, and acting on events
No custom API needed for
plugins and extensions
16
17. Services Layer
Standard methods for registering
service instances Service
Lifecycle
Security
Discover and bind to services
Bundles
Module
from other components Execution Environment
Hardware / OS
Reference tracking
17
18. Security Layer
Protect Visibility between
components Service
Lifecycle
Security
Example: Vendor A only wants to Bundles
Module
use the XYZ service provided by Execution Environment
Vendor A (could be multiple other Hardware / OS
implementations of XYZ)
Constraints can be expressed
using certificates from signed jars
or attributes for bundle, service,
etc.
18
19. Layer Details
Module, Lifecycle, and Service layers are where most work
is done for enterprise Java development
Security is important but not as widely utilized
19
20. Java Modularity Limitations
Scoping and visibility is limited to packages (no module
scoping)
Dependency management? Managed through classpath
Version handling? First package match on classpath
Modularity is not supported as a first class citizen
20
21. Module Layer
Bundles are the primary building
block
Service
Lifecycle
Security
Package visibility and sharing
Bundles
Module
Security constraints on packages
Execution Environment
Hardware / OS
Classloader per bundle
Graph of classloaders, resolved at
deploy time
21
22. Module Layer
Bundle - Just a jar file Service
Lifecycle
Security
Bundles
META-INF/manifest.mf
Module
Execution Environment
Hardware / OS
Dependencies
Versioning
Consistency
Manifest-Version: 1.0
Import-Package: ...
Export-Package: ...
Bundle-Activator: ...
Bundle-Version: 1.1.0.SNAPSHOT
Bundle-Name: ...
Bundle-ManifestVersion: 2
Bundle-SymbolicName: ...
22
23. Module Layer - Example Manifest
META-INF/manifest.mf
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitor
Bundle-Version: 1.1.0.SNAPSHOT
Import-Package: org.apache.commons.logging,org.apache.servicemix.kerne
l.filemonitor,org.osgi.framework,org.osgi.service.cm;version=quot;1.2quot;,or
g.osgi.service.packageadmin,org.osgi.util.tracker;version=quot;1.3quot;
Export-Package: org.apache.servicemix.kernel.filemonitor;uses:=quot;org.os
gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi.
Text
service.packageadmin,org.osgi.util.trackerquot;
Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivator
Bundle-Name: Apache ServiceMix Kernel :: File Monitor
Bundle-Description: This bundle monitors the deploy directory for new
OSGi bundles in jar form, expanded form or for configuration f
ile changes and undeploys/redeploys them
23
24. Module Layer - Example Manifest
META-INF/manifest.mf
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitor
Bundle-Version: 1.1.0.SNAPSHOT
Import-Package: org.apache.commons.logging,org.apache.servicemix.kerne
l.filemonitor,org.osgi.framework,org.osgi.service.cm;version=quot;1.2quot;,or
g.osgi.service.packageadmin,org.osgi.util.tracker;version=quot;1.3quot;
Export-Package: org.apache.servicemix.kernel.filemonitor;uses:=quot;org.os
gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi.
Text
service.packageadmin,org.osgi.util.trackerquot;
Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivator
Bundle-Name: Apache ServiceMix Kernel :: File Monitor
Bundle-Description: This bundle monitors the deploy directory for new
OSGi bundles in jar form, expanded form or for configuration f
ile changes and undeploys/redeploys them
24
25. Module Layer - Example Manifest
META-INF/manifest.mf
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitor
Bundle-Version: 1.1.0.SNAPSHOT
Import-Package: org.apache.commons.logging,org.apache.servicemix.kerne
l.filemonitor,org.osgi.framework,org.osgi.service.cm;version=quot;1.2quot;,or
g.osgi.service.packageadmin,org.osgi.util.tracker;version=quot;1.3quot;
Export-Package: org.apache.servicemix.kernel.filemonitor;uses:=quot;org.os
gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi.
Text
service.packageadmin,org.osgi.util.trackerquot;
Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivator
Bundle-Name: Apache ServiceMix Kernel :: File Monitor
Bundle-Description: This bundle monitors the deploy directory for new
OSGi bundles in jar form, expanded form or for configuration f
ile changes and undeploys/redeploys them
25
26. Module Layer - Example Manifest
META-INF/manifest.mf
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitor
Bundle-Version: 1.1.0.SNAPSHOT
Import-Package: org.apache.commons.logging,org.apache.servicemix.kerne
l.filemonitor,org.osgi.framework,org.osgi.service.cm;version=quot;1.2quot;,or
g.osgi.service.packageadmin,org.osgi.util.tracker;version=quot;1.3quot;
Export-Package: org.apache.servicemix.kernel.filemonitor;uses:=quot;org.os
gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi.
Text
service.packageadmin,org.osgi.util.trackerquot;
Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivator
Bundle-Name: Apache ServiceMix Kernel :: File Monitor
Bundle-Description: This bundle monitors the deploy directory for new
OSGi bundles in jar form, expanded form or for configuration f
ile changes and undeploys/redeploys them
26
27. Module Layer - Example Manifest
META-INF/manifest.mf
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitor
Bundle-Version: 1.1.0.SNAPSHOT
Import-Package: org.apache.commons.logging,org.apache.servicemix.kerne
l.filemonitor,org.osgi.framework,org.osgi.service.cm;version=quot;1.2quot;,or
g.osgi.service.packageadmin,org.osgi.util.tracker;version=quot;1.3quot;
Export-Package: org.apache.servicemix.kernel.filemonitor;uses:=quot;org.os
gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi.
Text
service.packageadmin,org.osgi.util.trackerquot;
Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivator
Bundle-Name: Apache ServiceMix Kernel :: File Monitor
Bundle-Description: This bundle monitors the deploy directory for new
OSGi bundles in jar form, expanded form or for configuration f
ile changes and undeploys/redeploys them
27
28. Module Layer - Example Manifest
META-INF/manifest.mf
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitor
Bundle-Version: 1.1.0.SNAPSHOT
Import-Package: org.apache.commons.logging,org.apache.servicemix.kerne
l.filemonitor,org.osgi.framework,org.osgi.service.cm;version=quot;1.2quot;,or
g.osgi.service.packageadmin,org.osgi.util.tracker;version=quot;1.3quot;
Export-Package: org.apache.servicemix.kernel.filemonitor;uses:=quot;org.os
gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi.
Text
service.packageadmin,org.osgi.util.trackerquot;
Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivator
Bundle-Name: Apache ServiceMix Kernel :: File Monitor
Bundle-Description: This bundle monitors the deploy directory for new
OSGi bundles in jar form, expanded form or for configuration f
ile changes and undeploys/redeploys them
28
29. Module Layer - Bundle Contents
Bundles may contain additional contents not
usually included in jars.
Native code - Multiple platforms, OS, etc.
loading rules specified in manifest
Embedded jar files - Bundle-Classpath allows
embedded jars to be referenced from
manifest.mf (not recommended)
Resource files - images, configuration, html
29
30. Module Layer - Bundle State
install
update
INSTALLED
uninstall resolve
uninstall
UNINSTALLED RESOLVED
start
STARTING STOPPING
stop
ACTIVE
30
31. Module Layer - Bundle State
Resolution of dependencies at
deployment install
INSTALLED
update
If resolution fails, bundle is not
uninstall resolve
uninstall
UNINSTALLED RESOLVED
available for use
start
STARTING STOPPING
stop
No runtime CNFE ACTIVE
Services and lifecycle not
called until AFTER resolution
31
32. Module Layer - Resolution Goodies
Exporters can name arbitrary attributes for matching
Attributes can be mandatory for matching
org.apache.mypackagea-1.0.0.jar org.apache.mypackageb-1.0.0.jar
META-INF/manifest.mf META-INF/manifest.mf
Export-Package: Export-Package:
org.apache.mypackage; org.apache.mypackage;
version=”1.0.0” version=”1.0.0”;flavor=”vanilla”
org.apache.bundlea-1.0.0.jar
META-INF/manifest.mf
Import-Package:
org.apache.mypackage;
version=”1.0.0”;flavor=”vanilla”
32
33. Module Layer - Resolution Goodies
Same package can be installed with different versions
Importers can specify version ranges for compatibility
Allows staged migration of multiple components
org.apache.mypackage-1.0.1.jar org.apache.mypackage-2.0.0.jar
META-INF/manifest.mf META-INF/manifest.mf
Export-Package: Export-Package:
org.apache.mypackage; org.apache.mypackage;
version=”1.0.1” version=”2.0.0”
org.apache.bundlea-1.0.0.jar
META-INF/manifest.mf
Import-Package:
org.apache.mypackage;
version=”[1.0.1,2.0.0)”
33
34. Lifecycle Layer
Bundle-Activator: Like main() for Service
your bundle. Lifecycle
Security
Bundles
Bundles can tap into the lifecycle
Module
Execution Environment
events of the framework Hardware / OS
Extremely powerful when
combined with startlevel features
Ability to register listeners for
bundle state changes
34
35. Lifecycle Layer
Bundle activation provides the Service
bundle context to the bundle Lifecycle
Security
Bundles
Module
Bundle context provides access
Execution Environment
Hardware / OS
to the framework and all services
Creates and registers service
instances
Get reference to required services
Register listeners for bundle
lifecycle BundleActivator.java
package org.osgi.framework;
public interface BundleActivator {
public void start(BundleContext context) throws Exception;
public void stop(BundleContext context) throws Exception;
}
35
36. Service Layer
Primary means of collaboration
between components
Service
Lifecycle
Security
Service Registry for discovering
Bundles
Module
and binding to services Execution Environment
Hardware / OS
Ability to listen for services
(Service Tracker)
Service oriented model for in VM
interactions
RFC-119 Brings distributed
services across instances
36
37. Start Level Service
Setting bundle start order
Similar to Unix init scripts
Start level can be changed up and down at runtime
Persistent start level on restarts
37
38. OSGi Compendium Services
Log Service UPnP
Http Service Declarative Services
Device Access Event Admin
Configuration Admin Deployment Admin
Metatype Service Auto Configuration
Preferences Service Application Admin
User Admin DMT Admin
Wire Admin Monitor Admin
IO Connector Service Tracker
Initial Provisioning
38
40. Best Practices
Use package names consistently - We use package name
for artifact name (Eclipse plugins do too), with one package
heirarchy per bundle. Quick recognition of artifact and
package purpose.
Keep public and private classes in separate packages
Use explicit Import-Package. Don’t use Require-Bundle.
Easy to use initially, but causes problems down the road.
Avoid Dynamic-Import - Defers classloading to runtime
Allows arbitrary Class.forName() use case but causes trouble
Use tools to compose manifest files and package bundles
(Maven plugins, Eclipse PDE, etc.)
40
41. Tools for OSGi - BND
Peter Kriens - BND
Tool for analyzing dependencies and validating manifest.mf
Uses reflection to analyze imports and lists dependencies
Great tool for analyzing non-OSGi jar files and converting
41
42. Tools for OSGi - Maven Felix Plugin
Uses BND to automate creation of manifest.mf
Use packaging=bundle to invoke plugin in package lifecycle
pom.xml
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Export-Package>org.apache.servicemix.kernel.gshell.activemq;version=${project.version};-
split-package:=merge-first</Export-Package>
<Import-Package>
org.apache.geronimo.gshell.wisdom.command,
org.apache.servicemix.kernel.gshell.core,
org.apache.geronimo.gshell.wisdom.registry,
*
</Import-Package>
<Private-Package>!*</Private-Package>
<Spring-Context>*;publish-context:=false;create-asynchronously:=false</Spring-Context>
</instructions>
</configuration>
</plugin>
42
43. Tools for OSGi - Spring DM
SpringDM bridges the Spring world with OSGi Service
Platform
Reuse existing knowledge of Spring
Limited knowledge of OSGi required
Expose spring beans in the OSGi Service Registry
Can be bound to by pure OSGi service consumers
Lookup and bind bean refs to OSGi services
43
45. Tools for OSGi - Spring DM
Example: Using OSGi Configuration Admin for Property
Placeholder
beans.xml
<bean id=quot;myTransformquot; class=quot;org.apache.servicemix.examples.camel.MyTransformquot;>
<property name=quot;prefixquot; value=quot;${prefix}quot;/>
</bean>
<osgix:property-placeholder persistent-id=quot;org.apache.servicemix.examplesquot;>
<osgix:default-properties>
<prop key=quot;prefixquot;>MyTransform</prop>
</osgix:default-properties>
</osgix:property-placeholder>
45
46. Conclusion
OSGi forces planning and engineering of bundle
interactions
More work up front, but rewards come later
No silver bullet, bad code and bad ideas are still bad
Many built in features and services provided for applications
Modularization is rewarded when creating new systems and
modifying existing ones
46