nbubna      2004/11/10 20:49:58

  Modified:    src/java/org/apache/velocity/tools/view/servlet
                        VelocityViewServlet.java
  Log:
  add convenience methods for param/prop retrieval, postpone creation of engine 
until initVelocity() is called, and add getVelocityEngine() for subclass access
  
  Revision  Changes    Path
  1.23      +60 -32    
jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet/VelocityViewServlet.java
  
  Index: VelocityViewServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet/VelocityViewServlet.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- VelocityViewServlet.java  11 Nov 2004 04:12:24 -0000      1.22
  +++ VelocityViewServlet.java  11 Nov 2004 04:49:58 -0000      1.23
  @@ -34,7 +34,6 @@
   import org.apache.commons.collections.ExtendedProperties;
   
   import org.apache.velocity.Template;
  -import org.apache.velocity.app.Velocity;
   import org.apache.velocity.app.VelocityEngine;
   import org.apache.velocity.context.Context;
   import org.apache.velocity.exception.ResourceNotFoundException;
  @@ -143,7 +142,8 @@
       /** Cache of writers */
       private static SimplePool writerPool = new SimplePool(40);
   
  -    private VelocityEngine velocity = new VelocityEngine();
  +    /* The engine used to process templates. */
  +    private VelocityEngine velocity = null;
   
       /**
        * The default content type.  When necessary, includes the
  @@ -159,6 +159,7 @@
       private boolean warnOfOutputStreamDeprecation = true;
   
   
  +
       /**
        * <p>Initializes servlet, toolbox and Velocity template engine.
        * Called by the servlet container on loading.</p>
  @@ -176,18 +177,12 @@
           initToolbox(config);
   
           // we can get these now that velocity is initialized
  -        defaultContentType = (String)velocity.getProperty(CONTENT_TYPE);
  -        if (defaultContentType == null)
  -        {
  -            defaultContentType = DEFAULT_CONTENT_TYPE;
  -        }
  +        defaultContentType = 
  +            (String)getVelocityProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
   
           String encoding = 
  -            (String)velocity.getProperty(RuntimeConstants.OUTPUT_ENCODING);
  -        if (encoding == null)
  -        {
  -            encoding = DEFAULT_OUTPUT_ENCODING;
  -        }
  +            (String)getVelocityProperty(RuntimeConstants.OUTPUT_ENCODING,
  +                                        DEFAULT_OUTPUT_ENCODING);
   
           // For non Latin-1 encodings, ensure that the charset is
           // included in the Content-Type header.
  @@ -215,6 +210,51 @@
   
   
       /**
  +     * Looks up an init parameter with the specified key in either the
  +     * ServletConfig or, failing that, in the ServletContext.
  +     */
  +    protected String findInitParameter(ServletConfig config, String key)
  +    {
  +        // check the servlet config
  +        String param = config.getInitParameter(key);
  +
  +        if (param == null || param.length() == 0) 
  +        {
  +            // check the servlet context
  +            ServletContext servletContext = config.getServletContext();
  +            param = servletContext.getInitParameter(key);
  +        }
  +        return param;
  +    }
  +
  +
  +    /**
  +     * Simplifies process of getting a property from VelocityEngine,
  +     * because the VelocityEngine interface sucks compared to the 
singleton's.
  +     * Use of this method assumes that [EMAIL PROTECTED] 
#initVelocity(ServletConfig)}
  +     * has already been called.
  +     */
  +    protected String getVelocityProperty(String key, String alternate)
  +    {
  +        String prop = (String)velocity.getProperty(key);
  +        if (prop == null || prop.length() == 0)
  +        {
  +            return alternate;
  +        }
  +        return prop;
  +    }
  +
  +
  +    /**
  +     * Returns the underlying VelocityEngine being used.
  +     */
  +    protected VelocityEngine getVelocityEngine()
  +    {
  +        return velocity;
  +    }
  +
  +
  +    /**
        * Initializes the ServletToolboxManager for this servlet's
        * toolbox (if any).
        *
  @@ -222,22 +262,14 @@
        */
       protected void initToolbox(ServletConfig config) throws ServletException
       {
  -        ServletContext servletContext = config.getServletContext();
  -
  -        /* check the servlet config for a toolbox */
  -        String file = config.getInitParameter(TOOLBOX_KEY);
  -
  -        /* check the servlet context for a toolbox */
  -        if (file == null || file.length() == 0) 
  -        {
  -            file = servletContext.getInitParameter(TOOLBOX_KEY);
  -        }
  +        /* check the servlet config and context for a toolbox param */
  +        String file = findInitParameter(config, TOOLBOX_KEY);
   
           /* if we have a toolbox, get a manager for it */
           if (file != null)
           {
               toolboxManager = 
  -                ServletToolboxManager.getInstance(servletContext, file);
  +                ServletToolboxManager.getInstance(getServletContext(), file);
           }
           else
           {
  @@ -260,6 +292,8 @@
        */
       protected void initVelocity(ServletConfig config) throws ServletException
       {
  +        velocity = new VelocityEngine();
  +
           velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, 
getServletContext());
   
           // default to servletlogger, which logs to the servlet engines log
  @@ -346,19 +380,13 @@
       protected ExtendedProperties loadConfiguration(ServletConfig config)
           throws IOException
       {
  -        ServletContext servletContext = config.getServletContext();
  -
           // grab the path to the custom props file (if any)
  -        String propsFile = config.getInitParameter(INIT_PROPS_KEY);
  -        if (propsFile == null || propsFile.length() == 0)
  -        {
  -            propsFile = servletContext.getInitParameter(INIT_PROPS_KEY);
  -        }
  +        String propsFile = findInitParameter(config, INIT_PROPS_KEY);
           
           ExtendedProperties p = new ExtendedProperties();
           if (propsFile != null)
           {
  -            p.load(servletContext.getResourceAsStream(propsFile));
  +            p.load(getServletContext().getResourceAsStream(propsFile));
   
               velocity.info("VelocityViewServlet: Custom Properties File: 
"+propsFile);
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to