Dashboard > WebWork 1 > WebWork CookBook > Controlling WebWork from within your application
  WebWork 1 Log In View a printable version of the current page.  
  Controlling WebWork from within your application
Added by Scott Farquhar, last edited by Scott Farquhar on Jan 16, 2005
Labels: 
(None)

If you are building very dynamic web applications (as we do), then you need to be able to control some of WebWork's properties from within your application.

There are two issues - controlling basic properties from within your application, and also adding actions at runtime.

Basic Properties

As of CVS Head (17th Jan 05), you can specify a list of configuration classes that WebWork will contact for properties. You need to implement the 'ConfigurationInterface' interface.

ConfigurationInterface
public interface ConfigurationInterface
{
    Object getImpl(String aName) throws IllegalArgumentException;

    void setImpl(String aName, Object aValue) throws IllegalArgumentException, UnsupportedOperationException;

    Iterator listImpl();
}

The key with implementing this interface is - you must throw an IllegalArgumentException if getImpl() does not find any value. You cannot just return null.

A simple implementation that looks up your own code for properties could look like this:

ApplicationPropertiesConfiguration.java
public class ApplicationPropertiesConfiguration implements ConfigurationInterface
{
    public Object getImpl(String aName) throws IllegalArgumentException
    {
        Object setting = ManagerFactory.getApplicationProperties().getString(aName);
        if (setting == null)
            throw new IllegalArgumentException("No such setting:" + aName);
        return setting;
    }

    public void setImpl(String aName, Object aValue) throws IllegalArgumentException, UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot set Application Properties through this interface");
    }

    public Iterator listImpl()
    {
        return ManagerFactory.getApplicationProperties().getKeys().iterator();
    }
}

Then - to set this in your code - edit webwork.properties, and add the following:

webwork.properties
webwork.configuration.class=com.atlassian.jira.config.webwork.ApplicationPropertiesConfiguration

Actions

Although you can add actions by just returning the correct properties from the above configuration, you probably want to use the built in XML parsing that WebWork gives to you.

As an example - in JIRA, we allow a webwork plugin type. The XML that we require looks something like this:

webwork-plugin.xml
<webwork key="webwork-test" name="Test webwork plugin" >
        <actions>
            <action name="JiraWebActionSupport" alias="Administrators">
                <view name="success">/views/administrators.jsp</view>
            </action>
        </actions>
    </webwork>

Here is some example code that uses WebWork's XML parser to then load the configuration:

JIRAWebworkXMLConfiguration.java
public class JIRAWebworkXMLConfiguration implements ConfigurationInterface
{
    private XMLConfigurationReader configurationReader;

    public void init(Plugin plugin, Element element) throws PluginParseException
    {
        super.init(plugin, element);
        try
        {
            electric.xml.Element root = new Document(element.asXML()).getRoot(); //hackiness needed to convert dom4j element to w3c element
            this.configurationReader = new XMLConfigurationReader((org.w3c.dom.Element) root.getFirstChild());
        }
        catch (Throwable e)
        {
            log.error("Could not parse webwork plugin, due to invalid XML " + e, e);
        }
    }

    public Object getImpl(String aName) throws IllegalArgumentException
    {
        Object actionMapping = configurationReader.getActionMapping(aName);
        if (actionMapping == null)
            throw new IllegalArgumentException("No such view mapping:" + aName);
        return actionMapping;
    }

    public void setImpl(String aName, Object aValue) throws IllegalArgumentException, UnsupportedOperationException
    {
        throw new UnsupportedOperationException("This configuration does not support updating a setting");
    }

    public Iterator listImpl()
    {
        return configurationReader.getActionMappingNames().iterator();
    }

As you will probably notice - there is some hackiness involved in going from dom4j (which JIRA uses) to w3c XML code. Apart from that - you should see that the code is quite simple.

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators