Deep Dive Subsystems, Repository, Contracts and more... David - - PDF document

deep dive
SMART_READER_LITE
LIVE PREVIEW

Deep Dive Subsystems, Repository, Contracts and more... David - - PDF document

OSGi Application Provisioning Deep Dive Subsystems, Repository, Contracts and more... David Bosschaert 1 of 41 About me David Bosschaert (davidb@apache.org) Works at Adobe R&D Basel Co-chair OSGi EEG Apache committer


slide-1
SLIDE 1

Subsystems, Repository, Contracts and more... David Bosschaert

OSGi Application Provisioning

Deep Dive

1 of 41

slide-2
SLIDE 2

About me

David Bosschaert (davidb@apache.org) ⦿ Works at Adobe R&D Basel ⦿ Co-chair OSGi EEG ⦿ Apache committer ⦿ Open-source and cloud enthusiast

2 of 41

slide-3
SLIDE 3

Topics

⦿ A look at the latest Declarative Services ⦿ Using Subsystems to package and deploy ⦿ OSGi Repository to resolve dependencies ⦿ Portable Java Contracts

... demo throughout ...

3 of 41

slide-4
SLIDE 4

Running Example

Device Monitor

... a little webapp to monitor all your gadgets ...

4 of 41

slide-5
SLIDE 5

Declarative Services

Being updated for Enterprise R6 Improved Configuration Admin integration Introspective API Prototype Service Factory ... other smaller improvements ...

5 of 41

slide-6
SLIDE 6

DS Configured Component

i im mp po

  • r

rt t javax.servlet.S Se er rv vl le et t; i im mp po

  • r

rt t org.osgi.service.component.annotations.*; i im mp po

  • r

rt t org.osgi.service.http.H Ht tt tp pS Se er rv vi ic ce e; @Component p pu ub bl li ic c c cl la as ss s D De ev vi ic ce eM Mo

  • n

ni it to

  • r

r { p pr ri iv va at te e H Ht tt tp pS Se er rv vi ic ce e httpService; @Reference p pu ub bl li ic c v vo

  • i

id d setHttpService(H Ht tt tp pS Se er rv vi ic ce e svc) { httpService = svc; } @Activate p pu ub bl li ic c v vo

  • i

id d activate(M Mo

  • n

ni it to

  • r

rC Co

  • n

nf fi ig g cfg) { S St tr ri in ng g rootCtx = cfg.ctxPrefix(); i if f (!rootCtx.endsWith("/")) rootCtx = rootCtx + "/"; registerServlet(rootCtx + "dmon", n ne ew w M Mo

  • n

ni it to

  • r

rS Se er rv vl le et t()); registerServlet(rootCtx + "device", n ne ew w D De ev vi ic ce eS Se er rv vl le et t()); } p pr ri iv va at te e v vo

  • i

id d registerServlet(S St tr ri in ng g ctx, S Se er rv vl le et t servlet) { httpService.registerServlet(ctx, servlet, n nu ul ll l, n nu ul ll l); } }

6 of 41

slide-7
SLIDE 7

Configuration using Annotations

p pu ub bl li ic c @interface M Mo

  • n

ni it to

  • r

rC Co

  • n

nf fi ig g { S St tr ri in ng g ctxPrefix() d de ef fa au ul lt t "/"; b bo

  • l

le ea an n autoRefresh() d de ef fa au ul lt t f fa al ls se e; i in nt t interval() d de ef fa au ul lt t 30; }

annotation used as an ordinary interface, with added defaults ... weird but works great ...

7 of 41

slide-8
SLIDE 8

Example Bundle Manifest

B Bu un nd dl le e-M Ma an ni if fe es st tV Ve er rs si io

  • n

n: 2 B Bu un nd dl le e-S Sy ym mb bo

  • l

li ic cN Na am me e: devicemon-ds B Bu un nd dl le e-V Ve er rs si io

  • n

n: 0.0.1 S Se er rv vi ic ce e-C Co

  • m

