•Have a chance to update Ecube’sarchitecture and want to do it “right” (or atleast “better”)

•The OSGi advertising sounds good,wanted to learn more

Questions to ask

•Is there a need?

•What do we get?

•What does it cost?

–How much do we need to change our code?

–How much bloat does it add?

–Any efficiency concerns?

What’s wrong with standard java?

•Simplistic version handling

–First found in class path

•Implicit dependences

–Class path ordering

•Split packages by default

–Could get mix of versions

•Limited support for dynamic code

–Can use class loaders but quite low level

What is OSGi?

•OSGi technology is the dynamic module system for Java™. The OSGiService Platform provides functionality to Java that makes Java the premierenvironment for software integration and thus for development. Javaprovides the portability that is required to support products on manydifferent platforms. The OSGi technology provides the standardizedprimitives that allow applications to be constructed from small, reusable andcollaborative components. These components can be composed into anapplication and deployed.

•The OSGi Service Platform provides the functions to change thecomposition dynamically on the device of a variety of networks, withoutrequiring restarts. To minimize the coupling, as well as make thesecouplings managed, the OSGi technology provides a service-orientedarchitecture that enables these components to dynamically discover eachother for collaboration. The OSGi Alliance has developed many standardcomponent interfaces for common functions like HTTP servers,configuration, logging, security, user administration, XML and many more.Plug-compatible implementations of these components can be obtainedfrom different vendors with different optimizations and costs. However,service interfaces can also be developed on a proprietary basis.

OK, so what is OSGi really?

Specification for:

•Packaging bundles (modules)

•Runtime

–Class loading policies

–Can express dependences between bundles

–Lifecycle: Install, start, stop, upgrade bundlesin running system

–Service registry

–Management console

Open Source Implmentations

•Eclipse Equinox

–If you use Eclipse 3, then you’re already usingOSGi

–Eclipse plugins are packaged as OSGibundles

–Eclipse support for OSGi bundle development

•Knoplerfish

•Apache Felix

Anatomy of a bundle

JAR file

META-INF / MANIFEST.MF

STUFF

Anatomy of a bundle

•Just jar files with a

META-INF/MANIFEST.MF

–But jar files are not necessarily bundles!

•Bundles can act as ordinary jar files

•Has own class path

•Can declare dependencies on native code

•Dependencies modelled by java packages, notbundles

•Can export packages to other bundles

•Can import packages from other bundles

META-INF / MANIFEST.MF

Bundle-ManifestVersion: 2

Bundle-Name: OSGI Demo 1

Bundle-SymbolicName:uk.co.ecube.osgi.demo1

Bundle-Version: 1.0.0

Bundle-Activator:uk.co.ecube.osgi.demo1.Activator

Export-Package: bar; version="1.0.0“

Import-Package: foo;version="[1.0.0,1.5.0)"

Will this work?

package uk.co.ecube.foo;

public class Thing {

….

}

package uk.co.ecube.bar;

import uk.co.ecube.foo.Thing;

public class ThingUser {

public void doSomething() {

Thing thingy = new Thing();

...

}

}

Activator

package uk.co.ecube.osgi.demo1;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

public void start(BundleContext context) throws Exception {

System.out.println("Hello World!!");

}

public void stop(BundleContext context) throws Exception {

System.out.println("Goodbye Cruel World!!");

}

}

Making a service available (1)

public class Activator implements BundleActivator {

ServiceRegistration buzzwordServiceRegistration;

public void start(BundleContext context) throws Exception {

BuzzwordService buzzwordService = new BuzzwordImpl();

buzzwordServiceRegistration =

context.registerService(BuzzwordService.class.getName(),

buzzwordService, new Hashtable());

}

public void stop(BundleContext context) throws Exception {

buzzwordServiceRegistration.unregister();

}

}

Service consumer (1)

public class Activator implements BundleActivator {

ServiceReference _buzzwordServiceReference = null;

public void start(BundleContext context) throws Exception {

_buzzwordServiceReference =

context.getServiceReference(

BuzzwordService.class.getName());

BuzzwordService buzzwordService = (BuzzwordService)

context.getService(_buzzwordServiceReference);

System.out.println("I think you need some “

+buzzwordService.buzzword());

}

public void stop(BundleContext context) throws Exception {

context.ungetService(_buzzwordServiceReference);

}

}

Bundle status

•Not in the list

-

If your bundle isn't in the list, then OSGidoesn't know anything about it.

•INSTALLED

-

This means that OSGi knows about yourbundle but there is something wrong and it couldn'tresolve.

•RESOLVED

-

All dependencies resolved but bundle notrunning (could be a startup problem).

•<<lazy>>

-

This means your bundle is resolved and ismarked to be lazy started. Everything should be ok.

•ACTIVE

-

your bundle is resolved and has been started,everything should be working as planned.

Console

Some sample commands:

•ss-

shows the installed bundles, their IDs andtheir status

•install <JAR>-

loads a jar file into the container

•diag <ID>-

diagnostic on the specified bundle.ie information about any missing importedpackages

•start <ID>-

start a bundle

•stop <ID>-

stop a bundle

•exit

Demo #1

•Consumer 1

•Service 1

•Simple service & consumer

•OSGi console

Service factories

•In previous example the service was asingle object

•What if we want to create a new object foreach consume?

–Use a service factory

Service factory

public class BuzzwordServiceFactory implements ServiceFactory {

public Object getService(Bundle bundle,

ServiceRegistration registration) {

BuzzwordService buzzwordService = new BuzzwordImpl();

return buzzwordService;

}

public void ungetService(Bundle bundle,

ServiceRegistration registration,

Object service) {

// whatever we need to do to shut down the service object

}

}

Activator for a factory

public class FactoryActivator implements BundleActivator {

ServiceRegistration buzzwordServiceRegistration;

public void start(BundleContext context) throws Exception {

BuzzwordServiceFactory buzzwordServiceFactory =

new BuzzwordServiceFactory();

buzzwordServiceRegistration =

context.registerService(BuzzwordService.class.getName(),

buzzwordServiceFactory, null);

}

public void stop(BundleContext context) throws Exception {

buzzwordServiceRegistration.unregister();

}

}

Tracking services

•Suppose we want to know when a serviceis available and when it becomesunavailable?

–Can use a service tracker to get notifications

Service tracker

public class BuzzwordServiceTracker extends ServiceTracker {

public BuzzwordServiceTracker(BundleContext context) {

super(context, BuzzwordService.class.getName(),null);

}

public Object addingService(ServiceReference reference) {

return super.addingService(reference);

}

public void removedService(ServiceReference reference,

Object service) {

super.removedService(reference, service);

}

}

Activator using a tracker

public class TrackerActivator implements BundleActivator {

BuzzwordServiceTracker buzzwordServiceTracker;

public void start(BundleContext context) throws Exception {

buzzwordServiceTracker = new BuzzwordServiceTracker(context);

buzzwordServiceTracker.open();

BuzzwordService buzzwordService =

(BuzzwordService) buzzwordServiceTracker.getService();

if (null != buzzwordService) {

System.out.println("I think you need some “

+buzzwordService.buzzword());

}

}

public void stop(BundleContext context) throws Exception {

buzzwordServiceTracker.close();

}

}

Demo #2

•Consumer 2

•Service 2

•Factories and trackers

Spring DM

•Programmatically exporting and locatingservices could get tedious very quickly

•Spring DM can locate services for us

•As far as we are concerned Spring DMwiring is much like a normal Springapplication.