OSGi for mere mortals


Published on

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

Published in: Technology

OSGi for mere mortals

  1. 1. OSGifor mere mortals Simple and robus t sof t ware is go o d f or yo ur wor k-life b alance .Bertrand DelacrétazSenior Developer, Adobe Digital Enterprise Systems, www.day.comApache So ware Foundation Member and (current) Directorh p://grep.codeconsult.ch - twi er: @bdelacretaz - bdelacretaz@apache.orgApacheCon North America 2011, Vancouver, November 2011slides revision: 2011-11-05 (on a plane)OSGi for 1Mere Mortals 1
  2. 2. what? a simple RESTful server built from scratch on OSGi to show that it’s not rocket science, even starting from scratch...OSGi forMere Mortals 2
  3. 3. RESTfulcontent:POST to store server?$ date | curl -T - -X POST -D - http://localhost:8080/store/exampleHTTP/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/exampleFri Nov 4 10:41:30 CET 2011OSGi forMere Mortals 3
  4. 4. RESTful server!GET to retrieve content:$ curl http://localhost:8080/store/exampleStoredBy:ch.x42.osgi.samples.osgi101.app.servlets.StorageServletStoredAt:Fri Nov 04 10:41:30 CET 2011Path:/store/exampleFri Nov 4 10:41:30 CET 2011 In terms of functionality, that’s it!OSGi forMere Mortals 4
  5. 5. 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...OSGi forMere Mortals 5
  6. 6. Components Dynamic Services Server OSGi framework Application core Application services Bootstrapping and standard services HTTP HttpService Dispatcher InMemoryStorage Servlet Services Components Default POST Runtime (SCR) StorageServlet Servlet Configuration Default GET PathsStorage Servlet Logging HTTP Felix Web Console shell Felix Shell indicates SCR ser vices also: maven-bundle plugin maven-scr-pluginOSGi for maven-dependency-pluginMere Mortals 6
  7. 7. Bootstrapping and loading bundlesOSGi for 7Mere Mortals 7
  8. 8. Framework start and stopCalled from a plain main() methodOSGi forMere Mortals 8
  9. 9. Get bundles from Maven repoCopy to a folder using maven-dependency-plugin in launcherOSGi forMere Mortals 9
  10. 10. Install bundles from lesystemBundleContext.installBundle(URL)Install all bundles rst, then start allOSGi forMere Mortals 10
  11. 11. Live bundles listFrom the Felix console at /system/console We’re good to go!OSGi forMere Mortals 11
  12. 12. Our OSGi bundles HttpService org.apache.felix.h p.je y Logging org.apache.sling.commons.log Services Components Runtime (SCR) org.apache.felix.scr Configuration org.apache.felix.metatype org.apache.felix.con gadmin Felix Shell org.apache.felix.shell org.apache.felix.shell.tui Felix Web Console org.apache.felix.webconsole Application Core osgi-for-mere-mortals-core Application Services osgi-for-mere-mortals-appOSGi forMere Mortals 12
  13. 13. Dispatcher ServletOSGi for 13Mere Mortals 13
  14. 14. DispatcherServletforComponentRegister with H pService and watch Servlet services Dispatcher ServletOSGi forMere Mortals 14
  15. 15. DispatcherServletServletsDispatch to OSGi services which are Dispatcher Servlet Default GET ServletOSGi forMere Mortals 15
  16. 16. Default GET ServletOSGi for 16Mere Mortals 16
  17. 17. Default GETSCR annotationsJust a servlet, with some servlet Default GET ServletOSGi forMere Mortals 17
  18. 18. Storage ServicesOSGi for 18Mere Mortals 18
  19. 19. Storage service interfaceDe ned in the core bundle, package is exportedOSGi forMere Mortals 19
  20. 20. Storage service#1: in memoryActive by defaultOSGi forMere Mortals 20
  21. 21. Storage service#2: can be activated in consoleAlternate service, inactive by default, just pathsOSGi forMere Mortals 21
  22. 22. Storage ServletOSGi for 22Mere Mortals 22
  23. 23. StorageServlet serviceUses @Reference to get a Storage Properties used by DispatcherSer vlet The Component will only start once a Storage service is availableOSGi forMere Mortals 23
  24. 24. Alternate Storage demoOSGi for 24Mere Mortals 24
  25. 25. Replaceone and enabling another ComponentStorageBy disabling In-memory with paths 1) disable inMemoryStorage -> StorageServlet stops 2) enable PathsStorage -> StorageServlet is backOSGi forMere Mortals 25
  26. 26. Con guration demoOSGi for 26Mere Mortals 26
  27. 27. Configuring the StorageServletCon gurationAdmin service, console and annotations1) User provides con guration data in web console (or Felix shell, Sling Installer, ...) Felix Web Console2) Console (con guration agent) changes values ConfigurationAdmin StorageServlet3) Con gurationAdmin deactivates andreactivates Component with new Con g 0) Component has metatype=true and someOSGi for non-private @Property annotationsMere Mortals 27
  28. 28. StorageServlet Configuration codeAnnotations + read values in activate() Parameter names + descriptions in metatype.propertiesOSGi forMere Mortals 28
  29. 29. Conclusions 29
  30. 30. ConclusionsPowerful out of the boxinfrastructure, not muchcode to write.Modular component-basedarchitecture.Dynamic (re)con guration,both in terms of componentsand parameters.OSGi materializes the component-based programmingvision in Java, and using it is not rocket science! Code at github.com/bdelacretaz/OSGi-for-mere-mortals Your host today: @bdelacretaz, grep.codeconsult.chOSGi forMere Mortals 30
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.