March 21, 2020
Estimated Post Reading Time ~

Overview on AEM/Adobe CQ5 OSGi Bundles

OSGi Bundles:
Open Services Gateway Initiative (OSGi) helps to develop and deploying modular applications and libraries in Java.Some of the major OSGi container implementations are Equinox, Knopflerfish, and Apache Felix. OSGi works on a set of services that an OSGi container must implement and a contract between the container and your application.
Resource folder can be used to put resource files like configuration files, XML files properties files, etc.

Metadata of bundles:
· Export-Package: *
Tells to export all packages to cq - makes available to cq
If we mention,
Export-Package: com.cq.test; it just makes this package available

· Import-Package: *
Tells to import all packages from other bundles to our bundle - makes available to use those classes.
· Private-Package: Packages that should not be exposed to the OSGi container
· Include-Resource: Used to include resource files like properties files available to our bundle.

Bundle-Activator Class:
Start() and stop() are the lifecycle of bundle activator class.
stop() can be implemented if any resource to be stopped if our bundle failure happens abruptly.
public void stop(BundleContext context) throws Exception

OSGi Vs Jar
OSGi can be told as an advanced version of JAR. It uses more metadata to show/hide the visibility of classes in a bundle. JAR is a private collection of classes where OSGI provides a safe way to make the classes available across.
Sample to create an AEM /Adobe CQ bundle and use it in a page

Create the folder structure below.


Create a bundle by right-clicking on src folder. Enter the details as required.



Now the new bundle will have below the structure.


Now under the test folder create an impl folder. This is used to write our custom Java class.
Now add TestService.class with a common output as below.

packagecom.cq.adobe.test.impl;

public class TestService{
public String getTestService(){
return "CQ developer training sessions";
}
}


Right-click com.adobe.test.bnd and build bundle (Tools>Build Bundle)
This creates a jar file under the install folder which is automatically placed to OSGi packages.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.