Demistifying OSGi

Preview:

DESCRIPTION

The slide deck used during Colombo Java meetup.

Citation preview

Demystifying OSGi

Sameera JayasomaWSO2 Inc.

Pradeep FernandoWSO2 Inc.

Colombo Java Meetup24-01-2013

Modular Systems...

Image courtsy of: http://www.thefeltsource.com/Human-Anatomy.html

Down the Memory Lane...

Functions

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

Modules

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

Classes/Objects

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

Packages

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

Modularity with Java

Can we build a pure modular system with Java?

Modularity with Java

● Yes we can... ○ Various Java libraries and Plugin systems.○ Eclipse IDE plugins○ IntelliJIDEA plugins.

● But...

Modularity with Java

What are the limitations?● No standard way of building a modular system.

○ Class loader based techniques. ● Runtime Vs Compile time difference.

○ Flat, single classpath.● No model for sharing of resource at runtime.

Runtime vs Compile time.

Java application. Compile time view

log4j-2.1.0.jar axis2-kernel-1.6.0.jar axiom-3.3.0.jar jdom.1.0.jar spring-core.1.0.jar

saxon-8.9.0.jar poi-3.5.0.jar wss4j-1.5.11.jar

cindy-5.7.0.jar rampart-2.3.0.jar derby-10.3.0.jar bcel-5.2.0.jar

quart-1.1.0.jar woden-1.0.0.jar

jaxb-2.2.5.jar

Runtime vs Compile time

Runtime classloading architecture.

Single linear classpath.

log4j-2.1.0.jaraxis2-kernel-1.6.0.jaraxiom-3.3.0.jarjdom.1.0.jarspring-core.1.0.jarsaxon-8.9.0.jarpoi-3.5.0.jarjaxb-2.2.5.jar

wss4j-1.5.11.jarquart-1.1.0.jarwoden-1.0.0.jarcindy-5.7.0.jarrampart-2.3.0.jarderby-10.3.0.jarbcel-5.2.0.jar

Multiple Library Versions

Single linear classpath.

log4j-2.1.0.jaraxis2-kernel-1.6.0.jaraxiom-3.3.0.jarjdom.1.0.jarspring-core.1.0.jarsaxon-8.9.0.jarpoi-3.5.0.jarjaxb-2.2.5.jar

wss4j-1.5.11.jarquart-1.1.0.jarwoden-1.0.0.jarcindy-5.7.0.jarrampart-2.3.0.jarderby-10.3.0.jarbcel-5.2.0.jarlog4j-3.0.0.jar

Can we achieve this in Java?

Tight Coupling among modules

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

What Next?

● How do we overcome those limitations in Java?

● We were discussing functions, module, objects and

packages.

● What next???

What Next???

● How about an abstraction where you can work at the package level as opposed to class/object level?○ Importing packages.○ Exporting packages.○ A way to have private packages.

● Can we achieve the limitations that we discussed in Java from this model?

Next Level of Modularity

? What next?

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

Next Level of Modularity...

Image courtsy of: http://www.slideshare.net/bjhargrave/why-osgi

Next Level of Modularity...

● Separate class loader per module. ○ creating a class-space per module.○ delegate class loading to other modules when necessary.

● Solves the single class path problem.● Now you have a class loader network formed from

classloader delegation.● This network is formed with import/export package

restrictions.● Now we have modularity at runtime as well as compile

time.

Next Level of Modularity...

Java application. Runtime view

Classloader Network.

Delegate each other for

classloading requests.

What is OSGi?

"The OSGi framework is a module system and service platform for the Java programming language that

implements a complete and dynamic component model, something that as of 2012 does not exist in standalone

Java/VM environments."--Wikipedia

What is OSGi?

● Widely accepted specification which introduces a standard way of building dynamic, modular systems with Java.

● OSGi framework implementations.(Open source)○ Eclipse Equinox.○ Apache Felix.○ Knopflerfish.

OSGi Layered Model

Image courtsy of: http://www.osgi.org/Technology/WhatIsOSGi

OSGi Layered Model● Bundles

○ Bundles are the OSGi components made by the developers.

● Services○ The services layer connects bundles in a dynamic way by offering a

publish-find-bind model for plain old Java objects.

● Life-Cycle○ The API to install, start, stop, update, and uninstall bundles.

OSGi Layered Model● Modules

○ The layer that defines how a bundle can import and export code.

● Security ○ The layer that handles the security aspects.

● Execution Environment○ Defines what methods and classes are available in a specific platform.

Bundles

● Bundle is the unit of modularization in OSGi

● OSGi based application can be considered as a collection of Bundle

● Bundles can share packages with other bundles and hide packages from otherbundles

● How does a bundle differs from a normal jar file ?

Bundles...

How does a bundle differs from a normal jar file?

MANIFEST.MF

Bundle-ManifestVersion : 2Bundle-Name: My First OSGi BundleBundle-SymbolicName: HelloWorldBundleBundle-Version: 1.0.0Export-Package: org.helloworldImport-package: org.osgi.framework

Private Package

Exported Package

Imported Package

Bundle

Bundle States

Image courtsy of: http://static.springsource.org/s2-dmserver/2.0.x/getting-started/html/ch01.html

Demo

Dynamism

Modularity achieved.. but what about dynamism?

Dynamism...

● The moment your instantiate a new object tight coupling among modules occurs.

● What if I want to update the platform or part of the platform during runtime.

● What we need is a ...

OSGi Services

● In-VM SOA model.● OSGi Service registry.● Bundles can publish/lookup services using interface

names

OSGi Services

public void start(BundleContext bc) {Hashtable props = new Hashtable();props.put("language", "en");//Registering the HelloWorld servicebc.registerService(HelloService.class.getName(),new HelloServiceImpl(), props);

}

Acquiring Services

public void start(BundleContext bc) {//Get the service reference for HelloServiceserviceRef = bc.getServiceReference(HelloService.class.getName());//service reference can be null, if the service is not registered.if(serviceRef != null) {

helloService = (HelloService)bc.getService(serviceRef);} else {

System.err.println("service reference not found."); }}

Listeners

● Bundle Listeners

● Service Listeners

● Service Trackers

● Declarative Services

Declarative Services

<?xml version="1.0" encoding="UTF-8"?><component name="helloservice.listen"><implementation class="org.sample.HelloComponent"/><reference name="HS"

interface="org.sample.HelloService"cardinality="0..n"policy="dynamic"target="(language=en)"bind="setHelloService"unbind="setHelloService" />

</component>

Demo

Questions

Thank you.

Recommended