1 package org.apache.velocity.runtime.resource.loader;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.JarURLConnection;
25 import java.net.URL;
26 import java.util.Enumeration;
27 import java.util.jar.JarEntry;
28 import java.util.jar.JarFile;
29 import java.util.Hashtable;
30
31 import org.apache.velocity.runtime.RuntimeServices;
32 import org.apache.velocity.runtime.log.Log;
33 import org.apache.velocity.exception.ResourceNotFoundException;
34
35
36
37
38
39
40
41 public class JarHolder
42 {
43 private String urlpath = null;
44 private JarFile theJar = null;
45 private JarURLConnection conn = null;
46
47 private Log log = null;
48
49
50
51
52
53 public JarHolder( RuntimeServices rs, String urlpath )
54 {
55 this.log = rs.getLog();
56
57 this.urlpath=urlpath;
58 init();
59
60 if (log.isDebugEnabled())
61 {
62 log.debug("JarHolder: initialized JAR: " + urlpath);
63 }
64 }
65
66
67
68
69 public void init()
70 {
71 try
72 {
73 if (log.isDebugEnabled())
74 {
75 log.debug("JarHolder: attempting to connect to " + urlpath);
76 }
77 URL url = new URL( urlpath );
78 conn = (JarURLConnection) url.openConnection();
79 conn.setAllowUserInteraction(false);
80 conn.setDoInput(true);
81 conn.setDoOutput(false);
82 conn.connect();
83 theJar = conn.getJarFile();
84 }
85 catch (IOException ioe)
86 {
87 log.error("JarHolder: error establishing connection to JAR at \""
88 + urlpath + "\"", ioe);
89 }
90 }
91
92
93
94
95 public void close()
96 {
97 try
98 {
99 theJar.close();
100 }
101 catch ( Exception e )
102 {
103 log.error("JarHolder: error closing the JAR file", e);
104 }
105 theJar = null;
106 conn = null;
107
108 log.trace("JarHolder: JAR file closed");
109 }
110
111
112
113
114
115
116 public InputStream getResource( String theentry )
117 throws ResourceNotFoundException {
118 InputStream data = null;
119
120 try
121 {
122 JarEntry entry = theJar.getJarEntry( theentry );
123
124 if ( entry != null )
125 {
126 data = theJar.getInputStream( entry );
127 }
128 }
129 catch(Exception fnfe)
130 {
131 log.error("JarHolder: getResource() error", fnfe);
132 throw new ResourceNotFoundException(fnfe);
133 }
134
135 return data;
136 }
137
138
139
140
141 public Hashtable getEntries()
142 {
143 Hashtable allEntries = new Hashtable(559);
144
145 Enumeration all = theJar.entries();
146 while ( all.hasMoreElements() )
147 {
148 JarEntry je = (JarEntry)all.nextElement();
149
150
151 if ( !je.isDirectory() )
152 {
153 allEntries.put( je.getName(), this.urlpath );
154 }
155 }
156 return allEntries;
157 }
158
159
160
161
162 public String getUrlPath()
163 {
164 return urlpath;
165 }
166 }
167
168
169
170
171
172
173