Dashboard > blojsom 3.0 > ... > Plugin API Examples > Addition Plugin Example
  blojsom 3.0 Log In   View a printable version of the current page.  
  Addition Plugin Example
Added by David Czarnecki, last edited by David Czarnecki on Dec 02, 2006  (view change)
Labels: 

Addition Plugin

This plugin will add two request parameters together and place their sum in the context.

Coding the Plugin

The following AdditionPlugin.java file should be placed in a package called org.blojsom.plugin.example and compiled.

org.blojsom.plugin.example.AdditionPlugin
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 org.blojsom.util.BlojsomUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class AdditionPlugin implements Plugin {

    public AdditionPlugin() {
    }

    public void init() throws PluginException {
    }

    public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException {
        String num1 = BlojsomUtils.getRequestValue("num1", httpServletRequest);
        String num2 = BlojsomUtils.getRequestValue("num2", httpServletRequest);

        if (!BlojsomUtils.checkNullOrBlank(num1) && !BlojsomUtils.checkNullOrBlank(num2)) {
            context.put("num1", num1);
            context.put("num2", num2);
            
            int sum = Integer.parseInt(num1) + Integer.parseInt(num2);

            context.put("SUM", new Integer(sum));
        }

        return entries;
    }

    public void cleanup() throws PluginException {
    }

    public void destroy() throws PluginException {
    }
}

Configuring the Plugin

The compiled AdditionPlugin.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="addition" class="org.blojsom.plugin.example.AdditionPlugin" 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 sum placed into the context from the plugin.

You could test this using request parameters num1 and num2 in the URL when requesting your blog, such as http://your.host.com/blojsom/blog/default/?num1=4&num2=6.

#if ($SUM)
I added $num1 and $num2 to get: $SUM
#end

This simply checks to see that a sum is available in the context and if so, prints the values and the sum.

Site running on a free Atlassian Confluence Open Source Project License granted to blojsom. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators