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.info("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 log.error("JarResourceLoader : can not load JAR - JAR path is null");
136 }
137 if ( !path.startsWith("jar:") )
138 {
139 log.error("JarResourceLoader : JAR path must start with jar: -> " +
140 "see java.net.JarURLConnection for information");
141 }
142 if ( !path.endsWith("!/") )
143 {
144 path += "!/";
145 }
146
147
148
149 closeJar( path );
150
151
152 JarHolder temp = new JarHolder( rsvc, path );
153
154 addEntries( temp.getEntries() );
155
156 jarfiles.put( temp.getUrlPath(), temp );
157 }
158
159
160
161
162
163 private void closeJar( String path )
164 {
165 if ( jarfiles.containsKey(path) )
166 {
167 JarHolder theJar = (JarHolder)jarfiles.get(path);
168 theJar.close();
169 }
170 }
171
172
173
174
175
176 private void addEntries( Hashtable entries )
177 {
178 entryDirectory.putAll( entries );
179 }
180
181
182
183
184
185
186
187
188
189
190 public InputStream getResourceStream( String source )
191 throws ResourceNotFoundException
192 {
193 InputStream results = null;
194
195 if (org.apache.commons.lang.StringUtils.isEmpty(source))
196 {
197 throw new ResourceNotFoundException("Need to have a resource!");
198 }
199
200 String normalizedPath = StringUtils.normalizePath( source );
201
202 if ( normalizedPath == null || normalizedPath.length() == 0 )
203 {
204 String msg = "JAR resource error : argument " + normalizedPath +
205 " contains .. and may be trying to access " +
206 "content outside of template root. Rejected.";
207
208 log.error( "JarResourceLoader : " + msg );
209
210 throw new ResourceNotFoundException ( msg );
211 }
212
213
214
215
216 if ( normalizedPath.startsWith("/") )
217 {
218 normalizedPath = normalizedPath.substring(1);
219 }
220
221 if ( entryDirectory.containsKey( normalizedPath ) )
222 {
223 String jarurl = (String)entryDirectory.get( normalizedPath );
224
225 if ( jarfiles.containsKey( jarurl ) )
226 {
227 JarHolder holder = (JarHolder)jarfiles.get( jarurl );
228 results = holder.getResource( normalizedPath );
229 return results;
230 }
231 }
232
233 throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " +
234 source );
235
236 }
237
238
239
240
241
242
243
244 public boolean isSourceModified(Resource resource)
245 {
246 return true;
247 }
248
249
250
251
252 public long getLastModified(Resource resource)
253 {
254 return 0;
255 }
256 }