org.apache.velocity.tools.view
Class WebappResourceLoader

java.lang.Object
  extended by org.apache.velocity.runtime.resource.loader.ResourceLoader
      extended by org.apache.velocity.tools.view.WebappResourceLoader
Direct Known Subclasses:
WebappLoader

public class WebappResourceLoader
extends org.apache.velocity.runtime.resource.loader.ResourceLoader

Resource loader that uses the ServletContext of a webapp to load Velocity templates. (it's much easier to use with servlets than the standard FileResourceLoader, in particular the use of war files is transparent). The default search path is '/' (relative to the webapp root), but you can change this behaviour by specifying one or more paths by mean of as many webapp.resource.loader.path properties as needed in the velocity.properties file. All paths must be relative to the root of the webapp. To enable caching and cache refreshing the webapp.resource.loader.cache and webapp.resource.loader.modificationCheckInterval properties need to be set in the velocity.properties file ... auto-reloading of global macros requires the webapp.resource.loader.cache property to be set to 'false'.

Version:
$Id: WebappResourceLoader.java 961796 2010-07-08 15:18:15Z apetrelli $
Author:
Geir Magnusson Jr., Nathan Bubna, Claude Brisson

Field Summary
protected  String[] paths
          The root paths for templates (relative to webapp's root).
protected  ServletContext servletContext
           
protected  HashMap templatePaths
           
 
Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc
 
Constructor Summary
WebappResourceLoader()
           
 
Method Summary
 long getLastModified(org.apache.velocity.runtime.resource.Resource resource)
          Checks to see when a resource was last modified
 InputStream getResourceStream(String name)
          Get an InputStream so that the Runtime can build a template with it.
 void init(ExtendedProperties configuration)
          This is abstract in the base class, so we need it.
 boolean isSourceModified(org.apache.velocity.runtime.resource.Resource resource)
          Checks to see if a resource has been deleted, moved or modified.
 
Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
commonInit, getClassName, getModificationCheckInterval, isCachingOn, resourceExists, setCachingOn, setModificationCheckInterval
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

paths

protected String[] paths
The root paths for templates (relative to webapp's root).


templatePaths

protected HashMap templatePaths

servletContext

protected ServletContext servletContext
Constructor Detail

WebappResourceLoader

public WebappResourceLoader()
Method Detail

init

public void init(ExtendedProperties configuration)
This is abstract in the base class, so we need it.
NOTE: this expects that the ServletContext has already been placed in the runtime's application attributes under its full class name (i.e. "javax.servlet.ServletContext").

Specified by:
init in class org.apache.velocity.runtime.resource.loader.ResourceLoader
Parameters:
configuration - the ExtendedProperties associated with this resource loader.

getResourceStream

public InputStream getResourceStream(String name)
                              throws org.apache.velocity.exception.ResourceNotFoundException
Get an InputStream so that the Runtime can build a template with it.

Specified by:
getResourceStream in class org.apache.velocity.runtime.resource.loader.ResourceLoader
Parameters:
name - name of template to get
Returns:
InputStream containing the template
Throws:
org.apache.velocity.exception.ResourceNotFoundException - if template not found in classpath.

isSourceModified

public boolean isSourceModified(org.apache.velocity.runtime.resource.Resource resource)
Checks to see if a resource has been deleted, moved or modified.

Specified by:
isSourceModified in class org.apache.velocity.runtime.resource.loader.ResourceLoader
Parameters:
resource - Resource The resource to check for modification
Returns:
boolean True if the resource has been modified

getLastModified

public long getLastModified(org.apache.velocity.runtime.resource.Resource resource)
Checks to see when a resource was last modified

Specified by:
getLastModified in class org.apache.velocity.runtime.resource.loader.ResourceLoader
Parameters:
resource - Resource the resource to check
Returns:
long The time when the resource was last modified or 0 if the file can't be read


Copyright © 2002-2011 The Apache Software Foundation. All Rights Reserved.