jahiaone 2015 - real world osgi modules by serge huber and julian maurel

27
Real World OSGi Modules Serge Huber ([email protected]) Julian Maurel ([email protected]) © 2002 - 2015 Jahia Solutions Group SA

Upload: jahia-solutions-group

Post on 31-Jul-2015

57 views

Category:

Technology


0 download

TRANSCRIPT

Real World OSGi ModulesSerge Huber ([email protected])Julian Maurel ([email protected])

© 2002 - 2015 Jahia Solutions Group SA

© 2002 - 2015 Jahia Solutions Group SA

What’s in this presentation

- What is OSGi- Why it’s important- How you can use it smoothly- What are the key features- How you can contribute

© 2002 - 2015 Jahia Solutions Group SA

OSGi in a few words« The Open Services Gateway Initiative (OSGi) allows you to break your application into multiple modules and thus more easily manage cross-dependencies between them. »

« An OSGi container must implement a contract between the container and your application »

© 2002 - 2015 Jahia Solutions Group SA

What’s important about OSGi

This is not a list of the key features of the framework, but a list of what’s important to know when working on a Jahia Module with OSGi.

© 2002 - 2015 Jahia Solutions Group SA

What’s important about OSGi - (1/5)

Modules States : Helps you understand in what state your module is once deployed on Digital Factory.

© 2002 - 2015 Jahia Solutions Group SA

What’s important about OSGi – (2/5)

Hot deployment : Each module is a simple bundle designed to handle a separate part of the functionality. It can be plugged and unplugged on the fly.

A module developer has to be aware that his module might be deactivated at any time.

© 2002 - 2015 Jahia Solutions Group SA

What’s important about OSGi – (3/5)

Module Versioning: Have several versions of a module deployed at the same time, and switch to another version in one click.

© 2002 - 2015 Jahia Solutions Group SA

What’s important about OSGi – (4/5)

Import/Export package: Choose which java packages are exposed to other modules, and which are not.

<Export-Package> org.jahia.modules.ldap, org.jahia.modules.UserProvider</Export-Package>

© 2002 - 2015 Jahia Solutions Group SA

What’s important about OSGi – (5/5)

Debugging tools: OSGi Console, gogo.shell, Maven Plugins

© 2002 - 2015 Jahia Solutions Group SA

Maven Jahia Plugin

Will try to do most of the work for you, scanning resources such as : Drools rule files Spring descriptors JSP files & taglibs JCR XML import files

… and more but it can still miss some dependencies!

© 2002 - 2015 Jahia Solutions Group SA

Maven Jahia Plugin

Much improved in Digital Factory 7.0.0.5 Detects more dependencies in more resources Classloading issues have been fixed in DF

server, making usage of maven-shade-plugin less necessary

New check-dependencies goal that will validate the automatic scanning performed by BND (by marking all dependencies as optional)

© 2002 - 2015 Jahia Solutions Group SA

Common problems Most problems are classloader problems

Libraries using ServiceLoaders Libraries scanning the classpath for resources Libraries with optional dependencies detected as strong

dependencies (plugin error) Libraries that use code generators (CGLIB)

Libraries partially exposed by the Digital Factory OSGi framework (Spring, Jahia Taglibs)

Automated (BND) scanning not smart enough

© 2002 - 2015 Jahia Solutions Group SA

Example : using Spring annotations

Goal : use Spring annotations for configuration:

<?xml version="1.0" encoding="UTF-8"?><beans>

<context:annotation-config/> <context:spring-configured/> <context:component-scan base-package="com.jahia.osgi.test"/>

</beans>

© 2002 - 2015 Jahia Solutions Group SA

Example : using Spring annotations

package com.jahia.osgi.test;

@Configurationpublic class Config {

private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);

@Bean public Test test(){ LOGGER.info("Creating bean test"); return new Test(); }

public static class Test {

}

}

© 2002 - 2015 Jahia Solutions Group SA

Project dependencies (pom.xml)

