OSGi for Application Developers Neil Bartlett WeigleWilczek UK - - PowerPoint PPT Presentation

osgi for application developers
SMART_READER_LITE
LIVE PREVIEW

OSGi for Application Developers Neil Bartlett WeigleWilczek UK - - PowerPoint PPT Presentation

OSGi for Application Developers Neil Bartlett WeigleWilczek UK QCon London 2009 Agenda Brief Intro to OSGi OSGi in Infrastructure The (Partial Failure) of Object Orientation Component Oriented Programming Implementing Components DEMO


slide-1
SLIDE 1

OSGi for Application Developers

Neil Bartlett – WeigleWilczek UK QCon London 2009

slide-2
SLIDE 2

Agenda

Brief Intro to OSGi OSGi in Infrastructure The (Partial Failure) of Object Orientation Component Oriented Programming Implementing Components DEMO

slide-3
SLIDE 3

Brief Intro to OSGi

slide-4
SLIDE 4

OSGi: The Module System for Java

slide-5
SLIDE 5

Standard Java

ABC.JAR

JARs are Deployment Units They are Not Modules

slide-6
SLIDE 6

Meaningful name Version Vendor Exports Dependencies

What’s Missing?

ABC.JAR

slide-7
SLIDE 7

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.”
slide-8
SLIDE 8

Module = “Bundle”

slide-9
SLIDE 9

Meaningful name Version Vendor Exports Dependencies

Just a JAR + Metadata

  • rg.foo.mylib
Manifest-Version: 1.0 Bundle-SymbolicName: com.mylib Bundle-Name: My Library Bundle Bundle-Vendor: Neil Bartlett Bundle-Version: 1.0.0 Import-Package: javax.swing, org.w3c.dom Export-Package: com.mylib1.ui;version=“1.0.0”, com.mylib1.util;version=“1.0.0” Bundle-RequiredExecutionEnvironment: J2SE-1.5
slide-10
SLIDE 10

MANIFEST.MF

Manifest-Version: 1.0 Bundle-SymbolicName: com.mylib Bundle-Name: My Library Bundle Bundle-Vendor: Neil Bartlett Bundle-Version: 1.0.0 Import-Package: javax.swing, org.w3c.dom Export-Package: com.mylib.ui;version=“1.0.0”, com.mylib.util;version=“1.0.0” Bundle-RequiredExecutionEnvironment: J2SE-1.5

slide-11
SLIDE 11

Works Outside OSGi

slide-12
SLIDE 12

Dependency Graphs

slide-13
SLIDE 13
slide-14
SLIDE 14

com.foo.bar javax.swing

  • rg.wibble

Imports..... and Exports

com.mylib com.mylib.ui com.mylib.util

} {

slide-15
SLIDE 15

Package Resolution

com.foo.bar

A

slide-16
SLIDE 16

Package Resolution

com.foo.bar

A

com.foo.bar

B

slide-17
SLIDE 17

Package Resolution

com.foo.bar

A

com.foo.bar

B

slide-18
SLIDE 18

Versioned Dependency

com.foo.bar [1.2.0,1.4.0)

A

slide-19
SLIDE 19

Versioned Dependency

com.foo.bar [1.2.0,1.4.0)

A

com.foo.bar 1.4.5

B

slide-20
SLIDE 20

Versioned Dependency

com.foo.bar [1.2.0,1.4.0)

A

com.foo.bar 1.3.12

B’

slide-21
SLIDE 21

Versioned Dependency

com.foo.bar [1.2.0,1.4.0)

A

com.foo.bar 1.3.12

B’

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

How it Works

Bundle-SymbolicName: com.mylib1 Bundle-Version: 1.2.0 Export-Package: com.mylib1.ui;version=“1.2.0”, com.mylib1.util;version=“1.2.0” Bundle-SymbolicName: com.app1 Bundle-Version: 2.2.3.alpha Import-Package: com.mylib1.ui;version=“[1.2.0,1.3.0)” com.mylib1.util;version=“[1.2.0,1.3.0)”

slide-25
SLIDE 25

Private Internals

Exports must be stated explicitly Packages not listed in “Export-Package” are not available to other bundles

slide-26
SLIDE 26

Versions

Standard numbering scheme with well-defined

  • rdering.

major.minor.micro.qualifier First three numeric, last alphanumeric Eg 1.0.0.beta2 Unspecified → 0.0.0

slide-27
SLIDE 27

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, ∞)

slide-28
SLIDE 28

Our Promise*: No More NoClassDefFoundErrors!

slide-29
SLIDE 29

Dynamic! Dynamic!

slide-30
SLIDE 30

Dynamic

Install Bundles Update Bundles Uninstall Bundles ... all “on the fly”

slide-31
SLIDE 31

OSGi for “Infrastructure”

slide-32
SLIDE 32

OSGi: The Standard Module System for Java

slide-33
SLIDE 33

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.

slide-34
SLIDE 34

Just The Full Members

Aplix Corporation Deutsche Telekom Ericsson Mobile Platforms AB GigaSpaces Technologies Harman/Becker Automotive Systems GmbH Hitachi, Ltd. IBM Corporation IONA Technologies LinkedIn Makewave Mitsubishi Electric Corporation Motorola, Inc. NEC Corporation Nokia Corporation NTT Oracle Corporation ProSyst Software GmbH Red Hat Samsung Electronics Co., Ltd. SAP AG Siemens AG Siemens Enterprise Communications Software AG SpringSource Sprint Sun Microsystems, Inc. Telcordia Technologies, Inc. TIBCO Software Inc.

slide-35
SLIDE 35