mp po

  • n

ne en nt t: OSGI-INF/component.xml I Im mp po

  • r

rt t-P Pa ac ck ka ag ge e: javax.servlet;version="[2.5,3)",

  • rg.coderthoughts.devicemon.ssh;version="[1.0,2)",
  • rg.osgi.service.http;version="[1.2,2)"

R Re eq qu ui ir re e-C Ca ap pa ab bi il li it ty y:

  • sgi.extender;filter:="(&(osgi.extender=osgi.component)

(version>=1.2.0)(!(version>=2.0.0)))",

  • sgi.whiteboard;filter:="(osgi.whiteboard=osgi.http)"

Note that the javax.servlet import should really use contracts!

8 of 41

slide-9
SLIDE 9

Finished app...

2 Bundles devicemon.jar

depends on DS and HTTP Service

devicemon-ssh.jar

depends on Apache Mina SSHD

9 of 41

slide-10
SLIDE 10

Deploy it...

Now I want to easily deploy my app ⟱ create a Subsystem of it!

10 of 41

slide-11
SLIDE 11

Subsystems

OSGi Enterprise spec 134

A subsystem... collection of bundles put together

feature - everything shared application - isolated, nothing shared out composite - configurable in-between

a zip file with .esa extension can be nested can depend on bundles or other subsystems Subsystems can use OSGi Repositories to resolve dependencies

11 of 41

slide-12
SLIDE 12

devicemon-ds.esa

Just a zip file...

$ tar tvf devicemon-ds.esa

  • rwxrwxrwx 99 8 J

Ja an n 15:40 OSGI-INF/SUBSYSTEM.MF

  • rwxrwxrwx 18184 8 J

Ja an n 16:01 devicemon-ds.jar

  • rwxrwxrwx 5968 8 J

Ja an n 16:02 devicemon-ssh.jar

Subsystem Manifest

$ cat OSGI-INF/SUBSYSTEM.MF S Su ub bs sy ys st te em m-S Sy ym mb bo

  • l

li ic cN Na am me e: devicemon-ds S Su ub bs sy ys st te em m-V Ve er rs si io

  • n

n: 0.0.1 S Su ub bs sy ys st te em m-T Ty yp pe e: osgi.subsystem.feature

note, I didn't put my dependencies in the .esa file

12 of 41

slide-13
SLIDE 13

Feature subsystems

devicemon-ds.esa: a Feature Subsystem

All the bundles inside work just as shared bundles in OSGi Subsystem installed/started/stopped as 1 unit Dependencies pulled in either from .esa or from repository

as needed are reference-counted

13 of 41

slide-14
SLIDE 14

Apache Felix + Apache Aries Subsystems

14 of 41

slide-15
SLIDE 15

Add HTTP subsystem

15 of 41

slide-16
SLIDE 16

3 bundles in 1 operation

16 of 41

slide-17
SLIDE 17

Our subsystem doesn't install!

NB: a failed subsystem install doesn't leave any bundles behind...

17 of 41

slide-18
SLIDE 18

OSGi Repository

OSGi Enterprise spec 132

Inspired by Felix OBR Simple but powerful Actual repo can be remote or local Find resources

based on their capabilities any resource any capability

OSGi has defined:

standard Bundle capabilities Bundle, Subsystem resource types some more general capabilities

18 of 41

slide-19
SLIDE 19

Add resources using standard XML

Example

< <r re ep po
  • s
si it to
  • r
ry y increment='1389281419631' name='MyRepo' xmlns='http://www.osgi.org/xmlns/repository/v1.0.0'> > < <r re es so
  • u
