OSGi – introduction to 
application modularity 
Marek Koniew 
the most probable future of core+
What is OSGi 
OSGi Alliance - Open Services Gateway Initiative 
http://www.osgi.org 
Membership $5000 - $25000 per year. 
Latest specification version is 5 (no implementation yet). 
Implementations: 
60
OSGi overview 
 Bundles - OSGi components made by the developers. 
 Services - connects bundles in a dynamic way. 
 Modules - defines how a bundle can import and export code. 
 Life-Cycle - to install, start, stop, update, and uninstall bundles. 
58
Modularity Benefits 
What OSGi brings: 
 Reuse – 3rd party components available as OSGi bundles 
 Dynamic updates – bundle can be updated in runtime 
 Adaptive – design forces to work when not everything is available 
 Versioning – many component versions at the same time 
 Simple – core API less than 30 classes/interfaces 
 Small – OSGi v4 implementation is 300KB JAR 
 Fast – working inside singe JVM – no serialization needed. 
 Lazy – loading only what is necessary 
 Reduced complexity – modules hides complexity from outside 
56
OSGi bundles 
 Jar files 
 Exporting packages : bundle definition 
 Exporting services : runtime 
54
OSGi bundles – manifest.mf 
Bundle-Activator: com.hybris.kernel.regioncache.osgi.OSGiBundleActivator 
Bundle-Name: kernel-cache 
Bundle-Version: 1.0.1 
Export-Package: com.hybris.kernel.regioncache 
Import-Package: com.hybris.commons.tenant,com.hybris.kernel.api; 
version="[3.0,4)" 
Private-Package: com.hybris.kernel.regioncache.counters,com.hybris.kerne 
l.regioncache.impl,com.hybris.kernel.regioncache.jmx,com.hybris.kernel. 
regioncache.osgi,com.hybris.kernel.regioncache.region 
52
OSGi bundles - versions 
Transitive dependencies conflict is not a problem anymore: 
50
OSGi bundles - lifecycle 
48 
GoGo shell
OSGi bundles - activator 
public class OSGiBundleActivator implements org.osgi.framework.BundleActivator 
{ 
@Override 
public void start(final BundleContext context) throws Exception 
{ 
System.out.println("Activating osgi module " + 
context.getBundle().getSymbolicName()); 
} 
@Override 
public void stop(final BundleContext context) throws Exception 
{ 
System.out.println("Stop " + context.getBundle().getSymbolicName()); 
} 
} 
46
Whiteboard pattern 
 Also applicable to configuration bundle. 
 How it behavies when there is no service at the moment? 
 Configuration can be provided this way. 
44
Semantic versioninig - definition 
1.0.0.abc 
Major.Minor.Micro.Qualifier 
• Major: incompatible change for providers and 
consumers 
• Minor: incompatible change for providers 
• Micro: compatible change 
• Qualifier: anything (e.g. Build identity) 
42
Semantic versioninig - reference 
 OSGI 
http://www.osgi.org/wiki/uploads/Links/SemanticVersioning.pdf 
 SemVer.org 
http://semver.org/ 
 Eclipse 
http://wiki.eclipse.org/Version_Numbering 
40
Semantic versioninig – version ranges 
38
Semantic versioninig – bundle hell 
 Bundle hell 
 Feature grouping 
 Apache ACE 
36
OSGi tools (selection) 
 Bndtools 
 Import analysis 
 Semantic versioning 
 Integrated testing 
 Apache Felix 
 OSGi implementation 
 Maven integration 
 GoGo shell 
 Apache ACE 
 Manage and distribute components 
34
Bndtools 
 Eclipse only 
 Feature reach 
 Import analysis 
 Repositories 
 OBR resolving (OSGi bundle repository) 
 Semantic versioning 
 Instant builder 
 Integrated testing 
 Live presentation 
 Releasing and version control 
 Calculated imports 
 Integration tests 
