osgi for application developers

101
OSGi for Application Developers Neil Bartlett – WeigleWilczek UK QCon London 2009

Upload: others

Post on 31-May-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OSGi for Application Developers

OSGi for Application DevelopersNeil Bartlett – WeigleWilczek UKQCon London 2009

Page 2: OSGi for Application Developers

Agenda

Brief Intro to OSGi

OSGi in Infrastructure

The (Partial Failure) of Object Orientation

Component Oriented Programming

Implementing Components

DEMO

Page 3: OSGi for Application Developers

Brief Intro to OSGi

Page 4: OSGi for Application Developers

OSGi: The Module System for Java

Page 5: OSGi for Application Developers

Standard Java

ABC.JAR

JARs are Deployment Units

They are Not Modules

Page 6: OSGi for Application Developers

Meaningful name

Version

Vendor

Exports

Dependencies

What’s Missing?

ABC.JAR

Page 7: OSGi for Application Developers

Dependencies

JARs do have dependencies

They are implicit.

A dependency is an assumption.

“I assume module X (version Y) is on the classpath. If not I will crash and burn.”

Page 8: OSGi for Application Developers

Module = “Bundle”

Page 9: OSGi for Application Developers

Meaningful name

Version

Vendor

Exports

Dependencies

Just a JAR + Metadata

org.foo.mylibManifest-Version: 1.0Bundle-SymbolicName: com.mylibBundle-Name: My Library BundleBundle-Vendor: Neil BartlettBundle-Version: 1.0.0Import-Package: javax.swing, org.w3c.domExport-Package: com.mylib1.ui;version=“1.0.0”, com.mylib1.util;version=“1.0.0”Bundle-RequiredExecutionEnvironment: J2SE-1.5

Page 10: OSGi for Application Developers

MANIFEST.MFManifest-Version: 1.0Bundle-SymbolicName: com.mylibBundle-Name: My Library BundleBundle-Vendor: Neil BartlettBundle-Version: 1.0.0Import-Package: javax.swing, org.w3c.domExport-Package: com.mylib.ui;version=“1.0.0”, com.mylib.util;version=“1.0.0”Bundle-RequiredExecutionEnvironment: J2SE-1.5

Page 11: OSGi for Application Developers

Works Outside OSGi

Page 12: OSGi for Application Developers

Dependency Graphs

Page 13: OSGi for Application Developers
Page 14: OSGi for Application Developers

com.foo.bar

javax.swing

org.wibble

Imports..... and Exports

com.mylib

com.mylib.ui

com.mylib.util} {

Page 15: OSGi for Application Developers

Package Resolution

com.foo.bar

A

Page 16: OSGi for Application Developers

Package Resolution

com.foo.bar

Acom.foo.bar

B

Page 17: OSGi for Application Developers

Package Resolution

com.foo.bar

Acom.foo.bar

B

Page 18: OSGi for Application Developers

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) A

Page 19: OSGi for Application Developers

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) Acom.foo.bar

1.4.5B

Page 20: OSGi for Application Developers

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) Acom.foo.bar

1.3.12B’

Page 21: OSGi for Application Developers

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) Acom.foo.bar

1.3.12B’

Page 22: OSGi for Application Developers

Side-by-Side Versions

com.foo.bar

1.4.5

com.foo.bar

1.3.12

com.foo.bar

[1.2.0,1.4.0)

com.foo.bar

[1.4.0,1.5.0)B

B’

X

Y

Page 23: OSGi for Application Developers

Side-by-Side Versions

com.foo.bar

1.4.5

com.foo.bar

1.3.12

com.foo.bar

[1.2.0,1.4.0)

com.foo.bar

[1.4.0,1.5.0)B

B’

X

Y

Page 24: OSGi for Application Developers

How it WorksBundle-SymbolicName: com.mylib1Bundle-Version: 1.2.0Export-Package: com.mylib1.ui;version=“1.2.0”, com.mylib1.util;version=“1.2.0”

Bundle-SymbolicName: com.app1Bundle-Version: 2.2.3.alphaImport-Package: com.mylib1.ui;version=“[1.2.0,1.3.0)” com.mylib1.util;version=“[1.2.0,1.3.0)”

Page 25: OSGi for Application Developers

Private Internals

Exports must be stated explicitly

Packages not listed in “Export-Package” are not available to other bundles

Page 26: OSGi for Application Developers

Versions

Standard numbering scheme with well-defined ordering.

major.minor.micro.qualifier

First three numeric, last alphanumeric

Eg 1.0.0.beta2

Unspecified → 0.0.0

Page 27: OSGi for Application Developers

Version Ranges

Open, closed or implicit

[1.0.0, 2.0.0] → 1.0.0 ≤ version ≤ 2.0.0

[1.0.0, 2.0.0) → 1.0.0 ≤ version < 2.0.0

Informally “1.*”

1 → [1.0.0, ∞)

Unspecified → [0.0.0, ∞)

Page 28: OSGi for Application Developers

