OSGi Overview
Presenter: Alex Proca
BJUG - 23 oct 2012
Plan
Problem
Solution
Architecture
Tools
Demo & Q
BJUG - 23 oct 2012
Plan
Problem
Dependency Management
Version Hell
Conflicting dependencies
Class loading issues
Dynamic Behavior
Difficulties to control component lifecycle
Lack of a pure modularity solution
Solution
Architecture
Tools
Demo & Q
BJUG - 23 oct 2012
Dependency Management
BJUG - 23 oct 2012
Version Hell - The first case is when a developer or deployer of a Java application
has accidentally made two different versions of a library available to the system. This will
not be considered an error by the system. Rather, the system will load classes from one
or the other library. Adding the new library to the list of available libraries instead of
replacing it, may see the application still behaving as though the old library is in use,
which it may well be
Conflicting dependencies - Another version of the problem arises when two
libraries (or a library and the application) require different versions of the same third
library. If both versions of the third library use the same class names, there is no way to
load both versions of the third library with the same classloader
Class loading issues - The most complex JAR hell problems arise in circumstances
that take advantage of the full complexity of the classloading system. A Java program is
not required to use only a single "flat" classloader, but instead may be composed of
several (potentially very many) nested, cooperating classloaders. Classes loaded by
different classloaders may interact in complex ways not fully comprehended by a
developer, leading to inexplicable errors or bugs.
Dynamic Behavior
BJUG - 23 oct 2012
Components and services needs to be installed / removed
Components need to get updated / replaced
Components need to be started / stopped
Example
Eclipse install/remove plugins/features
Lack of a pure modularity solution
BJUG - 23 oct 2012
JVM does not address modularity
JAR is a black box
No dependency and no version
Pure JVM modularity ?
Jigsaw for JDK 8
But OSGi is available now
Plan Problem
Solution OSGi layers
Deployment infrastructure Bundle = byte code + metadata (imports / exports)
Bundle Activator
Bundle Dependencies
Bundles have a lifecycle
Bundles have their own class loader
Bundle Versioning
Service Platform
Security Layer
Issues
Related Projects
Tools
Demo & Q
BJUG - 23 oct 2012
OSGi layers
BJUG - 23 oct 2012
OSGi Framework
Bundles (modules)
Execution environment
Application lifecycle
Services
Service registry
Security
Application share the same JVM
Bundle
BJUG - 23 oct 2012
Bundle - Basic deployment entity (~ application, library)
Versioned (1.0.2.rc3 – major, minor, micro, qualifier)
Declaratively specified dependencies
Represented as JAR file of
Code, resources
Extended Manifest file
Bundle fragments
Similar to bundles
Allow extending an existing bundle (often used for testing extended
bundle)
Requires a host bundle
Bundle Activator
BJUG - 23 oct 2012
Class defined in manifest.mf
Bundle-Activator header
Can be an external class
Handle bundle start / stop
Start
Register services
Create service trackers
Start threads
Stop
Release resources
Unregister own services
Release used services
Bundle dependencies
BJUG - 23 oct 2012
Expose packages
List all of (packages + versions + attributes)
Fine grained package filtering
Import Bundle
Require specific versions (ex. [10.0.0, 13.0.1) )
Require bundle
Restricts further changes in API (not recommended)
Bundle Class path
Embedded bundle dependencies
Maven bnd plugin uses this feature to transform some non OSGi
libraries to OSGi bundles
Bundle life cycle
BJUG - 23 oct 2012
Separate classloaders
BJUG - 23 oct 2012
Bundle Versioning
BJUG - 23 oct 2012
ZAB
1.0.0
ZAB
1.1.0
BAR
1.0.4
LIB
1.0.1
FOO
1.0.0
Different versions of ZAB in same JVM
FOO
ZAB
1.0.0
BAR
ZAB
1.0.0
Bad (ClassCastException) FOO
ZAB
1.0.0
BAR
Good
Common
Plan
Problem
Solution
OSGi layers
Deployment infrastructure
Service Platform
Registering Service
Consuming Service
Service Tracker
Security Layer
Issues
Related Projects
Tools
Demo & Q
BJUG - 23 oct 2012
Service
BJUG - 23 oct 2012
Bundles – modular but still static entities
How to communicate between bundles?
Services – dynamic in OSGi
Can appear / disappear at runtime in according to a condition
For one service name multiple providers can exist
Service
Object registered by a bundle
BundleContext.registerService(Interface, Impl, Properties)
Framework automatically unregister all services of stopped bundle
Registering Service
BJUG - 23 oct 2012
Programmatically in Bundle Activator
Problems : semantics not clear
Declaratively
DS
Service-Component header refers to xml declaring the service
Consuming Service
BJUG - 23 oct 2012
Bundle can search for service implementing specific
interface
Components
Declare getter components
Service tracker – Consuming Service
BJUG - 23 oct 2012
Tracking for service
Filters by some properties defined with the service
LDAP syntax (ex. (&(type=“dh”)(heavy=“false”)))
Plan
Problem
Solution
OSGi layers
Deployment infrastructure
Service Platform
Security Layer
Issues
Implementations
Related Projects
Tools
Demo & Q
BJUG - 23 oct 2012
Security Layer
BJUG - 23 oct 2012
Optional (most people does not use security)
Based on Java 2 Security Architecture
4 roles
Developer
Adds local permissions to the bundle by signning
Deployer
Sign the bundle and deploy
Operator
Full control all time
End User
Issues
BJUG - 23 oct 2012
No repository defined in standards
Planned for OSGi4
Equinox have p2 repo
Bundle dependency resolver
Resolving bundles can take long time
Implementations
BJUG - 23 oct 2012
Equinox
Bundles can be directories too
Used by eclipse IDE
Felix
Concierge
Knoplerfish
Related Projects
BJUG - 23 oct 2012
rOSGi – access services in a remote OSGi
Glassfish v3
OSGi replace HK2 (module system)
Uses Apache Felix
Spring DM
Donated to eclipse as Virgo project
Integration of OSGi inside spring
Spring application platform
Similar concepts to OSGi
Defines bundle repository
JSR 227
Java modules, proposed OSGi interoperability
Tools
BJUG - 23 oct 2012
m2eclipse – bridge between eclipse and maven
Tycho – build OSGi with maven
Tycho Extras – utils for OSGi builds
Credits
BJUG - 23 oct 2012
Polyglot OSGi -
http://www.slideshare.net/mstine/polyglot-osgi
Introduction to OSGi -
http://www.slideshare.net/iocanel/introduction-toosgi
OSGi presentation CZJUG -
http://www.slideshare.net/mmalohlava/osgi-presentation
Thank you
BJUG - 23 oct 2012