Bug Initializable Plugin Not Initialized
As of right now, I've been unable to make the InitializablePlugin functionality work. Check the bug above for latest info. /ErikAlm, 09-Aug-2005

Sometimes you may wish to run parts of your plug-in code only once per start up. To do this you implement the interface InitializablePlugin as well as the WikiPlugin interface. The function the wiki will call upon startup looks like this (also see the source code):

    public void initialize( WikiContext context, Map params )
        throws PluginException;
This method is called once per WikiEngine start-up (and I believe there is one WikiEngine start-up per web-app and application server)...
As a side note, if you feel that you need to perform code when the server shuts down you can register a shutdown hook (nope, finalize is not a good way to perform last minute shutdown code).

Example#

There may be several reasons why you would want code to be run only once. For example, expensive resources used by the plugin (files read from disk and cached in memory, data collected via the net), initial caching/lookup handling, class wide field values (for instance used for caching) etc.

Here is an abstract example of a plugin that stores variables from the wiki-properties file as static class variables:

/*
 * InitPlugin.java
 *
 */

package test;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.plugin.InitializablePlugin;
import com.ecyrd.jspwiki.plugin.PluginException;
import com.ecyrd.jspwiki.plugin.WikiPlugin;
import java.util.Map;
import java.util.Properties;

/** A simple test plugin that reads some data from the WikiProperties file and
 *  stores them in static class variables.
 *
 * @author erk
 */
public class InitPlugin implements WikiPlugin, InitializablePlugin {
    
    private static String somevalue;
    private static String anothervalue;
    
    /** Run only once per WikiEngine start-up.
     *  
     *  This means if the 
     *  getWikiProperties() call is expensive, we'd minimize strain on the 
     *  server.  On the other hand, by storing the values in static variables,
     *  they will not change unless the WikiEngine is restarted (probably by
     *  restarting the application server).
     */
    public void initialize(WikiContext wikiContext, Map map) 
            throws PluginException 
    {
        // get WikiProperties
        Properties wprop = wikiContext.getEngine().getWikiProperties();
        
        /*  Read values.
         *  In order to set these values, add the following lines to the
         *  jspwiki.properties file:
         *  somevalue = ...
         *  anothervalue = ...
         */
        somevalue = wprop.getProperty("somevalue", "undefined");
        anothervalue = wprop.getProperty("anothervalue", "undefined");
    }

    /** Run once per plugin access/view */
    public String execute(WikiContext wikiContext, Map map) 
            throws PluginException 
    {
        return "somevalue = " + somevalue + "<br/>anothervalue = " + 
               anothervalue;
    }
}

Source Code#

Here is the source for the InitializablePlugin directly from 2.2.27 source tree:

package com.ecyrd.jspwiki.plugin;

import java.util.Map;
import com.ecyrd.jspwiki.WikiContext;

/**
 *  If a plugin defines this interface, it is called during JSPWiki
 *  initialization, if it occurs on a page.
 *
 *  @since 2.2
 *
 *  @author Janne Jalkanen
 */
public interface InitializablePlugin
{
    /**
     *  The initialization routine.  The context is to a Wiki page,
     *  and the parameters are exactly like in the execute() -routine.
     *  However, this routine is not expected to return anything,
     *  as any output will be discarded. 
     */

    public void initialize( WikiContext context, Map params )
        throws PluginException;
}

Classification: ...
Originally by: Erik Alm (23 Jun 2005, 6 Aug 2005)

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-5) was last changed on 02-Oct-2005 08:43 by ErikAlm