Our Blog

OSGI Bundles

Welcome to part 2, in this part we will get familiar with OSGI Bundles.

After reading this article you should be able to

  • Describe what OSGI bundles are and what problem they solve
  • Build and OSGI bundle and deploy it to AEM

Bundles – First OSGI Layer

Bundles are OSGI modules i.e. the lower architectural layer of OSGI. Bundles run in OSGI environment i.e. how the container is called in OSGI environment.

OSGI Modules – Problems Addressed

Let’s see what problems bundles solve.

The problems that are common to non-modular Java applications or you can say rather one problem describes in 3 different ways

An ordinary Java application has a so called Flat classpath that means if we specify a library in the classpath its public classes are visible to all the other classes in the application that means 2 things.

  • First– It is not possible to hide a package or public class from other application modules, there is no way to prevent using for eg. A low level persistence class in the presentation layer that can lead to spaghetti code. The code with chaotic dependencies between classes that are hard to sort out. Bundles solve that by controlling the package visibility.
  • Second– It is not possible to use two different version of library, which can be the case in complex projects. The application cannot use two different versions of a class, when there are two versions of a class it will use one that is loaded first – Bundles solve that problem – One bundle can have version one of some class, another bundle can have version two of that same class and they both run at the same time.

OSGI Bundles

So, the OSGI approach to solving the problem that we just described is using bundle.

  • A Bundle is a set of classes with additional descriptors.
  • The point is that each bundle is loaded by a separate class loader and the bundle classes are not visible outside the bundle by default.
  • A bundle is packed and deployed as a JAR file with an additional descriptor.
  • That descriptor explicitly lists the packages that are visible outside the bundle i.e. Exported outside. Classes in the other packages are not visible outside even if they are public. Also the bundle descriptor explicitly lists the packages used by the bundle – that is one that are imported, as you have probably realized by now it gives the developers one more way to ensure the application modularity – you have modules and for each module you specify what it makes visible outside and what packages outside it uses.

Bundles in AEM Applications

Now let’s get to the AEM specifics. In AEM projects bundles are build using Maven Bundle Plugin that plugin generates the OSGI-specific information and places it in the JAR file.

AEM use bundles as part of AEM application that mean that the bundle is expected to be located in the application structure in /install folder of the application

Summary• Bundles are OSGI modules. Physically, they are JAR files with additional descriptors
• Each bundle’s classes are loaded using a separate classloader
• A bundle is identified by the symbolic name and version
• In AEM, bundles are part of AEM applications
• Bundles are usually built using Maven Bundle plugin
• Bundles are installed in packages, under “/install” folder
• AEM allows only one version of a bundle at a time
• AEM has several consoles for working with bundles

In the next part we will take a look at Package Import and Export in OSGI bundles. Click here to visit Page!