Your SlideShare is downloading. ×
0
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
JahiaOne - Jahia7 architecture: Learning more about OSGi
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JahiaOne - Jahia7 architecture: Learning more about OSGi

2,659

Published on

Designed as a follow-up to the first “Welcome to OSGi” session, this session will dive deeper into OSGi to teach you how to leverage all the new possibilities and also give you pointers on how to …

Designed as a follow-up to the first “Welcome to OSGi” session, this session will dive deeper into OSGi to teach you how to leverage all the new possibilities and also give you pointers on how to avoid common pitfalls when developing with OSGi.

We will also dive deeper into the tools that are available to help you in your module development, or any other OSGi services you want to integrate, such as OSGi services, Blueprint, OSGi enterprise, etc.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,659
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • mvn clean install jahia:deploy -Djahia.deploy.targetServerType=tomcat -Djahia.deploy.targetServerVersion=7 -Djahia.deploy.targetServerDirectory=/Users/loom/java/packages/Ent-Jahia_xCM_v7.0.0.0-SNAPSHOT-jahiaOne-demo/tomcat/
  • Transcript

    • 1. Learning more OSGi #jahiaon e Serge Huber, CTO, shuber@jahia.com
    • 2. OSGi module first steps  Create a new project using a Maven Archetype: mvn archetype:generate -DarchetypeCatalog= https://devtools.jahia.com/nexus/content/repositories/jahia-snapshots/arc  Select archetype : 4: https://devtools.jahia.com/nexus/content/repositories/jahia-snapshots/archetype-catalog.xml -> org.jahia.archetypes:jahia-module-archetype (Archetype for creating a new module project to be run on a Jahia xCM server)  Enter project metadata and confirm  Change into project directory and build using : mvn clean install
    • 3. OSGi module first steps  Let’s look at the generated JAR METAINF/MANIFEST. MF file: Manifest-Version: 1.0 Bnd-LastModified: 1390915616802 Build-Jdk: 1.7.0_45 Built-By: loom Bundle-Category: jahia-module Bundle-Description: This is the custom module (Sample Module) for runnin g on a Jahia xCM server. Bundle-DocURL: http://www.jahia.com Bundle-ManifestVersion: 2 Bundle-Name: Sample Module Bundle-SymbolicName: sample Bundle-Vendor: Jahia Solutions Group SA Bundle-Version: 1.0.0.SNAPSHOT Created-By: Apache Maven Bundle Plugin Implementation-Title: Sample Module Implementation-URL: http://www.jahia.com Implementation-Vendor: Jahia Solutions Group SA Implementation-Version: 1.0-SNAPSHOT Import-Package: org.jahia.defaults.config.spring,org.jahia.exceptions,or g.jahia.services Jahia-Depends: default Jahia-GroupId: org.jahia.modules Jahia-Module-Type: module Jahia-Required-Version: 7.0.0.0-SNAPSHOT Jahia-Source-Control-Connection: scm:dummy:uri Jahia-Source-Control-DeveloperConnection: scm:dummy:uri Jahia-Source-Folders: /Users/loom/temp/sample Jahia-Static-Resources: /css,/icons,/images,/img,/javascript Specification-Title: Sample Module Specification-Vendor: Jahia Solutions Group SA Specification-Version: 1.0-SNAPSHOT Tool: Bnd-2.1.0.20130426-122213
    • 4. Quicker way to view Manifest headers  Simply use new Jahia plugin goal : mvn jahia:osgi-inspect  Produces easy to read output of ImportPackage attribute Bnd-LastModified: 1390915616802 Build-Jdk: 1.7.0_45 Built-By: loom Bundle-Category: jahia-module Bundle-Description: This is the custom module (Sample Module) for running on a Jahia x Bundle-DocURL: http://www.jahia.com Bundle-ManifestVersion: 2 Bundle-Name: Sample Module Bundle-SymbolicName: sample Bundle-Vendor: Jahia Solutions Group SA Bundle-Version: 1.0.0.SNAPSHOT Created-By: Apache Maven Bundle Plugin Implementation-Title: Sample Module Implementation-URL: http://www.jahia.com Implementation-Vendor: Jahia Solutions Group SA Implementation-Version: 1.0-SNAPSHOT Import-Package: org.jahia.defaults.config.spring org.jahia.exceptions org.jahia.services Jahia-Depends: default Jahia-GroupId: org.jahia.modules Jahia-Module-Type: module Jahia-Required-Version: 7.0.0.0-SNAPSHOT Jahia-Source-Control-Connection: scm:dummy:uri Jahia-Source-Control-DeveloperConnection: scm:dummy:uri Jahia-Source-Folders: /Users/loom/temp/sample Jahia-Static-Resources: /css /icons /images /img /javascript Manifest-Version: 1.0 Specification-Title: Sample Module Specification-Vendor: Jahia Solutions Group SA Specification-Version: 1.0-SNAPSHOT Tool: Bnd-2.1.0.20130426-122213
    • 5. Using osgi-inspect on any JAR  You can beautify MANIFEST headers of any JAR using the following goal syntax: mvn org.jahia.server:jahia-maven-plugin:4.0-SNAPSHOT:osgi-inspect -DjarBundles=org.osgi.compendium-4.3.1.jar
    • 6. Let’s add to our module… A content definition file (METAINF/definitions.cnd): <jnt = 'http://www.jahia.org/jahia/nt/1.0'> <jmix = 'http://www.jahia.org/jahia/mix/1.0'> [jnt:sampleHello] > jnt:content, jmix:editorialContent, mix:title, jmix:tagged, jmix:basicContent - helloName (string) A view for the content definition (jnt_sampleHello/html/sampleHello.jsp): <%@ taglib prefix="jcr" uri="http://www.jahia.org/tags/jcr" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="utility" uri="http://www.jahia.org/tags/utilityLib" %> <%@ taglib prefix="template" uri="http://www.jahia.org/tags/templateLib" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> Hello <jcr:nodeProperty node="${currentNode}" name="helloName"/> !
    • 7. Generated imports Import-Package: org.apache.taglibs.standard.tag.rt.core,org.jahia.taglib s.template.pager;version="[7.0,8)",org.jahia.taglibs.jcr.query;version= "[7.0,8)",org.apache.taglibs.standard.tag.common.core,org.jahia.taglibs .utility.siteproperties;version="[7.0,8)",org.jahia.taglibs.utility.i18 n;version="[7.0,8)",org.apache.taglibs.standard.functions,org.jahia.tag libs.user;version="[7.0,8)",org.jahia.taglibs.template.include;version= "[7.0,8)",org.apache.taglibs.unstandard;version="[7.0,8)",org.jahia.tag libs.utility.constants;version="[7.0,8)",javax.jcr;version="[2.0,3)",or g.jahia.services.render,org.apache.taglibs.standard.tag.rt.fmt,org.apac he.taglibs.standard.tei,org.jahia.taglibs.template.layoutmanager;versio n="[7.0,8)",org.jahia.taglibs.utility;version="[7.0,8)",org.jahia.servi ces.content,org.jahia.taglibs.template.gwt;version="[7.0,8)",org.jahia. taglibs.jcr;version="[7.0,8)",org.jahia.taglibs.internal.date;version=" [7.0,8)",org.jahia.taglibs.jcr.node;version="[7.0,8)",org.jahia.default s.config.spring,org.jahia.taglibs.template;version="[7.0,8)",org.jahia. services,org.jahia.taglibs.utility.session;version="[7.0,8)",org.jahia. taglibs;version="[7.0,8)",org.jahia.services.content.nodetypes,org.jahi a.exceptions
    • 8. mvn jahia:osgi-inspect Import-Package: org.apache.taglibs.standard.tag.rt.core org.jahia.taglibs.template.pager; version=[7.0,8) org.jahia.taglibs.jcr.query; version=[7.0,8) org.apache.taglibs.standard.tag.common.core org.jahia.taglibs.utility.siteproperties; version=[7.0,8) org.jahia.taglibs.utility.i18n; version=[7.0,8) org.apache.taglibs.standard.functions org.jahia.taglibs.user; version=[7.0,8) org.jahia.taglibs.template.include; version=[7.0,8) org.apache.taglibs.unstandard; version=[7.0,8) org.jahia.taglibs.utility.constants; version=[7.0,8) javax.jcr; version=[2.0,3) org.jahia.services.render org.apache.taglibs.standard.tag.rt.fmt org.apache.taglibs.standard.tei org.jahia.taglibs.template.layoutmanager; version=[7.0,8) org.jahia.taglibs.utility; version=[7.0,8) org.jahia.services.content org.jahia.taglibs.template.gwt; version=[7.0,8) org.jahia.taglibs.jcr; version=[7.0,8) org.jahia.taglibs.internal.date; version=[7.0,8) org.jahia.taglibs.jcr.node; version=[7.0,8) org.jahia.defaults.config.spring org.jahia.taglibs.template; version=[7.0,8) org.jahia.services org.jahia.taglibs.utility.session; version=[7.0,8) org.jahia.taglibs; version=[7.0,8) org.jahia.services.content.nodetypes org.jahia.exceptions
    • 9. Generated capabilities Provide-Capability: com.jahia.services.content; nodetypes:List<String>=jnt:sampleHello Require-Capability: com.jahia.services.content; filter:=(nodetypes=jmix:basicContent) com.jahia.services.content; filter:=(nodetypes=jmix:editorialContent) com.jahia.services.content; filter:=(nodetypes=jmix:tagged) com.jahia.services.content; filter:=(nodetypes=jnt:content) com.jahia.services.content; filter:=(nodetypes=mix:title)
    • 10. Package dependencies  OSGi framework will only let you access a Java package if :  it is exported by an OSGi bundle  it is imported by your own bundle  OSGi imports and exports must be declared in the META-INF/MANIFEST.MF file  If an import or export is missing, or if versions of packages don’t match -> no access to the package is possible
    • 11. Felix Maven bundle plugin  Main plugin to generate OSGi bundle out of a Maven project  Based on the standard OSGi BND tool that parses compiled Java classes to extract package uses  Plugin is mostly a wrapper around the BND tool, preparing input for it and postprocessing its output.
    • 12. What the bundle plugin can NOT do  Scan in non-Java resources such as:  JSPs  Taglibs  Groovy files  Spring descriptors  Content definitions  Content import files
    • 13. Jahia Maven plugin OSGi extensions  jahia:dependencies goal “complements” Felix bundle maven plugin to extract dependencies from other resources  Can scan a lot of resources for dependencies  JSPs  Taglibs  Groovy files  Drools rule files  Spring descriptors  Jahia content definitions  Jahia content import files  And more !  Will generate precise imports (with version numbers) based on automatic scanning
    • 14. OSGi header generation Jahia Maven Plugin Jahia Maven Plugin jahia:dependencies jahia:dependencies Project Source Code Felix Bundle Maven Plugin Felix Bundle Maven Plugin pom.xml Instructions META-INF/MANIFEST.MF OSGi headers
    • 15. Integrating external (non OSGi libraries)  Three possibilities:  Transform them into OSGi compliant bundles (multiple options, from dynamic transforming to new projects)  Embed them inside your bundle (either as a JAR inside a JAR or by inlining them). Not optimal in terms of re-use but quite nice in terms of isolation and easy to do. Be careful with Embed-Transitive though !  Deploy them at the framework level, exposing them to the whole OSGi runtime. This is not recommended unless you have a very specific reason to do so and know what you are doing. In any case you should always specify the version of all the packages you are exporting.
    • 16. Finding OSGi bundles  Most Apache projects now have OSGi bundles  Apache ServiceMix maintains OSGi bundles of popular libraries ( http://servicemix.apache.org/developers/source/bun )  SpringSource has a bundle repository here ( http://ebr.springsource.com/repository/app/). Be careful though some bundles are not always correct, prefer ServiceMix ones
    • 17. In the real world  There is an OSGi bundle of the library you need:  It might have been poorly done  It might have other bundle dependencies  There is no OSGi bundle for the library you’re interested in:  Embedding the library in your bundle is usually the easiest solution  Generating and sharing an OSGi bundle version will help everyone
    • 18. Package uses origins  In resources such as JSPs, Spring descriptor files  In third party libraries (usually these imports are optional, although not always)  Requires understanding how BND parses class files to generate imports (BND is the engine behind the Felix Maven bundle plugin)
    • 19. Finding packages in dependencies [fileinstall-./deploy] WARN org.apache.felix.fileinstall - Error while starting bundle: file:/Users/loom/java/projects/configtool/standalone/target/distribution/deploy/config.tool.plugin.jahiaconfig.service-1.0-SNAPSHOT.jar org.osgi.framework.BundleException: Unresolved constraint in bundle org.jahia.server.config.tool.plugin.jahiaconfig.service [127]: Unable to resolve 127.1: missing requirement [127.1] osgi.wiring.package; (osgi.wiring.package=com.ibm.icu.text) at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974) at org.apache.felix.framework.Felix.startBundle(Felix.java:2037) at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955) at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1247) at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1219) at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1208) at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:503) at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:291)mvn jahia:find-package-uses -DpackageNames=org.eclipse.aether.spi.connector
    • 20. Finding packages in dependencies mvn jahia:find-package-uses -DpackageNames=com.ibm.icu.text Please use CMSClassUnloadingEnabled in place of CMSPermGenSweepingEnabled in the future [INFO] Scanning for projects... [INFO] [INFO] -----------------------------------------------------------------------[INFO] Building Jahia Configuration Service implementation 1.0-SNAPSHOT [INFO] -----------------------------------------------------------------------[INFO] [INFO] --- jahia-maven-plugin:4.0-SNAPSHOT:find-package-uses (default-cli) @ config.tool.plugin.jahiaconfig.service --[INFO] Scanning project dependencies... [INFO] org.jahia.server:config.tool.plugin.jahiaconfig.service:bundle:1.0-SNAPSHOT [INFO] +- org.jahia.configuration:configurators:jar:2.53.2 [INFO] +- jaxen:jaxen:jar:1.1.1 [INFO] +- xom:xom:jar:1.0 (/Users/loom/.m3/repository/xom/xom/1.0/xom-1.0.jar) :
    • 21. Finding packages, continued <dependency> <groupId>org.jahia.configuration</groupId> <artifactId>configurators</artifactId> <version>2.53.2</version> <exclusions> <exclusion> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> </exclusion> </exclusions> </dependency>
    • 22. Packages uses, continued  Package uses may sometimes be :  In optional dependencies  Import-Package: resolution:=optional  In regular dependencies but not used at runtime  Exclusion  Iterative process of deployment, error, modification until it works
    • 23. Other example [fileinstall-./deploy] WARN org.apache.felix.fileinstall - Error while starting bundle: file:/Users/loom/java/projects/configtool/standalone/target/distribution/deploy/config.tool.plugin.jahiaconfig.service-1.0-SNAPSHOT.jar org.osgi.framework.BundleException: Unresolved constraint in bundle org.jahia.server.config.tool.plugin.jahiaconfig.service [127]: Unable to resolve 127.3: missing requirement [127.3] osgi.wiring.package; (osgi.wiring.package=com.ibm.jvm) at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974) at org.apache.felix.framework.Felix.startBundle(Felix.java:2037) at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955) at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1247) at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1219) at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1208) at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:503) at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:291)
    • 24. Other example Sheldon:jahiaconfig-service loom$ mvn jahia:find-package-uses -DpackageNames=com.ibm.jvm Please use CMSClassUnloadingEnabled in place of CMSPermGenSweepingEnabled in the future [INFO] Scanning for projects... [INFO] [INFO] -----------------------------------------------------------------------[INFO] Building Jahia Configuration Service implementation 1.0-SNAPSHOT [INFO] -----------------------------------------------------------------------[INFO] [INFO] --- jahia-maven-plugin:4.0-SNAPSHOT:find-package-uses (default-cli) @ config.tool.plugin.jahiaconfig.service --[INFO] Scanning project dependencies... [INFO] org.jahia.server:config.tool.plugin.jahiaconfig.service:bundle:1.0-SNAPSHOT [INFO] +- org.jahia.configuration:configurators:jar:2.53.2 [INFO] +- org.apache.derby:derby:jar:10.9.1.0 (/Users/loom/.m3/repository/org/apache/derby/derby/10.9.1.0/derby-10.9.1.0.jar) : [INFO] +--> Found class org.apache.derby.iapi.services.info.JVMInfo that uses package com.ibm.jvm [INFO] ================================================================================= [INFO] SEARCH RESULTS SUMMARY [INFO] --------------------------------------------------------------------------------[INFO] Package com.ibm.jvm used in classes : [INFO] org.apache.derby.iapi.services.info.JVMInfo ( /Users/loom/.m3/repository/org/apache/derby/derby/10.9.1.0/derby10.9.1.0.jar)
    • 25. Finding packages  search.maven.org -> org.apache.derby  Nothing  Google for class org.apache.derby.iapi.services.info.JVMInfo  Found code at https://svn.apache.org/repos/asf/db/derby/code/trunk/ja  Class.forName(“com.ibm.jvm.Dump");  Probably OK to declare optional  Import-Package : com.ibm.jvm;resolution:=optional
    • 26. More complex OSGi bundle  Bundle Activator  A class declared in the bundle's MANIFEST that will be called on bundle start and bundle stop.  Useful for initial wiring of the bundle's services and class, or any other startup/shutdown work needed.
    • 27. Bundle Activator example  In MANIFEST.MF: Bundle-Activator: com.example.MyActivator  Class implementation: package com.example; public class MyActivator implements org.osgi.framework.BundleActivator { public void start(org.osgi.framework.BundleContext context) throws Exception { // do something at startup } public void stop(org.osgi.framework.BundleContext context) throws Exception { } }
    • 28. Bundle activators  Historically the first activation system available in OSGi  Very low-level, may require a lot of manual wiring code  Usually better to use declarative services, or Blueprint to initialize services or components at bundle start
    • 29. OSGi Services  Bundle can expose or use services, basically object instances published in a central service registry. Services usually expose an interface that is implemented by a private and non-accessible class.  Services may appear or disappear anytime, because bundles may be undeployed or deployed at runtime  Although you can do the service tracking yourself, OSGi offers different mechanisms to make it easier to track the availability of services : ServiceTracker helper class, declarative services or the Blueprint dependency injection technology that was inspired by Spring Dynamic modules.
    • 30. Service registration package foo.simple; package foo.simple; /** * Our multifunctional sample interface */ public interface Foo { } /** * The implementation of the Foo interface */ public class FooImpl implements Foo { public FooImpl() { } } Inside bundle activator: Foo foo = new FooImpl(); //typed service registration with one interface sr = bundleContext.registerService( Foo.class, foo, serviceProperties );
    • 31. Service access and use ServiceReference<Foo> sr = bundleContext.getServiceReference( Foo.class ); Foo foo = bundleContext.getService( sr ); Be careful with service references as services may appear and disappear at any time (as bundles are deployed or un-deployed at runtime !
    • 32. ServiceTracker  A helper class that will use a separate thread to track the availability of services you are interested in, and will invoke callbacks when the service become available or disappear.  Powerful and highly dynamic  Might be a little complex to setup  Requires a lot of custom code
    • 33. ServiceTracker example class HttpTracker extends ServiceTracker { int count; HttpTracker(BundleContext context) { super(context, HttpService.class.getName(), null); } public Object addingService(ServiceReference reference) { try { HttpService http = (HttpService) super.addingService(reference); http.registerServlet("/hello", new MyServlet(), null, null); return http; } catch (Exception nse) { } return null; } public void removedService(ServiceReference reference, Object service) { super.removedService(reference, service); } }
    • 34. ServiceTracker example public class Tracker implements BundleActivator { ServiceTracker logTracker; HttpTracker httpTracker; public void start(BundleContext context) throws Exception { try { httpTracker = new HttpTracker(context, logTracker); httpTracker.open(); } catch (Throwable t) { t.printStackTrace(); } } public void stop(BundleContext context) throws Exception { httpTracker.close(); } }
    • 35. Using Blueprint  Open Standard version of the SpringFramework Dependency Injection component contributed by SpringSource  Configuration and Dependencies declared in Blueprint XML or Spring XML (if using Gemini Blueprint implementation)  Alternative implementation at Apache Aries also available
    • 36. Felix shell extension example /** * An example of a Felix shell command */ public class ShellCommands { public String hello(String name) { String message = "Hello " + name; return message; } }
    • 37. Blueprint XML OSGI-INF/blueprint/shell-hello.xml <?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <bean id="shellCommands" class="org.jahia.modules.sample.osgi.ShellCommands" /> <service id="shellCommandsService" interface="org.jahia.modules.sample.osgi.ShellCommands" ref="shellCommands"> <service-properties> <entry key="osgi.command.scope"> <value>sample</value> </entry> <entry key="osgi.command.function"> <value>hello</value> </entry> </service-properties> </service> </blueprint>
    • 38. Spring XML META-INF/spring/shell-hello.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.eclipse.org/gemini/blueprint/schema/blueprint" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans-3.0.xsd http://www.eclipse.org/gemini/blueprint/schema/blueprint http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd"> <bean id="shellCommands" class="org.jahia.modules.sample.osgi.ShellCommands" /> <osgi:service id="shellCommandsService" interface="org.jahia.modules.sample.osgi.ShellCommands" ref="shellCommands"> <osgi:service-properties> <entry key="osgi.command.scope"> <value>sample</value> </entry> <entry key="osgi.command.function"> <value>hello</value> </entry> </osgi:service-properties> </osgi:service> </beans>
    • 39. Which should I use ?  Sprint XML for most common Jahia usage, integrates with all Jahia module functionality and lifecycle  Blueprint XML for non Jahia-specific common usages (or DS annotations but not supported out of the box, simply deploy DS bundle)  ServiceTracker if you need to do it in code for some special reason (for example for implementing Bundle listeners)  If all else fails do it accessing the service registry directly, but it is very difficult to do right
    • 40. OSGi standard services  Compendium services  Package admin  Start level  Permission admin  URL handler  Remote services  HTTP  Blueprint container (Enterprise OSGi)  Configuration admin  Metatype  User admin  Declarative services  and much more….
    • 41. Troubleshooting tips  Do NOT mix Activator, DS or Blueprint -> multiple startup threads can lead to deadlocks  Split complex bundles into simpler bundles, preferably seperating APIs from implementations, making it easier to reuse them in different projects  Use the Jahia Maven plugin to help understand where package imports are coming from
    • 42. OSGi fragments  Fragments are JARs that "attach" to a host bundle, adding resources to the SAME classloader as the host.  Makes it possible to add resources such as additional translations, new classes or any other files you want to the same classloader as the host JAR  By default a fragment cannot override existing resources, except if this was initially planned in the host bundle, by using specific class loader directives that reference resources inside a fragment.
    • 43. OSGi fragment patch example Host bundle METAINF/MANIFEST.MF Bundle-SymbolicName: the.host Bundle-ClassPath: patch.jar,. Fragment bundle METAINF/MANIFEST.MF Bundle-SymbolicName: the.host.fragment Fragment-Host: the.host In this scenario, the patch.jar is not available in the host bundle itself, but may be supplied by the fragment. Since it is ahead of '.' it will allow classes to be preferentially loaded from the fragment instead of the host.
    • 44. Bundle listeners and events  Bundle listeners are classes that are registered in the service registry and will be called by the OSGi framework when corresponding events are fired.  They listen to bundle events, that are fired when bundles change state  Be careful when developing listeners to test the complete lifecycle, including stopping and uninstalling bundles, as your code needs to handle all these cases for the system to be truly solid
    • 45. Bundle extenders  Makes it possible to build listeners that will perform certain tasks when bundles become available, such as scanning for specific resources to register them, or register additional services. These types of listeners are called "bundle extenders".  Jahia implements all of it's specific module registration as a bundle extender, by detecting CNDs, import files in modules and importing the content, etc...  You could of course build your own extenders for your applications.  An additional difficulty comes from the fact that since in OSGi you have no control in the order of startup, your extender might be started AFTER some of the bundle it will process have been started, so you must scan for already started bundles in your extender's startup code.
    • 46. Bundle extender example  Listen for bundle deployment/undeplo yment  Check if bundle has a WEB-INF/web.xml file  Register all servlets and filters declared in the bundle
    • 47. Whiteboard pattern  Source and listeners are 100% independent  Listeners register themselves in OSGi registry  Source call registry to dispatch to all listeners  Cleaner separation than JVM listener model
    • 48. Summary  Building Jahia modules that includes services can help you extend functionality while staying modular  It is possible to leverage existing services or find existing bundles to integrate  Highly flexible and dynamic modular system now available in Jahia
    • 49. Resources  Jahia Hello World module : https://github.com/Jahia/helloworld  Jahia Sample OSGi module : https://github.com/sergehuber/sample-osgimodule  OSGi specifications (core, compendium & EE)  Apache Felix (basic OSGi framework)  Apache Karaf (equivalent to Tomcat for OSGi)  Apache Aries (OSGi EE implementation)
    • 50. Thank you ! shuber@jahia.com #jahiaone
    • 51. Foo service declarative services (DS) declaration METAINF/MANIFEST.MF Service-Component: OSGI-INF/component.xml OSGI-INF/component.xml <?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="FooService"> <implementation class=“foo.sample.FooImpl”/> <service> <provide interface=“foo.sample.Foo”/> </service> </scr:component>
    • 52. Foo service DS consumer METAINF/MANIFEST.MF Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: FooServiceConsumer Bundle-SymbolicName: foo.sample.consumer.FooConsumer Bundle-Version: 1.0.4 Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Import-Package: foo.sample Service-Component: OSGI-INF/component.xml OSGI-INF/component.xml <?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name=“foo.sample.consumer.FooConsumer”> <implementation class=“foo.sample.consumer.FooConsumer”/> <reference bind="setFoo" cardinality="1..1" interface=“foo.sample.Foo” name="FooService" policy="static" unbind="unsetFoo"/> </scr:component>
    • 53. Foo service DS consumer package foo.sample.consumer; import foo.sample.Foo; public class FooConsumer { private Foo service; // Method will be used by DS to set the quote service public synchronized void setFoo(Foo service) { this.service = service; } // Method will be used by DS to unset the quote service public synchronized void unsetFoo(Foo service) { if (this.service == service) { this.service = null; } } }
    • 54. Jahia OSGi Request Flow
    • 55. Jahia OSGi Architecture
    • 56. Jahia Classloader Graph

    ×