Our Promise*: No More NoClassDefFoundErrors!

Page 29: OSGi for Application Developers

Dynamic!Dynamic!

Page 30: OSGi for Application Developers

Dynamic

Install Bundles

Update Bundles

Uninstall Bundles

... all “on the fly”

Page 31: OSGi for Application Developers

OSGi for “Infrastructure”

Page 32: OSGi for Application Developers

OSGi: The Standard Module System for Java

Page 33: OSGi for Application Developers

De Facto AND De Jure

de facto: No other module system is more widely used (or is even close)

Three independent implementations in open source (EPL, APL and BSD).

de jure: JSR 291 (Java SE), JSR 232 (Java ME).

OSGi Alliance: Founded 1999, 28 Full Members.

Page 34: OSGi for Application Developers

Just The Full MembersAplix CorporationDeutsche TelekomEricsson Mobile Platforms ABGigaSpaces TechnologiesHarman/Becker Automotive Systems GmbHHitachi, Ltd.IBM CorporationIONA TechnologiesLinkedInMakewaveMitsubishi Electric CorporationMotorola, Inc.NEC CorporationNokia Corporation

NTTOracle CorporationProSyst Software GmbHRed HatSamsung Electronics Co., Ltd.SAP AGSiemens AGSiemens Enterprise CommunicationsSoftware AGSpringSourceSprintSun Microsystems, Inc.Telcordia Technologies, Inc.TIBCO Software Inc.

Page 35: OSGi for Application Developers

Where is OSGi Used Today?

Page 36: OSGi for Application Developers

Application Servers

WebSphere

WebLogic

JBoss

GlassFish (JEE RI)

Geronimo

SAP NetWeaver

JOnAS

Spring dm Server

NB: SOME ARE PLANNED OR IN-PROGRESS

Page 37: OSGi for Application Developers

ESBs

ServiceMix

Mule

WS02 Carbon

Project Fuji

Celtix

Page 38: OSGi for Application Developers

Mobile & Embedded

Too numerous to list (and many are obscure)

Just 2 examples:

BMW 7-Series cars

Sprint Titan platform

Page 39: OSGi for Application Developers
Page 40: OSGi for Application Developers

This Is All “Infrastructure”

Page 41: OSGi for Application Developers

Infrastructure ♥ OSGi

Lots of Libraries To Manage

Designed for Extensibility

Dynamic Deployment, Uptime

Page 42: OSGi for Application Developers

Where are the “Business” Apps?

Page 43: OSGi for Application Developers

The (Partial) Failure of Object Orientation

Page 44: OSGi for Application Developers

Reuse – Harder Than It First Seemed

Image credit: s. bär on Flickr

Page 45: OSGi for Application Developers

“Object Oriented technology was going to change the world... we would have

all these objects in our library and building a new system would be a snap. Just get a few classes, bunch

them together... and voila!”

- Peter Kriens

Page 46: OSGi for Application Developers

What Went Wrong?

Page 47: OSGi for Application Developers

Coupling

Page 48: OSGi for Application Developers

No Man ClassIs an Island...

Page 49: OSGi for Application Developers

Classes are Coupled to Other Classes...

Page 50: OSGi for Application Developers

Packages are Coupled to Other Packages...

Page 51: OSGi for Application Developers

Packages are Coupled to JARs...

“CLASSPATH”

Page 52: OSGi for Application Developers

Which are Coupled to More JARs...

“CLASSPATH”

Page 53: OSGi for Application Developers

Which Are Coupled to Yet More JARs...

“CLASSPATH”

Page 54: OSGi for Application Developers
Page 55: OSGi for Application Developers

A Solution...?

Page 56: OSGi for Application Developers

Give Up!

Page 57: OSGi for Application Developers

Reuse In Other Contexts is Hard, so...

Page 58: OSGi for Application Developers

Don’t Try!

Page 59: OSGi for Application Developers

Leave Code “In-Place”, and Call Remotely

Page 60: OSGi for Application Developers

...a.k.a. “SOA”

Page 61: OSGi for Application Developers

Component-OrientedProgramming

Page 62: OSGi for Application Developers

“COP” Sounds Cooler...

Page 63: OSGi for Application Developers

... Than “OOP”

Page 64: OSGi for Application Developers

Okay Maybe Not...

Page 65: OSGi for Application Developers

OOP Not Broken – Just Not The Whole Answer

Page 66: OSGi for Application Developers

Can We Ever Build Software Like Lego?

Page 67: OSGi for Application Developers
Page 68: OSGi for Application Developers

Just What Is a “Component”?

Page 69: OSGi for Application Developers

I Prefer a Different Analogy to Lego Bricks:

Page 70: OSGi for Application Developers
Page 71: OSGi for Application Developers

Components

Active players in the system

Aware of and adapt to their environment

May provide services to other components

Have a life cycle

Page 72: OSGi for Application Developers

The “Environment”

“Environment” means:

Services provided by other components

Resources, devices, etc

Page 73: OSGi for Application Developers

