OSGi Services Plugin
This bundle plugin is an example of how to add a simple service class that can be used as a proxy by other plugins inside the Felix OSGI container (dotCMS/felix/load)
How to Create a Bundle Plugin with Services#
Let’s review the file organization on the Service (com.dotcms.service) example:
META-INF/MANIFEST.MF: The manifest file is very important for the deployment, it lists the Bundle Name, Version and Packages.
In this MANIFEST you must specify the following (see template plugin- dotCMS/docs/examples/):
- Bundle-Name: The name of your bundle
- Bundle-SymbolicName: A short and UNIQUE name for the bundle
- Bundle-Activator: Package and name of your Activator class (example: com.dotmarketing.osgi.service.Activator)
- Export-Package: This is a comma separated list of package names. List the packages that you want to make available to other bundles.
- Import-Package: This is a comma separated list of package names. List the packages that you are using inside the bundle plugin and that are exported by dotCMS.
$ more META-INF/MANIFEST.MF Manifest-Version: 1.0 Bundle-Name: Custom service Bundle-SymbolicName: com.dotmarketing.osgi.service Bundle-Description: A bundle that creates a custom service Bundle-Version: 1.0.0 Bundle-Activator: com.dotmarketing.osgi.service.Activator Export-Package: com.dotmarketing.osgi.service Import-Package: org.osgi.framework, com.dotmarketing.osgi
Beware!
In order to work inside the Apache Felix OSGI runtime, the import and export directive must be bi-directional.
dotCMS must declare the set of packages that will be available to the OSGI plugins by changing the file: dotCMS/WEB-INF/felix/osgi-extra.conf.
This is possible also using the dotCMS UI (System -> Dynamic Plugins -> Exported Packages).
Only after exported packages are defined in this list, can a plugin then Import the packages to use them inside the OSGI bundle.
Source#
Under the src folder you will find all the Java classes needed to create a new Service:
$ pwd dotCMS/docs/examples/osgi/com.dotcms.service/src/com/dotmarketing/osgi/service $ ls -l -rw-r--r-- 1 Activator.java -rw-r--r-- 1 HelloWorld.java -rw-r--r-- 1 HelloWorldService.java
Exposed service#
The exposed service for this example will be a simple Interface class with its implementation (com.dotmarketing.osgi.service.HelloWorld AND com.dotmarketing.osgi.service.HelloWorldService).
Activator#
This bundle activator extends from com.dotmarketing.osgi.GenericBundleActivator and implements BundleActivator.start.
It registers an instance of a our test service using the bundle context; and attaches properties to the service that can be queried when performing a service look-up.
The newly registered service will be used in the example: com.dotcms.servlet.