Successfully reported this slideshow.
Your SlideShare is downloading. ×

Getting modular with OSGI

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
MySql Restore Script
MySql Restore Script
Loading in …3
×

Check these out next

1 of 32 Ad
Advertisement

More Related Content

Advertisement

Similar to Getting modular with OSGI (20)

Recently uploaded (20)

Advertisement

Getting modular with OSGI

  1. 1. GETTING MODULARWITH OSGI
  2. 2. Agenda › How to make my Java more modular › Discover and exploit OSGI › Tools, tools, tools… › Output
  3. 3. About me Who: Andrii Krokhmalnyi Domain: Java/Java EE Company: EPAM Systems Experience in OSGI: about 2 years
  4. 4. Modular design › Separates functionality into distinct isolated sections (modules) › Defines a standard interface for modules › Adds ability to replace modules transparently for users of the system › Improves scalability and robustness of the system › Makes functional parts of the system reusable and easily updatable › …But adds some extent of complexity in overall system orchestration
  5. 5. Modularity in Java: Service Provider Interface META-INF/services/com.foo.logging.LogRenderer com.foo.logging.impl.ConsoleLogRenderer com.foo.service.MyService.java public static void main(String[] args) { ServiceLoader<LogRenderer> renderers = ServiceLoader.load(LogRenderer.class); for (LogRenderer renderer : renderers) { ... }
  6. 6. Modularity in Java: OSGI MANIFEST.MF META-INF/MANIFEST.MF Manifest-Version: 1.0 Bundle-SymbolicName: foo-service Bundle-Activator: com.foo.service.Activator Export-Package: com.foo.dao,com.foo.domain,com.foo.service Import-Package: com.foo.logging;version="[2.0,3)" image from http://wallpapersus.com/
  7. 7. Project Jigsaw src/main/java/com/foo/service/module-info.java module foo-service @ 1.2 { requires jdk.base; requires optional jdk.jaxp; exports com.foo.bar; permits foo-ui; permits foo-console; class com.foo.service.Main; view dao { exports com.foo.dao; } } image from http://wallpapersgroup.net/
  8. 8. History & versioning
  9. 9. OSGI specification (version 4.3) Framework Specification FrameworkAPI Start LevelAPI Remote Services Bundle WiringAPI Additional Services Specifications Security Layer Module Layer Life Cycle Layer Service Layer
  10. 10. Core :: Container and bundles image from http://www.osgi.org OSGI Container Class Loader #1 Bundle #1 Class Loader #2 Bundle #2 Class Loader #N Bundle #N (modules) a regular jar archive with tweaked manifest
  11. 11. Core :: Bundles life-cycle
  12. 12. Core :: MANIFEST.MF headers foo-service.jar/META-INF/MANIFEST.MF ... Bundle-ManifestVersion: 2 Bundle-SymbolicName: com.foo.service Bundle-Name: Foo Service Bundle-Description: Foo Service Module Bundle-Version: 1.0 Bundle-Activator: com.foo.service.BundleActivator Require-Bundle: com.foo.logging Export-Package: com.foo.domain,com.foo.dao,com.foo.service;uses:=“org.apache.log4j” Import-Package: com.foo.logging;version=“[1.0,2)”,org.apache.log4j;version=“[2.0,3)” DynamicImport-Package: * ...
  13. 13. Core :: MANIFEST.MF and Maven pom.xml <build><plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> <Bundle-Activator>com.foo.service.BundleActivator</Bundle-Activator> <Export-Package>com.foo.service</Export-Package> <Import-Package> com.foo.domain, com.foo.dao, * </Import-Package> <DynamicImport-Package>*</DynamicImport-Package> <Require-Bundle>org.apache.commons.io</Require-Bundle> </instructions> </configuration> </plugin> </plugins></build>
  14. 14. OSGI Services :: Service producer ServiceProducerBundleActivator.java public class ServiceProducerBundleActivator implements BundleActivator { private ServiceRegistration registration; public void start(BundleContext context) throws Exception { registration = context.registerService(CustomService.class.getName(), new CustomServiceImpl(), null); } public void stop(BundleContext context) throws Exception { registration.unregister(); } }
  15. 15. OSGI Services :: Service consumer ServiceConsumerBundleActivator.java public class ServiceConsumerBundleActivator implements BundleActivator { public void start(BundleContext context) throws Exception { ServiceReference ref = context.getServiceReference(CustomService.class.getName()); CustomService customService = (CustomService) context.getService(ref); customService.customMethod(); ... } }
  16. 16. OSGI Services :: Spring version /META-INF/spring/spring-osgi.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="..." xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="..."> <osgi:service ref="customService" interface="com.foo.service.CustomService"/> </beans> Producer /META-INF/spring/spring-osgi.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="..." xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="..."> <osgi:reference id="customService" interface="com.foo.service.CustomService"/> </beans> Consumer
  17. 17. WhereWAR becomesWAB foo-web.war/META-INF/MANIFEST.MF ... Bundle-ManifestVersion: 2 Bundle-SymbolicName: com.foo.web Bundle-Version: 1.0 Bundle-Activator: com.foo.web.BundleActivator Web-ContextPath: mainapp/foo Webapp-Context: mainapp/foo Bundle-ClassPath: WEB-INF/classes,. Import-Package: com.foo.service;version=“[1.0,2)”, com.foo.logging;version=“[1.0,2)”;resolution:="optional" ...
  18. 18. Compendium services Compendium OSGI Services Preferences UserAdmin Monitor Admin Log HTTP DeviceAccess Configuration Admin … and many more
  19. 19. OSGI and unit testing Spring DM MockBundle MockBundleContext MockServiceRegistrationMockServiceReference MockBundleActivator
  20. 20. OSGI and integration testing CustomServiceIntegrationTest.java @RunWith(PaxExam.class) @ExamReactorStrategy(PerMethod.class) public class CustomServiceIntegrationTest { @Inject CustomService customService; @Configuration public Option[] config() { return options(mavenBundle("com.foo", "foo-service", "1.0"), junitBundles()); } }
  21. 21. BND image from http://freemarket.kiev.ua/ Compiled classes Instruction file OSGI bundle
  22. 22. OSGI containers/frameworks Apache Felix 36% Equinox 37% SpringSource dm Server 8% Knopflerfish 8% Concierge 8% ProSyst 3% OSGI CONTAINERS POPULARITY (BYVOTES AT STACKOVERFLOW.COM)
  23. 23. Overview of Apache Felix Apache License 2.0 OSGI R4.2 compatible Provides a web console Provides a Maven plugin Active development Proved solution Good documentation
  24. 24. Overview of Apache Karaf Apache License 2.0 Supports both Felix and Equinox Lightweight and extensible Interactive shell console Hot deployment Native OS integration Extra management tools Active development
  25. 25. OSGI in real world
  26. 26. Positives
  27. 27. Negatives

Editor's Notes

  • permits = friendly modules list (current module is accessible only for those)class = entry class
  • Framework API:org.osgi.framework packageBundle Wiring API: for wiring bundles (requirements and capabilities), classes BundleRevision, BundleWiring, FrameworkWiringStart Level API:describes how to enable a management agent to control the relative starting and stopping order of bundles in an OSGi Service Platform (setting start level, in Karafosgi:bundle-level, osgi:start-level).Remote Services: setup of remote access for the services
  • Class loading: “uses” marks packages that exported API use (method signatures, parent classes), so the framework can check if those don’t conflict with imported packagesif java.* =&gt; parentif in Import-Package =&gt; exportersearch in Require-Bundlesearch in Bundle-ClassPathif in DynamicImport-Package =&gt; wiring
  • Web-ContextPath: OSGI 4.2 specWebapp-Context: PAX Web specific
  • Device Access specification supports the coordination of automatic detection and attachment ofexisting devices on an OSGi Service Platform, facilitates hot-plugging and -unplugging of newdevices, and downloads and installs device drivers on demand.Preferences = system or user-specific data storageUser Admin = authentication/authorizationMonitor Admin = status variables get/set/reset from administrative bundles
  • Management tools: configuration (property files), logging, security, etc

×