ur rc ce e> > < <c ca ap pa ab bi il li it ty y namespace='osgi.identity'> > < <a at tt tr ri ib bu ut te e name='osgi.identity' value='org.apache.sshd.core'/ /> > < <a at tt tr ri ib bu ut te e name='type' value='osgi.bundle'/ /> > < <a at tt tr ri ib bu ut te e name='version' type='Version' value='0.9.0'/ /> > < </ /c ca ap pa ab bi il li it ty y> > < <c ca ap pa ab bi il li it ty y namespace='osgi.content'> > < <a at tt tr ri ib bu ut te e name='osgi.content' value='a1c64578808c38a63cd6563e9936f025638aeaf9de70f36765367db81c0afc38' < <a at tt tr ri ib bu ut te e name='url' value='local-repo/sshd-core.jar'/ /> > < <a at tt tr ri ib bu ut te e name='size' type='Long' value='464733'/ /> > < <a at tt tr ri ib bu ut te e name='mime' value='application/vnd.osgi.bundle'/ /> > < </ /c ca ap pa ab bi il li it ty y> > < <c ca ap pa ab bi il li it ty y namespace='osgi.wiring.package'> > < <a at tt tr ri ib bu ut te e name='osgi.wiring.package' value='org.apache.sshd'/ /> > < <a at tt tr ri ib bu ut te e name='version' type='Version' value='0.5.0'/ /> > < <a at tt tr ri ib bu ut te e name='bundle-symbolic-name' value='org.apache.sshd.core'/ /> > < <a at tt tr ri ib bu ut te e name='bundle-version' type='Version' value='0.9.0'/ /> > < <d di ir re ec ct ti iv ve e name='uses' value='org.apache.sshd.client'/ /> > < </ /c ca ap pa ab bi il li it ty y> > <!-- More capabilities --> < <r re eq qu ui ir re em me en nt t namespace='osgi.wiring.package'> > < <d di ir re ec ct ti iv ve e name='filter' value='(&(osgi.wiring.package=org.slf4j)(version>=1.6.0)(!(version>=2.0.0)))'/> < </ /r re eq qu ui ir re em me en nt t> > <!-- More requirements --> < </ /r re es so
  • u
ur rc ce e> > <!-- More resources --> < </ /r re ep po
  • s
si it to
  • r
ry y> >

19 of 41

slide-20
SLIDE 20

Repository XML

Format defined by OSGi Repository Spec Standard way to feed a repository with information Standard way for repositories to exchange data Generate it with the bindex/RepoIndex tool: https://github.com/osgi/bindex (https://github.com/osgi/bindex)

20 of 41

slide-21
SLIDE 21

Repository Service

Obtain resources from the repository

Find a bundle...

R Re ep po

  • s

si it to

  • r

ry y repo = ... // from Service Registry ... R Re eq qu ui ir re em me en nt tB Bu ui il ld de er r rb = n ne ew w R Re eq qu ui ir re em me en nt tB Bu ui il ld de er r("osgi.wiring.package"); rb.addDirective("filter", "(&(osgi.wiring.package=org.apache.ssh)(version=0.5.0))"); R Re eq qu ui ir re em me en nt t req = rb.build(); C Co

  • l

ll le ec ct ti io

  • n

n<R Re es so

  • u

ur rc ce e> bundleResources = repo.findProviders(req);

... or find some photo's from the North Pole ...

R Re eq qu ui ir re em me en nt tB Bu ui il ld de er r rb = n ne ew w R Re eq qu ui ir re em me en nt tB Bu ui il ld de er r("com.acme.photo"); rb.addDirective("filter", "(latitude>=66.5622)"); R Re eq qu ui ir re em me en nt t req = rb.build(); C Co

  • l

ll le ec ct ti io

  • n

n<R Re es so

  • u

ur rc ce e> photoResources = repo.findProviders(req);

21 of 41

slide-22
SLIDE 22

Add and prime a Repository

22 of 41

slide-23
SLIDE 23

Our subsystem works!

23 of 41

slide-24
SLIDE 24

Descriptor Subsystems

A subsystem can be just a descriptor

... with all resources obtained from a Repository

$ jar tvf generated/devicemon-ds-decl.esa 0 T Th hu u J Ja an n 09 12:37:32 GMT 2014 OSGI-INF/ 175 W We ed d J Ja an n 08 15:50:28 GMT 2014 OSGI-INF/SUBSYSTEM.MF