Adaptation

When env is good, the component flourishes

When env is harsh, the component survives

When very harsh, the component sleeps or dies

Page 74: OSGi for Application Developers

Composable

Components use the services of other components

Whole systems can be composed this way

Page 75: OSGi for Application Developers

Loosely Coupled

Components don’t depend on internal details of other components

Yes, this is the same as OO

Page 76: OSGi for Application Developers

Implementing Components

Page 77: OSGi for Application Developers

Services

Components provide Services

Registered with a central Service Registry

POJOs!

Look-up by Java interface name

Page 78: OSGi for Application Developers

In-Process SOA

ServiceProviderService

Consumer

ServiceBroker

Client Service

RegisterFind

Bind

ServiceContract

Page 79: OSGi for Application Developers

Plain Old Java Objects

Page 80: OSGi for Application Developers

The Glue Between Components

Page 81: OSGi for Application Developers

Dynamics Make Services Slippery

Page 82: OSGi for Application Developers

We Don’t Code Directly to Services

Page 83: OSGi for Application Developers

Let a Framework do the Fiddly Stuff

Page 84: OSGi for Application Developers

Several Frameworks

Declarative Services (DS)

Spring-DM

Apache iPOJO

Peaberry (Guice)

Page 85: OSGi for Application Developers

Perfect Interoperability At Services Level!

Page 86: OSGi for Application Developers

Examining DS

Like a Dependency Injection framework

But with dynamic injection...

... and “uninjection”

Page 87: OSGi for Application Developers

DS Componentpackage org.example.ds;

import javax.sql.DataSource;import org.example.osgi.mailbox.api.Mailbox;import org.example.osgi.mailbox.api.MailboxException;import org.example.osgi.mailbox.api.Message;

public class DbMailbox implements Mailbox { private DataSource dataSource; protected void setDataSource(DataSource dataSource) { /* ... */ } protected void unsetDataSource(DataSource dataSource) { /* ... */ } @Override public long[] getAllMessages() throws MailboxException { /* ... */ }}

No OSGi Imports

JavaBeans Style => Testable

Page 88: OSGi for Application Developers

DS Declaration

<?xml version="1.0" encoding="UTF-8"?><component name="DbMailbox"> <implementation class="org.example.ds.DbMailbox"/> <service> <provide interface="org.example.mailbox.api.Mailbox"/> </service> <reference name="DataSource" interface="javax.sql.DataSource" bind="setDataSource" unbind="unsetDataSource"/> </component>

Page 89: OSGi for Application Developers

Optional vs Mandatory

<reference name="DataSource" interface="javax.sql.DataSource" bind="setDataSource" unbind="unsetDataSource" policy="static"/> <reference name="Log" interface="org.osgi.service.log.LogService" bind="setLog" unbind="unsetLog" cardinality="0..1"/>

Page 90: OSGi for Application Developers

Lifecycle

protected void activate(Map configuration) { /* ... */ } protected void deactivate() { /* ... */ }

Page 91: OSGi for Application Developers

Dynamic Binding/Unbinding

<reference name="DataSource" interface="javax.sql.DataSource" bind="setDataSource" unbind="unsetDataSource" policy="static"/> <reference name="Log" interface="org.osgi.service.log.LogService" bind="setLog" unbind="unsetLog" cardinality="0..1" policy="dynamic"/>

Page 92: OSGi for Application Developers

Truly Reusable Components are Thread-Safe

Page 93: OSGi for Application Developers

Thread Safety

private LogService log;

protected void setLog(LogService log) { /* ... */ }protected void unsetLog(LogService log) { /* ... */ }

protected void activate(Map configuration) { /* ... */ }protected void deactivate() { /* ... */ }

@Overridepublic long[] getAllMessages() throws MailboxException{ /* ... */ }

Can be calledconcurrently!}

Page 94: OSGi for Application Developers

Truly Reusable Components are Stateless

Page 95: OSGi for Application Developers

DEMO

Page 96: OSGi for Application Developers

CONCLUSION

Page 97: OSGi for Application Developers

When is Modularity Not Relevant?

Page 98: OSGi for Application Developers

Getting Involved

OSGi User’s Forum UK just founded. Free for companies and individuals.

http://uk.osgiusers.org/

Page 99: OSGi for Application Developers

Getting up to Speed

SkillsMatter Training in April (4 days)

http://tinyurl.com/sm-osgi

Half price for “1st Teach”

Page 100: OSGi for Application Developers

Getting up to Speed

EclipseCon in 2 weeks!

http://www.eclipsecon.org/

4 tutorials on OSGi (4 hours each) and 15+ sessions

Everything you ever wanted to know about OSGi, and then some

Page 101: OSGi for Application Developers

Thank You!

Demo Code: github.com/njbartlett/osgidemo

User Forum: uk.osgiusers.org/

Training: tinyurl.com/sm-osgi

Me: neilbartlett.name/

OSGi is a trademark or a registered trademark of the OSGi Alliance in the United States, other countries, or both