nbubna      2003/02/19 11:59:02

  Modified:    view/src/java/org/apache/velocity/tools/view/servlet
                        ServletLogger.java VelocityViewServlet.java
                        WebappLoader.java
  Removed:     view/src/java/org/apache/velocity/tools/view/servlet
                        WebappLoaderAppContext.java
  Log:
  -Allow logger and resource loader defaults to be overridden by velocity.properties
  -Remove wrapper around servlet context and place it directly in application 
attributes
   under an appropriate key
  -Convert ServletLogger and WebappResourceLoader to retrieve servlet context from
   application attributes during initialization
  
  much thanks to Anthony for contributing the fundamentals of the patch
  
  PR: 17942
  
  Submitted by: Anthony Kay
  
  Revision  Changes    Path
  1.2       +18 -7     
jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletLogger.java
  
  Index: ServletLogger.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletLogger.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ServletLogger.java        3 Jan 2002 20:21:55 -0000       1.1
  +++ ServletLogger.java        19 Feb 2003 19:59:02 -0000      1.2
  @@ -60,6 +60,7 @@
   import org.apache.velocity.runtime.log.LogSystem;
   import org.apache.velocity.runtime.RuntimeConstants;
   import org.apache.velocity.runtime.RuntimeServices;
  +import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
   
   /**
    *  Simple wrapper for the servlet log
  @@ -72,21 +73,31 @@
   
       public static final String PREFIX = " Velocity ";
   
  -    public ServletLogger( ServletContext sc )
  -    {
  -        servletContext = sc;
  -    }
  -
  -    private ServletLogger()
  +    /**
  +     * Construct a logger for VelocityViewServlets. Instances of this class
  +     * assume that the ServletContext will be available through the 
  +     * Application Attributes of Velocity, under the
  +     * VelocityViewServlet.SERVLET_CONTEXT_KEY.
  +     */
  +    public ServletLogger()
       {
       }
   
       /**
  -     *  init()
  +     * init()
  +     * 
  +     * @throws IllegalStateException if the ServletContext is not available
  +     *         in the application attributes under the appropriate key.
        */
       public void init( RuntimeServices rs ) 
           throws Exception
       {
  +        Object obj = 
rs.getApplicationAttribute(VelocityViewServlet.SERVLET_CONTEXT_KEY);
  +        if (obj == null)
  +        {
  +            throw new IllegalStateException("Could not retrieve ServletContext from 
application attributes!");
  +        }
  +        servletContext = (ServletContext)obj;
       }
   
       /**
  
  
  
  1.8       +18 -42    
jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/VelocityViewServlet.java
  
  Index: VelocityViewServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/VelocityViewServlet.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- VelocityViewServlet.java  13 Feb 2003 04:42:16 -0000      1.7
  +++ VelocityViewServlet.java  19 Feb 2003 19:59:02 -0000      1.8
  @@ -80,6 +80,7 @@
   import org.apache.velocity.tools.view.context.ViewContext;
   import org.apache.velocity.tools.view.context.ChainedContext;
   import org.apache.velocity.tools.view.servlet.ServletToolboxManager;
  +import org.apache.velocity.tools.view.servlet.WebappLoader;
   
   
   /**
  @@ -126,6 +127,12 @@
   
   
       /**
  +     * Key used to access the ServletContext in the Velocity
  +     */
  +    public static final String SERVLET_CONTEXT_KEY = "javax.servlet.ServletContext";
  +
  +
  +    /**
        * A reference to the toolbox manager.
        */
       protected ServletToolboxManager toolboxManager = null;
  @@ -173,7 +180,16 @@
       protected void initVelocity( ServletConfig config )
            throws ServletException
       {
  -        // Try reading Velocity configuration
  +        // default to servletlogger, which logs to the servlet engines log
  +        Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, 
  +                             ServletLogger.class.getName());
  +
  +        // by default, load resources with webapp resource loader
  +        Velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, getServletContext());
  +        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp");
  +        Velocity.setProperty("webapp.resource.loader.class", 
WebappLoader.class.getName());
  +
  +        // Try reading an overriding Velocity configuration
           try
           {
               Properties p = super.loadConfiguration(config);
  @@ -185,16 +201,6 @@
               getServletContext().log("Using default Velocity configuration.");
           }   
   
  -        // define servletlogger, which logs to the servlet engines log
  -        ServletLogger sl = new ServletLogger( getServletContext() );
  -        Velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, sl );
  -
  -        // load resources with webapp resource loader
  -        VelocityStrutsServletAppContext vssac = new 
VelocityStrutsServletAppContext( getServletContext() );
  -        Velocity.setApplicationAttribute( 
"org.apache.velocity.tools.view.servlet.WebappLoader",  vssac );
  -        Velocity.setProperty( "resource.loader", "webapp" );
  -        Velocity.setProperty( "webapp.resource.loader.class", 
"org.apache.velocity.tools.view.servlet.WebappLoader" );
  -
           // now all is ready - init Velocity
           try
           {
  @@ -269,34 +275,4 @@
       }
   
   
  -    /**
  -     * <p>Wrapper class to safely pass the servlet context to the web app
  -     * loader.</p>
  -     */
  -    public class VelocityStrutsServletAppContext implements WebappLoaderAppContext
  -    {
  -        /**
  -         * A reference to the servlet context
  -         */
  -        ServletContext servletContext = null;
  -
  -
  -        /**
  -         * Default constructor.
  -         */
  -        VelocityStrutsServletAppContext( ServletContext sc )
  -        {
  -            servletContext = sc;
  -        }
  -
  -
  -        /**
  -         * Returns a reference to the servlet context.
  -         */
  -        public ServletContext getServletContext()
  -        {
  -           return servletContext;
  -        }
  -   }
  -   
   }
  
  
  
  1.2       +6 -4      
jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/WebappLoader.java
  
  Index: WebappLoader.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/WebappLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebappLoader.java 3 Jan 2002 20:21:55 -0000       1.1
  +++ WebappLoader.java 19 Feb 2003 19:59:02 -0000      1.2
  @@ -64,6 +64,8 @@
   
   import org.apache.velocity.exception.ResourceNotFoundException;
   
  +import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
  +
   import org.apache.commons.collections.ExtendedProperties;
   
   /**
  @@ -82,11 +84,11 @@
       {
           rsvc.info("WebappLoader : initialization starting.");
   
  -        Object o = rsvc.getApplicationAttribute( 
"org.apache.velocity.tools.view.servlet.WebappLoader" );
  +        Object obj = 
rsvc.getApplicationAttribute(VelocityViewServlet.SERVLET_CONTEXT_KEY);
   
  -        if ( o instanceof WebappLoaderAppContext)
  +        if (obj instanceof ServletContext)
           {
  -            servletContext = ( (WebappLoaderAppContext) o ).getServletContext();
  +            servletContext = (ServletContext)obj;
           }
           else
               rsvc.error("WebappLoader : unable to retrieve ServletContext");
  
  
  

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

Reply via email to