OSGi Introduction
Ly Minh Phuong – Social Team
2
Objective
OSGi architecture overview Bundles Services Lifecycle
DemoQ&A
OSGi framework Motivation
3
Why OSGi?
Java not support natively dynamic module system.(@2011)
JAR dependences management is missing.
No way to restrict using class in JAR file.
4
Why OSGi?
Java not support natively dynamic module system.(@2011)
5
Why OSGi?
JAR dependences management is missing.
What happened when two jar file have duplicate packages and classes ?
What happened when your classes only work with a library which version < 2.3.0 ?
Can it be prevent in run time ? No way to restrict using class in JAR file.
When writing a lib/component can we control what user can import or not ?
6
Why OSGi?
Resolve in OSGi way.
7
What is OSGi
OSGi alliance
Open Service Gateway initiative
Founded 1999
Specification first released in 2000
Newest version of specification is 4.3 (April 2011)
Target: embedded, desktop, enterprise application.
Widely adopted: Eclipse Equinox, Apache Felix, Glassfish, Knopflerfish, ProSyst, Hitachi SuperJ Engine, …
8
Basic architect
9
Bundle
Bundle
Basic deployment entity ( app, component, library)
Version convention ( major.minor.micro.qualifier )
Specify the dependence, export package, activator
Package like JAR file and added the bundle meta info
Bundle-ManifestVersion: 2Bundle-Name: Greeting ClientBundle-SymbolicName: org.foo.hello.clientBundle-Version: 1.2.1.SNAPSHOT
10
Bundle Lifecycle
11
Bundle dependency resolution The framework prefer already resolved bundle
If both are resolved the framework prefer highest matching version.
If the version is equal it's prefer the older one.
12
Bundle Activator Bundle Activator
Define by Bundle-Activator header
Handle when bundle start/stop
public final class Activator implements BundleActivator{ public void start( BundleContext bc ) throws Exception{ .... } public void stop( BundleContext bc ) throws Exception{ .... }}
13
Bundle Mainifest example
Bundle-ManifestVersion: 2Bundle-Name: Greeting ClientBundle-SymbolicName: org.foo.hello.clientBundle-Version: 1.2.1.SNAPSHOTBundle-Activator: org.foo.hello.client.Activator Import-Package: org.foo.hello;version="[1.0,2.0)"Export-Package: org.org.foo.hello.api;version="1.0"Bundle-ClassPath: .,WEB-INF/classes,WEB-INF/lib/a.jar
14
Service
Service
Bundle can register/unregister service using bundle activator.
One service name can have multi provider
Service can have properties to describe the service info
15
Registering a service
Programmatically using Activator
Dictionary properties = new Properties();properties.setProperty("BookStorageType", "MySQL");
bundleContext.registerService(BookStorage.class.getName(), new BookStorageImpl(), properties );
16
Consuming a service
Programatically using bundleContext.getSerivce()ServiceReference bookStorageRef = bc.getServiceReference(BookStorage.class.getName());if(bookStorageRef != null){
bookStorage = (BookStorage) bc.getService(bookStorageRef);System.out.println("Got BookStorage Service");
}
Use Service ListenerActivator.javaString filter = "(" + Constants.OBJECTCLASS + "=" + BookStorage.class.getName() + ")";context.addServiceListener(new BookStorageListener(), filter);
BookStorageListener.javaclass BookStorageListener implements ServiceListener { public void serviceChanged(ServiceEvent event) { .... }}
17
Service consuming
Use Service Tracker
public class XyzServiceTracker extends ServiceTracker { public XyzServiceTracker(BundleContext context) { super(context, ServiceToTrack.class.getName(), null); ... } public Object addingService(ServiceReference reference) { ... } public void removedService(ServiceReference reference, Object service) { context.ungetService(reference); ... }}
18
Problems about OSGi
OSGi look great but tool and development process not widely public yet.
Not every lib is bundle.
Version management can be nightmare.
Runtime dynamic can result untested situation.
19
DEMO
20
Q&A