org.apache.velocity.tools.config
Class EasyFactoryConfiguration

java.lang.Object
  extended by org.apache.velocity.tools.config.Configuration
      extended by org.apache.velocity.tools.config.CompoundConfiguration<ToolboxConfiguration>
          extended by org.apache.velocity.tools.config.FactoryConfiguration
              extended by org.apache.velocity.tools.config.EasyFactoryConfiguration
All Implemented Interfaces:
java.lang.Comparable<Configuration>

public class EasyFactoryConfiguration
extends FactoryConfiguration

FactoryConfiguration subclass that simplifies the process of configuring a ToolboxFactory in Java without the use of an xml or properties configuration file. Below is an example:


 EasyFactoryConfiguration config = new EasyFactoryConfiguration();
 config.toolbox(Scope.REQUEST).property("locale", Locale.US)
  .tool(DateTool.class)
  .tool("myTool", MyTool.class);
 config.toolbox(Scope.APPLICATION)
  .tool(NumberTool.class).property("locale", Locale.FR);
 ToolboxFactory factory = config.createFactory();
 

Doing the above without this class would require the following to create an equivalent FactoryConfiguration in Java:


 FactoryConfiguration factoryConfig = new FactoryConfiguration();
 ToolboxConfiguration toolbox = new ToolboxConfiguration();
 toolbox.setScope(Scope.REQUEST);
 toolbox.setProperty("locale", Locale.US);
 ToolConfiguration tool = new ToolConfiguration();
 tool.setClassname(DateTool.class.getName());
 tool = new ToolConfiguration();
 tool.setKey("myTool");
 tool.setClassname(MyTool.class.getName());
 toolbox.addTool(tool);
 toolbox = new ToolboxConfiguration();
 toolbox.setScope(Scope.APPLICATION);
 tool = new ToolConfiguration();
 tool.setClassname(NumberTool.class.getName());
 tool.setProperty("locale", Locale.FR);
 toolbox.addTool(tool);
 factoryConfig.addToolbox(toolbox);
 ToolboxFactory factory = factoryConfig.createFactory();
 

Of course, you could directly configure a ToolboxFactory with relatively little effort as well:


 ToolboxFactory factory = new ToolboxFactory();
 factory.putProperty(Scope.REQUEST, "locale", Locale.US);
 factory.addToolInfo(Scope.REQUEST, new ToolInfo("date", DateTool.class));
 factory.addToolInfo(Scope.REQUEST, new ToolInfo("render", ViewRenderTool.class));
 ToolInfo info = new ToolInfo("number", NumberTool.class);
 info.setProperty("locale", Locale.FR);
 factory.addToolInfo(Scope.APPLICATION, info);
 
But this is not reusable. Why does that matter? Well, it doesn't matter for application developers. But, if another framework wishes to provide a common VelocityTools configuration for application developers, this may come in handy. Or it may not. In any case, it was mildly fun to write. :)

Version:
$Id: EasyFactoryConfiguration.java 511959 2007-02-26 19:24:39Z nbubna $
Author:
Nathan Bubna

Nested Class Summary
static class EasyFactoryConfiguration.EasyData
           
 class EasyFactoryConfiguration.EasyWrap<C extends Configuration>
           
 
Field Summary
private  boolean addedDefaults
           
private  EasyFactoryConfiguration.EasyWrap<ToolboxConfiguration> toolbox
           
 
Constructor Summary
EasyFactoryConfiguration()
           
EasyFactoryConfiguration(boolean includeDefaults)
           
EasyFactoryConfiguration(boolean includeDefaults, java.lang.String source)
           
 
Method Summary
 EasyFactoryConfiguration addDefaultTools()
          Adds the ConfigurationUtils.getDefaultTools() configuration to this the current configuration.
 EasyFactoryConfiguration autoLoad()
           
 EasyFactoryConfiguration autoLoad(boolean includeDefaults)
           
 EasyFactoryConfiguration bool(java.lang.String key, java.lang.Object value)
           