Where is OSGi Used Today?

slide-36
SLIDE 36

Application Servers

WebSphere WebLogic JBoss GlassFish (JEE RI) Geronimo SAP NetWeaver JOnAS Spring dm Server

NB: SOME ARE PLANNED OR IN-PROGRESS

slide-37
SLIDE 37

ESBs

ServiceMix Mule WS02 Carbon Project Fuji Celtix

slide-38
SLIDE 38

Mobile & Embedded

Too numerous to list (and many are obscure) Just 2 examples: BMW 7-Series cars Sprint Titan platform

slide-39
SLIDE 39
slide-40
SLIDE 40

This Is All “Infrastructure”

slide-41
SLIDE 41

Infrastructure ♥ OSGi

Lots of Libraries To Manage Designed for Extensibility Dynamic Deployment, Uptime

slide-42
SLIDE 42

Where are the “Business” Apps?

slide-43
SLIDE 43

The (Partial) Failure of Object Orientation

slide-44
SLIDE 44

Reuse – Harder Than It First Seemed

Image credit: s. bär on Flickr

slide-45
SLIDE 45

“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
slide-46
SLIDE 46

What Went Wrong?

slide-47
SLIDE 47

Coupling

slide-48
SLIDE 48

No Man Class Is an Island...

slide-49
SLIDE 49

Classes are Coupled to Other Classes...

slide-50
SLIDE 50

Packages are Coupled to Other Packages...

slide-51
SLIDE 51

Packages are Coupled to JARs...

“CLASSPATH”

slide-52
SLIDE 52

Which are Coupled to More JARs...

“CLASSPATH”

slide-53
SLIDE 53

Which Are Coupled to Yet More JARs...

“CLASSPATH”

slide-54
SLIDE 54
slide-55
SLIDE 55

A Solution...?

slide-56
SLIDE 56

Give Up!

slide-57
SLIDE 57

Reuse In Other Contexts is Hard, so...

slide-58
SLIDE 58

Don’t Try!

slide-59
SLIDE 59

Leave Code “In-Place”, and Call Remotely

slide-60
SLIDE 60

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

slide-61
SLIDE 61

Component-Oriented Programming

slide-62
SLIDE 62

“COP” Sounds Cooler...

slide-63
SLIDE 63

... Than “OOP”

slide-64
SLIDE 64

Okay Maybe Not...

slide-65
SLIDE 65

OOP Not Broken – Just Not The Whole Answer

slide-66
SLIDE 66

Can We Ever Build Software Like Lego?

slide-67
SLIDE 67
slide-68
SLIDE 68

Just What Is a “Component”?

slide-69
SLIDE 69

I Prefer a Different Analogy to Lego Bricks:

slide-70
SLIDE 70
slide-71
SLIDE 71

Components

Active players in the system Aware of and adapt to their environment May provide services to other components Have a life cycle

slide-72
SLIDE 72

The “Environment”

“Environment” means: Services provided by other components Resources, devices, etc

slide-73
SLIDE 73

Adaptation

When env is good, the component flourishes When env is harsh, the component survives When very harsh, the component sleeps or dies

slide-74
SLIDE 74

Composable

Components use the services of other components Whole systems can be composed this way

slide-75
SLIDE 75

Loosely Coupled

Components don’t depend on internal details of

  • ther components

Yes, this is the same as OO

slide-76
SLIDE 76

Implementing Components

slide-77
SLIDE 77

Services

Components provide Services Registered with a central Service Registry POJOs! Look-up by Java interface name

slide-78
SLIDE 78

In-Process SOA

Service Provider Service Consumer Service Broker Client Service Register Find Bind Service Contract

slide-79
SLIDE 79

Plain Old Java Objects

slide-80
SLIDE 80

The Glue Between Components

slide-81
SLIDE 81

Dynamics Make Services Slippery

slide-82
SLIDE 82

We Don’t Code Directly to Services

slide-83
SLIDE 83

Let a Framework do the Fiddly Stuff

slide-84
SLIDE 84

Several Frameworks

Declarative Services (DS) Spring-DM Apache iPOJO Peaberry (Guice)

slide-85
SLIDE 85

Perfect Interoperability At Services Level!

slide-86
SLIDE 86

Examining DS

Like a Dependency Injection framework But with dynamic injection... ... and “uninjection”

slide-87
SLIDE 87

DS Component

package 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

slide-88
SLIDE 88

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>

slide-89
SLIDE 89

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"/>

slide-90
SLIDE 90

Lifecycle

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

slide-91
SLIDE 91

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"/>

slide-92
SLIDE 92

Truly Reusable Components are Thread-Safe

slide-93
SLIDE 93

Thread Safety

private LogService log; protected void setLog(LogService log) { /* ... */ } protected void unsetLog(LogService log) { /* ... */ } protected void activate(Map configuration) { /* ... */ } protected void deactivate() { /* ... */ } @Override public long[] getAllMessages() throws MailboxException { /* ... */ }

Can be called concurrently!

}

slide-94
SLIDE 94

Truly Reusable Components are Stateless

slide-95
SLIDE 95

DEMO

slide-96
SLIDE 96

CONCLUSION

slide-97
SLIDE 97

When is Modularity Not Relevant?

slide-98
SLIDE 98

Getting Involved

OSGi User’s Forum UK just founded. Free for companies and individuals. http://uk.osgiusers.org/

slide-99
SLIDE 99

Getting up to Speed

SkillsMatter Training in April (4 days) http://tinyurl.com/sm-osgi Half price for “1st Teach”

slide-100
SLIDE 100

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

slide-101
SLIDE 101

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