Introduction To OSGi
Upcoming SlideShare
Loading in...5
×
 

Introduction To OSGi

on

  • 5,514 views

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

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

Statistics

Views

Total Views
5,514
Views on SlideShare
5,484
Embed Views
30

Actions

Likes
2
Downloads
245
Comments
0

3 Embeds 30

http://www.slideshare.net 26
http://www.linkedin.com 3
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction To OSGi Introduction To OSGi Presentation Transcript

  • Introduction to OSGi Chris Custine ccustine@apache.org Denver Java User Group - November 12, 2008 1
  • 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
  • Presentation Agenda OSGi Overview and History OSGi Architecture and Layers Best Practices Recommended Tools 3
  • What is OSGi? OSGi - Formerly Open Services Gateway Initiative Now just OSGi due to generalization of market OSGi Alliance - Consortium of Vendors 4
  • 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
  • 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
  • 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
  • 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
  • OSGi is... About building systems from smaller components Components working together Managing components 9
  • 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
  • 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
  • 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
  • OSGi Architecture and Layers Service Lifecycle Security Bundles Module Execution Environment Hardware / OS 13
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Module Layer - Bundle State install update INSTALLED uninstall resolve uninstall UNINSTALLED RESOLVED start STARTING STOPPING stop ACTIVE 30
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Configuration Admin Configuration provided by framework service Notification to bundles when configuration changed 39
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Tour of ServiceMix 4 OSGi Features 47
  • Questions? 48