services first migration to osgi - osgi users forum uk 16-nov2011

80
Services-First Migration to OSGi BJ Hargrave (IBM) Peter Kriens (aQute) woensdag 16 november 11

Upload: mfrancis

Post on 28-Nov-2014

1.748 views

Category:

Technology


0 download

DESCRIPTION

Presentation by Peter Kriens at OSGi Users' Forum UK meeting on Nov 16, 2011 in London.Abstract: The OSGi Service Platform is a standard that specifies a comprehensive model of modules that communicate through a powerful, no overhead, Inter Module Communication mechanism: OSGi Services. OSGi enforces module-level isolation that forbids ad hoc cross-module class loading but this conflicts with prevalent Java software patterns that assume application-wide class visibility. OSGi Connect proposes the OSGi programming model without the module-level isolation. This presentation will show you how you can use OSGi Connect to begin to modularize your existing applications with the OSGi programming model with a minimal upfront investment.

TRANSCRIPT

Page 1: Services first migration to osgi - osgi users forum uk 16-nov2011

Services-First Migration to OSGi

BJ Hargrave (IBM)Peter Kriens (aQute)

woensdag 16 november 11

Page 2: Services first migration to osgi - osgi users forum uk 16-nov2011

Agenda

• Modularity Maturity Model

• The Brick Wall

• Couplings

• µServices

• What Broker?

• Example

• Conclusion

woensdag 16 november 11

Page 3: Services first migration to osgi - osgi users forum uk 16-nov2011

Modularity Maturity Model

• 1 Ad Hoc

• 2 Managed

• 3 Contracts

• 4 Fences

• 5 Optimize

Page 2 OSGi Alliance Marketing © 2008-2011 . All Rights Reserved, © IBM Corp. 2011

21.09.2011

Inspired by Graham Charters’ excellent OSGi Community Event 2011 presentation @ http://slidesha.re/q8EHFp

woensdag 16 november 11

Page 4: Services first migration to osgi - osgi users forum uk 16-nov2011

Modularity Maturity Model

woensdag 16 november 11

Page 5: Services first migration to osgi - osgi users forum uk 16-nov2011

1. Ad Hoc• Flat, manual class path

• Single name space

• Full visibility and normal accessibility

• Monolithic

• Highly coupled

• Split Packages

• Add/Shuffle JARs until it works

woensdag 16 november 11

Page 6: Services first migration to osgi - osgi users forum uk 16-nov2011

1. Ad Hoc• Flat, manual class path

• Single name space

• Full visibility and normal accessibility

• Monolithic

• Highly coupled

• Split Packages

• Add/Shuffle JARs until it works

JAR H

ell

woensdag 16 november 11

Page 7: Services first migration to osgi - osgi users forum uk 16-nov2011

2. Managed

• Treat JARs as a module

• JAR Identity

• Naming

• Versioning

• Dependencies

• Repositories

woensdag 16 november 11

Page 8: Services first migration to osgi - osgi users forum uk 16-nov2011

2. Managed

• Treat JARs as a module

• JAR Identity

• Naming

• Versioning

• Dependencies

• Repositories

Down

load

the

inter

net

woensdag 16 november 11

Page 9: Services first migration to osgi - osgi users forum uk 16-nov2011

3. Contracts

• Interface based design, POJOs

• Provide contracts for each connection between JARs

• Implementation details inside the JAR

• Versioning on contracts

woensdag 16 november 11

Page 10: Services first migration to osgi - osgi users forum uk 16-nov2011

3. Contracts

• Interface based design, POJOs

• Provide contracts for each connection between JARs

• Implementation details inside the JAR

• Versioning on contracts

classloa

der

hacks

woensdag 16 november 11

Page 11: Services first migration to osgi - osgi users forum uk 16-nov2011

4. Fences

• Explicitly Import/Export packages

• Explicitly specify requirements and capabilities

• Enforce the module boundaries

• Semantic Versioning

• Side-by-side versioning supported

woensdag 16 november 11

Page 12: Services first migration to osgi - osgi users forum uk 16-nov2011

4. Fences

• Explicitly Import/Export packages

• Explicitly specify requirements and capabilities

• Enforce the module boundaries

