Expand my Community achievements bar.

SOLVED

OSGI third party libraries

Avatar

Level 2

This might be a dumb question. Why OSGI doesn't support using third party directly? If my bundle requires a non-osgi dependency, I got options to use maven Felix maven-bundle-plugin (as import resource) or just add as separate bundle via the install folder(any other option?). What I don't understand is when we build the project with maven, the libraries have already been added to the target jar and should be available in class path in run time. Why OSGI can't handle the rest or should even bother to do those additional bundlsation and import?

This is my understanding. Please correct me if I am wrong anywhere.

1 Accepted Solution

Avatar

Correct answer by
Level 10

When you build a custom AEM service and you reference a Java library that is not part of AEM service layer - you can successfully build that bundle in Maven by specifying the correct POM dependencies. However - just because you can build it - does not mean it will run within AEM if those libs are missing from AEM.  They need to be exported from an existing OSGi bundle.

Say you want your service to use Class A in Package X (assuming this is not already part of AEM). You need to take the JAR that contains Package X and wrap into an OSGi bundle. Then you need to deploy that OSGi bundle to AEM and place it in an Active State.

In the MF file, make sure that Package X is specified in the Export section. Once you place that bundle into an Active State, then all OSGi bundles in AEM can use methods exposed by Class A.

To see a real example of this - see how we wrapped the Simple JSON JAR file in this article -

Scott's Digital Community: Submitting Adobe Experience Manager form data to custom Sling Servlets

I personally like using Eclipse Plug-in project to produce the OSGi bundle that contains a JAR.

View solution in original post

5 Replies

Avatar

Employee Advisor

Simple JAR do not work inside OSGI, they require certain additional headers, for example to indicate the packages they export. If you want to use a thirdparty library, which is not available in an OSGI version, you can try to embed it into your own bundle; when you use the maven-bundle-plugin for it, it adds the correct headers so that this embedded library is added to the classpath of the bundle. In that case this embedded lib can only be used by the bundle embedding it; it is not visible to other OSGI bundles.

Does that help?

Jörg

Avatar

Correct answer by
Level 10

When you build a custom AEM service and you reference a Java library that is not part of AEM service layer - you can successfully build that bundle in Maven by specifying the correct POM dependencies. However - just because you can build it - does not mean it will run within AEM if those libs are missing from AEM.  They need to be exported from an existing OSGi bundle.

Say you want your service to use Class A in Package X (assuming this is not already part of AEM). You need to take the JAR that contains Package X and wrap into an OSGi bundle. Then you need to deploy that OSGi bundle to AEM and place it in an Active State.

In the MF file, make sure that Package X is specified in the Export section. Once you place that bundle into an Active State, then all OSGi bundles in AEM can use methods exposed by Class A.

To see a real example of this - see how we wrapped the Simple JSON JAR file in this article -

Scott's Digital Community: Submitting Adobe Experience Manager form data to custom Sling Servlets

I personally like using Eclipse Plug-in project to produce the OSGi bundle that contains a JAR.

Avatar

Level 2

Thanks. This is very helpful.  Two more small questions to your reply. As you mentioned,

"In the MF file, make sure that Package X is specified in the Export section. Once you place that bundle into an Active State, then all OSGi bundles in AEM can use methods exposed by Class A."

I have seen some examples that in Package X would also specified in the import section in the POM file (guess this is what MF your refer? ) of the bundle which requires it. Should it mean optional?

As I can find, putting the jars into the install folder in app will create new bundles of each jar, which also works. Which is better in practice?

Many thanks

Avatar

Administrator

Request you to create separate questions for the followup question to help us track them better!!
-Kautuk



Kautuk Sahni