Getting modular with OSGI


Published on

The presentation was shared on Lviv JUG meeting #21

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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.* => parentif in Import-Package => exportersearch in Require-Bundlesearch in Bundle-ClassPathif in DynamicImport-Package => 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
  • Getting modular with OSGI

    2. 2. Agenda› How to make my Java more modular› Discover and exploit OSGI› Tools, tools, tools…› Output
    3. 3. About meWho: Andrii KrokhmalnyiDomain: Java/Java EECompany: EPAM SystemsExperience 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 InterfaceMETA-INF/services/ static void main(String[] args) {ServiceLoader<LogRenderer> renderers =ServiceLoader.load(LogRenderer.class);for (LogRenderer renderer : renderers) {...}
    6. 6. Modularity in Java: OSGIMANIFEST.MFMETA-INF/MANIFEST.MFManifest-Version: 1.0Bundle-SymbolicName: foo-serviceBundle-Activator:,,;version="[2.0,3)"image from
    7. 7. Project Jigsawsrc/main/java/com/foo/service/module-info.javamodule foo-service @ 1.2 {requires jdk.base;requires optional jdk.jaxp;exports;permits foo-ui;permits foo-console;class;view dao {exports;}}image from
    8. 8. History & versioning
    9. 9. OSGI specification (version 4.3)Framework SpecificationFrameworkAPIStart LevelAPIRemoteServicesBundleWiringAPIAdditionalServicesSpecificationsSecurity LayerModule LayerLife Cycle LayerService Layer
    10. 10. Core :: Container and bundlesimage from http://www.osgi.orgOSGI ContainerClass Loader #1Bundle #1Class Loader #2Bundle #2Class Loader #NBundle #N(modules)a regular jar archive with tweaked manifest
    11. 11. Core :: Bundles life-cycle
    12. 12. Core :: MANIFEST.MF headersfoo-service.jar/META-INF/MANIFEST.MF...Bundle-ManifestVersion: 2Bundle-SymbolicName: Foo ServiceBundle-Description: Foo Service ModuleBundle-Version: 1.0Bundle-Activator:,,;uses:=“org.apache.log4j”Import-Package:;version=“[1.0,2)”,org.apache.log4j;version=“[2.0,3)”DynamicImport-Package: *...
    13. 13. Core :: MANIFEST.MF and Mavenpom.xml<build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><configuration><instructions><Bundle-Activator></Bundle-Activator><Export-Package></Export-Package><Import-Package>,,*</Import-Package><DynamicImport-Package>*</DynamicImport-Package><Require-Bundle></Require-Bundle></instructions></configuration></plugin></plugins></build>
    14. 14. OSGI Services :: Service producerServiceProducerBundleActivator.javapublic 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 consumerServiceConsumerBundleActivator.javapublic 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="" xmlns:xsi="..."xmlns:osgi="" xsi:schemaLocation="..."><osgi:service ref="customService" interface=""/></beans>Producer/META-INF/spring/spring-osgi.xml<beans xmlns="" xmlns:xsi="..."xmlns:osgi="" xsi:schemaLocation="..."><osgi:reference id="customService" interface=""/></beans>Consumer
    17. 17. WhereWAR becomesWABfoo-web.war/META-INF/MANIFEST.MF...Bundle-ManifestVersion: 2Bundle-SymbolicName: 1.0Bundle-Activator: mainapp/fooWebapp-Context: mainapp/fooBundle-ClassPath: WEB-INF/classes,.Import-Package:;version=“[1.0,2)”,;version=“[1.0,2)”;resolution:="optional"...
    18. 18. Compendium servicesCompendiumOSGI ServicesPreferences UserAdmin Monitor AdminLog HTTPDeviceAccessConfigurationAdmin… and many more
    19. 19. OSGI and unit testingSpringDMMockBundleMockBundleContextMockServiceRegistrationMockServiceReferenceMockBundleActivator
    20. 20. OSGI and integration class CustomServiceIntegrationTest {@InjectCustomService customService;@Configurationpublic Option[] config() {return options(mavenBundle("", "foo-service", "1.0"), junitBundles());}}
    21. 21. BNDimage from
    22. 22. OSGI containers/frameworksApache Felix36%Equinox37%SpringSourcedm Server8%Knopflerfish8%Concierge8%ProSyst3%OSGI CONTAINERS POPULARITY(BYVOTES AT STACKOVERFLOW.COM)
    23. 23. Overview of Apache FelixApache License 2.0OSGI R4.2 compatibleProvides a web consoleProvides a Maven pluginActive developmentProved solutionGood documentation
    24. 24. Overview of Apache KarafApache License 2.0Supports both Felix and EquinoxLightweight and extensibleInteractive shell consoleHot deploymentNative OS integrationExtra management toolsActive development
    25. 25. OSGI in real world
    26. 26. Positives
    27. 27. Negatives