1 package org.apache.velocity.runtime.resource.loader; 2 3 18 19 import java.io.InputStream ; 20 21 import java.util.Hashtable ; 22 import java.util.Vector ; 23 24 import org.apache.velocity.util.StringUtils; 25 import org.apache.velocity.runtime.resource.Resource; 26 import org.apache.velocity.exception.ResourceNotFoundException; 27 import org.apache.commons.collections.ExtendedProperties; 28 29 59 public class JarResourceLoader extends ResourceLoader 60 { 61 66 private Hashtable entryDirectory = new Hashtable (559); 67 68 73 private Hashtable jarfiles = new Hashtable (89); 74 75 78 public void init( ExtendedProperties configuration) 79 { 80 rsvc.info("JarResourceLoader : initialization starting."); 81 82 Vector paths = configuration.getVector("path"); 83 84 87 88 if( paths == null || paths.size() == 0) 89 { 90 paths = configuration.getVector("resource.path"); 91 92 if (paths != null && paths.size() > 0) 93 { 94 rsvc.warn("JarResourceLoader : you are using a deprecated configuration" 95 + " property for the JarResourceLoader -> '<name>.resource.loader.resource.path'." 96 + " Please change to the conventional '<name>.resource.loader.path'."); 97 } 98 } 99 100 rsvc.info("JarResourceLoader # of paths : " + paths.size() ); 101 102 for ( int i=0; i<paths.size(); i++ ) 103 { 104 loadJar( (String )paths.get(i) ); 105 } 106 107 rsvc.info("JarResourceLoader : initialization complete."); 108 } 109 110 private void loadJar( String path ) 111 { 112 rsvc.info("JarResourceLoader : trying to load: " + path); 113 114 if ( path == null ) 116 { 117 rsvc.error("JarResourceLoader : can not load JAR - JAR path is null"); 118 } 119 if ( !path.startsWith("jar:") ) 120 { 121 rsvc.error("JarResourceLoader : JAR path must start with jar: -> " + 122 "see java.net.JarURLConnection for information"); 123 } 124 if ( !path.endsWith("!/") ) 125 { 126 path += "!/"; 127 } 128 129 closeJar( path ); 132 133 JarHolder temp = new JarHolder( rsvc, path ); 135 addEntries( temp.getEntries() ); 137 jarfiles.put( temp.getUrlPath(), temp ); 139 } 140 141 145 private void closeJar( String path ) 146 { 147 if ( jarfiles.containsKey(path) ) 148 { 149 JarHolder theJar = (JarHolder)jarfiles.get(path); 150 theJar.close(); 151 } 152 } 153 154 158 private synchronized void addEntries( Hashtable entries ) 159 { 160 entryDirectory.putAll( entries ); 161 } 162 163 172 public synchronized InputStream getResourceStream( String source ) 173 throws ResourceNotFoundException 174 { 175 InputStream results = null; 176 177 if ( source == null || source.length() == 0) 178 { 179 throw new ResourceNotFoundException("Need to have a resource!"); 180 } 181 182 String normalizedPath = StringUtils.normalizePath( source ); 183 184 if ( normalizedPath == null || normalizedPath.length() == 0 ) 185 { 186 String msg = "JAR resource error : argument " + normalizedPath + 187 " contains .. and may be trying to access " + 188 "content outside of template root. Rejected."; 189 190 rsvc.error( "JarResourceLoader : " + msg ); 191 192 throw new ResourceNotFoundException ( msg ); 193 } 194 195 198 if ( normalizedPath.startsWith("/") ) 199 { 200 normalizedPath = normalizedPath.substring(1); 201 } 202 203 if ( entryDirectory.containsKey( normalizedPath ) ) 204 { 205 String jarurl = (String )entryDirectory.get( normalizedPath ); 206 207 if ( jarfiles.containsKey( jarurl ) ) 208 { 209 JarHolder holder = (JarHolder)jarfiles.get( jarurl ); 210 results = holder.getResource( normalizedPath ); 211 return results; 212 } 213 } 214 215 throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " + 216 source ); 217 218 } 219 220 221 public boolean isSourceModified(Resource resource) 224 { 225 return true; 226 } 227 228 public long getLastModified(Resource resource) 229 { 230 return 0; 231 } 232 } 233 234 235 236 237 238 239 240 241 242 243 | Popular Tags |