1 of 38

Third-Party Libraries and OSGi

a complicated relationship

2 of 38

About Me

Senior Solution Architect

6 years of WCMS experience

Loves working with Java(script)

Hobbies: Biking, Hiking, Travelling

Contact: sb@eggs.de @sasusu

Sascha Brinkmann

3 of 38

About Us

4 of 38

About Us

1984 Foundation year

2004 Adobe Business Partner

2005 Adobe Authorized Training Center

2010 Winner of Siemens top+ Award

2014 100% Adobe focussed for 10 years

Employees: 45

Located: Munich, Germany

Happy Adobe customers: +40

5 of 38

Table of Content

  • Motivation
  • Challenge
  • Analysis Possible Strategies
  • Tips & Tricks
  • Conclusion

  • OSGi Basics

6 of 38

Motivation

Internal

External

Integration of AEM into a heterogeneous IT systems landscape with interfaces to various subsystems.

AEM

LDAP / Active Directory

Microsoft SharePoint

Microsoft Enterprise Search (FAST)

IBM Lotus Notes Domino

SAP / Hybris

SQL Database

Rest- and Webservices

Amazon S3

Facbook

Twitter

YouTube

Brightcove

Email

FTP

Google Search Appliance

7 of 38

Motivation

Our work is all about solving business problems. Solutions for almost every technical challenge are provided by third party libraries from the central maven repository.

8 of 38

Challenge

How can I use various types of third party libraries and frameworks within my projects?

9 of 38

OSGi Basics

All rights reserved

Gosainkunda, Nepal

10 of 38

OSGi Basics - Apache Felix

Apache Felix is a fundamental element of the technology stack of the Adobe Experience Manager (AEM). It provides the environment for all Java written components.

Java Content Repository (CRX)

Apache Sling

Adobe Experience Manager (WCMS)

OSGi Runtime (Apache Felix)

11 of 38

OSGi Basics - Classloading

one Classloader for the �whole Java application

one Classloader per OSGi bundle

Bundle B

Bundle A

Bundle C

Bundle D

Library A

Library C

Library B

Library D

Key difference between traditional Java Applications and Applications running within OSGi.

12 of 38

OSGi Basics - Bundle Structure

Bundle

Manifest

Consumes

Provides

13 of 38

OSGi Basics - Bundle Manifest

The manifest file is a set of OSGi specific headers. It defines how to deploy and run the OSGi bundle.

Bundle Plugin

Bundle Identifier

Packages provided to other bundles

Packages Consumed from other bundles

Is created by the

during the

build process.

14 of 38

OSGi Basics - Maven POM Config

The Apache Felix Bundle Plugin for Maven enhances artifacts with the bundle manifest.

Apache Felix Bundle Plugin

Dependency to

other Bundle

15 of 38

Strategies to use third party libraries

First Option

Embedding Libraries

Second Option

Libraries as Bundles

16 of 38

First Option Embedding Libraries

All rights reserved

Nanuku, Fiji

17 of 38

First Option - Embedding Libraries

Bundle

Manifest

Same classloader for external libraries and bundle classes.

18 of 38

First Option - Embedding Libraries

Bundle Manifest

Libraries embedded

within the bundle

19 of 38

First Option - Embedding Libraries

Maven POM snippet

Dependency to be embedded with implicit scope ‘compile’

Instruction to embed all dependencies with scope compile & runtime

20 of 38

First Option - Embedding Libraries

Apache Felix needs to resolve all dependencies prior to start the bundle.

21 of 38

First Option - Embedding Libraries

Maven Artifact Dependency Tree

Transitive dependencies are not embedded automatically!

22 of 38

First Option - Embedding Libraries

A large number of embedded libraries within your OSGi bundle ...

… can cause an even larger number of unresolveable packages.

23 of 38

First Option - Embedding Libraries

Additional dependencies to resolve the missing packages.

Instruction containing a list of packages which should be ignored by Apache Felix.

Instruction to embed transitive dependencies

Further configuration parameters to adjust the generated manifest.

24 of 38

First Option - Embedding Libraries

The result has to be that the bundle is deployed with no unresolved packages and activated successfully

Bundle is activated and ready for operation.

25 of 38

First Option - Embedding Libraries

Advantages

  • Easy to archive
  • Usage is encapsulated
  • Same classloader between class-files and library

Disadvantages

  • Usage is limited to bundle
  • No separation of concerns
  • Multiple usage in different bundles increase the memory consumption and the probability of conflicts when sharing references

26 of 38

Second Option Libraries as Bundle

All rights reserved

Hollywood, USA

27 of 38

Second Option - Libraries as Bundle

Transformation or encapsulation of Java libraries to turn them into OSGi bundles

Searching for existing OSGi bundles

End of availability after September 2014

Just kicked off

Successor

Commandline Tool

Bundle Plugin

28 of 38

Second Option - Libraries as Bundle

Advantages

  • Functionality can be shared across multiple bundles
  • Separation of concerns
  • Bundles can be updated independently
  • Class reference conflicts can be avoided, as there is only one shared source

Disadvantages

  • Most libraries are not available as OSGi bundles
  • Dynamic class resolving via Class.forName() or reflection is difficult

29 of 38

OSGi Basics - Bundles vs. Fragments

the bundle where the fragment is attached to

Bundle

Fragment

Has its own classloader and lifecycle. Can be referenced by other bundles.

Is part of another Bundle and utilizes its classloader. Extends the imports and exports of the host bundle.

Fragment Manifest

30 of 38

Second Option - Libraries as Bundle

Hibernate ORM is an object-relational mapping library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database.

Entities

and DAO

JDBC

Driver

Hibernate

Core Bundle

Consumes

Entity

Bridge

Consumes

Extends

Extends

OSGi

Fragment

OSGi

Bundle

31 of 38

OSGi Basics - System Bundle

The system bundle is a special bundle that represents the OSGi framework itself, from within that framework.

Characteristics

  • Bundle ID 0
  • It exports packages from the JRE
  • Only packages which are available in any JVM are provided. All java* packages for example javax.swing, org.w3c.com etc. are excluded
  • Exports the OSGi framework packages such as org.osgi.framework

32 of 38

Second Option - Libraries as Bundle

Apache Axis2 is a Web Services / SOAP / WSDL engine and the successor to the widely used Apache Axis SOAP stack.

System Bundle

Axis 2

Fragment

Extends

Entities

and DAO

Consumes

OSGi

Fragment

OSGi

Bundle

33 of 38

Dealing with Class Loader Issues

Some third party libraries make us of the current thread classloader that definitely runs within another context than the bundle class loader. To allow the third party library to access bundle resources, the current thread classloader has to be replaced for this action.

34 of 38

Conclusion

  • There is no best practice. Each framework needs its individual solution!
  • Working with third party libraries is like a box of chocolates - you never know what you're gonna get.
  • You know it’s working when you see it’s working!
  • Most frameworks don’t support OSGi or are not properly tested with Apache Felix.
  • It needs a community with support from Adobe sharing experiences and best practices including the use of third party libraries.

35 of 38

Your chance for remarks and questions!

All rights reserved

Los Angeles, USA

36 of 38

The End

All rights reserved

Nadi, Fiji

37 of 38

Get in Touch

Presentation

http://www.eggs.de/connectwebex2014

The Author

Sascha Brinkmann - sb@eggs.de

Sales Representative

Martin Brösamle - mb@eggs.de

38 of 38

Terms of Use

All photos copyright © Sascha Brinkmann, all rights reserved.