The talk is introduction to OSGi specification and its implementations. It summarizes corner stones of OSGi (bundles, services, components) and describes a technical background of OSGi implementations on a simple example.
1. OSGi
Michal Malohlava
Charles University in Prague
Faculty of Mathematics and Physics
Czech Republic
2. Introduction
OSGi overview CZJUG, Prague – May 28, 2008
• Distributed Systems Research Group
http://dsrg.mff.cuni.cz/
Formal methods sub-group
Benchmarking gang
Components team
• Components team
SOFA2 component system
• http://sofa.ow2.org/
2
4. OSGi history
OSGi overview CZJUG, Prague – May 28, 2008
• OSGi alliance
Open Services Gateway initiative
• Founded 1999
• OSGi specification release 1 – 2000
• OSGi specification target
Define Java-based service platform, full dynamic
component model
Why?
• JVM does not support natively dynamic module system -
starting, stopping, updating application at runtime
• JAR dependencies management missing
No way how to restrict exported packages, enforce imports
4
5. OSGi motivation
OSGi overview CZJUG, Prague – May 28, 2008
• OSGi – kind of Java Operating System
• Configuration of application
“LEGO” principle :-)
• Reusability
• Scalability
• Portability
Abstraction of underlying hardware
Standardized execution environment
• Application isolation/security
Well defined classloading
• Easy managing life-cycle of application
Dynamic updates, modification of running modules
Not-only for enterprise solutions
• Embedded devices
Service oriented architecture 5
6. OSGi current state
OSGi overview CZJUG, Prague – May 28, 2008
• OSGi specification
Release 4.1 – May 2007
• core + compendium (contains mobile spec. from R3)
• Implementation
Open source
• Eclipse Equinox
Many extensions of OSGi, e.g. bundles aspects
• Apache Felix
Based on Oscar (implementation of OSGi R3)
• Knopflerfish
• Concierge
Implementation of OSGi R3, optimized for embedded devices
Commercial
• ProSyst, Knopflerfish Pro
6
7. OSGi architecture
OSGi overview CZJUG, Prague – May 28, 2008
• OSGi framework
Bundles (modules)
Execution environment
Application life cycle
Services
• Service registry
Security
• Application share
the same JVM
Isolation/security
7
8. Execution environment
OSGi overview CZJUG, Prague – May 28, 2008
• OSGi platform can run on different target devices
Profiles in specification
• OSGi/Minimum-1.1, CDC-1.1/Foundation-1.1
Other profiles
• J2SE-1.4, J2SE-1.5
OSGi APIs only use a subset of J2SE and J2ME
• Matches most profiles
• Application can enforce the profile
8
9. Bundle
OSGi overview CZJUG, Prague – May 28, 2008
• Bundle
Basic deployment entity (~ application, library)
Versioned (1.0.2.rc3 – major, minor, micro, qualifier)
Declaratively specified dependencies
Represented as JAR file of Manifest-Version: 1.0
• Code, Resources Bundle-Name: LogTargetBundle
Bundle-Activator: LogTargetActivator
• Extended manifest file Bundle-SymbolicName: LogTargetBundle
• Bundle fragments Bundle-Version: 1.0.0
Import-Package: org.osgi.framework;
Similar to bundles version=”1.3.1”
• Allow extending an existing bundle
represent e.g. native implementation, require a host bundle
• Bundle context
Representation of bundle instance in the framework
Installation of new bundle, bundle properties
Access to persistent storage
Services management, Listeners management 9
11. Bundle Activator
OSGi overview CZJUG, Prague – May 28, 2008
• Class defined in MANIFEST
Bundle-Activator header
public class SimpleLogTargetActivator {
• Can be an external class
@Override
• Handle bundle start/stop public void start(BundleContext context){
/* ... */
}
Start @Override
• Register services public void stop(BundleContext context) {
/* ... */
• Create service trackers }
}
• Start threads
Stop
• Release resources
Unregister services
Release services
11
12. Bundle dependencies
OSGi overview CZJUG, Prague – May 28, 2008
Export-Package: cz.*;
• Expose packages exclude=”*Impl”
List all of packages + versions + attributes
Fine grained package filtering
• Import bundle Import-Package: cz.mff.*;
version=”[1.0,1.3.1)”
Require specific version(s)
• e.g. [1.0, 2.0)
• Require bundle Require-Bundle: logger-api-bundle
Not recommended because it restricts further changes
in API
• Bundle class path Bundle-Classpath: ., lib/bsh.jar
12
13. Technical background of classloading
OSGi overview CZJUG, Prague – May 28, 2008
• Separated class loaders for each bundle
• Defined lookup order
Parent (only for class from java.* package)
Imported packages
Required bundles
Local class path
Image from the book OSGi in Practice, Neil Barlett
13
14. Service
OSGi overview CZJUG, Prague – May 28, 2008
• Bundles – static entities
Static dependencies through packages
How to communicate between bundles?
• Services – dynamics in OSGi
Can appear, disappear at runtime in according to a condition
For one service name multiple providers can exist
Bundles collaboration
• Well defined communication points
Services permit bundles to detect environment and adapt their behavior
• Query language
• Service
Object, registered by a bundle
• BundleContext.registerService(iface, implementation, properties)
• Service registry
• Framework automatically unregister all services of stopped bundle
Service has properties (can be modified)
Framework manages relation service <-> using bundle 14
15. Registering service (1)
OSGi overview CZJUG, Prague – May 28, 2008
• Programatically in BundleActivator
public void start(BundleContext context) {
SimpleLogTargetImpl logTargetImpl = new SimpleLogTargetImpl();
registration = context.registerService(
ILogTarget.class.getName(),
logTargetImpl,
null);
}
• Problems
Code
• Semantics is not clear
dependencies, properties, implementation v. provided interface
• Simplicity
15
16. Registering service (2)
OSGi overview CZJUG, Prague – May 28, 2008
• Declaratively
Automated service management by framework
• Declarative service
• Dependency injection of required services
Services provided by components
• Component
Entity providing/requiring exactly specified interfaces
Optional, mandatory, collection interface
<component name=quot;logger-componentquot;>
<implementation class=quot;cz.cuni...LoggerImplquot;/>
<service>
<provide interface=quot;cz...ILoggerquot;/>
</service>
<reference name=quot;TARGETquot;
interface=quot;cz...ILogTargetquot;
bind=quot;addLogTargetquot;
unbind=quot;removeLogTargetquot;
cardinality=quot;0..nquot;
policy=quot;dynamicquot;/>
</component>
16
17. Service consuming
OSGi overview CZJUG, Prague – May 28, 2008
• Bundle can search for service which implements
specific interface (defines semantics of the
service) Service reference ref =
context.getServiceRef(“cz.bar”);
• Several bad solution if (ref!=null) {
Bar bar = (Bar) context.getService(ref);
if (bar != null) {
context.getService(...) ...
context.ungetService(ref)
• Nasty code }
}
Listeners
• Just inform about changes
• Components <component name=”getServiceComp”>
<implementation class=”GetLoggerService”>
<reference name=”log”
Declare getter component interface=”org.osgi...LogService”
bind=”setLog”
• Service tracker unbind=”unsetLog”
</component>
17
18. Service tracker
OSGi overview CZJUG, Prague – May 28, 2008
• Tracking for service
Filters (name, id, property, owning bundle, ...)
• LDAP syntax (e.g. (&(objectName=”foo”)(property1=”Xyz”)) )
Adding service to registry, removing, update
//In Bundle Activator - start Name of service to track
tracker = new ServiceTracker(context,
ILogger.class.getName(),
null);
tracker.open(); ServiceTrackerCustomizer
- customize behavior of adding,
// get the service(s) removing, modifying the service
ILogger log = (ILogger)tracker.getService();
Ilogger log = (Ilogger)
tracker.waitForService(1000);
// stop tracking
tracker.close();
18
19. Whiteboard pattern
OSGi overview CZJUG, Prague – May 28, 2008
• Services dependencies
Content provider v. content consumers
• e.g. Register new service if and only if the specified service
appears
“Don't look for content providers, let them to register as
services and track for the services”
ServiceTracker capture service life-cycle
• via ServiceTrackerCustomizer
Capture process of adding/removing/modifying service
19
21. Services
OSGi overview CZJUG, Prague – May 28, 2008
• Http
Exposing registered servlets
• Event
Messaging Producer <-> Consumer
• Device manager
• Diagnostics/Monitoring
JMX (in Equinox sandbox)
• Application manager
Application package – set of resource (bundles, data,...)
• Can be deployed/install
• Location/measurement services
21
22. Security layer
OSGi overview CZJUG, Prague – May 28, 2008
• Optional
• Based on Java 2 security architecture
• 4 roles
Developer
• Adds local permissions to the bundle by signing
Admin, Service, Bundle permissions
Deployer
• Signs the bundle and deploys it
• Resulting bundle permissions = intersection of bundle and
target environment permissions
Operator
• Full control all the time
End user
22
23. Issues of OSGi R4
OSGi overview CZJUG, Prague – May 28, 2008
• No repository defined
No single trusted point for downloading bundles
• Each implementation has its own OBR (OSGi bundle
repo.)
No automatic download of required bundles/packages
Currently just RFC-112
• Searching by capabilities
Planned in OSGi R5
• Bundle dependency resolver
Resolving bundles can take long time
Maximal sharing of packages
• Stale service problem
Services can exchange objects connected to a bundle classloader.
What happens when this bundle is stopped? 23
24. Related projects
OSGi overview CZJUG, Prague – May 28, 2008
• rOSGi
Access services in a remote OSGi
• Glassfish v3
OSGi replace HK2 (module subsystem)
Uses Apache Felix
• SpringDM (Spring-OSGi)
Integration of OSGi inside Spring
• Spring application platform
Similar concept to OSGi, cooperates with it, reuses bundles
Defines bundles repository
• JSR 277
Java Modules, proposed OSGi interoperability
24