• Semantic Versioning

• Side-by-side versioning supported

Good!

woensdag 16 november 11

Page 13: Services first migration to osgi - osgi users forum uk 16-nov2011

5. Optimize• Exploit concepts for

modules and contracts

• Delegated Control

• Optimize the code base to leverage new patterns:

• Extender

• Whiteboard

• Repeat

woensdag 16 november 11

Page 14: Services first migration to osgi - osgi users forum uk 16-nov2011

5. Optimize• Exploit concepts for

modules and contracts

• Delegated Control

• Optimize the code base to leverage new patterns:

• Extender

• Whiteboard

• Repeat

woensdag 16 november 11

Page 15: Services first migration to osgi - osgi users forum uk 16-nov2011

Today1.

Ad Hoc

2.Managed

3.Contracts

4.Fences

5.Optimize

woensdag 16 november 11

Page 16: Services first migration to osgi - osgi users forum uk 16-nov2011

Today1.

Ad Hoc

2.Managed

3.Contracts

4.Fences

5.Optimize

mvn

Ivy

jigsaw

guice

,CDI

sprin

g

Enterprise

woensdag 16 november 11

Page 17: Services first migration to osgi - osgi users forum uk 16-nov2011

OSGi

Today1.

Ad Hoc

2.Managed

3.Contracts

4.Fences

5.Optimize

mvn

Ivy

jigsaw

guice

,CDI

sprin

g

Enterprise

woensdag 16 november 11

Page 18: Services first migration to osgi - osgi users forum uk 16-nov2011

OSGi

Today1.

Ad Hoc

2.Managed

3.Contracts

4.Fences

5.Optimize

mvn

Ivy

jigsaw

guice

,CDI

sprin

g

Enterprise

4.Fences

woensdag 16 november 11

Page 19: Services first migration to osgi - osgi users forum uk 16-nov2011

woensdag 16 november 11

Page 20: Services first migration to osgi - osgi users forum uk 16-nov2011

woensdag 16 november 11

Page 21: Services first migration to osgi - osgi users forum uk 16-nov2011

Class.forName(String)

woensdag 16 november 11

Page 22: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

class

package

woensdag 16 november 11

Page 23: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

class

package

woensdag 16 november 11

Page 24: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

class

package

woensdag 16 november 11

Page 25: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

V1 V2

woensdag 16 november 11

Page 26: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

V1 V2

woensdag 16 november 11

Page 27: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

V1 V2

Class.forName

woensdag 16 november 11

Page 28: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

V1 V2

xxClass.forName

woensdag 16 november 11

Page 29: Services first migration to osgi - osgi users forum uk 16-nov2011

Class Space

V1 V2

xxClass.forName

woensdag 16 november 11

Page 30: Services first migration to osgi - osgi users forum uk 16-nov2011

Why Class.forName?

woensdag 16 november 11

Page 31: Services first migration to osgi - osgi users forum uk 16-nov2011

Why Class.forName?

COUP

LING!!!!!!!!!!!!!

woensdag 16 november 11

Page 32: Services first migration to osgi - osgi users forum uk 16-nov2011

How to get an Instance?That is the Question!

woensdag 16 november 11

Page 33: Services first migration to osgi - osgi users forum uk 16-nov2011

Couplings

modular not modular

Example

type FooImpl foo;

instanceFoo foo = (Foo) Class.forName(“FooImpl”) .newInstance();

woensdag 16 november 11

Page 34: Services first migration to osgi - osgi users forum uk 16-nov2011

Type Couplingclass FooImpl{}class Client {}

woensdag 16 november 11

Page 35: Services first migration to osgi - osgi users forum uk 16-nov2011

Type Couplingclass FooImpl{}class Client {}

woensdag 16 november 11

Page 36: Services first migration to osgi - osgi users forum uk 16-nov2011

Type Couplingclass FooImpl{}

interface Foo{}

class Client {}

woensdag 16 november 11

Page 37: Services first migration to osgi - osgi users forum uk 16-nov2011

Type Couplingclass FooImpl{}

interface Foo{}

class Client {}

uses implements

woensdag 16 november 11

Page 38: Services first migration to osgi - osgi users forum uk 16-nov2011

