Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Demistifying OSGi


Published on

The slide deck used during Colombo Java meetup.

Published in: Education
  • Be the first to comment

  • Be the first to like this

Demistifying OSGi

  1. 1. Demystifying OSGi Colombo Java Meetup 24-01-2013Sameera Jayasoma Pradeep FernandoWSO2 Inc. WSO2 Inc.
  2. 2. Modular Systems...
  3. 3. Image courtsy of:
  4. 4. Down the Memory Lane...
  5. 5. FunctionsImage courtsy of:
  6. 6. ModulesImage courtsy of:
  7. 7. Classes/Objects Image courtsy of:
  8. 8. PackagesImage courtsy of:
  9. 9. Modularity with JavaCan we build a pure modular system with Java?
  10. 10. Modularity with Java● Yes we can... ○ Various Java libraries and Plugin systems. ○ Eclipse IDE plugins ○ IntelliJIDEA plugins.● But...
  11. 11. Modularity with JavaWhat are the limitations? ● No standard way of building a modular system. ○ Class loader based techniques. ● Runtime Vs Compile time difference. ○ Flat, single classpath. ● No model for sharing of resource at runtime.
  12. 12. Runtime vs Compile time. log4j-2.1.0.jar axis2-kernel-1 .6.0.jar axiom -3.3.0.ja r jdom.1.0.jar spring-core.1 .0.jar saxon-8.9.0.ja r poi-3.5.0.jar wss4j-1.5.11. jar quart-1.1.0.ja r woden-1.0.0.ja rcindy-5.7.0.ja r rampart-2.3.0 .ja r derby-10.3.0.ja r bcel-5.2.0.jar jaxb-2.2.5.jar Java application. Compile time view
  13. 13. Runtime vs Compile time log4j-2.1.0.jar wss4j-1.5.11.jar axis2-kernel-1.6.0 .jar quart-1.1.0.ja r axiom-3.3.0.jar woden-1.0.0.jar jdom.1.0.jar cindy-5.7.0.jar spring-core.1.0.ja r rampart-2.3.0.jar saxon-8.9.0.jar derby-10.3.0.jarSingle linear poi-3.5.0.jar bcel-5.2.0.jar classpath. jaxb-2.2.5.jar Runtime classloading architecture.
  14. 14. Multiple Library Versions log4j-2.1.0.jar wss4j-1.5.11.jar axis2-kernel-1.6.0 .jar quart-1.1.0.jar axiom-3.3.0.jar woden-1.0.0.jar jdom.1.0.jar cindy-5.7.0.jar spring-core.1.0.ja r rampart-2.3.0.jar saxon-8.9.0.jar derby-10.3.0.jar poi-3.5.0.jarSingle linear bcel-5.2.0.jar jaxb-2.2.5.jar classpath. log4j-3.0.0.jar Can we achieve this in Java?
  15. 15. Tight Coupling among modules Image courtsy of:
  16. 16. What Next?● How do we overcome those limitations in Java?● We were discussing functions, module, objects and packages.● What next???
  17. 17. What Next???● How about an abstraction where you can work at the package level as opposed to class/object level? ○ Importing packages. ○ Exporting packages. ○ A way to have private packages.● Can we achieve the limitations that we discussed in Java from this model?
  18. 18. Next Level of Modularity? What next? Image courtsy of:
  19. 19. Next Level of Modularity... Image courtsy of:
  20. 20. Next Level of Modularity...● Separate class loader per module. ○ creating a class-space per module. ○ delegate class loading to other modules when necessary.● Solves the single class path problem.● Now you have a class loader network formed from classloader delegation.● This network is formed with import/export package restrictions.● Now we have modularity at runtime as well as compile time.
  21. 21. Next Level of Modularity... Classloader Network.Delegate each other forclassloading requests. Java application. Runtime view
  22. 22. What is OSGi? "The OSGi framework is a module system and service platform for the Java programming language thatimplements a complete and dynamic component model, something that as of 2012 does not exist in standalone Java/VM environments." --Wikipedia
  23. 23. What is OSGi?● Widely accepted specification which introduces a standard way of building dynamic, modular systems with Java.● OSGi framework implementations.(Open source) ○ Eclipse Equinox. ○ Apache Felix. ○ Knopflerfish.
  24. 24. OSGi Layered Model Image courtsy of:
  25. 25. OSGi Layered Model● Bundles ○ Bundles are the OSGi components made by the developers.● Services ○ The services layer connects bundles in a dynamic way by offering a publish-find-bind model for plain old Java objects.● Life-Cycle ○ The API to install, start, stop, update, and uninstall bundles.
  26. 26. OSGi Layered Model● Modules ○ The layer that defines how a bundle can import and export code.● Security ○ The layer that handles the security aspects.● Execution Environment ○ Defines what methods and classes are available in a specific platform.
  27. 27. Bundles● Bundle is the unit of modularization in OSGi● OSGi based application can be considered as a collection of Bundle● Bundles can share packages with other bundles and hide packages from otherbundles● How does a bundle differs from a normal jar file ?
  28. 28. Bundles...How does a bundle differs from a normal jar file?
  29. 29. MANIFEST.MFBundle-ManifestVersion : 2Bundle-Name: My First OSGi BundleBundle-SymbolicName: HelloWorldBundleBundle-Version: 1.0.0Export-Package: org.helloworldImport-package: org.osgi.framework Bundle Exported Package Imported Package Private Package
  30. 30. Bundle StatesImage courtsy of:
  31. 31. Demo
  32. 32. DynamismModularity achieved.. but what about dynamism?
  33. 33. Dynamism...● The moment your instantiate a new object tight coupling among modules occurs.● What if I want to update the platform or part of the platform during runtime.● What we need is a ...
  34. 34. OSGi Services● In-VM SOA model.● OSGi Service registry.● Bundles can publish/lookup services using interface names
  35. 35. OSGi Servicespublic void start(BundleContext bc) { Hashtable props = new Hashtable(); props.put("language", "en"); //Registering the HelloWorld service bc.registerService(HelloService.class. getName(), new HelloServiceImpl(), props);}
  36. 36. Acquiring Servicespublic void start(BundleContext bc) { //Get the service reference for HelloService serviceRef = bc.getServiceReference(HelloService.class. getName()); //service reference can be null, if the service is not registered. if(serviceRef != null) { helloService = (HelloService)bc.getService(serviceRef); } else { System.err.println("service reference not found."); }}
  37. 37. Listeners● Bundle Listeners● Service Listeners● Service Trackers● Declarative Services
  38. 38. Declarative Services<?xml version="1.0" encoding="UTF-8"?><component name="helloservice.listen"><implementation class="org.sample.HelloComponent"/><reference name="HS" interface="org.sample.HelloService" cardinality="0..n" policy="dynamic" target="(language=en)" bind="setHelloService" unbind="setHelloService" /></component>
  39. 39. Demo
  40. 40. Questions
  41. 41. Thank you.