velocity-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sid...@apache.org
Subject cvs commit: jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/tools ViewTool.java ContextViewTool.java LogEnabledViewTool.java LogEnabledViewToolImpl.java ServletViewTool.java ThreadSafeViewTool.java ViewToolLogger.java
Date Fri, 10 May 2002 05:42:18 GMT
sidler      02/05/09 22:42:18

  Modified:    struts   build.xml
               struts/examples/struts/examples/context_tools
                        mathtool-demo.txt mathtool-demo.vm
               struts/src/java/org/apache/velocity/tools/struts
                        ErrorsTool.java FormTool.java LinkTool.java
                        MessageTool.java
               tools    build.xml
               tools/src/java/org/apache/velocity/tools/tools DateTool.java
                        MathTool.java ParameterParser.java ToolLoader.java
               view     build.xml
               view/src/java/org/apache/velocity/tools/view/context
                        ViewContext.java
               view/src/java/org/apache/velocity/tools/view/i18n
                        MultiViewsTool.java
               view/src/java/org/apache/velocity/tools/view/servlet
                        ServletToolboxManager.java
  Added:       view/src/java/org/apache/velocity/tools/view DataInfo.java
                        ToolInfo.java ToolboxManager.java ViewToolInfo.java
                        XMLToolboxManager.java
               view/src/java/org/apache/velocity/tools/view/servlet
                        ServletToolInfo.java
               view/src/java/org/apache/velocity/tools/view/tools
                        ViewTool.java
  Removed:     view/src/java/org/apache/velocity/tools/view/servlet
                        ServletViewToolLogger.java
               view/src/java/org/apache/velocity/tools/view/tools
                        ContextViewTool.java LogEnabledViewTool.java
                        LogEnabledViewToolImpl.java ServletViewTool.java
                        ThreadSafeViewTool.java ViewToolLogger.java
  Log:
  Redesigned handling of view tools (contribution by Nathan Bubna)
  
  Revision  Changes    Path
  1.10      +1 -1      jakarta-velocity-tools/struts/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/build.xml,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- build.xml	20 Apr 2002 16:57:15 -0000	1.9
  +++ build.xml	10 May 2002 05:42:17 -0000	1.10
  @@ -23,7 +23,7 @@
     <property name="project.title"         value=""/>
   
     <!-- The current version number of this component -->
  -  <property name="project.version"       value="0.7"/>
  +  <property name="project.version"       value="0.8"/>
   
     <!-- The current version number of this component -->
     <property name="project.date"          value="29-Mar-2002"/>
  
  
  
  1.6       +6 -3      jakarta-velocity-tools/struts/examples/struts/examples/context_tools/mathtool-demo.txt
  
  Index: mathtool-demo.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/examples/struts/examples/context_tools/mathtool-demo.txt,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- mathtool-demo.txt	15 Apr 2002 18:30:27 -0000	1.5
  +++ mathtool-demo.txt	10 May 2002 05:42:17 -0000	1.6
  @@ -7,10 +7,13 @@
   
           <h2>MathTool Demo (Velocity)</h2>
   
  -        First, load the MathTool view tool into the context with key "math":<br>
  -        <code>&#36;toolLoader.load("math", "org.apache.velocity.tools.tools.MathTool")</code><br><br>
  +        This example demonstrates the loading of tools from within the template
  +        using a tool loader tool.<br>
  +        <br>
  +        Example: Load the MathTool into the context with key "math":<br>
  +        <code>&#35;set (&#36;math = toolLoader.load("org.apache.velocity.tools.tools.MathTool"))</code><br><br>
   
  -        $toolLoader.load("math", "org.apache.velocity.tools.tools.MathTool")
  +        #set ($math = $toolLoader.load("org.apache.velocity.tools.tools.MathTool"))
       
           Some of the functions provided by MathTool:<br>
       
  
  
  
  1.6       +6 -3      jakarta-velocity-tools/struts/examples/struts/examples/context_tools/mathtool-demo.vm
  
  Index: mathtool-demo.vm
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/examples/struts/examples/context_tools/mathtool-demo.vm,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- mathtool-demo.vm	15 Apr 2002 18:30:27 -0000	1.5
  +++ mathtool-demo.vm	10 May 2002 05:42:17 -0000	1.6
  @@ -7,10 +7,13 @@
   
           <h2>MathTool Demo (Velocity)</h2>
   
  -        First, load the MathTool view tool into the context with key "math":<br>
  -        <code>&#36;toolLoader.load("math", "org.apache.velocity.tools.tools.MathTool")</code><br><br>
  +        This example demonstrates the loading of tools from within the template
  +        using a tool loader tool.<br>
  +        <br>
  +        Example: Load the MathTool into the context with key "math":<br>
  +        <code>&#35;set (&#36;math = toolLoader.load("org.apache.velocity.tools.tools.MathTool"))</code><br><br>
   
  -        $toolLoader.load("math", "org.apache.velocity.tools.tools.MathTool")
  +        #set ($math = $toolLoader.load("org.apache.velocity.tools.tools.MathTool"))
       
           Some of the functions provided by MathTool:<br>
       
  
  
  
  1.5       +23 -37    jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/ErrorsTool.java
  
  Index: ErrorsTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/ErrorsTool.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ErrorsTool.java	15 Apr 2002 18:30:28 -0000	1.4
  +++ ErrorsTool.java	10 May 2002 05:42:17 -0000	1.5
  @@ -66,18 +66,16 @@
   import org.apache.struts.action.*;
   
   import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.LogEnabledViewToolImpl;
  -import org.apache.velocity.tools.view.tools.ServletViewTool;
  +import org.apache.velocity.tools.view.tools.ViewTool;
   
   
   /**
    * <p>View tool to work with the Struts error messages.</p>
    *
    * <p>This class is equipped to be used with a toolbox manager, for example
  - * the ServletToolboxManager included with VelServlet. The class extends 
  - * ServletViewToolLogger to profit from the logging facilities of that class.
  - * Furthermore, this class implements interface ServletViewTool, which allows
  - * a toolbox manager to pass the required context information.</p>
  + * the ServletToolboxManager included with VelServlet. This class implements 
  + * interface ViewTool, which allows a toolbox manager to pass the required
  + * context information.</p>
    *
    * <p>This class is not thread-safe by design. A new instance is needed for
    * the processing of every template request.</p>
  @@ -85,11 +83,10 @@
   
    * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
    *
  - * @version $Id: ErrorsTool.java,v 1.4 2002/04/15 18:30:28 sidler Exp $
  + * @version $Id: ErrorsTool.java,v 1.5 2002/05/10 05:42:17 sidler Exp $
    * 
    */
  -public class ErrorsTool extends LogEnabledViewToolImpl 
  -    implements ServletViewTool
  +public class ErrorsTool implements ViewTool
   {
   
       // --------------------------------------------- Properties ---------------
  @@ -134,21 +131,26 @@
       // --------------------------------------------- Constructors -------------
   
       /**
  -     * Returns a factory for instances of this class. Use method 
  -     * {@link #getInstance(ViewContext context)} to obtain instances 
  -     * of this class. Do not use instance obtained from this method
  -     * in templates. They are not properly initialized.
  +     * Default constructor. Tool must be initialized before use.
        */
       public ErrorsTool()
       {}
       
       
       /**
  -     * For internal use only! Use method {@link #getInstance(ViewContext context)} 
  -     * to obtain instances of the tool.
  +     * Initializes this tool.
  +     *
  +     * @param obj the current ViewContext
  +     * @throws IllegalArgumentException if the param is not a ViewContext
        */
  -    private ErrorsTool(ViewContext context)
  +    public void init(Object obj)
       {
  +        if (!(obj instanceof ViewContext))
  +        {
  +            throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
  +        }
  +
  +        ViewContext context = (ViewContext)obj;
           this.request = context.getRequest();
           this.session = request.getSession(false);
           this.application = context.getServletContext();    
  @@ -158,28 +160,12 @@
           errors = StrutsUtils.getActionErrors(request);
       }
       
  -
  -
  -    // ----------------------------------- Interface ServletViewTool ----------
  -
  -    /**
  -     * Returns an initialized instance of this view tool.
  -     */
  -    public Object getInstance(ViewContext context)
  -    {
  -        return new ErrorsTool(context);
  -    }
  -
       
       /**
  -     * <p>Returns the default life cycle for this tool. This is 
  -     * {@link ServletViewTool#REQUEST}. Do not overwrite this
  -     * per toolbox configuration. No alternative life cycles are 
  -     * supported by this tool</p>
  +     * Log messages are sent to the servlet context
        */
  -    public String getDefaultLifecycle()
  -    {
  -        return ServletViewTool.REQUEST; 
  +    private void log(String s) {
  +        application.log(s);
       }
   
   
  @@ -278,7 +264,7 @@
           
           if (resources == null) 
           {
  -            log(ERROR, "Message resources are not available.");
  +            log("[ERROR] Message resources are not available.");
               //FIXME? should we return the list of error keys instead?
               return null;
           }
  @@ -313,7 +299,7 @@
               else
               {
                   // if error message cannot be found for a key, return key instead
  -                log(WARN, "Message for key " + errormsg.getKey() + " could not be found in message resources.");
  +                log("[WARN] Message for key " + errormsg.getKey() + " could not be found in message resources.");
                   list.add(errormsg.getKey());
               }
           }
  
  
  
  1.5       +18 -40    jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/FormTool.java
  
  Index: FormTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/FormTool.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FormTool.java	15 Apr 2002 18:30:28 -0000	1.4
  +++ FormTool.java	10 May 2002 05:42:17 -0000	1.5
  @@ -62,29 +62,26 @@
   import org.apache.struts.action.*;
   
   import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.LogEnabledViewToolImpl;
  -import org.apache.velocity.tools.view.tools.ServletViewTool;
  +import org.apache.velocity.tools.view.tools.ViewTool;
   
   
   /**
    * <p>View tool to work with HTML forms in Struts.</p> 
    *
    * <p>This class is equipped to be used with a toolbox manager, for example
  - * the ServletToolboxManager included with VelServlet. The class extends 
  - * ServletViewToolLogger to profit from the logging facilities of that class.
  - * Furthermore, this class implements interface ServletViewTool, which allows
  - * a toolbox manager to pass the required context information.</p>
  + * the ServletToolboxManager included with VelServlet. This class implements 
  + * interface ViewTool, which allows a toolbox manager to pass the required
  + * context information.</p>
    *
    * <p>This class is not thread-safe by design. A new instance is needed for
    * the processing of every template request.</p>
    *
    * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
    *
  - * @version $Id: FormTool.java,v 1.4 2002/04/15 18:30:28 sidler Exp $
  + * @version $Id: FormTool.java,v 1.5 2002/05/10 05:42:17 sidler Exp $
    * 
    */
  -public class FormTool extends LogEnabledViewToolImpl 
  -    implements ServletViewTool
  +public class FormTool implements ViewTool
   {
   
       // --------------------------------------------- Properties ---------------
  @@ -106,10 +103,7 @@
       // --------------------------------------------- Constructors -------------
   
       /**
  -     * Returns a factory for instances of this class. Use method 
  -     * {@link #getInstance(ViewContext context)} to obtain instances 
  -     * of this class. Do not use instance obtained from this method
  -     * in templates. They are not properly initialized.
  +     * Default constructor. Tool must be initialized before use.
        */
       public FormTool()
       {
  @@ -117,37 +111,21 @@
       
       
       /**
  -     * For internal use only! Use method {@link #getInstance(ViewContext context)} 
  -     * to obtain instances of the tool.
  +     * Initializes this tool.
  +     *
  +     * @param obj the current ViewContext
  +     * @throws IllegalArgumentException if the param is not a ViewContext
        */
  -    private FormTool(ViewContext context)
  +    public void init(Object obj)
       {
  +        if (!(obj instanceof ViewContext))
  +        {
  +            throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
  +        }
  +
  +        ViewContext context = (ViewContext)obj;
           this.request = context.getRequest();
           this.session = request.getSession(false);
  -    }
  -    
  -
  -
  -    // ----------------------------------- Interface ServletViewTool -------
  -
  -    /**
  -     * Returns an initialized instance of this view tool.
  -     */
  -    public Object getInstance(ViewContext context)
  -    {
  -        return new FormTool(context);
  -    }
  -
  -
  -    /**
  -     * <p>Returns the default life cycle for this tool. This is 
  -     * {@link ServletViewTool#REQUEST}. Do not overwrite this
  -     * per toolbox configuration. No alternative life cycles are 
  -     * supported by this tool</p>
  -     */
  -    public String getDefaultLifecycle()
  -    {
  -        return ServletViewTool.REQUEST; 
       }
   
   
  
  
  
  1.7       +27 -55    jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/LinkTool.java
  
  Index: LinkTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/LinkTool.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- LinkTool.java	29 Apr 2002 05:20:51 -0000	1.6
  +++ LinkTool.java	10 May 2002 05:42:17 -0000	1.7
  @@ -67,18 +67,16 @@
   import org.apache.struts.action.*;
   
   import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.LogEnabledViewToolImpl;
  -import org.apache.velocity.tools.view.tools.ServletViewTool;
  +import org.apache.velocity.tools.view.tools.ViewTool;
   
   
   /**
    * <p>View tool to work with URI links in Struts.</p> 
    * 
    * <p>This class is equipped to be used with a toolbox manager, for example
  - * the ServletToolboxManager included with VelServlet. The class extends 
  - * ServletViewToolLogger to profit from the logging facilities of that class.
  - * Furthermore, this class implements interface ServletViewTool, which allows
  - * a toolbox manager to pass the required context information.</p>
  + * the ServletToolboxManager included with VelServlet. This class implements 
  + * interface ViewTool, which allows a toolbox manager to pass the required
  + * context information.</p>
    *
    * <p>This class is not thread-safe by design. A new instance is needed for
    * the processing of every template request.</p>
  @@ -86,11 +84,10 @@
    * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
    * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
    *
  - * @version $Id: LinkTool.java,v 1.6 2002/04/29 05:20:51 sidler Exp $
  + * @version $Id: LinkTool.java,v 1.7 2002/05/10 05:42:17 sidler Exp $
    * 
    */
  -public class LinkTool extends LogEnabledViewToolImpl 
  -    implements ServletViewTool
  +public class LinkTool implements ViewTool
   {
   
       // --------------------------------------------- Properties ---------------
  @@ -111,12 +108,6 @@
        * A reference to the HttpServletResponse.
        */ 
       protected HttpServletResponse response;
  -    
  -
  -    /**
  -     * A reference to the HttpSession.
  -     */ 
  -    protected HttpSession session;
   
   
       /**
  @@ -134,37 +125,42 @@
       // --------------------------------------------- Constructors -------------
   
       /**
  -     * Returns a factory for instances of this class. Use method 
  -     * {@link #getInstance(ViewContext context)} to obtain instances 
  -     * of this class. Do not use instance obtained from this method
  -     * in templates. They are not properly initialized.
  +     * Default constructor. Tool must be initialized before use.
        */
       public LinkTool()
       {
  -        request = null;
  -        response = null;
  -        session = null;
  -        application = null;
  -        
           uri = null;
           queryData = null;
       }
       
       
       /**
  -     * For internal use only! Use method {@link #getInstance(ViewContext context)} 
  -     * to obtain instances of this tool.
  +     * Initializes this tool.
  +     *
  +     * @param obj the current ViewContext
  +     * @throws IllegalArgumentException if the param is not a ViewContext
        */
  -    private LinkTool(ViewContext context)
  +    public void init(Object obj)
       {
  +        if (!(obj instanceof ViewContext))
  +        {
  +            throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
  +        }
  + 
  +        ViewContext context = (ViewContext)obj;
           this.request = context.getRequest();
           this.response = context.getResponse();
           this.application = context.getServletContext();    
  -
  -        this.uri = null;
  -        this.queryData = null;
       }
       
  +    
  +    /**
  +     * Log messages are sent to the servlet context
  +     */
  +    private void log(String s) {
  +        application.log(s);
  +    }
  +
   
       /**
        * For internal use.
  @@ -213,30 +209,6 @@
           this.queryData = that.queryData;
       }
   
  -
  -
  -    // ----------------------------------- Interface ServletViewTool -------
  -
  -    /**
  -     * Returns an initialized instance of this view tool.
  -     */
  -    public Object getInstance(ViewContext context)
  -    {
  -        return new LinkTool(context);
  -    }
  -
  -
  -    /**
  -     * <p>Returns the default life cycle for this tool. This is 
  -     * {@link ServletViewTool#REQUEST}. Do not overwrite this
  -     * per toolbox configuration. No alternative life cycles are 
  -     * supported by this tool</p>
  -     */
  -    public String getDefaultLifecycle()
  -    {
  -        return ServletViewTool.REQUEST; 
  -    }
  -
       
   
       
  @@ -301,7 +273,7 @@
           
           if (mapping == null)
           {
  -            log(WARN, "In method setForward(" + forward + "): Parameter does not map to a valid forward.");
  +            log("[Warn] In method setForward(" + forward + "): Parameter does not map to a valid forward.");
               return null;
           }
   
  
  
  
  1.5       +25 -41    jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/MessageTool.java
  
  Index: MessageTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/struts/src/java/org/apache/velocity/tools/struts/MessageTool.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MessageTool.java	15 Apr 2002 18:30:28 -0000	1.4
  +++ MessageTool.java	10 May 2002 05:42:17 -0000	1.5
  @@ -65,29 +65,26 @@
   import org.apache.struts.action.*;
   
   import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.LogEnabledViewToolImpl;
  -import org.apache.velocity.tools.view.tools.ServletViewTool;
  +import org.apache.velocity.tools.view.tools.ViewTool;
   
   
   /** 
    * View tool that provides methods to render Struts message resources.</p>
    *
    * <p>This class is equipped to be used with a toolbox manager, for example
  - * the ServletToolboxManager included with VelServlet. The class extends 
  - * ServletViewToolLogger to profit from the logging facilities of that class.
  - * Furthermore, this class implements interface ServletViewTool, which allows
  - * a toolbox manager to pass the required context information.</p>
  + * the ServletToolboxManager included with VelServlet. This class implements
  + * interface ViewTool, which allows a toolbox manager to pass the 
  + * required context information.</p>
    *
    * <p>This class is not thread-safe by design. A new instance is needed for
    * the processing of every template request.</p>
    *
    * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
    *
  - * @version $Id: MessageTool.java,v 1.4 2002/04/15 18:30:28 sidler Exp $
  + * @version $Id: MessageTool.java,v 1.5 2002/05/10 05:42:17 sidler Exp $
    * 
    */
  -public class MessageTool extends LogEnabledViewToolImpl 
  -    implements ServletViewTool
  +public class MessageTool implements ViewTool
   {
   
       // --------------------------------------------- Properties -------
  @@ -126,10 +123,7 @@
       // --------------------------------------------- Constructors -------------
   
       /**
  -     * Returns a factory for instances of this class. Use method 
  -     * {@link #getInstance(ViewContext context)} to obtain instances 
  -     * of this class. Do not use instance obtained from this method
  -     * in templates. They are not properly initialized.
  +     * Default constructor. Tool must be initialized before use.
        */
       public MessageTool()
       {
  @@ -137,43 +131,33 @@
       
       
       /**
  -     * For internal use only! Use method {@link #getInstance(ViewContext context)} 
  -     * to obtain instances of the tool.
  +     * Initializes this tool.
        *
  -     * @param context the Velocity context
  +     * @param obj the current ViewContext
  +     * @throws IllegalArgumentException if the param is not a ViewContext
        */
  -    private MessageTool(ViewContext context)
  +    public void init(Object obj)
       {
  +        if (!(obj instanceof ViewContext))
  +        {
  +            throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
  +        }
  +
  +        ViewContext context = (ViewContext)obj;
           this.request = context.getRequest();
           this.session = request.getSession(false);
           this.application = context.getServletContext();    
  -        
  +
           resources = StrutsUtils.getMessageResources(application);
           locale = StrutsUtils.getLocale(request, session);
       }
       
  -
  -
  -    // ----------------------------------- Interface ServletViewTool -------
  -
  -    /**
  -     * Returns an initialized instance of this view tool.
  -     */
  -    public Object getInstance(ViewContext context)
  -    {
  -        return new MessageTool(context);
  -    }
  -
  -
  +    
       /**
  -     * <p>Returns the default life cycle for this tool. This is 
  -     * {@link ServletViewTool#REQUEST}. Do not overwrite this
  -     * per toolbox configuration. No alternative life cycles are 
  -     * supported by this tool</p>
  +     * Log messages are sent to the servlet context
        */
  -    public String getDefaultLifecycle()
  -    {
  -        return ServletViewTool.REQUEST; 
  +    private void log(String s) {
  +        application.log(s);
       }
   
   
  @@ -194,7 +178,7 @@
       {
           if (resources == null)
           {
  -            log(ERROR, "Message resources are not available.");
  +            log("[ERROR] Message resources are not available.");
               return null;
           }
           return resources.getMessage(locale, key);
  @@ -217,7 +201,7 @@
       {
           if (resources == null)
           {
  -            log(ERROR, "Message resources are not available.");
  +            log("[ERROR] Message resources are not available.");
               return null;
           }
           
  @@ -263,7 +247,7 @@
       {
           if (resources == null)
           {
  -            log(ERROR, "Message resources are not available.");
  +            log("[ERROR] Message resources are not available.");
               return false;
           }
   
  
  
  
  1.6       +1 -1      jakarta-velocity-tools/tools/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/tools/build.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- build.xml	20 Apr 2002 16:57:15 -0000	1.5
  +++ build.xml	10 May 2002 05:42:17 -0000	1.6
  @@ -23,7 +23,7 @@
     <property name="project.title"         value=""/>
   
     <!-- The current version number of this component -->
  -  <property name="project.version"       value="0.1"/>
  +  <property name="project.version"       value="0.2"/>
   
     <!-- The current version number of this component -->
     <property name="project.date"          value="24-Mar-2002"/>
  
  
  
  1.4       +2 -10     jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/DateTool.java
  
  Index: DateTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/DateTool.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DateTool.java	7 May 2002 08:58:35 -0000	1.3
  +++ DateTool.java	10 May 2002 05:42:17 -0000	1.4
  @@ -62,25 +62,17 @@
   import java.util.Calendar;
   import java.util.Locale;
   
  -import org.apache.velocity.tools.view.tools.ThreadSafeViewTool;
  -
   
   /**
    * <p>Tool for manipulating {@link Date} and {@link Calendar}
    * objects in Velocity templates.</p> 
  - * 
  - * <p>The tool is tread-safe and implements interface 
  - * ThreadSafeViewTool. This allows a compatible toolbox 
  - * manager like {@link org.apache.velocity.tools.view.servlet.ServletToolboxManager}
  - * to automatically load the tool into the context and reuse
  - * the same instance for the entire runtime.</p>
    *
    * @author <a href="mailto:sidler@teamup.com">Gabriel Sidler</a>
    * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   
  -public class DateTool implements ThreadSafeViewTool
  +public class DateTool
   {
   
   
  
  
  
  1.3       +2 -3      jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/MathTool.java
  
  Index: MathTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/MathTool.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MathTool.java	15 Apr 2002 18:30:28 -0000	1.2
  +++ MathTool.java	10 May 2002 05:42:17 -0000	1.3
  @@ -54,7 +54,6 @@
   
   package org.apache.velocity.tools.tools;
   
  -import org.apache.velocity.tools.view.tools.ThreadSafeViewTool;
   import java.lang.Math;
   
   
  @@ -84,10 +83,10 @@
    * </ul>
    * 
    * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
  - * @version $Revision: 1.2 $ $Date: 2002/04/15 18:30:28 $
  + * @version $Revision: 1.3 $ $Date: 2002/05/10 05:42:17 $
    */
   
  -public class MathTool implements ThreadSafeViewTool
  +public class MathTool
   {
       /**
        * @param num1 the first number
  
  
  
  1.3       +2 -3      jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/ParameterParser.java
  
  Index: ParameterParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/ParameterParser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ParameterParser.java	15 Apr 2002 18:30:28 -0000	1.2
  +++ ParameterParser.java	10 May 2002 05:42:17 -0000	1.3
  @@ -55,7 +55,6 @@
   
   package org.apache.velocity.tools.tools;
   
  -import org.apache.velocity.tools.view.tools.ThreadSafeViewTool;
   import javax.servlet.ServletRequest;
   
   
  @@ -64,10 +63,10 @@
    *
    * @author <a href="mailto:sidler@teamup.com">Gabriel Sidler</a>
    * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
  - * @version $Revision: 1.2 $ $Date: 2002/04/15 18:30:28 $
  + * @version $Revision: 1.3 $ $Date: 2002/05/10 05:42:17 $
    */
   
  -public class ParameterParser implements ThreadSafeViewTool
  +public class ParameterParser
   {
   
       /**
  
  
  
  1.3       +13 -79    jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/ToolLoader.java
  
  Index: ToolLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/ToolLoader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ToolLoader.java	15 Apr 2002 18:30:28 -0000	1.2
  +++ ToolLoader.java	10 May 2002 05:42:17 -0000	1.3
  @@ -54,112 +54,46 @@
   
   package org.apache.velocity.tools.tools;
   
  -import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.ContextViewTool;
  -import org.apache.velocity.tools.view.tools.LogEnabledViewToolImpl;
  -import org.apache.velocity.context.Context;
  -
  -
   /**
    * <p>A view tool that allows template designers to load
  - * other view tools from within the template. Any object
  + * an arbitrary object into the context. Any object
    * with a public constructor without parameters can be used
    * as a view tool.</p>
    *
  - * <p>Example: Assuming that an instance of this class has
  - * been loaded into the Velocity context under key "toolloader",
  - * then from within a template a designer would call:<br>
  - * <br>
  - * <code>$toolloader.load("math", "xxx.yyy.zzz.MathTool")</code><br>
  - * <br>
  - * to load a math tool into the context under key "math". This tool
  - * is then available for use within the template, for example:<br>
  - * <br>
  - * <code>$math.random(1, 100)</code><br>
  - * </p>
  - *
  - * <p>THIS CLASS IS HERE AS A PROOF OF CONCEPT ONLY. IT NEEDS TO BE
  - * REFACTORED.</p>
  + * <p>THIS CLASS IS HERE AS A PROOF OF CONCEPT ONLY. IT IS NOT
  + * INTENDED FOR USE IN PRODUCTION ENVIRONMENTS. USE AT YOUR OWN RISK.</p>
    * 
    * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
    * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    *
  - * @version $Id: ToolLoader.java,v 1.2 2002/04/15 18:30:28 sidler Exp $
  + * @version $Id: ToolLoader.java,v 1.3 2002/05/10 05:42:17 sidler Exp $
    * 
    */
   
  -public class ToolLoader extends LogEnabledViewToolImpl 
  -    implements ContextViewTool
  +public class ToolLoader
   {
   
  -    // -------------------------------------------- Properties ----------------
  -
  -    /**
  -     * <p>A reference to the Velocity context.</p>
  -     */
  -    private Context ctx;
  -    
  -
  -    
  -    // -------------------------------------------- Constructors --------------
  -    
  -    /**
  -     * Returns a factory for instances of this class. Use method 
  -     * {@link #getInstance(Context context)} to obtain instances 
  -     * of this class. Do not use instance obtained from this method
  -     * in templates. They are not properly initialized.
  -     */
       public ToolLoader()
       {
       }
   
  -    
  -    /**
  -     * Contructor for internal use only. 
  -     */
  -    private ToolLoader(Context context)
  -    {
  -        this.ctx = context;
  -    }
  -
  -    
  -
  -    // --------------------------------------- Interface ContextViewTool ---
  -    
  -    /**
  -     * Returns an initialized instance of this view tool.
  -     */
  -    public Object getInstance(Context context)
  -    {
  -        return new ToolLoader(context);
  -    }
  -
  -
  -
  -    // -------------------------------------------- Public Utility Methods ----
  -
       /**
  -     * <p>Loads a view tool of class <i>clazz</i> and inserts it
  -     * into the Velocity context with key <i>key</i>. On order to be
  -     * loadable, view tools must provide a constructor with no 
  -     * parameters. The life cycle of a view tool loaded using
  -     * this method is the current request.</p>
  +     * Creates and returns an object of the specified classname.
  +     * The object must have a valid default constructor.
        *
  -     * @param key the key used to add the tool to the context
  -     * @param clazz the fully qualified class name of the tool that
  -     *     is to be instantiated and added to the context
  +     * @param clazz the fully qualified class name of the object
  +     * @return an instance of the specified class or null if the class
  +     *         could not be instantiated.
        */
  -    public void load(String key, String clazz)
  +    public Object load(String clazz)
       {
           try
           {
  -            Object tool = Class.forName(clazz).newInstance();
  -            ctx.put(key, tool);
  -            log(INFO, "Loaded tool: Key: " + key + " Class: " + clazz);
  +            return Class.forName(clazz).newInstance();
           }
           catch (Exception e)
           {
  -            log(ERROR, "Error loading view tool: " + clazz + " with key: " + key + ". " + e);            
  +            return null; 
           }
       }
   
  
  
  
  1.12      +1 -1      jakarta-velocity-tools/view/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/view/build.xml,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- build.xml	20 Apr 2002 16:57:15 -0000	1.11
  +++ build.xml	10 May 2002 05:42:18 -0000	1.12
  @@ -23,7 +23,7 @@
     <property name="project.title"         value=""/>
   
     <!-- The current version number of this component -->
  -  <property name="project.version"       value="0.6"/>
  +  <property name="project.version"       value="0.7"/>
   
     <!-- The base directory for compilation targets -->
     <property name="build.home"              value="${basedir}/target"/>
  
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/DataInfo.java
  
  Index: DataInfo.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view;
  
  
  /**
   * ToolInfo implementation to handle "primitive" data types.
   * It currently supports String, Number, and Boolean data.
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   *
   * @version $Id: DataInfo.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public class DataInfo implements ToolInfo
  {
  
      public static String TYPE_STRING = "string";
      public static String TYPE_NUMBER = "number";
      public static String TYPE_BOOLEAN = "boolean";
  
      private String key;
      private Object data;
  
  
      /**
       * Parses the value string into a recognized type. If
       * the type specified is not supported, the data will
       * be held and returned as a string.
       *
       * @param key the context key for the data
       * @param type the data type
       * @param value the data
       */
      public DataInfo(String key, String type, String value)
      {
          this.key = key;
  
          if (type.equalsIgnoreCase(TYPE_BOOLEAN))
          {
              this.data = Boolean.valueOf(value);
          }
          else if (type.equalsIgnoreCase(TYPE_NUMBER))
          {
              if (value.indexOf('.') >= 0)
              {
                  this.data = new Double(value);
              }
              else
              {
                  this.data = new Integer(value);
              }
          }
          else
          {
              this.data = value;
          }
      }
  
  
      public String getKey()
      {
          return key;
      }
  
  
      public String getClassname()
      {
          return data.getClass().getName();
      }
  
  
      /**
       * Returns the data. Always returns the same
       * object since the data is a constant. Initialization
       * data is ignored.
       */
      public Object getInstance(Object initData)
      {
          return data;
      }
  
  
  }
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/ToolInfo.java
  
  Index: ToolInfo.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view;
  
  
  /**
   * Interface to simplify and abstract tool handling.
   *
   * Implementations of this class should hold both the context
   * key for the tool and sufficient information to return 
   * an instance of the tool.
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   *
   * @version $Id: ToolInfo.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public interface ToolInfo
  {
  
  
      /**
       * @return the context key for the tool
       */
      String getKey();
  
  
      /**
       * @return the fully qualified classname for the tool
       */
      String getClassname();
  
  
      /**
       * Returns an instance of the tool.
       *
       * Instances returned may be new on each call, pooled, or 
       * the be same instance every time depending on the
       * implementation.  The object passed to this method may
       * be used to initialize or create the tool that is returned,
       * or it may be null if no such data is required.
       *
       * @param initData an object that may be used to initialize the instance
       * @return an instance of the tool
       */
      Object getInstance(Object initData);
  
  
  }
  
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/ToolboxManager.java
  
  Index: ToolboxManager.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view;
  
  
  import org.apache.velocity.tools.view.context.ToolboxContext;
  
  
  /**
   * Common interface for toolbox manager implementations.
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
   * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
   *
   * @version $Id: ToolboxManager.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public interface ToolboxManager
  {
  
  
      /**
       * Adds a tool to be managed
       */
      void addTool(ToolInfo info);
  
  
      /**
       * Creates a {@link ToolboxContext} from the tools and data 
       * in this manager.  Tools that implement the ViewTool
       * interface should be initialized using the given initData.
       *
       * @param initData data used to initialize ViewTools
       * @return the created ToolboxContext
       */
      ToolboxContext getToolboxContext(Object initData);
  
  
  }
  
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/ViewToolInfo.java
  
  Index: ViewToolInfo.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view;
  
  
  import org.apache.velocity.tools.view.tools.ViewTool;
  
  
  /**
   * ToolInfo implementation for view tools. New instances
   * are returned for every call to getInstance(obj), and tools
   * that implement (@link ViewTool} are initialized with the
   * given object before being returned.
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   *
   * @version $Id: ViewToolInfo.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public class ViewToolInfo implements ToolInfo
  {
  
      private String key;
      private Class clazz;
      private boolean initializable = false;
  
  
      /**
       * Constructor.  If an instance of the tool cannot be created from
       * the classname, it will throw an exception.
       *
       * @param key the context key for the tool
       * @param classname the fully qualified java.lang.Class of the tool
       */
      public ViewToolInfo(String key, String classname) throws Exception
      {
          this.key = key;
          this.clazz = Class.forName(classname);
  
          //create an instance and see if it is initializable
          if (clazz.newInstance() instanceof ViewTool)
          {
              this.initializable = true;
          }
      }
  
  
      public String getKey()
      {
          return key;
      }
  
  
      public String getClassname()
      {
          return clazz.getName();
      }
  
  
      /**
       * Returns a new instance of the tool. If the tool
       * implements {@link ViewTool}, the new instance
       * will be initialized using the given data.
       */
      public Object getInstance(Object initData)
      {
          Object tool = null;
          try
          {
              tool = clazz.newInstance();
          }
          catch (Exception e)
          {
              //we should never get here since we
              //got a new instance just fine when we
              //created this tool info
          }
          
          if (initializable) {
              ((ViewTool)tool).init(initData);
          }
          return tool;
      }
  
  
  }
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/XMLToolboxManager.java
  
  Index: XMLToolboxManager.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view;
  
  
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import org.dom4j.Document;
  import org.dom4j.Element;
  import org.dom4j.Node;
  import org.dom4j.datatype.InvalidSchemaException;
  import org.dom4j.io.SAXReader;
  
  import org.apache.velocity.tools.view.context.ToolboxContext;
  
  
  /**
   * A ToolboxManager for loading a toolbox from xml.
   *
   * <p>A toolbox manager is responsible for automatically filling the Velocity
   * context with a set of view tools. This class provides the following 
   * features:</p>
   * <ul>
   *   <li>configurable through an XML-based configuration file</li>   
   *   <li>assembles a set of view tools (the toolbox) on request</li>
   *   <li>supports any class with a public constructor without parameters 
   *     to be used as a view tool</li>
   *   <li>supports adding primitive data values to the context(String,Number,Boolean)</li>
   * </ul>
   * 
   *
   * <p><strong>Configuration</strong></p>
   * <p>The toolbox manager is configured through an XML-based configuration
   * file. The configuration file is passed to the {@link #load(java.io.InputStream input)}
   * method. The required format is shown in the following example:</p>
   * <pre> 
   * &lt;?xml version="1.0"?&gt;
   * 
   * &lt;toolbox&gt;
   *   &lt;tool&gt;
   *      &lt;key&gt;toolLoader&lt;/key&gt;
   *      &lt;class&gt;org.apache.velocity.tools.tools.ToolLoader&lt;/class&gt;
   *   &lt;/tool&gt;
   *   &lt;tool&gt;
   *      &lt;key&gt;math&lt;/key&gt;
   *      &lt;class&gt;org.apache.velocity.tools.tools.MathTool&lt;/class&gt;
   *   &lt;/tool&gt;
   *   &lt;data type="Number"&gt;
   *      &lt;key&gt;luckynumber&lt;/key&gt;
   *      &lt;value&gt;1.37&lt;/class&gt;
   *   &lt;/data&gt;
   *   &lt;data type="String"&gt;
   *      &lt;key&gt;greeting&lt;/key&gt;
   *      &lt;value&gt;Hello World!&lt;/class&gt;
   *   &lt;/data&gt;
   * &lt;/toolbox&gt;    
   * </pre>
   *
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
   *
   * @version $Id: XMLToolboxManager.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public abstract class XMLToolboxManager implements ToolboxManager
  {
  
      public static final String BASE_NODE        = "toolbox";
      public static final String ELEMENT_TOOL     = "tool";
      public static final String ELEMENT_DATA     = "data";
      public static final String ELEMENT_KEY      = "key";
      public static final String ELEMENT_CLASS    = "class";
      public static final String ELEMENT_VALUE    = "value";
      public static final String ATTRIBUTE_TYPE   = "type";
  
      private List toolinfo;
  
  
      /**
       * Default constructor
       */
      public XMLToolboxManager()
      {
          toolinfo = new ArrayList();
      }
  
  
  
      // ------------------------------- ToolboxManager interface ------------
  
  
      public void addTool(ToolInfo info)
      {
          toolinfo.add(info);
      }
  
  
      public ToolboxContext getToolboxContext(Object initData)
      {
          Map toolbox = new HashMap();
  
          Iterator i = toolinfo.iterator();
          while(i.hasNext())
          {
              ToolInfo info = (ToolInfo)i.next();
              toolbox.put(info.getKey(), info.getInstance(initData));
          }
  
          return new ToolboxContext(toolbox);
      }
  
  
  
      // ------------------------------- toolbox loading methods ------------
  
  
      /**
       * Default implementation logs messages to system out.
       */
      protected void log(String s) {
         System.out.println("XMLToolboxManager - "+s);
      }
  
  
      /**
       * Reads an XML document from an {@link InputStream}
       * using <a href="http://dom4j.org">dom4j</a> and
       * sets up the toolbox from that.
       *
       * The DTD for toolbox schema is:
       * <pre>
       *  &lt;?xml version="1.0"?&gt;
       *  &lt;!ELEMENT toolbox (tool*,data*)&gt;
       *  &lt;!ELEMENT tool    (key,class,#PCDATA)&gt;
       *  &lt;!ELEMENT data    (key,value)&gt;
       *      &lt;!ATTLIST data type (string|number|boolean) "string"&gt;
       *  &lt;!ELEMENT key     (#CDATA)&gt;
       *  &lt;!ELEMENT class   (#CDATA)&gt;
       *  &lt;!ELEMENT value   (#CDATA)&gt;
       * </pre>
       * 
       * @param input the InputStream to read from
       */
      public void load(InputStream input) throws Exception
      {
          log("Loading toolbox...");
          Document document = new SAXReader().read(input);
          List elements = document.selectNodes("//"+BASE_NODE+"/*");
  
          Iterator i = elements.iterator();
          while(i.hasNext())
          {
              Element e = (Element)i.next();
              String name = e.getName();
              
              ToolInfo info;
  
              if (name.equalsIgnoreCase(ELEMENT_TOOL))
              {
                  info = readToolInfo(e);
              }
              else if (name.equalsIgnoreCase(ELEMENT_DATA)) 
              {
                  info = readDataInfo(e);
              }
              else 
              {
                  throw new InvalidSchemaException("Unknown element: "+name);
              }
  
              addTool(info);
              log("Added "+info.getClassname()+" as "+info.getKey());
          }
  
          log("Toolbox loaded.");
      }
  
  
      protected ToolInfo readToolInfo(Element e) throws Exception
      {
          Node n = e.selectSingleNode(ELEMENT_KEY);
          String key = n.getText();
  
          n = e.selectSingleNode(ELEMENT_CLASS);
          String classname = n.getText();
  
          return new ViewToolInfo(key, classname);
      }
  
  
      protected ToolInfo readDataInfo(Element e) throws Exception
      {
          Node n = e.selectSingleNode(ELEMENT_KEY);
          String key = n.getText();
  
          n = e.selectSingleNode(ELEMENT_VALUE);
          String value = n.getText();
  
          String type = e.attributeValue(ATTRIBUTE_TYPE, DataInfo.TYPE_STRING);
  
          return new DataInfo(key, type, value);
      }
  
  
  }
  
  
  
  1.5       +2 -4      jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/context/ViewContext.java
  
  Index: ViewContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/context/ViewContext.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ViewContext.java	15 Apr 2002 18:30:29 -0000	1.4
  +++ ViewContext.java	10 May 2002 05:42:18 -0000	1.5
  @@ -68,14 +68,12 @@
    * 
    * <p>The interface provides view tools in a servlet environment 
    * access to relevant context information, like servlet request, servlet 
  - * context and the velocity context. See 
  - * {@link org.apache.velocity.tools.view.tools.ServletViewTool} for an 
  - * example.</p>
  + * context and the velocity context.</p>
    *
    * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
    *
  - * @version $Id: ViewContext.java,v 1.4 2002/04/15 18:30:29 sidler Exp $ 
  + * @version $Id: ViewContext.java,v 1.5 2002/05/10 05:42:18 sidler Exp $ 
    */
   public interface ViewContext
   {
  
  
  
  1.6       +14 -28    jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java
  
  Index: MultiViewsTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MultiViewsTool.java	15 Apr 2002 18:30:29 -0000	1.5
  +++ MultiViewsTool.java	10 May 2002 05:42:18 -0000	1.6
  @@ -55,6 +55,7 @@
   
   package org.apache.velocity.tools.view.i18n;
   
  +
   import java.util.Locale;
   import javax.servlet.ServletContext;
   
  @@ -62,7 +63,7 @@
   import org.apache.velocity.context.Context;
   
   import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.ServletViewTool;
  +import org.apache.velocity.tools.view.tools.ViewTool;
   
   /**
    * <p>Allows for transparent content negotiation in a manner mimicking
  @@ -76,8 +77,7 @@
    *
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    */
  -public class MultiViewsTool
  -    implements ServletViewTool
  +public class MultiViewsTool implements ViewTool
   {
       /**
        * The key used to search initialization, context, and JVM
  @@ -93,8 +93,8 @@
       protected String defaultLanguage;
   
       /**
  -     * Creates a new uninitialized instance.  Call {@link
  -     * #init(ViewContext)} to initialize it.
  +     * Creates a new uninitialized instance.  Call {@link #init} 
  +     * to initialize it.
        */
       public MultiViewsTool()
       {
  @@ -108,10 +108,17 @@
        * easy to setup language overrides at different levels within
        * your application.
        *
  -     * @param context The context to use.
  +     * @param obj the current ViewContext
  +     * @throws IllegalArgumentException if the param is not a ViewContext
        */
  -    protected MultiViewsTool(ViewContext context)
  +    public void init(Object obj)
       {
  +        if (!(obj instanceof ViewContext))
  +        {
  +            throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
  +        }
  +
  +        ViewContext context = (ViewContext)obj;
           Context vc = context.getVelocityContext();
           defaultLanguage = (String) vc.get(DEFAULT_LANGUAGE_KEY);
           if (defaultLanguage == null || defaultLanguage.trim().equals(""))
  @@ -195,26 +202,5 @@
           return localizedName;
       }
   
  -    /**
  -     * Unneccessary cruft required by our interface.  Hopefully this
  -     * method will go away soon.
  -     *
  -     * @see org.apache.velocity.tools.view.tools.ServletViewTool#getInstance(ViewContext)
  -     */
  -    public Object getInstance(ViewContext context)
  -    {
  -        return new MultiViewsTool(context);
  -    }
   
  -    /**
  -     * Denotes the global/application scope of this tool.  Note that
  -     * this is merely to signify that it is thread-safe, and can
  -     * actually be used in any scope.
  -     *
  -     * @see org.apache.velocity.tools.view.tools.ServletViewTool#getDefaultLifecycle()
  -     */
  -    public String getDefaultLifecycle()
  -    {
  -        return APPLICATION;
  -    }
   }
  
  
  
  1.3       +123 -412  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java
  
  Index: ServletToolboxManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ServletToolboxManager.java	15 Apr 2002 18:30:29 -0000	1.2
  +++ ServletToolboxManager.java	10 May 2002 05:42:18 -0000	1.3
  @@ -52,10 +52,9 @@
    * <http://www.apache.org/>.
    */
   
  -package org.apache.velocity.tools.view.servlet;
   
  +package org.apache.velocity.tools.view.servlet;
   
  -import java.io.InputStream;
   
   import java.util.List;
   import java.util.Iterator;
  @@ -63,23 +62,17 @@
   import java.util.Map;
   import java.util.ArrayList;
   
  -
   import javax.servlet.http.HttpSession;
   import javax.servlet.ServletContext;
   
  -import org.dom4j.Document;
   import org.dom4j.Element;
   import org.dom4j.Node;
  -import org.dom4j.io.SAXReader;
  -
  -import org.apache.velocity.context.Context;
   
  +import org.apache.velocity.tools.view.DataInfo;
  +import org.apache.velocity.tools.view.ToolInfo;
  +import org.apache.velocity.tools.view.XMLToolboxManager;
   import org.apache.velocity.tools.view.context.ToolboxContext;
   import org.apache.velocity.tools.view.context.ViewContext;
  -import org.apache.velocity.tools.view.tools.ServletViewTool;
  -import org.apache.velocity.tools.view.tools.LogEnabledViewTool;
  -import org.apache.velocity.tools.view.tools.ContextViewTool;
  -import org.apache.velocity.tools.view.tools.ThreadSafeViewTool;
   
   
   /**
  @@ -91,122 +84,64 @@
    * <ul>
    *   <li>configurable through an XML-based configuration file</li>   
    *   <li>assembles a set of view tools (the toolbox) on request</li>
  - *   <li>handles view tools with different life cycles</li>
  - *   <li>efficiently reuses view tool instances where possible</li>
  - *   <li>provides special handling to known classes of view tools</li>
  + *   <li>handles different tool scopes (request, session, application)</li>
    *   <li>supports any class with a public constructor without parameters 
    *     to be used as a view tool</li>
  + *   <li>supports adding primitive data values to the context(String,Number,Boolean)</li>
    * </ul>
    * 
    *
    * <p><strong>Configuration</strong></p>
    * <p>The toolbox manager is configured through an XML-based configuration
  - * file. The configuration file is passed to the {@link #load(InputStream input)}
  - * method. The required format is show in the following example:</p>
  + * file. The configuration file is passed to the {@link #load(java.io.InputStream input)}
  + * method. The required format is shown in the following example:</p>
    * <pre> 
    * &lt;?xml version="1.0"?&gt;
    * 
    * &lt;toolbox&gt;
    *   &lt;tool&gt;
    *      &lt;key&gt;toolLoader&lt;/key&gt;
  - *      &lt;class&gt;org.apache.velocity.tools.tools.ToolLoaderTool&lt;/class&gt;
  + *      &lt;scope&gt;application&lt;/scope&gt;
  + *      &lt;class&gt;org.apache.velocity.tools.tools.ToolLoader&lt;/class&gt;
    *   &lt;/tool&gt;
    *   &lt;tool&gt;
  - *      &lt;key>math&lt;/key&gt;
  + *      &lt;key&gt;math&lt;/key&gt;
  + *      &lt;scope&gt;application&lt;/scope&gt;
    *      &lt;class&gt;org.apache.velocity.tools.tools.MathTool&lt;/class&gt;
    *   &lt;/tool&gt;
  + *   &lt;data type="Number"&gt;
  + *      &lt;key&gt;luckynumber&lt;/key&gt;
  + *      &lt;value&gt;1.37&lt;/class&gt;
  + *   &lt;/data&gt;
  + *   &lt;data type="String"&gt;
  + *      &lt;key&gt;greeting&lt;/key&gt;
  + *      &lt;value&gt;Hello World!&lt;/class&gt;
  + *   &lt;/data&gt;
    * &lt;/toolbox&gt;    
    * </pre>
    * <p>The recommended location for the configuration file is the WEB-INF directory of the
  - * web application. Note that some classes of view tools may allow or
  - * require additional configuration attributes. Please consult the documentation 
  - * of the view tools for more details.
  - * 
  - *
  - * <p><strong>Recognized Classes of View Tools</strong></p>
  - * <p>ServletToolboxManager provides special support for the following classes
  - * of view tools:
  - * <dl>
  - *   <dt>{@link LogEnabledViewTool}</dt>
  - *   <dd>Receive a reference to a logger object that enables them to log error 
  - *     conditions.</dd>
  - *
  - *   <dt>{@link ThreadSafeViewTool}</dt>
  - *   <dd>Instances are considered to be thread-safe. One single instance of 
  - *     the tool is re-used the entire runtime. This is much more efficient 
  - *     than the default handling (see below)</dd>
  - *
  - *   <dt>{@link ServletViewTool}</dt>
  - *   <dd>View tools that implement this interface support the additional 
  - *     configuration attribute <i>lifecycle</i>. This allows an
  - *     application developer to explicitely assign a <i>lifecycle</i> to the tool.
  - *     Supported are the values <code>request</code>, <code>session</code> and 
  - *     <code>application</code>. The <i>lifecycle</i> attribute is optional.
  - *     If not specified, a tool-specific default lifecycle is used. In the 
  - *     following configuration example a <i>lifecycle</i> of <code>session</code> 
  - *     is assigned to tool instances:
  - *     <pre>
  - *       &lt;tool&gt;
  - *          &lt;key&gt;xyz&lt;/key&gt;
  - *          &lt;class&gt;org.apache.velocity.tools.tools.XYZ&lt;/class&gt;
  - *          &lt;scope&gt;session&lt;/scope&gt;
  - *       &lt;/tool&gt;
  - *     </pre>    
  - *     Furthermore, view tools of this class get access to the current 
  - *     servlet request, the current session and the servlet context.</dd>
  - *
  - *   <dt>{@link ContextViewTool}</dt>
  - *   <dd>View tools of this class receive a reference to the Velocity
  - *     context.</dd>
  - * </dl>
  - *
  - *
  - * <p><strong>Default Handling</strong></p>
  - * <p>Any object with a public constructor without parameters can be used
  - * as a view tool. For classes of view tools that are not listed above,
  - * the following default handling is applied:</p>
  - * <ul>
  - *   <li>instances are created using a constructor without parameters</li>
  - *   <li>tools are assumed to be not thread-safe, a new instance is created
  - *     for every template being processed</li>
  - *   <li>tools have no access to logging</li>
  - *   <li>tools have no access to contextual information, like the servlet
  - *     environment or the Velocity context</li>
  - * </ul>
  - *
  - *
  - * <p><strong>Other Environments</strong></p>
  - * <p>Note that while the implementation of this toolbox manager is specific
  - * to the Servlet environment, it can be easily adapted to be used in other
  - * environments, like the DVSL ant task, for example.</p>
  - * 
  + * web application. 
    *
    * @author <a href="mailto:sidler@teamup.com">Gabriel Sidler</a>
    * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
    * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    *
  - * @version $Id: ServletToolboxManager.java,v 1.2 2002/04/15 18:30:29 sidler Exp $
  + * @version $Id: ServletToolboxManager.java,v 1.3 2002/05/10 05:42:18 sidler Exp $
    * 
    */
  -public class ServletToolboxManager
  +public class ServletToolboxManager extends XMLToolboxManager
   {
   
       // --------------------------------------------------- Properties ---------
   
  -    public static final String TOOL_KEY = "key";
  -    public static final String TOOL_LIFECYCLE = "lifecycle";
  -    public static final String TOOL_CLASS = "class";
  +    public static final String ELEMENT_SCOPE = "scope";
   
       public static final String SESSION_TOOLS_KEY = "org.apache.velocity.tools.view.tools.ServletToolboxManager.SessionTools";
   
  -    private ServletViewToolLogger logger;
  -    private ServletContext scontext;
  - 
  -    private SAXReader saxReader;
  -    private Map applicationTools;
  -    private ArrayList applicationToolsNotInitialized;
  -    private ArrayList sessionTools;
  -    private ArrayList requestTools;
  +    private ServletContext servletContext;
  +    private Map appTools;
  +    private ArrayList sessionToolInfo;
  +    private ArrayList requestToolInfo;
   
   
   
  @@ -215,374 +150,150 @@
       /**
        * Default constructor
        */
  -    public ServletToolboxManager(ServletContext scontext)
  +    public ServletToolboxManager(ServletContext servletContext)
       {
  -        this.scontext = scontext;
  -
  -        saxReader = new SAXReader();
  -        applicationToolsNotInitialized = new ArrayList();
  -        sessionTools = new ArrayList();
  -        requestTools = new ArrayList();
  -        logger = new ServletViewToolLogger(scontext);
  +        this.servletContext = servletContext;
  +        appTools = new HashMap();
  +        sessionToolInfo = new ArrayList();
  +        requestToolInfo = new ArrayList();
       }
   
   
   
       // --------------------------------------------------- Methods ------------
   
  -    private void log(String s) 
  +    /**
  +     * Overrides XMLToolboxManager to log to the servlet context
  +     */
  +    protected void log(String s) 
       {
  -        scontext.log("ServletToolboxManager: " + s);
  +        servletContext.log("ServletToolboxManager: " + s);
       }
   
   
       /**
  -     * Reads an XML document from an {@link InputStream}
  -     * using <a href="http://dom4j.org">dom4j</a> and
  -     * sets up the toolbox for the servlet from that.
  -     * 
  -     * Assumes toolbox.xml has a format like
  -     *
  -     *  <toolbox>
  -     *    <tool>
  -     *      <key>foo</key>
  -     *      <lifecycle>request</lifecycle>
  -     *      <class>com.mycompany.tools.Foo</class>
  -     *    </tool>
  -     *    <tool>
  -     *      <key>bar</key>
  -     *      <class>org.yourorganization.tools.Bar</class>
  -     *    </tool>
  -     *  </toolbox>
  -     *
  -     * @param input the InputStream to read from
  +     * Overrides XMLToolboxManager to read a {@link ServletToolInfo}
  +     * instead of a {@link org.apache.velocity.tools.view.ViewToolInfo}.
        */
  -    public void load(InputStream input) throws Exception
  +    protected ToolInfo readToolInfo(Element e) throws Exception
       {
  -        log("Initializing the toolbox...");
  -        Document document = saxReader.read(input);
  -        List tools = document.selectNodes("//toolbox/*");
  +        Node n = e.selectSingleNode(ELEMENT_KEY);
  +        String key = n.getText();
   
  -        Iterator i = tools.iterator();
  -        while(i.hasNext())
  +        n = e.selectSingleNode(ELEMENT_CLASS);
  +        String classname = n.getText();
  +        
  +        String scope = ServletToolInfo.REQUEST_SCOPE;
  +        n = e.selectSingleNode(ELEMENT_SCOPE);
  +        if (n != null)
           {
  -            //
  -            // Read definition of a tool in toolbox config file
  -            //
  -            Element e = (Element)i.next();
  -            String name = e.getName();
  -            log("Loading " + name);
  -
  -            // Read tool's key
  -            Node n = e.selectSingleNode(TOOL_KEY);
  -            String key = n.getText();
  -            log("  Context key: " + key);
  -
  -            // Read the tool's class
  -            n = e.selectSingleNode(TOOL_CLASS);
  -            String classname = n.getText();
  -            log("  Class: " + classname);
  -            
  -            // Create an instance of the tool
  -            Object obj;
  -            try
  -            {
  -                obj = Class.forName(classname).newInstance();
  -            }
  -            catch(Exception ex)
  -            {
  -                log("  Error creating instance for class: " + classname +
  -                    ". " + ex);
  -                log("  Tool not loaded.");    
  -                continue;
  -            }
  +            scope = n.getText();
  +        }
   
  -            // Try to read the tool's lifecycle (specific to ServletViewTool)
  -            
  -            // First, check of a lifecycle has been configured. 
  -            n = e.selectSingleNode(TOOL_LIFECYCLE);
  -            String lifecycle;
  -            if (n != null)
  -            {
  -                lifecycle = n.getText();
  -            }
  -            else
  -            {
  -                // Secondly, try to read default lifecycle
  -                try 
  -                {
  -                    lifecycle = ((ServletViewTool)obj).getDefaultLifecycle();
  -                }
  -                catch (ClassCastException cce)
  -                {
  -                    lifecycle = "";
  -                }
  -            }
  -            log("  Life cycle: " + lifecycle);
  +        return new ServletToolInfo(key, classname, scope);
  +    }
  +
  +
  +    /**
  +     * Overrides XMLToolboxManager to separate tools by scope.
  +     * For this to work, we obviously override getToolboxContext as well.
  +     */
  +    public void addTool(ToolInfo info)
  +    {
  +        if (info instanceof DataInfo)
  +        {
  +            //add static data to the appTools map
  +            appTools.put(info.getKey(), info.getInstance(null));
  +        }
  +        else if (info instanceof ServletToolInfo)
  +        {
  +            ServletToolInfo stInfo = (ServletToolInfo)info;
               
  -            //
  -            // Pass a logger to the tools that implement interface LogEnabledViewTool
  -            //
  -            if (obj instanceof LogEnabledViewTool)
  +            if (stInfo.getScope().equalsIgnoreCase(ServletToolInfo.REQUEST_SCOPE))
               {
  -                ((LogEnabledViewTool)obj).setLogger(logger);
  -                log("  Known interface: LogEnabledViewTool");
  +                requestToolInfo.add(stInfo);
               }
  -            
  -            //
  -            // Store the tool instance in the appropriate list
  -            //
  -            
  -            // First, handle tools that implement interface ServletViewTool
  -            // (the interface ThreadSafeViewTool is not considered in this
  -            // case because the 'lifecycle' attribute takes precedence over the 
  -            // 'thread safe' attribute)
  -            if (obj instanceof ServletViewTool)
  +            else if (stInfo.getScope().equalsIgnoreCase(ServletToolInfo.SESSION_SCOPE))
               {
  -                log("  Known interface: ServletViewTool");
  -                if (lifecycle.equalsIgnoreCase(ServletViewTool.REQUEST))
  -                {
  -                    requestTools.add(new ToolInfo(key, obj, classname));
  -                }
  -                else if (lifecycle.equalsIgnoreCase(ServletViewTool.SESSION))
  -                {
  -                    sessionTools.add(new ToolInfo(key, obj, classname));
  -                }
  -                else if (lifecycle.equalsIgnoreCase(ServletViewTool.APPLICATION))
  -                {
  -                    applicationToolsNotInitialized.add(new ToolInfo(key, obj, classname));
  -                }
  -                else
  -                {
  -                    log("  Error: Unknown lifecycle: \""+ lifecycle +"\".");
  -                    log("  Tool not loaded.");
  -                }
  -                continue;
  +                sessionToolInfo.add(stInfo);
               }
  -
  -            // Secondly, handle tools that implement interface ContextViewTool
  -            // (The interface ThreadSafeViewTool is not considered in this case
  -            // because it doesn't matter. A new tool instance is created for every 
  -            // request anyway since the Velocity context needs to be passed.)
  -            if (obj instanceof ContextViewTool)
  +            else if (stInfo.getScope().equalsIgnoreCase(ServletToolInfo.APPLICATION_SCOPE))
               {
  -                log("  Known interface: ContextViewTool");
  -                // These tools always have a life cycle of 'request'
  -                requestTools.add(new ToolInfo(key, obj, classname));
  -                continue;
  +                //add application scoped tools to appTools and
  +                //initialize them with the ServletContext
  +                appTools.put(stInfo.getKey(), stInfo.getInstance(servletContext));
               }
  -            
  -            // Third, handle tools that implement interface ThreadSafeViewTool.
  -            // In this case, the one and only instance of the tool is reused the 
  -            // entire runtime.
  -            if (obj instanceof ThreadSafeViewTool)
  +            else
               {
  -                log("  Known interface: ThreadSafeViewTool");
  -                applicationToolsNotInitialized.add(new ToolInfo(key, obj, classname));
  -                continue;
  +                log("Unknown scope: "+stInfo.getScope()+" "+stInfo.getKey()+" will be request scoped.");
  +                requestToolInfo.add(stInfo);
               }
  -            
  -            // Fourth, handle tools that implement no known interface.
  -            // Unknown view tools are considered not thread-safe and 
  -            // therefore a new instance is created for every template processing
  -            // request. 
  -            log("  Known interface: None. Apply default handling.");
  -            requestTools.add(new ToolInfo(key, obj, classname));
           }
  -
  -        log("Done initializing the toolbox.");
  +        else
  +        {
  +            //default is request scope
  +            requestToolInfo.add(info);
  +        }
       }
   
   
       /**
  -     * Creates a {@link ToolboxContext} from the tools loaded
  -     * in this manager. It uses the given {@link ViewContext}
  -     * to create instances of session and request tools. Request
  -     * tools are created on every call to this method. Session
  -     * tools are created once per session. Application tool instances
  -     * are re-used for the entire runtime.
  +     * Overrides XMLToolboxManager to handle the separate
  +     * scopes.
        *
  -     * @param vcontext the current Velocity context
  -     * @return the created ToolboxContext
  +     * Application scope tools were initialized when the toolbox was loaded.
  +     * Session scope tools are initialized once per session and stored in a
  +     * map in the session attributes.
  +     * Request scope tools are initialized on every request.
  +     * 
        */
  -    public ToolboxContext getToolboxContext(ViewContext vcontext)
  +    public ToolboxContext getToolboxContext(Object initData)
       {
  -        // Only on first request, initialize tools with a life 
  -        // cycle of 'application'. This cannot be done earlier because 
  -        // some tools may need access to data that is only available 
  -        // with a template processing request.
  -        if (applicationTools == null)
  -        {
  -            synchronized (applicationToolsNotInitialized)
  -            {
  -                if (applicationTools == null)
  -                {
  -                    applicationTools = new HashMap(applicationToolsNotInitialized.size());
  -                    Iterator i = applicationToolsNotInitialized.iterator();
  -                    while (i.hasNext())
  -                    {
  -                        ToolInfo info = (ToolInfo)i.next();
  -                        Object tool = info.getTool();
  -
  -                        // First, handle tools that implement ServletViewTool and have
  -                        // a defined life cycle of 'application'
  -                        if (tool instanceof ServletViewTool)
  -                        {
  -                            applicationTools.put(info.getKey(), 
  -                                ((ServletViewTool)tool).getInstance(vcontext));
  -                            continue;
  -                        }
  -
  -                        // Secondly, handle ContextViewTools
  -                        // => they never have an 'application' life cycle, => skip
  -
  -                        // Third, handle tools that implement ThreadSafeViewTool
  -                        if (tool instanceof ThreadSafeViewTool)
  -                        {
  -                            applicationTools.put(info.getKey(), tool);
  -                            continue;
  -                        }
  -
  -                        // There shouldn't be any tool left, otherwise it's an error
  -                        log("Error trying to load unknown tool with a life cycle " +
  -                            "of 'application': key=" + info.getKey() + " class=" + 
  -                            tool.getClass() + ". Tool not loaded.");
  -                    }
  -                }
  -            }
  -        }
  -        
  -        //
  -        // Assemble toolbox
  -        //
  +        //we know the initData is a ViewContext
  +        ViewContext ctx = (ViewContext)initData;
           
  -        // First, add tools with an 'application' life cycle, if any.
  -        Map toolbox = new HashMap(applicationTools);
  -        
  -        // Secondly, add tools with a 'session' life cycle, if any.
  -        if (!sessionTools.isEmpty())
  -        {
  -            HttpSession session = vcontext.getRequest().getSession();
  +        //create the toolbox map with the application tools in it
  +        Map toolbox = new HashMap(appTools);
   
  -            // get the initialized session tools
  -            Map stmap = (Map)session.getAttribute(SESSION_TOOLS_KEY);
  +        if (!sessionToolInfo.isEmpty())
  +        {
  +            HttpSession session = ctx.getRequest().getSession();
   
  -            // if session tools aren't initialized, do so and store 
  -            // them in the session attributes.
  -            if (stmap == null)
  +            //synchronize session tool initialization to avoid potential
  +            //conflicts from multiple simultaneous requests in the same session
  +            synchronized(session)
               {
  -                synchronized (session)
  +                //get the initialized session tools
  +                Map stmap = (Map)session.getAttribute(SESSION_TOOLS_KEY);
  +
  +                //if session tools aren't initialized,
  +                //do so and store them in the session
  +                if (stmap == null)
                   {
  -                    if (stmap == null)
  +                    stmap = new HashMap(sessionToolInfo.size());
  +                    Iterator i = sessionToolInfo.iterator();
  +                    while(i.hasNext())
                       {
  -                        stmap = new HashMap(sessionTools.size());
  -                        Iterator i = sessionTools.iterator();
  -                        while(i.hasNext())
  -                        {
  -                            ToolInfo info = (ToolInfo)i.next();
  -                            Object tool = info.getTool();
  -
  -                            // Only tools of class ServletViewTool can
  -                            // have a life cycle of session.
  -                            try
  -                            {
  -                            stmap.put(info.getKey(), 
  -                                ((ServletViewTool)tool).getInstance(vcontext));
  -                            }
  -                            catch(ClassCastException cce)
  -                            {
  -                                log("Error trying to load unknown tool with" +
  -                                    " a life cycle of 'session': key=" + 
  -                                    info.getKey() + " class=" + tool.getClass() + 
  -                                    ". Tool not loaded.");
  -                            }
  -                            
  -                        }
  -                        session.setAttribute(SESSION_TOOLS_KEY, stmap);
  +                        ToolInfo info = (ToolInfo)i.next();
  +                        stmap.put(info.getKey(), info.getInstance(ctx));
                       }
  +                    session.setAttribute(SESSION_TOOLS_KEY, stmap);
                   }
  -            }
  -            //add the initialized session tools to the toolbox
  -            toolbox.putAll(stmap);
  -        }
  -        
  -        // Thirdly, add tools with a 'request' life cycle, if any.
  -        Iterator i = requestTools.iterator();
  -        while(i.hasNext())
  -        {
  -            ToolInfo info = (ToolInfo)i.next();
  -            Object tool = info.getTool();
   
  -            // First, handle tools that implement ServletViewTool.
  -            // They are initialized with an instance of ViewContext.
  -            if (tool instanceof ServletViewTool)
  -            {
  -                toolbox.put(info.getKey(), 
  -                        ((ServletViewTool)(info.getTool())).getInstance(vcontext));
  -                continue;
  -            }
  -            
  -            // Secondly, handle tools that implement ContextViewTool.
  -            // They are initialized with an instance of Context.
  -            if (tool instanceof ContextViewTool)
  -            {
  -                toolbox.put(info.getKey(), 
  -                        ((ContextViewTool)(info.getTool())).getInstance((Context)vcontext));
  -                continue;
  -            }
  -            
  -            // Third, handle any other tool that does not implement any known
  -            // interface. It is required that these tools have a public 
  -            // contructor with no parameters.
  -            try
  -            {
  -                tool = Class.forName(info.getClassname()).newInstance();
  -                toolbox.put(info.getKey(), tool);
  -                continue;
  -            }
  -            catch(Exception e)
  -            {
  -               log("Error creating instance for class: " + info.getClassname() +
  -                ". " + e);
  -               continue;
  +                //add the initialized session tools to the toolbox
  +                toolbox.putAll(stmap);
               }
           }
  -        
  -        return new ToolboxContext(toolbox);
  -    }
   
  -
  -    /**
  -     * This class holds a view tool's key and original instance.
  -     */
  -    protected final class ToolInfo
  -    {
  -        private String key;
  -        private Object tool;
  -        private String classname;
  -        
  -        ToolInfo(String key, Object tool, String classname)
  -        {
  -            this.key = key;
  -            this.tool = tool;
  -            this.classname = classname;
  -        }
  -        
  -        String getKey()
  -        {
  -            return key;
  -        }
  -        
  -        Object getTool()
  +        //add and initialize request tools
  +        Iterator i = requestToolInfo.iterator();
  +        while(i.hasNext())
           {
  -            return tool;
  +            ToolInfo info = (ToolInfo)i.next();
  +            toolbox.put(info.getKey(), info.getInstance(ctx));
           }
   
  -        String getClassname()
  -        {
  -            return classname;
  -        }
  -        
  +        return new ToolboxContext(toolbox);
       }
   
   
  
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
  
  Index: ServletToolInfo.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view.servlet;
  
  
  import org.apache.velocity.tools.view.ViewToolInfo;
  
  
  /**
   * ToolInfo implementation that holds scope information for tools
   * used in a servlet environment.
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   *
   * @version $Id: ServletToolInfo.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public class ServletToolInfo extends ViewToolInfo
  {
  
      public static final String REQUEST_SCOPE = "request";
      public static final String SESSION_SCOPE = "session";
      public static final String APPLICATION_SCOPE = "application";
          
      private String scope;
  
  
      /**
       * Creates a new tool of the specified class with the given key and scope.
       */
      public ServletToolInfo(String key, String classname, String scope)
          throws Exception
      {
          super(key, classname);
          this.scope = scope;
      }
  
  
      /**
       * @return the scope of the tool
       */
      public String getScope()
      {
          return scope;
      }
  
  
  }
  
  
  
  1.1                  jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/tools/ViewTool.java
  
  Index: ViewTool.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  package org.apache.velocity.tools.view.tools;
  
  
  /**
   * Generic view tool interface to assist in tool management.
   * This interface provides the {@link #init(Object initData)} method 
   * as a hook for ToolboxManager implementations to pass data in to
   * tools to initialize them.  See 
   * {@link org.apache.velocity.tools.view.ViewToolInfo} for more on this.
   *
   * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
   *
   * @version $Id: ViewTool.java,v 1.1 2002/05/10 05:42:18 sidler Exp $
   */
  public interface ViewTool
  {
  
      /**
       * Initializes this instance using the given data
       *
       * @param initData the initialization data 
       */
      public void init(Object initData);
  
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:velocity-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-dev-help@jakarta.apache.org>


Mime
View raw message