Hello World Plugin
This plugin will add the string, "Hello world", to the context so that you can print out that string in your templates.
Coding the Plugin
The following HelloWorldPlugin.java file should be placed in a package called org.blojsom.plugin.example and compiled.
package org.blojsom.plugin.example;
import org.blojsom.plugin.Plugin;
import org.blojsom.plugin.PluginException;
import org.blojsom.blog.Blog;
import org.blojsom.blog.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public class HelloWorldPlugin implements Plugin {
public HelloWorldPlugin() {
}
public void init() throws PluginException {
}
public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException {
context.put("HELLO_WORLD", "Hello world");
return entries;
}
public void cleanup() throws PluginException {
}
public void destroy() throws PluginException {
}
}
You'll notice that in the plugin's process method, we add the "Hello world" string under the key, HELLO_WORLD. It's as easy as that!
Configuring the Plugin
The compiled HelloWorldPlugin.class should be placed in blojsom's /WEB-INF/classes directory keeping the org/blojsom/plugin/example directory structure.
Add the following to blojsom's plugin configuration file. /WEB-INF/classes/blojsom-plugins.xml.
<bean id="hello-world" class="org.blojsom.plugin.example.HelloWorldPlugin" init-method="init" destroy-method="destroy"/>
After starting your server, you can login to blojsom's web administration console and add this plugin to run in one of your plugin chains. This is configured under Plugins | Mappings. As this plugin doesn't depend on any other plugins, you can add it anywhere in the plugin chain.
Using the Plugin
In your template, for example using Velocity, you can now print out the string placed into the context from the plugin.
My plugin says: $HELLO_WORLD
Congratulations! You have just written your first plugin for blojsom.