What are Plugins?
Plugins allow developers to add to the capabilities of blojsom in a well-defined way. Plugins can be responsible for changing how an entry is displayed, adding or removing information to an entry, making information or objects available to the templates, etc. You are not limited to what you can do with plugins. In fact, the web-based administration console was developed as a series of plugins.
You may also be interested in the plugin API examples.
Plugin API Methods
The blojsom plugin API was designed to be very straightforward. A developer that wants to write a plugin for blojsom will implement the org.blojsom.plugin.Plugin interface. This interface defines 4 methods that must be overridden:
- init(): This method is called once when the plugin class is instantiated by blojsom.
- process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries): After retrieving the blog entries for a request, blojsom will execute the plugins specified for the flavor of the request. The process() method is where the plugin can, for example, manipulate the individual blog entries.
- cleanup(): The cleanup() method is called directly after the process method and is provided so that developers can perform any cleanup after processing a plugin request.
- destroy(): The destroy() method is called for each plugin when BlojsomServlet is taken out of service by the servlet container.
As a developer, you must ensure that any access to class member variables is thread-safe.
If you are considering writing a plugin, take a look at the source code for any of the individual plugins.
Plugin Context
The plugin API's process method contains a Map context object. This object allows you to pass data between plugins or to pass data or objects that can be used in the template. As the context is a placeholder for any key/value pair, you can make available any object. Keys used to retrieve and set values in the context must be String objects. Before templates are processed, the various template dispatchers take the key/value pairs from the context and make them available to the templates.
If you are using the context to pass data between plugins, be aware that plugins are executed in the order specified in the plugin configuration.