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.InputStream;
23
24 import java.util.Hashtable;
25 import java.util.Vector;
26 import java.util.Map;
27 import java.util.HashMap;
28
29 import org.apache.velocity.util.StringUtils;
30 import org.apache.velocity.runtime.resource.Resource;
31 import org.apache.velocity.exception.ResourceNotFoundException;
32 import org.apache.commons.collections.ExtendedProperties;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class JarResourceLoader extends ResourceLoader
66 {
67
68
69
70
71
72 private Map entryDirectory = new HashMap(559);
73
74
75
76
77
78
79 private Map jarfiles = new HashMap(89);
80
81
82
83
84
85 public void init( ExtendedProperties configuration)
86 {
87 log.trace("JarResourceLoader : initialization starting.");
88
89
90
91
92 Vector paths = configuration.getVector("path");
93 StringUtils.trimStrings(paths);
94
95
96
97
98
99 if( paths == null || paths.size() == 0)
100 {
101 paths = configuration.getVector("resource.path");
102 StringUtils.trimStrings(paths);
103
104 if (paths != null && paths.size() > 0)
105 {
106 log.debug("JarResourceLoader : you are using a deprecated configuration"
107 + " property for the JarResourceLoader -> '<name>.resource.loader.resource.path'."
108 + " Please change to the conventional '<name>.resource.loader.path'.");
109 }
110 }
111
112 if (paths != null)
113 {
114 log.debug("JarResourceLoader # of paths : " + paths.size() );
115
116 for ( int i=0; i<paths.size(); i++ )
117 {
118 loadJar( (String)paths.get(i) );
119 }
120 }
121
122 log.trace("JarResourceLoader : initialization complete.");
123 }
124
125 private void loadJar( String path )
126 {
127 if (log.isDebugEnabled())
128 {
129 log.debug("JarResourceLoader : trying to load \"" + path + "\"");
130 }
131
132
133 if ( path == null )
134 {
135 String msg = "JarResourceLoader : can not load JAR - JAR path is null";
136 log.error(msg);
137 throw new RuntimeException(msg);
138 }
139 if ( !path.startsWith("jar:") )
140 {
141 String msg = "JarResourceLoader : JAR path must start with jar: -> see java.net.JarURLConnection for information";
142 log.error(msg);
143 throw new RuntimeException(msg);
144 }
145 if ( path.indexOf("!/") < 0 )
146 {
147 path += "!/";
148 }
149
150
151
152 closeJar( path );
153
154
155 JarHolder temp = new JarHolder( rsvc, path );
156
157 addEntries( temp.getEntries() );
158
159 jarfiles.put( temp.getUrlPath(), temp );
160 }
161
162
163
164
165
166 private void closeJar( String path )
167 {
168 if ( jarfiles.containsKey(path) )
169 {
170 JarHolder theJar = (JarHolder)jarfiles.get(path);
171 theJar.close();
172 }
173 }
174
175
176
177
178
179 private void addEntries( Hashtable entries )
180 {
181 entryDirectory.putAll( entries );
182 }
183
184
185
186
187
188
189
190
191
192
193 public InputStream getResourceStream( String source )
194 throws ResourceNotFoundException
195 {
196 InputStream results = null;
197
198 if (org.apache.commons.lang.StringUtils.isEmpty(source))
199 {
200 throw new ResourceNotFoundException("Need to have a resource!");
201 }
202
203 String normalizedPath = StringUtils.normalizePath( source );
204
205 if ( normalizedPath == null || normalizedPath.length() == 0 )
206 {
207 String msg = "JAR resource error : argument " + normalizedPath +
208 " contains .. and may be trying to access " +
209 "content outside of template root. Rejected.";
210
211 log.error( "JarResourceLoader : " + msg );
212
213 throw new ResourceNotFoundException ( msg );
214 }
215
216
217
218
219 if ( normalizedPath.startsWith("/") )
220 {
221 normalizedPath = normalizedPath.substring(1);
222 }
223
224 if ( entryDirectory.containsKey( normalizedPath ) )
225 {
226 String jarurl = (String)entryDirectory.get( normalizedPath );
227
228 if ( jarfiles.containsKey( jarurl ) )
229 {
230 JarHolder holder = (JarHolder)jarfiles.get( jarurl );
231 results = holder.getResource( normalizedPath );
232 return results;
233 }
234 }
235
236 throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " +
237 source );
238
239 }
240
241
242
243
244
245
246
247 public boolean isSourceModified(Resource resource)
248 {
249 return true;
250 }
251
252
253
254
255 public long getLastModified(Resource resource)
256 {
257 return 0;
258 }
259 }