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.loaders = jar
        resource.loader.jar.class = org.apache.velocity.runtime.resource.loader.JarResourceLoader
        resource.loader.jar.path = list of JAR <URL>s
     

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

    
        resource.loader.jar.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$
    • Field Detail

      • entryDirectory

        private java.util.Map<java.lang.String,​java.lang.String> entryDirectory
        Maps entries to the parent JAR File Key = the entry *excluding* plain directories Value = the JAR URL
      • jarfiles

        private java.util.Map<java.lang.String,​JarHolder> jarfiles
        Maps JAR URLs to the actual JAR Key = the JAR URL Value = the JAR
    • Constructor Detail

      • JarResourceLoader

        public JarResourceLoader()
    • Method Detail

      • init

        public void init​(ExtProperties configuration)
        Called by Velocity to initialize the loader
        Specified by:
        init in class ResourceLoader
        Parameters:
        configuration -
      • loadJar

        private void loadJar​(java.lang.String path)
      • closeJar

        private void closeJar​(java.lang.String path)
        Closes a Jar file and set its URLConnection to null.
      • addEntries

        private void addEntries​(java.util.Map<java.lang.String,​java.lang.String> entries)
        Copy all the entries into the entryDirectory It will overwrite any duplicate keys.
      • getResourceReader

        public java.io.Reader getResourceReader​(java.lang.String source,
                                                java.lang.String encoding)
                                         throws ResourceNotFoundException
        Get a Reader so that the Runtime can build a template with it.
        Specified by:
        getResourceReader in class ResourceLoader
        Parameters:
        source - name of template to get
        encoding - asked encoding
        Returns:
        InputStream containing the template
        Throws:
        ResourceNotFoundException - if template not found in the file template path.
        Since:
        2.0