29
OSGi testing 
 Unit tests – use mocks 
 Integration tests 
 bndtools in eclipse 
 bndtools ant task (also from maven) 
 tycho-surefire maven plugin 
 OSGi Assertion library 
https://github.com/dpishchukhin/org.knowhowlab.osgi.testing 
 assertServiceAvailable(CacheController.class) 
 findBundle(„kernel-cache”).stop() 
 … 
27
Maven integration 
<packaging>bundle</packaging> 
… 
<plugin> 
<groupId>org.apache.felix</groupId> 
<artifactId>maven-bundle-plugin</artifactId> 
<configuration> 
<instructions> 
<Export-Package>com.hybris.kernel.regioncache</Export- 
Package> 
<Private-Package>com.hybris.kernel.regioncache.*</Private- 
Package> 
<Bundle-Activator> 
com.hybris.kernel.regioncache.osgi.OSGiBundleActivator 
</Bundle-Activator> 
<Import-Package>com.hybris.kernel.api</Import-Package> 
</instructions> 
</configuration> 
</plugin> 
25
OSGi – DI 
 Declarative services 
http://wiki.osgi.org/wiki/Declarative_Services 
 Blueprint 
http://wiki.osgi.org/wiki/Blueprint 
 CDI-OSGi (draft) 
https://speakerdeck.com/paulbakker/rfc-193-osgi-cdi 
• Hide OSGi service management complexity 
• Still to implement manually situation when service is not available !!! 
• Only singleton services supported. RFC-195 (draft) introduces prototype. 
23
OSGi – Declarative Services 
 Functionality provided by OSGi 4 specification 
package com.hybris; 
import aQute.bnd.annotation.component.*; 
@Component 
Class Hello { 
private Log log; 
@Reference(unbind = „unSetLog”) 
public void setLog(Log log) { … } 
public void unSetLog(Log log) { … } 
} 
 Manifest.mf 
Service-Component: com.hybris.*; 
21
OSGi – Blueprint 
 Defined in OSGi v4.2 
 Implemented 
 Apache Aries 
 Eclipse Gemini 
 Spring DM (dead) 
 Manifest.mf 
Spring-Context: config/hello-context.xml; wait-for-dependencies:=true; timeout:=1000 
 ServiceUnavailableException after timeout 19
OSGi – CDI (Contexts and Dependency Injection) 
 Draft RFC-193 no implementation yet 
 https://speakerdeck.com/paulbakker/rfc-193-osgi-cdi - Luminis Tech. 
@OSGIComponent 
public class MyComponent { 
@Inject 
@OSGiService(required=false) 
MyService myService; 
} 
 Service dynamics 
 Injection happens every time MyService is registered/deregistered 
 With required=true MyCOmponent is deregistered when MyService is not 
available 
 Declarative service listeners @ServiceAdded, @ServiceRemoved 
17
Core+ cache as external module - motivation 
15
Core+ cache as external module - overview 
15 
Embedded Felix. Separate Spring context. Service Proxy. Cache Client. API is a bundle.
Core+ cache as external module - demo 
 Architecture 
• embedded Felix OSGi container 
• Spring context startup in bundle activator 
• OSGi Service proxy – can act as proxy over any protocol 
• Possibility to run without OSGi 
• Cache checker 
 Features 
• No thigh coupling to OSGi 
• Connect client to service without OSGi (direct spring) 
• Incremental migration to OSGi 
 Life demo 
• Start/stop bundle 
• How application reacts 
10
OSGi readings 
Building Modular Cloud Apps with OSGi 
Paul Bakker 
8
OSGi conference 
 Never A Wrong (Semantic) Version Again! http://youtu.be/LBgNRsEJCWo 
 DS, BP, EJB, CDI, WTF!? http://youtu.be/wr85daWNpZo 
 Testing OSGi the "groovy" way http://youtu.be/UEWIoyIvT3Y 
 Continuous Automated Deployment with Apache ACE 