Subsystem Manifest

$ cat OSGI-INF/SUBSYSTEM.MF S Su ub bs sy ys st te em m-S Sy ym mb bo

  • l

li ic cN Na am me e: devicemon-ds-decl S Su ub bs sy ys st te em m-V Ve er rs si io

  • n

n: 0.0.1 S Su ub bs sy ys st te em m-T Ty yp pe e: osgi.subsystem.feature S Su ub bs sy ys st te em m-C Co

  • n

nt te en nt t: devicemon-ds;version=0.0.1, devicemon-ssh;version=0.0.1

24 of 41

slide-25
SLIDE 25

Start with only Subsystems + Repository

25 of 41

slide-26
SLIDE 26

Dependencies at work http.esa pulled in automatically

26 of 41

slide-27
SLIDE 27

Unpredictable dependency both 0.9.0 and 0.9.1 satisfy requirement

27 of 41

slide-28
SLIDE 28

Subsystem Deployment Descriptor

Once QA-ed, create a DEPLOYMENT.MF to fix dependencies

$ jar tvf generated/devicemon-ds-decl-dd.esa 283 W We ed d J Ja an n 15 16:39:34 GMT 2014 OSGI-INF/DEPLOYMENT.MF 175 W We ed d J Ja an n 08 15:50:28 GMT 2014 OSGI-INF/SUBSYSTEM.MF

it can freeze deployments

$ cat OSGI-INF/DEPLOYMENT.MF S Su ub bs sy ys st te em m-V Ve er rs si io

  • n

n: 0.0.1 S Su ub bs sy ys st te em m-T Ty yp pe e: osgi.subsystem.feature D De ep pl lo

  • y

ye ed d-C Co

  • n

nt te en nt t: devicemon-ds;deployed-version=0.0.1, devicemon-ssh;deployed-version=0.0.1 P Pr ro

  • v

vi is si io

  • n

n-R Re es so

  • u

ur rc ce e: org.apache.sshd.core;deployed-version=0.9.1,

  • rg.apache.felix.scr;deployed-version=1.8.2,

http-subsystem;type=osgi.subsystem.feature;deployed-version=1.2.0

note version 0.9.1 for org.apache.sshd.core Ensures that the runtime deployment is the same as the QA one.

28 of 41

slide-29
SLIDE 29

SSHD dependency now as predicted

29 of 41

slide-30
SLIDE 30

Portable Contracts

Our bundle contains:

I Im mp po

  • r

rt t-P Pa ac ck ka ag ge e: javax.servlet;version="[2.5,3)",

This API is defined by the JCP.

but... spot the problem!

30 of 41

slide-31
SLIDE 31

Semantic Versioning

... versioning policy for exported packages. OSGi versions: <major>.<minor>.<micro>.<qualifier> Updating package versions:

fix/patch (no change to API):

update micro

extend API (affects implementers, not clients):

update minor

API breakage:

update major

Using semantic versioning allows creating components that can work with future patches and other compatible releases of deps just Import-Package a range like: [5.3, 6)

31 of 41

slide-32
SLIDE 32

javax.servlet Problem

I Im mp po

  • r

rt t-P Pa ac ck ka ag ge e: javax.servlet;version="[2.5,3)",

Servlet 3.0 is actually compatible with 2.5

JCP specs don't follow semantic versioning 3.0 is more of a marketing version But what version to use for javax.servlet in OSGi? Different providers made different decisions

Some use 2.6 to export javax.servlet from Servlet 3 spec Others use 3.0 Some even use 0.0.0

This prohibits creating portable bundles using these APIs

32 of 41

slide-33
SLIDE 33

Portable Java Contracts

  • sgi.contract capability to the rescue

client bundle requires capability

with single 'marketing' or 'spec' version

and also imports the package without version

  • sgi.contract provider binds the

contract version to package versions

I Im mp po

  • r