<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.8.RELEASE</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.hateoas</groupId> <artifactId>spring-hateoas</artifactId> <version>0.16.0.RELEASE</version> </dependency></dependencies>

© 2002 - 2015 Jahia Solutions Group SA

- We notice :- Embedded-Artifacts will tell us

which libraries where embedded- Code generation library (cglib) is

used- Code generation generates

dynamic imports- BND & Maven plugins cannot

scan at compilation for such imports

- Imports must be specified manually

<Import-Package> org.springframework.cglib.core, ${jahia.plugin.projectPackageImport}, *</Import-Package>

Using mvn jahia:osgi-inspect

© 2002 - 2015 Jahia Solutions Group SA

In the real world Not all dependencies can be automatically

detected (especially with large/complex libraries)

Setting up proper imports will require some deploy/test cycles, but should stabilize quickly

Ask us or others if you need help !

© 2002 - 2015 Jahia Solutions Group SA

OSGi technologies for modules

• Modules can do a lot more thanks to OSGi• Let’s look at two examples:

• Consuming and Providing new Services• Bundle Listeners (and related patterns)

© 2002 - 2015 Jahia Solutions Group SA

OSGi Services

OSGi framework contains a centralized registry of all available services

Any class can be registered as a service, and then use from any bundle

Service register either using their class or using any interface it implements

OSGi provides lots of built-in services !

© 2002 - 2015 Jahia Solutions Group SA

Accessing a service using Spring

<osgi:reference id="httpService" interface="org.osgi.service.http.HttpService" />

<bean id="myBean" class="org.jahia.osgi.examples.MyBean"> <property name="httpService" ref="httpService" /></bean>

The HttpService is built-into the OSGi framework, and makes it possible to register servlets and static resources

© 2002 - 2015 Jahia Solutions Group SA

Exposing a service using Spring

<bean id="myBean" class="org.jahia.osgi.examples.MyBean"> <property name="httpService" ref="httpService" /></bean>

<osgi:service id="myOSGiBeanService" ref=”myBean” />

Now any code in any bundle accessing the OSGi service registry can retrieve the myBean instance by looking it up using on of it’s interfaces

© 2002 - 2015 Jahia Solutions Group SA

Bundle listeners Make it possible to execute code when a bundle is deployed/started/stopped/undeployed

Very useful to scan the deployed bundle for resources that can be processed Example : pax-web is a bundle listener that scans bundles for WEB-INF/web.xml files and then sets up a web application !

© 2002 - 2015 Jahia Solutions Group SA

Your own Bundle Listenerpublic class Activator implements InitializingBean, BundleContextAware, DisposableBean, BundleListener {

BundleContext bundleContext;

@Overridepublic void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext;}

@Overridepublic void afterPropertiesSet() throws Exception { bundleContext.addBundleListener(this)}

public void destroy() throws Exception { bundleContext.removeBundleListener(this)}

public void bundleChanged(BundleEvent event) { final Bundle bundle = event.getBundle(); switch (event.getType()) { case BundleEvent.STARTED : // process bundle resources break; case BundleEvent.STOPPED : // unprocess bundle resources break; }}

}

In Spring deployment descriptor :

<bean id="activatorBean" class="org.jahia.osgi.examples.Activator">

© 2002 - 2015 Jahia Solutions Group SA

How you can help • Find missing extension points in Digital Factory• Either:

• Report them• Patch the code and submit the modification for review

& inclusion• Tell us about your integration problems, we will use the

feedback to improve/build tools

© 2002 - 2015 Jahia Solutions Group SA

Missing view extension example

• Script languages : adding Thymeleaf support• Missing extensions points : - Extension registration (.xhtml)- ScriptEngineManager can’t find new engines from

modules• Found a workaround but created a ticket to add support to

work without workaround• Code available at :

https://github.com/Jahia/scriptlanguages-thymeleaf

© 2002 - 2015 Jahia Solutions Group SA

Q & A

© 2002 - 2015 Jahia Solutions Group SA

Thank you !

Serge Huber, [email protected] Maurel, [email protected]