Average Size Plugin
Calculates the average size of the entries that pass through the plugin and places that average in the context.
Coding the Plugin
The following AverageSizePlugin.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.Entry;
import org.blojsom.blog.Blog;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public class AverageSizePlugin implements Plugin {
public AverageSizePlugin() {
}
public void init() throws PluginException {
}
public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException {
int size = 0;
for (int i = 0; i < entries.length; i++) {
Entry entry = entries[i];
size += entry.getDescription().length();
}
context.put("AVERAGE_ENTRY_SIZE", new Integer(size / entries.length));
return entries;
}
public void cleanup() throws PluginException {
}
public void destroy() throws PluginException {
}
}
Configuring the Plugin
The compiled AverageSizePlugin.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="average-size" class="org.blojsom.plugin.example.AverageSizePlugin" 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 average placed into the context from the plugin.
#if ($AVERAGE_ENTRY_SIZE)
The average size of the entries on this page is: $AVERAGE_ENTRY_SIZE
#end
This simply checks to see that an average is available in the context and if so, prints the value.