rt t-P Pa ac ck ka ag ge e: javax.servlet, javax.servlet.http R Re eq qu ui ir re e-C Ca ap pa ab bi il li it ty y: osgi.contract; filter:="(&(osgi.contract=JavaServlet)(version=2.5))"

Enables creating portable bundles importing non-semantically versioned packages.

33 of 41

slide-34
SLIDE 34

Our bundle manifest should really be

B Bu un nd dl le e-M Ma an ni if fe es st tV Ve er rs si io

  • n

n: 2 B Bu un nd dl le e-S Sy ym mb bo

  • l

li ic cN Na am me e: devicemon-ds B Bu un nd dl le e-V Ve er rs si io

  • n

n: 0.0.1 I Im mp po

  • r

rt t-P Pa ac ck ka ag ge e: javax.servlet,

  • rg.coderthoughts.devicemon.ssh;version="[1.0,2)",
  • rg.osgi.service.http;version="[1.2,2)"

R Re eq qu ui ir re e-C Ca ap pa ab bi il li it ty y:

  • sgi.extender;filter:="(&(osgi.extender=osgi.component)

(version>=1.2)(!(version>=2.0)))",

  • sgi.whiteboard;filter:="(osgi.whiteboard=osgi.http)

(version>=1.0)(!(version>=2.0)))",

  • sgi.contract;filter:="(&(osgi.contract=JavaServlet)

(version=2.5))"

34 of 41

slide-35
SLIDE 35

Providing Portable Contracts

The bundle exporting the package should do this:

E Ex xp po

  • r

rt t-P Pa ac ck ka ag ge e: javax.servlet;version=2.6, javax.servlet.http;version=2.6 P Pr ro

  • v

vi id de e-C Ca ap pa ab bi il li it ty y: osgi.contract;

  • sgi.contract=J

Ja av va aS Se er rv vl le et t;version:V Ve er rs si io

  • n

n=2.5; uses:="javax.servlet, javax.servlet.http",

  • sgi.contract;osgi.contract=J

Ja av va aS Se er rv vl le et t;version:V Ve er rs si io

  • n

n=3; uses:="javax.servlet, javax.servlet.http"

as it only knows how the mapping is done.

Each marketing version is mapped to a package version.

For more details, see OSGi RFC 180 (https://github.com/osgi/design/raw/master /rfcs/rfc0180/rfc-0180-portable- java-contracts.pdf).

35 of 41

slide-36
SLIDE 36

For all packages other than JCP-ones

36 of 41

slide-37
SLIDE 37

http://www.bndtools.org (http://www.bndtools.org) can help with version maintenance! really bndtools

Use Semantic Versioning!

37 of 41

slide-38
SLIDE 38

Where can I get it?

Core R6 spec released this week:

http://www.osgi.org/Specifications/HomePage (http://www.osgi.org/Specifications/HomePage)

Enterprise R6 draft released this week:

http://www.osgi.org/Specifications/Drafts (http://www.osgi.org/Specifications/Drafts) RFCs 189, 190, 208 included in zip

38 of 41

slide-39
SLIDE 39

Links

OSGi specs:

http://www.osgi.org/Specifications (http://www.osgi.org/Specifications)

Apache Felix Project:

http://felix.apache.org (http://felix.apache.org)

OSGi RFC 180

https://github.com/osgi/design/raw/master /rfcs/rfc0180/rfc-0180-portable-java-contracts.pdf (https://github.com/osgi/design/raw/master /rfcs/rfc0180/rfc-0180-portable-java-contracts.pdf)

devicemon project:

https://github.com/bosschaert/devicemon (https://github.com/bosschaert/devicemon)

Subsystem Gogo command

https://github.com/bosschaert/coderthoughts

39 of 41

slide-40
SLIDE 40

/tree/master/subsystem-gogo-command (https://github.com/bosschaert/coderthoughts /tree/master/subsystem-gogo-command)

40 of 41

slide-41
SLIDE 41

Questions?

41 of 41