org.apache.velocity.runtime.resource.loader

Class JarResourceLoader


public class JarResourceLoader
extends ResourceLoader

ResourceLoader to load templates from multiple Jar files.

The configuration of the JarResourceLoader is straightforward - You simply add the JarResourceLoader to the configuration via

    resource.loader = jar
    jar.resource.loader.class = org.apache.velocity.runtime.resource.loader.JarResourceLoader
    jar.resource.loader.path = list of JAR <URL>s
 

So for example, if you had a jar file on your local filesystem, you could simply do

    jar.resource.loader.path = jar:file:/opt/myfiles/jar1.jar
    

Note that jar specification for the .path configuration property conforms to the same rules for the java.net.JarUrlConnection class.

For a working example, see the unit test case, org.apache.velocity.test.MultiLoaderTestCase class

Version:
$Id: JarResourceLoader.java,v 1.16.4.1 2004/03/03 23:23:02 geirm Exp $
Author:
Dave Bryson

Field Summary

private Hashtable
entryDirectory
Maps entries to the parent JAR File Key = the entry *excluding* plain directories Value = the JAR URL
private Hashtable
jarfiles
Maps JAR URLs to the actual JAR Key = the JAR URL Value = the JAR

Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader

className, isCachingOn, modificationCheckInterval, rsvc

Method Summary

private void
addEntries(Hashtable entries)
Copy all the entries into the entryDirectory It will overwrite any duplicate keys.
private void
closeJar(String path)
Closes a Jar file and set its URLConnection to null.
long
getLastModified(Resource resource)
InputStream
getResourceStream(String source)
Get an InputStream so that the Runtime can build a template with it.
void
init(ExtendedProperties configuration)
Called by Velocity to initialize the loader
boolean
isSourceModified(Resource resource)
private void
loadJar(String path)

Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader

commonInit, getClassName, getLastModified, getModificationCheckInterval, getResourceStream, init, isCachingOn, isSourceModified, setCachingOn, setModificationCheckInterval

Field Details

entryDirectory

private Hashtable entryDirectory
Maps entries to the parent JAR File Key = the entry *excluding* plain directories Value = the JAR URL

jarfiles

private Hashtable jarfiles
Maps JAR URLs to the actual JAR Key = the JAR URL Value = the JAR

Method Details

addEntries

private void addEntries(Hashtable entries)
Copy all the entries into the entryDirectory It will overwrite any duplicate keys.

closeJar

private void closeJar(String path)
Closes a Jar file and set its URLConnection to null.

getLastModified

public long getLastModified(Resource resource)
Overrides:
getLastModified in interface ResourceLoader

getResourceStream

public InputStream getResourceStream(String source)
            throws ResourceNotFoundException
Get an InputStream so that the Runtime can build a template with it.
Overrides:
getResourceStream in interface ResourceLoader
Parameters:
Returns:
InputStream containing the template
Throws:
ResourceNotFoundException - if template not found in the file template path.

init

public void init(ExtendedProperties configuration)
Called by Velocity to initialize the loader
Overrides:
init in interface ResourceLoader

isSourceModified

public boolean isSourceModified(Resource resource)
Overrides:
isSourceModified in interface ResourceLoader

loadJar

private void loadJar(String path)

Copyright B) 2002 Apache Software Foundation. All Rights Reserved.