1 package org.apache.velocity.runtime.resource.loader; 2 3 18 19 import java.io.InputStream ; 20 import java.net.JarURLConnection ; 21 import java.net.URL ; 22 import java.util.Enumeration ; 23 import java.util.jar.JarEntry ; 24 import java.util.jar.JarFile ; 25 import java.util.Hashtable ; 26 27 import org.apache.velocity.runtime.RuntimeServices; 28 29 import org.apache.velocity.exception.ResourceNotFoundException; 30 31 37 public class JarHolder 38 { 39 private String urlpath = null; 40 private JarFile theJar = null; 41 private JarURLConnection conn = null; 42 43 private RuntimeServices rsvc = null; 44 45 public JarHolder( RuntimeServices rs, String urlpath ) 46 { 47 rsvc = rs; 48 49 this.urlpath=urlpath; 50 init(); 51 52 rsvc.info(" JarHolder : initialized JAR: " + urlpath ); 53 } 54 55 public void init() 56 { 57 try 58 { 59 rsvc.info(" JarHolder : attempting to connect to "+ urlpath); 60 URL url = new URL ( urlpath ); 61 conn = (JarURLConnection ) url.openConnection(); 62 conn.setAllowUserInteraction(false); 63 conn.setDoInput(true); 64 conn.setDoOutput(false); 65 conn.connect(); 66 theJar = conn.getJarFile(); 67 } 68 catch (Exception e) 69 { 70 rsvc.error(" JarHolder : error establishing connection to JAR "+ e); 71 } 72 } 73 74 public void close() 75 { 76 try 77 { 78 theJar.close(); 79 } 80 catch ( Exception e ) 81 { 82 rsvc.error(" JarHolder : error Closing JAR the file " + e); 83 } 84 theJar = null; 85 conn = null; 86 87 rsvc.info(" JarHolder : JAR file closed"); 88 } 89 90 public InputStream getResource( String theentry ) 91 throws ResourceNotFoundException { 92 InputStream data = null; 93 94 try 95 { 96 JarEntry entry = theJar.getJarEntry( theentry ); 97 98 if ( entry != null ) 99 { 100 data = theJar.getInputStream( entry ); 101 } 102 } 103 catch( Exception fnfe ) 104 { 105 rsvc.error(" JarHolder : getResource() error : exception : " + fnfe ); 106 throw new ResourceNotFoundException( fnfe.getMessage() ); 107 } 108 109 return data; 110 } 111 112 public Hashtable getEntries() 113 { 114 Hashtable allEntries = new Hashtable (559); 115 116 Enumeration all = theJar.entries(); 117 while ( all.hasMoreElements() ) 118 { 119 JarEntry je = (JarEntry )all.nextElement(); 120 121 if ( !je.isDirectory() ) 123 { 124 allEntries.put( je.getName(), this.urlpath ); 125 } 126 } 127 return allEntries; 128 } 129 130 public String getUrlPath() 131 { 132 return urlpath; 133 } 134 } 135 136 137 138 139 140 141 142 | Popular Tags |