Magnolia's module mechanism facilitates smoother version changes. The idea is that an upgrade or update should be as easy as replacing a module JAR. Magnolia uses the version handler to determine which tasks should be executed. On each startup of Magnolia the version handler provides a list of deltas.
The ModuleVersionHandler interface is a special class which contains a series of tasks (deltas) which are executed during installation or during specific updates. If your module needs to handle its own installation and updates you should provide an implementation of this interface.
Delta is a list of Tasks to be executed during a version change. The sample module's version handler provides three example deltas.
Delta (from Version)
More precisely, a
Delta can store and apply an additional set of
Conditions to be executed only when a version update or revision is deployed. The
Tasks of a
Delta will only be executed if all specified
Conditions are met. We examine
Conditions in more detail later.
Abstract version handler classes
To create your own version handler you do not have to start from scratch. There are some abstract classes for common cases which can be used. Good practice is to extend one these base classes and implement only the methods you need. There are two abstract version handler classes in the package
info.magnolia.module which can be used to create a custom version handler.
Extend this class and register your deltas in the constructor using the
If you do not specify a version handler in the module descriptor,
DefaultModuleVersionHandler is used and performs basic install tasks:
Bootstraps empty repositories defined in the module descriptor, grants them to the superuser and subscribes them so that activation can be used.
Bootstraps the necessary module repository content which is provided as multiple XML-export files under /mgnl-bootstrap/moduleName".
Bootstraps the module's sample repository content which is provided as multiple XML-export files under "/mgnl-bootstrap-samples/moduleName".
Copies all files under
Registers the necessary servlets for the module.
Task is a lightweight class with the minimal necessary code to augment configuration during module installation. The important method in the
Taskshould execute responsibly and respond to issues appropriately.
- To allow developers/users to fix issues at a later time, fixable or irrelevant issues should be logged and standard
Taskshould be in place to perform backups of nodes when extensive modifications are performed, meaning that a user can refer to a pre-alteration copy.
- In the event of an unrecoverable issue, a
Taskshould automatically perform a
TaskExecutionExceptionto interrupt and cancel the module installation, update and startup. If a
TaskExecutionExceptionis thrown, the exception message should be shown to the end user.
- Exception messages should be simple and intuitive.
A set of predefined and abstract
Tasks is available in the
info.magnolia.module.delta package which can be used. Here are some of the most useful:
|AbstractTask||Abstract implementation of the |
|AbstractRepositoryTask||An abstract implementation of |
|AbstractConditionalRepositoryTask||An abstract implementation of a |
|AllChildrenNodesOperation||Executes the abstract method on every child node.|
|AllModulesNodeOperation||Abstract that performs an operation on all modules node found in the configuration repository.|
Also interesting are delegate
|ArrayDelegateTask||A task that simply delegates to an array of other tasks.|
|ConditionalDelegateTask||A task that delegates to another if a condition is true, or to an optional other if it is false.|
|PropertyExistsDelegateTask||A task that delegates to another depending on whether a specified property exists or not.|
|PropertyValueDelegateTask||A task which delegates to another if a property has a given value.|
For the complete list of
Tasks, see the
It is customary for modules to expose some tasks that can be re-used by other modules when needed, such as
. The API is designed so that it should be easy for you to write your own specific
Conditions are checked prior to the installation or update of a module. They check for system configuration which can't be automatically updated, like configuration, dependencies, etc. Modules register their conditions like their tasks, for each successive version. Only if all conditions in delta evaluate positively will the tasks of the delta be executed. The most important method in the Condition interface is:
Node builder API
The NodeBuilder API is commonly used in update tasks.
Here's a snippet from STKModuleVersionHandler that demonstrates its use:
Properties in the module descriptor can be used to define values for install tasks. You can get the current module descriptor from the
The properties defined in the module descriptor will be included in the Magnolia system properties too, so you might want to use the
SystemProperty class instead: