Page 1
OSGi for Mere Mortals
OSGi
Senior Developer, Adobe CQ5 team, BaselApache So!ware Foundation Member and (current) Directorh"p://grep.codeconsult.ch - twi"er: @bdelacretaz - [email protected]
ApacheCon EU 2012, Sinsheim, November 2012
slides revision: 2012-11-06
1
Bertrand Delacrétaz
for mere mortals
Simple and robus t sof t ware is good for your work-l i fe ba lance .
Page 2
OSGi for Mere Mortals
slides atslideshare.net/bdelacretaz/osgi-for-mere-mortals
code atgithub.com/bdelacretaz/OSGi-for-mere-mortals
BTW...we are hiring!
[email protected]
Page 3
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...
Page 4
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:
Page 5
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!
Page 6
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...
Page 7
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
Page 8
OSGi for Mere Mortals
Bootstrappingand loading bundles
8
Page 9
OSGi for Mere Mortals
Framework start and stopCalled from a plain main() method
Page 10
OSGi for Mere Mortals
Get bundles from Maven repoCopy to a folder using maven-dependency-plugin in launcher
Page 11
OSGi for Mere Mortals
Install bundles from !lesystemBundleContext.installBundle(URL)Install all bundles !rst, then start all
Page 12
OSGi for Mere Mortals
Live bundles listFrom the Felix console at /system/console
We’re good to go!
Page 13
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
Page 14
OSGi for Mere Mortals
DispatcherServlet
14
Page 15
OSGi for Mere Mortals
DispatcherServlet ComponentDispatcher
Servlet
Register with H"pService and watch for Servlet services
Page 16
OSGi for Mere Mortals
DispatcherServletDispatcher
Servlet
Dispatch to OSGi services which are Servlets
Default GETServlet
Page 17
OSGi for Mere Mortals
Default GETServlet
17
Page 18
OSGi for Mere Mortals
Default GET servletJust a servlet, with some SCR annotations
Default GETServlet
Page 19
OSGi for Mere Mortals
StorageServices
19
Page 20
OSGi for Mere Mortals
Storage service interfaceDe!ned in the core bundle, package is exported
Page 21
OSGi for Mere Mortals
Storage service#1: in memoryActive by default
Page 22
OSGi for Mere Mortals
Storage service#2: just pathsAlternate service, inactive by default, can be activated in console
Page 23
OSGi for Mere Mortals
StorageServlet
23
Page 24
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
Page 25
OSGi for Mere Mortals
Alternate Storage demo
25
Page 26
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
Page 27
OSGi for Mere Mortals
Con!gurationdemo
27
Page 28
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
Page 29
OSGi for Mere Mortals
StorageServlet Configuration codeAnnotations + read values in activate()
Parameter names + descriptions in metatype.properties
Page 31
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
BTW...we are hiring!
[email protected]