http://www.slideshare.net/mfrancis/continuous-automated-deployment-with-apache-ace-jago-de- 
vreede-marcel-offermans 
 More: http://www.osgi.org/CommunityEvent2013/Schedule 
6
Questions
Introduction to OSGGi

Introduction to OSGGi

  • 2.
    OSGi – introductionto application modularity Marek Koniew the most probable future of core+
  • 3.
    What is OSGi OSGi Alliance - Open Services Gateway Initiative http://www.osgi.org Membership $5000 - $25000 per year. Latest specification version is 5 (no implementation yet). Implementations: 60
  • 4.
    OSGi overview Bundles - OSGi components made by the developers.  Services - connects bundles in a dynamic way.  Modules - defines how a bundle can import and export code.  Life-Cycle - to install, start, stop, update, and uninstall bundles. 58
  • 5.
    Modularity Benefits WhatOSGi brings:  Reuse – 3rd party components available as OSGi bundles  Dynamic updates – bundle can be updated in runtime  Adaptive – design forces to work when not everything is available  Versioning – many component versions at the same time  Simple – core API less than 30 classes/interfaces  Small – OSGi v4 implementation is 300KB JAR  Fast – working inside singe JVM – no serialization needed.  Lazy – loading only what is necessary  Reduced complexity – modules hides complexity from outside 56
  • 6.
    OSGi bundles Jar files  Exporting packages : bundle definition  Exporting services : runtime 54
  • 7.
    OSGi bundles –manifest.mf Bundle-Activator: com.hybris.kernel.regioncache.osgi.OSGiBundleActivator Bundle-Name: kernel-cache Bundle-Version: 1.0.1 Export-Package: com.hybris.kernel.regioncache Import-Package: com.hybris.commons.tenant,com.hybris.kernel.api; version="[3.0,4)" Private-Package: com.hybris.kernel.regioncache.counters,com.hybris.kerne l.regioncache.impl,com.hybris.kernel.regioncache.jmx,com.hybris.kernel. regioncache.osgi,com.hybris.kernel.regioncache.region 52
  • 8.
    OSGi bundles -versions Transitive dependencies conflict is not a problem anymore: 50
  • 9.
    OSGi bundles -lifecycle 48 GoGo shell
  • 10.
    OSGi bundles -activator public class OSGiBundleActivator implements org.osgi.framework.BundleActivator { @Override public void start(final BundleContext context) throws Exception { System.out.println("Activating osgi module " + context.getBundle().getSymbolicName()); } @Override public void stop(final BundleContext context) throws Exception { System.out.println("Stop " + context.getBundle().getSymbolicName()); } } 46
  • 11.
    Whiteboard pattern Also applicable to configuration bundle.  How it behavies when there is no service at the moment?  Configuration can be provided this way. 44
  • 12.
    Semantic versioninig -definition 1.0.0.abc Major.Minor.Micro.Qualifier • Major: incompatible change for providers and consumers • Minor: incompatible change for providers • Micro: compatible change • Qualifier: anything (e.g. Build identity) 42
  • 13.
    Semantic versioninig -reference  OSGI http://www.osgi.org/wiki/uploads/Links/SemanticVersioning.pdf  SemVer.org http://semver.org/  Eclipse http://wiki.eclipse.org/Version_Numbering 40
  • 14.
    Semantic versioninig –version ranges 38
  • 15.
    Semantic versioninig –bundle hell  Bundle hell  Feature grouping  Apache ACE 36
  • 16.
    OSGi tools (selection)  Bndtools  Import analysis  Semantic versioning  Integrated testing  Apache Felix  OSGi implementation  Maven integration  GoGo shell  Apache ACE  Manage and distribute components 34
  • 17.
    Bndtools  Eclipseonly  Feature reach  Import analysis  Repositories  OBR resolving (OSGi bundle repository)  Semantic versioning  Instant builder  Integrated testing  Live presentation  Releasing and version control  Calculated imports  Integration tests 29
  • 18.
    OSGi testing Unit tests – use mocks  Integration tests  bndtools in eclipse  bndtools ant task (also from maven)  tycho-surefire maven plugin  OSGi Assertion library https://github.com/dpishchukhin/org.knowhowlab.osgi.testing  assertServiceAvailable(CacheController.class)  findBundle(„kernel-cache”).stop()  … 27
  • 19.
    Maven integration <packaging>bundle</packaging> … <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> <Export-Package>com.hybris.kernel.regioncache</Export- Package> <Private-Package>com.hybris.kernel.regioncache.*</Private- Package> <Bundle-Activator> com.hybris.kernel.regioncache.osgi.OSGiBundleActivator </Bundle-Activator> <Import-Package>com.hybris.kernel.api</Import-Package> </instructions> </configuration> </plugin> 25
  • 20.
    OSGi – DI  Declarative services http://wiki.osgi.org/wiki/Declarative_Services  Blueprint http://wiki.osgi.org/wiki/Blueprint  CDI-OSGi (draft) https://speakerdeck.com/paulbakker/rfc-193-osgi-cdi • Hide OSGi service management complexity • Still to implement manually situation when service is not available !!! • Only singleton services supported. RFC-195 (draft) introduces prototype. 23
  • 21.
    OSGi – DeclarativeServices  Functionality provided by OSGi 4 specification package com.hybris; import aQute.bnd.annotation.component.*; @Component Class Hello { private Log log; @Reference(unbind = „unSetLog”) public void setLog(Log log) { … } public void unSetLog(Log log) { … } }  Manifest.mf Service-Component: com.hybris.*; 21
  • 22.
    OSGi – Blueprint  Defined in OSGi v4.2  Implemented  Apache Aries  Eclipse Gemini  Spring DM (dead)  Manifest.mf Spring-Context: config/hello-context.xml; wait-for-dependencies:=true; timeout:=1000  ServiceUnavailableException after timeout 19
  • 23.
    OSGi – CDI(Contexts and Dependency Injection)  Draft RFC-193 no implementation yet  https://speakerdeck.com/paulbakker/rfc-193-osgi-cdi - Luminis Tech. @OSGIComponent public class MyComponent { @Inject @OSGiService(required=false) MyService myService; }  Service dynamics  Injection happens every time MyService is registered/deregistered  With required=true MyCOmponent is deregistered when MyService is not available  Declarative service listeners @ServiceAdded, @ServiceRemoved 17
  • 24.
    Core+ cache asexternal module - motivation 15
  • 25.
    Core+ cache asexternal module - overview 15 Embedded Felix. Separate Spring context. Service Proxy. Cache Client. API is a bundle.
  • 26.
    Core+ cache asexternal module - demo  Architecture • embedded Felix OSGi container • Spring context startup in bundle activator • OSGi Service proxy – can act as proxy over any protocol • Possibility to run without OSGi • Cache checker  Features • No thigh coupling to OSGi • Connect client to service without OSGi (direct spring) • Incremental migration to OSGi  Life demo • Start/stop bundle • How application reacts 10
  • 27.
    OSGi readings BuildingModular Cloud Apps with OSGi Paul Bakker 8
  • 28.
    OSGi conference Never A Wrong (Semantic) Version Again! http://youtu.be/LBgNRsEJCWo  DS, BP, EJB, CDI, WTF!? http://youtu.be/wr85daWNpZo  Testing OSGi the "groovy" way http://youtu.be/UEWIoyIvT3Y  Continuous Automated Deployment with Apache ACE http://www.slideshare.net/mfrancis/continuous-automated-deployment-with-apache-ace-jago-de- vreede-marcel-offermans  More: http://www.osgi.org/CommunityEvent2013/Schedule 6
  • 29.