Type Couplingclass FooImpl{}

interface Foo{}

class Client {}

uses implements

get instance

woensdag 16 november 11

Page 39: Services first migration to osgi - osgi users forum uk 16-nov2011

Type Couplingclass FooImpl{}

interface Foo{}

class Client {}

uses implements

get instance

Contro

l

Issue

s?

woensdag 16 november 11

Page 40: Services first migration to osgi - osgi users forum uk 16-nov2011

Instance Coupling

• New

• Factory

• Inversion of Control

• Broker

woensdag 16 november 11

Page 41: Services first migration to osgi - osgi users forum uk 16-nov2011

Instance Coupling

• New

• Factory

• Inversion of Control

• Broker

implclient container

woensdag 16 november 11

Page 42: Services first migration to osgi - osgi users forum uk 16-nov2011

Instance Coupling

• New

• Factory

• Inversion of Control

• Broker

implclient container

woensdag 16 november 11

Page 43: Services first migration to osgi - osgi users forum uk 16-nov2011

Instance Coupling

• New

• Factory

• Inversion of Control

• Broker

implclient container

woensdag 16 november 11

Page 44: Services first migration to osgi - osgi users forum uk 16-nov2011

Instance Coupling

• New

• Factory

• Inversion of Control

• Broker

implclient container

woensdag 16 november 11

Page 45: Services first migration to osgi - osgi users forum uk 16-nov2011

Instance Coupling

• New

• Factory

• Inversion of Control

• Broker

implclient container

woensdag 16 november 11

Page 46: Services first migration to osgi - osgi users forum uk 16-nov2011

µServices Primitive• Broker Pattern

• Direction

• Contract

• Publish

• Find

• Bind

• Who’s Listening?

• Cardinality

woensdag 16 november 11

Page 47: Services first migration to osgi - osgi users forum uk 16-nov2011

µServices Primitive• Broker Pattern

• Direction

• Contract

• Publish

• Find

• Bind

• Who’s listening?

• Cardinality

BA S

woensdag 16 november 11

Page 48: Services first migration to osgi - osgi users forum uk 16-nov2011

Patterns

• Factory

• Listener

• Discovery

• Distribution

woensdag 16 november 11

Page 49: Services first migration to osgi - osgi users forum uk 16-nov2011

5. Optimize

E

woensdag 16 november 11

Page 50: Services first migration to osgi - osgi users forum uk 16-nov2011

5. Optimize

D

A

G

B

E

H

C

F

I

woensdag 16 november 11

Page 51: Services first migration to osgi - osgi users forum uk 16-nov2011

Back to the Brick Wall

woensdag 16 november 11

Page 52: Services first migration to osgi - osgi users forum uk 16-nov2011

Migration

D

A

G

B

E

H

C

F

I

woensdag 16 november 11

Page 53: Services first migration to osgi - osgi users forum uk 16-nov2011

Migration

D

A

G

B

E

H

C

F

I

woensdag 16 november 11

Page 54: Services first migration to osgi - osgi users forum uk 16-nov2011

D

A

G

B

E

H

C

F

I

Migration

D

A

G

B

E

H

C

F

I

woensdag 16 november 11

Page 55: Services first migration to osgi - osgi users forum uk 16-nov2011

D

A

G

B

E

H

C

F

I

Migration

D

A

G

B

E

H

C

F

I

woensdag 16 november 11

Page 56: Services first migration to osgi - osgi users forum uk 16-nov2011

Migration

D

A

G

B

E

H

C

F

I

woensdag 16 november 11

Page 57: Services first migration to osgi - osgi users forum uk 16-nov2011

So we need a broker that runs without

fences...

woensdag 16 november 11

Page 58: Services first migration to osgi - osgi users forum uk 16-nov2011

So we need a Broker that runs without

Fences...

PojoSR

woensdag 16 november 11

Page 59: Services first migration to osgi - osgi users forum uk 16-nov2011

POJOSR

• Developed by Karl Pauls

• JavaOne presentation 24811

• http://pojosr.googlecode.com

• Based on Apache Felix

woensdag 16 november 11

Page 60: Services first migration to osgi - osgi users forum uk 16-nov2011

