velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Barbara Baughman <baugh...@utdallas.edu>
Subject Re: ResourceNotFoundException trying to use FileResouceLoader
Date Wed, 19 Mar 2003 21:14:58 GMT
Since Velocity is a templating language that cannot assume it is being
used by a web application, it assumes the home directory is the directory
the program is running in.  In the case of the web application, it is
being run by the servlet container, which for tomcat is tomcat.home/bin.

To make it do something different, you'll need to specifically tell
Velocity that the parameters in properties are relative to the webapp.home
directory.  If you are working with VelocityServlet, then overload the
loadConfiguration() method with the following code.

    protected Properties loadConfiguration(ServletConfig config )
            throws IOException, FileNotFoundException {
        /* get properties file and load it from web application root */
     
        String propsFile = config.getInitParameter(INIT_PROPS_KEY);
        Properties p = new Properties();
     
        if ( propsFile != null ) {
            String realPath = context.getRealPath(propsFile);
            if ( realPath != null ) {
                propsFile = realPath;
            }
            p.load( new FileInputStream(propsFile) );
            PropertyConfigurator.configure(propsFile);
        }
        
        /* Set velocity log file relative to web application root */
        String log = p.getProperty( Velocity.RUNTIME_LOG);
        if (log != null ) {
            log = context.getRealPath( log );
            if (log != null) {
                p.setProperty( Velocity.RUNTIME_LOG, log );
            }
        }
         
        /*
         *  The file loader resource path is the path to the templates
         *  Make this relative to web application root
         */
        String path = p.getProperty( Velocity.FILE_RESOURCE_LOADER_PATH );
        if ( path != null) {
            path = context.getRealPath(  path );
            if ( path != null) {
                p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path );
            }
        }
        return p;
    }

If you are building your own VelocityEngine, then first create a
Properties object from the properties file in your servlet's init() method
and then copy the code above that sets the velocity log file and
fileloader relative to the webapp home.  Getting the properties object
depends on how you setup your web.xml file and what version of the servlet
API you're using.  This works for me using tomcat 4.1 and the servlet 2.3
API, provided web.xml contains a reference to a param-name "properties"
that points to the velocity properties filename relative to the
webapp home directory.  It looks first in the ServletConfig object and
then in the ServletContext object for the param-name.

    public static Properties getProperties(ServletConfig config)
        Vector msg=new Vector();
        Properties p=new Properties();
        ServletContext ctx=config.getServletContext();
        String propFile=config.getInitParameter("properties");
        if (propFile==null) {
            propFile=ctx.getInitParameter("properties");
        }
        if (propFile==null) {
            msg.add("No properties param in web.xml");
            return p;
        }
        String realPath=ctx.getRealPath(propFile);
        if (realPath!=null) {
            propFile=realPath;
        }
        try {
            p.load(new FileInputStream(propFile));
            msg.add("Loaded properties from "+propFile);
        } catch (FileNotFoundException fnfe) {
            msg.add("Properties file not found "
                    +propFile);
        } catch (IOException ioe) {
            msg.add("Cannot read file "+propFile);
        }
        return p;
    }

Best wishes.

Barbara Baughman
X2157

On Wed, 19 Mar 2003, Kevin Hooke wrote:

> Hi - I am calling the Velocity engine as a singleton in a class used by a
> servlet on tomcat 4.1.
> 
> My templates are currently being loaded from tomcat4.1/bin.
> 
> Following other suggestions in the archive of this mail list I have set the
> following properties to init the engine to use the file resource loader, and
> set the base directory:
> 
> Velocity.init();
> Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, _outputDir);
> Velocity.setProperty(Velocity.RESOURCE_LOADER, "file");
> 
> where _output dir is "f:/somedir"
> 
> The engine is still loading the template from tomcat4.1/bin, despite the
> above settings. Am I on the right track here or are there some other
> settings I need to set to get Velocity to load from the dir I want?
> 
> I notice in the velocity.log (also in tomcat4.1/bin) that it is reading the
> default velocity.properties file from the velocity jar. Should I set up my
> own properties file, or is it ok to set these properties programatically?
> 
> Thanks,
> 
> Kevin Hooke
> MindBeans Software Consulting
> http://www.mindbeans.net
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message