This bundle plugin is an example of how to add dotCMS hook classes with our bundle plugin.
How to Create a Bundle Plugin for dotCMS Hook Classes
Let’s review the file organization on the Hooks (com.dotcms.hooks)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 (see template plugin- dotCMS/docs/examples/):
- Bundle-Name: The name of your bundle
- Bundle-SymbolicName: A short an unique name for the bundle
- Bundle-Activator: Package and name of your Activator class (example: com.dotmarketing.osgi.hooks.Activator)
- DynamicImport-Package: *Dynamically adds required imports the plugin may need without adding them explicitly
- Import-Package:This is a comma separated list of package names.List the packages that you are using insidethe bundle plugin and that are exported by dotCMS.
In order to work inside the Apache Felix OSGI runtime, the importand 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 also possible using the dotCMS UI (System → Dynamic Plugins → Exported Packages).
Only after exported packages are defined in this list, can a plugin Import the packages to use them inside the OSGI blundle.
Under the src folder you will find all the Java classes needed to create a new Hook:
$ ls -l
-rw-r–r– 1 Activator.java
-rw-r–r– 1 SamplePostContentHook.java
-rw-r–r– 1 SamplePreContentHook.java
Both of the above are hook classes that will override the contentletCount method.
This bundle activator extends from com.dotmarketing.osgi.GenericBundleActivator and implements BundleActivator.start().
The Activator calls ContentletAPI.contentletCount() who will fire theoverriddenhook methods.
After building and loading the com.dotcms.hooks dynamic plugin, you will find the following messages in the log: catalina.out if the bundle loaded correctly.
+++++++++++++++++++++++++++++++++++++++++++++++ INSIDE SamplePreContentHook.contentletCount() +++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++ INSIDE SamplePostContentHook.contentletCount() -->1142 +++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++ ContentletAPI.contentletCount() = 1142 ++++++++++++++++++++++++++++++++++++++++++++++