package org.apache.velocity.runtime.resource.loader; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.exception.VelocityException; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.util.ExtProperties; import org.apache.velocity.util.StringUtils; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Vector; /** *

* 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 *

* * @author Aki Nieminen * @author Dave Bryson * @version $Id$ */ public class JarResourceLoader extends ResourceLoader { /** * Maps entries to the parent JAR File * Key = the entry *excluding* plain directories * Value = the JAR URL */ private Map entryDirectory = new HashMap(559); /** * Maps JAR URLs to the actual JAR * Key = the JAR URL * Value = the JAR */ private Map jarfiles = new HashMap(89); /** * Called by Velocity to initialize the loader * @param configuration */ public void init( ExtProperties configuration) { log.trace("JarResourceLoader : initialization starting."); // rest of Velocity engine still use legacy Vector // and Hashtable classes. Classes are implicitly // synchronized even if we don't need it. Vector paths = configuration.getVector("path"); StringUtils.trimStrings(paths); if (paths != null) { log.debug("JarResourceLoader # of paths : {}", paths.size() ); for ( int i=0; i