You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Anthony La Forge <e_...@hotmail.com> on 2002/02/03 04:08:44 UTC

Fix for Caucho and VelocityServlet Template Loading Issue

This morning I came accross an error with Caucho and Velocity in which I could not find any template files in my .war file.

I was storing my template files in:
  resin-2.0.1\webapps\myapp

I was storing my servlets class files in:
  resin-2.0.1\webapps\myapp\web-inf\classes

I was storing my library files (including velocity) in:
  resin-2.0.1\webapps\myapp\web-inf\lib

After about an decent amount of time moving the template files around Resin, and tinkering with
my code it occured to me to modify the velocity base code. Accordingly I went into the
VelocityServlet class and changed the init method, and added a setProperty, which changed the
base path.  This seemed to correct the issue.  Are there any implications to this for other
servlet engines?  I realize that this bug technically is more of an issue with Caucho, however
since I don't think they will be changing their code any time soon (and I'm not about to tinker
with the inner workings of their servlet engine for a 3 line fix), it might be nice if you guys
included this change, or something like it in future versions.


The fix is below:


    public void init( ServletConfig config )
        throws ServletException
    {
        super.init( config );
                
        try
        {
            /*
             *  call the overridable method to allow the 
             *  derived classes a shot at altering the configuration
             *  before initializing Runtime
             */

            Properties props = loadConfiguration( config );

            //Code correction for Caucho added here
            //<Fix>
  
            String path = getServletContext().getRealPath("\\");

            if(path != null)
            {
             props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);             
            };

            //</Fix>

            
            Velocity.init(props);            
             
            defaultContentType = RuntimeSingleton.getString( CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
            
            encoding = RuntimeSingleton.getString( RuntimeSingleton.OUTPUT_ENCODING, DEFAULT_OUTPUT_ENCODING);
        }
        catch( Exception e )
        {
            throw new ServletException("Error configuring the loader: " + e);
        }
    }

Re: Fix for Caucho and VelocityServlet Template Loading Issue

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
If you look at the example code in
examples/servlet_example2/SampleServlet2.java, you can see that we do a
similar trick there.

One issue someone might have with your solution is that they might want to
keep templates outside of the web context, and if we did this in the base
code, there would be a problem.  That's why it's offered as an 'suggestion'
in the example listed above.

geir


On 2/2/02 10:08 PM, "Anthony La Forge" <e_...@hotmail.com> wrote:

> This morning I came accross an error with Caucho and Velocity in which I could
> not find any template files in my .war file.
> 
> I was storing my template files in:
> resin-2.0.1\webapps\myapp
> 
> I was storing my servlets class files in:
> resin-2.0.1\webapps\myapp\web-inf\classes
> 
> I was storing my library files (including velocity) in:
> resin-2.0.1\webapps\myapp\web-inf\lib
> 
> After about an decent amount of time moving the template files around Resin,
> and tinkering with
> my code it occured to me to modify the velocity base code. Accordingly I went
> into the
> VelocityServlet class and changed the init method, and added a setProperty,
> which changed the
> base path.  This seemed to correct the issue.  Are there any implications to
> this for other
> servlet engines?  I realize that this bug technically is more of an issue with
> Caucho, however
> since I don't think they will be changing their code any time soon (and I'm
> not about to tinker
> with the inner workings of their servlet engine for a 3 line fix), it might be
> nice if you guys
> included this change, or something like it in future versions.
> 
> 
> The fix is below:
> 
> 
>   public void init( ServletConfig config )
>       throws ServletException
>   {
>       super.init( config );
>               
>       try
>       {
>           /*
>            *  call the overridable method to allow the
>            *  derived classes a shot at altering the configuration
>            *  before initializing Runtime
>            */
> 
>           Properties props = loadConfiguration( config );
> 
>           //Code correction for Caucho added here
>           //<Fix>
> 
>           String path = getServletContext().getRealPath("\\");
> 
>           if(path != null)
>           {
>            props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
>           };
> 
>           //</Fix>
> 
>           
>           Velocity.init(props);
>            
>           defaultContentType = RuntimeSingleton.getString( CONTENT_TYPE,
> DEFAULT_CONTENT_TYPE);
>           
>           encoding = RuntimeSingleton.getString(
> RuntimeSingleton.OUTPUT_ENCODING, DEFAULT_OUTPUT_ENCODING);
>       }
>       catch( Exception e )
>       {
>           throw new ServletException("Error configuring the loader: " + e);
>       }
>   }
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>