protected  EasyFactoryConfiguration data(java.lang.String key, Data.Type type, java.lang.Object value)
           
 EasyFactoryConfiguration.EasyData data(java.lang.String key, java.lang.Object value)
           
 EasyFactoryConfiguration data(java.lang.String key, java.lang.String type, java.lang.Object value)
           
 EasyFactoryConfiguration number(java.lang.String key, java.lang.Object value)
           
 EasyFactoryConfiguration property(java.lang.String name, java.lang.Object value)
           
 EasyFactoryConfiguration string(java.lang.String key, java.lang.Object value)
           
 EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.Class clazz)
           
 EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.String classname)
           
 EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.String key, java.lang.Class clazz)
           
 EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.String key, java.lang.String classname)
           
 EasyFactoryConfiguration.EasyWrap<ToolboxConfiguration> toolbox(java.lang.String scope)
           
 
Methods inherited from class org.apache.velocity.tools.config.FactoryConfiguration
addConfiguration, addData, addSource, addToolbox, createFactory, equals, getData, getData, getData, getSource, getSources, getToolbox, getToolboxes, hasData, hashCode, removeData, removeToolbox, setData, setSource, setToolboxes, toString, toString, validate
 
Methods inherited from class org.apache.velocity.tools.config.CompoundConfiguration
addChild, addConfiguration, appendChildren, getChild, getChildren, hasChildren, removeChild, setChildren
 
Methods inherited from class org.apache.velocity.tools.config.Configuration
addConfiguration, addProperty, appendProperties, compareTo, getProperties, getProperty, getPropertyMap, hasProperties, removeProperty, removeProperty, setProperties, setProperty, setPropertyMap
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

addedDefaults

private boolean addedDefaults

toolbox

private EasyFactoryConfiguration.EasyWrap<ToolboxConfiguration> toolbox
Constructor Detail

EasyFactoryConfiguration

public EasyFactoryConfiguration()

EasyFactoryConfiguration

public EasyFactoryConfiguration(boolean includeDefaults)
Parameters:
includeDefaults - Sets whether this instance should start with the ConfigurationUtils.getDefaultTools() configuration or not.

EasyFactoryConfiguration

public EasyFactoryConfiguration(boolean includeDefaults,
                                java.lang.String source)
Parameters:
includeDefaults - Sets whether this instance should start with the ConfigurationUtils.getDefaultTools() configuration or not.
source - a string identify where this instance was created, or something else useful to identify this instance during debugging
Method Detail

addDefaultTools

public EasyFactoryConfiguration addDefaultTools()
Adds the ConfigurationUtils.getDefaultTools() configuration to this the current configuration.


autoLoad

public EasyFactoryConfiguration autoLoad()

autoLoad

public EasyFactoryConfiguration autoLoad(boolean includeDefaults)

data

public EasyFactoryConfiguration.EasyData data(java.lang.String key,
                                              java.lang.Object value)

data

public EasyFactoryConfiguration data(java.lang.String key,
                                     java.lang.String type,
                                     java.lang.Object value)

data

protected EasyFactoryConfiguration data(java.lang.String key,
                                        Data.Type type,
                                        java.lang.Object value)

string

public EasyFactoryConfiguration string(java.lang.String key,
                                       java.lang.Object value)

number

public EasyFactoryConfiguration number(java.lang.String key,
                                       java.lang.Object value)

bool

public EasyFactoryConfiguration bool(java.lang.String key,
                                     java.lang.Object value)

toolbox

public EasyFactoryConfiguration.EasyWrap<ToolboxConfiguration> toolbox(java.lang.String scope)

tool

public EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.String classname)

tool

public EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.Class clazz)

tool

public EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.String key,
                                                                 java.lang.String classname)

tool

public EasyFactoryConfiguration.EasyWrap<ToolConfiguration> tool(java.lang.String key,
                                                                 java.lang.Class clazz)

property

public EasyFactoryConfiguration property(java.lang.String name,
                                         java.lang.Object value)


Copyright (c) 2003-2007 Apache Software Foundation