Gain• Existing libraries and many bundles work without

modification

• Bundle (JAR) activation (have their own local main)

• µServices

• Dynamicity (in µServices)

• Extender pattern (react on JAR content)

• Migration path to Fences (OSGi)

• Existing OSGi and non-OSGi tooling can be used

woensdag 16 november 11

Page 61: Services first migration to osgi - osgi users forum uk 16-nov2011

Loss

• No dynamic install/uninstall/update

• No side-by-side versioning

• No module privacy

• No explicit dependencies

• No Lazy activation

• No Bundle classpath

• No Native Code support

woensdag 16 november 11

Page 62: Services first migration to osgi - osgi users forum uk 16-nov2011

Migrating

• Example: extend ant … the tool we all (sometimes) love and (sometimes) hate

• We make different <helloXXX/> tasks

woensdag 16 november 11

Page 63: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

woensdag 16 november 11

Page 64: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

pojosrant

......

woensdag 16 november 11

Page 65: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojosrant

woensdag 16 november 11

Page 66: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojosrant

Class<Task>

woensdag 16 november 11

Page 67: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojosrant

helloactivator

Class<Task>

woensdag 16 november 11

Page 68: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojosrant

helloactivator

Class<Task>

woensdag 16 november 11

Page 69: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

<project name="master">

<path id="bundles"> <fileset dir="bundles"> <include name="*.jar" /> </fileset> </path>

<taskdef name="auto" classname="aQute.ant.connect.AutoTask" classpathref="bundles" /> <auto />

<helloActivator />

</project>

woensdag 16 november 11

Page 70: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Servicepublic class Activator implements BundleActivator {

public void start(BundleContext context) throws Exception { Properties props = new Properties(); props.put("ant", "helloActivator"); context.registerService(Class.class.getName(), HelloTask.class, props); }

public void stop(BundleContext context) throws Exception { }}public class HelloTask extends Task { String message = "Hello Activator"; public void execute() { System.out.println(message); } public void setMessage(String m) { this.message = m; }}

woensdag 16 november 11

Page 71: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Servicepublic class Activator implements BundleActivator {

public void start(BundleContext context) throws Exception { Properties props = new Properties(); props.put("ant", "helloActivator"); context.registerService(Class.class.getName(), HelloTask.class, props); }

public void stop(BundleContext context) throws Exception { }}public class HelloTask extends Task { String message = "Hello Activator"; public void execute() { System.out.println(message); } public void setMessage(String m) { this.message = m; }}

cruft

woensdag 16 november 11

Page 72: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojoant

helloactivator

Class<Task>

pojosrant

woensdag 16 november 11

Page 73: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojoant

helloactivator

Class<Task>

pojosrant

helloextendee

woensdag 16 november 11

Page 74: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

• Base ant

• Activator

• Extenderant

......

pojoant

helloactivator

Class<Task>

pojosrant

helloextendee

Manifest-Version: 1.0Ant-Task: helloExtender=HelloTask

woensdag 16 november 11

Page 75: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

public class HelloTask extends Task { String message = "Hello Extender"; public void execute() { System.out.println(message); } public void setMessage(String m) { this.message = m; }}

woensdag 16 november 11

Page 76: Services first migration to osgi - osgi users forum uk 16-nov2011

Ant Your Service

public class HelloTask extends Task { String message = "Hello Extender"; public void execute() { System.out.println(message); } public void setMessage(String m) { this.message = m; }}

NO cr

uft

woensdag 16 november 11

Page 77: Services first migration to osgi - osgi users forum uk 16-nov2011

Conclusion

• Moving to Fences is hard because popular instance creation patterns are fundamentally not modular

• Services-First approach works without Fences

• After completion, moving to Fences is much easier

woensdag 16 november 11

Page 78: Services first migration to osgi - osgi users forum uk 16-nov2011

Good FencesMake Good Neighbors

woensdag 16 november 11

Page 79: Services first migration to osgi - osgi users forum uk 16-nov2011

Good FencesMake Good Neighborsmodules

woensdag 16 november 11

Page 80: Services first migration to osgi - osgi users forum uk 16-nov2011

Q&A

woensdag 16 november 11