OSGi for mere mortals

Post on 12-May-2015

3,629 views 1 download

Tags:

description

Slides of my "OSGi for mere mortals" talk at ApacheCon NA 2011, http://na11.apachecon.com/talks/19416

transcript

OSGi for Mere Mortals

OSGi

Senior Developer, Adobe Digital Enterprise Systems, www.day.comApache So!ware Foundation Member and (current) Directorh"p://grep.codeconsult.ch - twi"er: @bdelacretaz - bdelacretaz@apache.org

ApacheCon North America 2011, Vancouver, November 2011

slides revision: 2011-11-05 (on a plane)

1

Bertrand Delacrétaz

for mere mortals

Simple and robus t sof t ware is good for your work-l i fe ba lance .

1

OSGi for Mere Mortals

what?a simple RESTful server

built from scratch on OSGito show that it’s not rocket science,

even starting from scratch...

2

OSGi for Mere Mortals

RESTful server?

$ date | curl -T - -X POST -D - http://localhost:8080/store/example

HTTP/1.1 201 CreatedLocation: /store/exampleContent-Type: text/plain; charset=utf-8Content-Length: 178Server: Jetty(6.1.x)

Stored at /store/exampleStoredBy:ch.x42.osgi.samples.osgi101.app.servlets.StorageServletStoredAt:Fri Nov 04 10:41:30 CET 2011Path:/store/example

Fri Nov 4 10:41:30 CET 2011

POST to store content:

3

OSGi for Mere Mortals

RESTful server!

$ curl http://localhost:8080/store/example

StoredBy:ch.x42.osgi.samples.osgi101.app.servlets.StorageServletStoredAt:Fri Nov 04 10:41:30 CET 2011Path:/store/example

Fri Nov 4 10:41:30 CET 2011

GET to retrieve content:

In terms of functionality, that’s it!

4

OSGi for Mere Mortals

It’s not big... 107 ./app/pom.xml 35 ./app/.../InMemoryStorage.java 34 ./app/.../PathsStorage.java 147 ./app/.../StorageServlet.java 101 ./core/pom.xml 6 ./core/...CoreConstants.java 32 ./core/...DefaultGetServlet.java 32 ./core/...DefaultPostServlet.java 116 ./core/...DispatcherServlet.java 8 ./core/...Storage.java 208 ./launcher/pom.xml 84 ./launcher/...OsgiBootstrap.java 52 ./pom.xml

962 lines in total, including 468 lines in pom.xml files...

5

OSGi for Mere Mortals

ComponentsServer

Bootstrapping

HttpService

Felix Web Console

Felix Shell

Services Components Runtime (SCR)

Logging

Configuration

OSGi framework and standard services

DispatcherServlet

Default GETServlet

Default POSTServlet

Application core

InMemoryStorage

PathsStorage

StorageServlet

Application services

indicates SCR services

HTTP

HTTP

shell

Dynamic Services

also:maven-bundle pluginmaven-scr-pluginmaven-dependency-plugin

6

OSGi for Mere Mortals

Bootstrappingand loading bundles

7

7

OSGi for Mere Mortals

Framework start and stopCalled from a plain main() method

8

OSGi for Mere Mortals

Get bundles from Maven repoCopy to a folder using maven-dependency-plugin in launcher

9

OSGi for Mere Mortals

Install bundles from !lesystemBundleContext.installBundle(URL)Install all bundles !rst, then start all

10

OSGi for Mere Mortals

Live bundles listFrom the Felix console at /system/console

We’re good to go!

11

OSGi for Mere Mortals

Our OSGi bundlesorg.apache.felix.h"p.je"yHttpService

Felix Web Console

Felix Shell

Services Components Runtime (SCR)

Logging

Configuration

Application Core

Application Services

org.apache.sling.commons.log

org.apache.felix.scr

org.apache.felix.metatypeorg.apache.felix.con!gadminorg.apache.felix.shellorg.apache.felix.shell.tui

org.apache.felix.webconsole

osgi-for-mere-mortals-core

osgi-for-mere-mortals-app

12

OSGi for Mere Mortals

DispatcherServlet

13

13

OSGi for Mere Mortals

DispatcherServlet ComponentDispatcher

Servlet

Register with H"pService and watch for Servlet services

14

OSGi for Mere Mortals

DispatcherServletDispatcher

Servlet

Dispatch to OSGi services which are Servlets

Default GETServlet

15

OSGi for Mere Mortals

Default GETServlet

16

16

OSGi for Mere Mortals

Default GET servletJust a servlet, with some SCR annotations

Default GETServlet

17

OSGi for Mere Mortals

StorageServices

18

18

OSGi for Mere Mortals

Storage service interfaceDe!ned in the core bundle, package is exported

19

OSGi for Mere Mortals

Storage service#1: in memoryActive by default

20

OSGi for Mere Mortals

Storage service#2: just pathsAlternate service, inactive by default, can be activated in console

21

OSGi for Mere Mortals

StorageServlet

22

22

OSGi for Mere Mortals

StorageServletUses @Reference to get a Storage service

Properties used by DispatcherServlet

The Component will only start once a Storage service is available

23

OSGi for Mere Mortals

Alternate Storage demo

24

24

OSGi for Mere Mortals

Replace In-memory with paths StorageBy disabling one and enabling another Component

1) disable inMemoryStorage -> StorageServlet stops

2) enable PathsStorage -> StorageServlet is back

25

OSGi for Mere Mortals

Con!gurationdemo

26

26

OSGi for Mere Mortals

Configuring the StorageServletCon!gurationAdmin service, console and annotations

Felix Web Console

ConfigurationAdmin StorageServlet

3) Con!gurationAdmin deactivates andreactivates Component with new Con!g

0) Component has metatype=true and somenon-private @Property annotations

1) User provides con!guration data in web console (or Felix shell, Sling Installer, ...)

2) Console (con!guration agent) changes values

27

OSGi for Mere Mortals

StorageServlet Configuration codeAnnotations + read values in activate()

Parameter names + descriptions in metatype.properties

28

Conclusions

29

OSGi for Mere Mortals

ConclusionsPowerful out of the box infrastructure, not muchcode to write.

Modular component-basedarchitecture.

Dynamic (re)con!guration,both in terms of componentsand parameters.

OSGi materializes the component-based programming vision in Java, and using it is not rocket science!

Code at github.com/bdelacretaz/OSGi-for-mere-mortals Your host today: @bdelacretaz, grep.codeconsult.ch

30