Introduction to OSGi
              Chris Custine
           ccustine@apache.org




Denver Java User Group - November 12, ...
Introduction - Chris Custine

Apache ServiceMix - Committer
 Open Source ESB
 JBI 1.0 Implementation
 ServiceMix 4 - OSGi ...
Presentation Agenda

OSGi Overview and History


OSGi Architecture and Layers


Best Practices


Recommended Tools




   ...
What is OSGi?

OSGi - Formerly Open Services Gateway Initiative
Now just OSGi due to generalization of market
OSGi Allianc...
What Does the OSGi Alliance Do?

Defines OSGi Service Platform specification
Multiple groups define the specifications
 Core P...
Where did OSGi come from?

Originally aimed at Home Automation market
 Home Gateway device to monitor and interact with de...
Where did OSGi come from?

Requirement for standard plugin model for Gateway device
 GUI Model for administration of devic...
How we got here

Original use case (Home Automation) faltered around 2000
OSGi used in niche areas (automotive, mobile) fo...
OSGi is...

About building systems from smaller components
Components working together
Managing components




           ...
Why OSGi?

Convergence of mobile and embedded devices creates
huge manageability problems
 Portability - Hardware, OS
 Man...
OSGi Relationship with Java

Always focused on Java
Bundles may contain native libraries used via JNI
 Example: Eclipse us...
OSGi Service Platform

Two distinct specifications
Core Specification - Core framework, packaging,
modularity, security, API...
OSGi Architecture and Layers


                                 Service


                            Lifecycle




      ...
Java Execution Environment

Managing the runtime environment
                                                             ...
Module Layer

Packaging and deployment
standards                                                     Service


           ...
Lifecycle Layer

Fine grained events for
application and component                                         Service



life...
Services Layer

Standard methods for registering
service instances                                                Service
...
Security Layer

Protect Visibility between
components                                                         Service


  ...
Layer Details

Module, Lifecycle, and Service layers are where most work
is done for enterprise Java development
Security ...
Java Modularity Limitations

Scoping and visibility is limited to packages (no module
scoping)
Dependency management? Mana...
Module Layer

Bundles are the primary building
block
                                                                    S...
Module Layer

Bundle - Just a jar file                                                                  Service


         ...
Module Layer - Example Manifest

                          META-INF/manifest.mf

Bundle-ManifestVersion: 2
Bundle-Symbolic...
Module Layer - Example Manifest

                          META-INF/manifest.mf

Bundle-ManifestVersion: 2
Bundle-Symbolic...
Module Layer - Example Manifest

                          META-INF/manifest.mf

Bundle-ManifestVersion: 2
Bundle-Symbolic...
Module Layer - Example Manifest

                          META-INF/manifest.mf

Bundle-ManifestVersion: 2
Bundle-Symbolic...
Module Layer - Example Manifest

                          META-INF/manifest.mf

Bundle-ManifestVersion: 2
Bundle-Symbolic...
Module Layer - Example Manifest

                          META-INF/manifest.mf

Bundle-ManifestVersion: 2
Bundle-Symbolic...
Module Layer - Bundle Contents

Bundles may contain additional contents not
usually included in jars.
 Native code - Multi...
Module Layer - Bundle State


                           install

                                     update
            ...
Module Layer - Bundle State

Resolution of dependencies at
deployment                                                   in...
Module Layer - Resolution Goodies

Exporters can name arbitrary attributes for matching
 Attributes can be mandatory for m...
Module Layer - Resolution Goodies

Same package can be installed with different versions
 Importers can specify version ra...
Lifecycle Layer

Bundle-Activator: Like main() for                                  Service


your bundle.                ...
Lifecycle Layer

Bundle activation provides the                                                                 Service


...
Service Layer

Primary means of collaboration
between components
                                                         ...
Start Level Service

Setting bundle start order
Similar to Unix init scripts
Start level can be changed up and down at run...
OSGi Compendium Services

Log Service            UPnP
Http Service           Declarative Services
Device Access          E...
Configuration Admin

Configuration provided by framework service
Notification to bundles when configuration changed




      ...
Best Practices

Use package names consistently - We use package name
for artifact name (Eclipse plugins do too), with one ...
Tools for OSGi - BND

Peter Kriens - BND
 Tool for analyzing dependencies and validating manifest.mf
 Uses reflection to an...
Tools for OSGi - Maven Felix Plugin

            Uses BND to automate creation of manifest.mf
            Use packaging=bu...
Tools for OSGi - Spring DM

SpringDM bridges the Spring world with OSGi Service
Platform
Reuse existing knowledge of Sprin...
Tools for OSGi - Spring DM

Example: ActiveMQ broker definition using SpringDM and
OSGi
                                   ...
Tools for OSGi - Spring DM

Example: Using OSGi Configuration Admin for Property
Placeholder


                            ...
Conclusion

OSGi forces planning and engineering of bundle
interactions
More work up front, but rewards come later
No silv...
Tour of ServiceMix 4 OSGi Features




                                     47
Questions?



             48
Upcoming SlideShare
Loading in...5
×

Introduction To OSGi

4,599

Published on

Introduction to OSGi presentation given to the Denver Java User group on November 12, 2008

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,599
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
271
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Introduction To OSGi

  1. 1. Introduction to OSGi Chris Custine ccustine@apache.org Denver Java User Group - November 12, 2008 1
  2. 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
  3. 3. Presentation Agenda OSGi Overview and History OSGi Architecture and Layers Best Practices Recommended Tools 3
  4. 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. 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. 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. 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. 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. 9. OSGi is... About building systems from smaller components Components working together Managing components 9
  10. 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. 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. 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. 13. OSGi Architecture and Layers Service Lifecycle Security Bundles Module Execution Environment Hardware / OS 13
  14. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 30. Module Layer - Bundle State install update INSTALLED uninstall resolve uninstall UNINSTALLED RESOLVED start STARTING STOPPING stop ACTIVE 30
  31. 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. 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. 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. 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. 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. 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. 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. 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
  39. 39. Configuration Admin Configuration provided by framework service Notification to bundles when configuration changed 39
  40. 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. 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. 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. 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
  44. 44. Tools for OSGi - Spring DM Example: ActiveMQ broker definition using SpringDM and OSGi broker.xml <bean id=quot;pooledConnectionFactoryquot; class=quot;org.apache.activemq.pool.PooledConnectionFactoryBeanquot;> <property name=quot;maxConnectionsquot; value=quot;8quot; /> <property name=quot;transactionManagerquot; ref=quot;transactionManagerquot; /> <property name=quot;connectionFactoryquot; ref=quot;activemqConnectionFactoryquot; /> <property name=quot;resourceNamequot; value=quot;activemq.${name}quot; /> </bean> <osgi:reference id=quot;transactionManagerquot; interface=quot;javax.transaction.TransactionManagerquot; cardinality=quot;0..1quot;/> <osgi:service ref=quot;pooledConnectionFactoryquot;> <osgi:interfaces> <value>javax.jms.ConnectionFactory</value> </osgi:interfaces> <osgi:service-properties> <entry key=quot;namequot; value=quot;${name}quot;/> </osgi:service-properties> </osgi:service> 44
  45. 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. 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
  47. 47. Tour of ServiceMix 4 OSGi Features 47
  48. 48. Questions? 48
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×