Third party libraries and OSGi - a complicated relationship

Preview:

Citation preview

Third-Party Libraries and OSGia complicated relationship

Senior Solution Arch

itect

6 years of WCMS expe

rience

Loves working with J

ava

(script)

Sascha Brinkmann

Hobbies: Biking, Hik

ing, Travelling

Contact: sb@eggs.de

@sasusu

About Me

About Us

About Us

1984 Foundation year2004 Adobe Business Partner2005 Adobe Authorized Training Center2010 Winner of Siemens top+ Award2014 100% Adobe focussed for 10 years

Employees: 45Located: Munich, GermanyHappy Adobe customers: +40

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

➢ OSGi Basics

Table of Content

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

Motivation

InternalExternal

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.

Motivation

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

Challenge

OSGi Basics

All rights reserved Gosainkunda, Nepal

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)

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

OSGi Basics - Classloading

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

BundleManifest

Consumes Provides

OSGi Basics - Bundle Structure

Bundle Plugin

Bundle Identifier

Packages provided to other bundles

Packages Consumed from other bundles

Is created by the during the build process.

OSGi Basics - Bundle Manifest

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

Apache Felix Bundle Plugin

Dependency toother Bundle

OSGi Basics - Maven POM Config

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

Strategies to use third party libraries

First Option

Embedding LibrariesSecond Option

Libraries as Bundles

First Option Embedding Libraries

All rights reserved Nanuku, Fiji

BundleManifest

First Option - Embedding Libraries

Same classloader for external libraries and bundle classes.

Libraries embeddedwithin the bundle

First Option - Embedding Libraries

Bundle Manifest

Dependency to be embedded with implicit scope ‘compile’

Instruction to embed all dependencies with scope

compile & runtime

First Option - Embedding Libraries

Maven POM snippet

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

First Option - Embedding Libraries

Transitive dependencies are not embedded

automatically!

First Option - Embedding Libraries

Maven Artifact Dependency Tree

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

… can cause an even larger number of unresolveable

packages.

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

First Option - Embedding Libraries

Further configuration parameters to adjust the generated manifest.

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.

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

First Option - Embedding Libraries

Second Option Libraries as Bundle

All rights reserved Hollywood, USA

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

Second Option - Libraries as Bundle

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

OSGi Basics - Bundles vs. Fragments

the bundle where the fragment is attached to

Bundle

FragmentHas 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

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.

Entitiesand DAO

JDBCDriverHibernate

Core Bundle

Consumes

EntityBridge Consumes

Exte

nds

ExtendsOSGi

Fragment

OSGiBundle

Second Option - Libraries as 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

OSGi Basics - System Bundle

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

System Bundle

Axis 2FragmentExtends

Entitiesand DAO

Consumes

Second Option - Libraries as Bundle

OSGiFragment

OSGiBundle

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.

Dealing with Class Loader Issues

➢ 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.

Conclusion

Your chance for remarks and questions!

All rights reserved Los Angeles, USA

The End

All rights reserved

Nadi, Fiji

Presentationhttp://www.eggs.de/connectwebex2014

The AuthorSascha Brinkmann - sb@eggs.de

Sales RepresentativeMartin Brösamle - mb@eggs.de

Get in Touch

Terms of Use

This work is licensed under a Creative Commons Attribution-NoDerivatives

4.0 International License

All photos copyright © Sascha Brinkmann, all rights reserved.

Recommended