Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ProjectResourceLoader |
|
| 5.0;5 |
1 | package org.apache.maven.resource.loader; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2006 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import org.apache.commons.collections.ExtendedProperties; |
|
20 | import org.apache.velocity.exception.ResourceNotFoundException; |
|
21 | import org.apache.velocity.runtime.resource.Resource; |
|
22 | import org.apache.velocity.runtime.resource.loader.ResourceLoader; |
|
23 | import org.apache.velocity.util.StringUtils; |
|
24 | ||
25 | import java.io.BufferedInputStream; |
|
26 | import java.io.File; |
|
27 | import java.io.FileInputStream; |
|
28 | import java.io.FileNotFoundException; |
|
29 | import java.io.InputStream; |
|
30 | import java.util.Hashtable; |
|
31 | import java.util.Vector; |
|
32 | ||
33 | /** |
|
34 | * Resource Loader for external projects. |
|
35 | * |
|
36 | * @version $Id: ProjectResourceLoader.java 422497 2006-07-16 18:54:49 +0000 (sö, 16 jul 2006) dennisl $ |
|
37 | */ |
|
38 | 0 | public class ProjectResourceLoader extends ResourceLoader |
39 | { |
|
40 | 0 | /** |
41 | * The paths to search for templates. |
|
42 | */ |
|
43 | 0 | private Vector paths = null; |
44 | ||
45 | 0 | /** |
46 | * Used to map the path that a template was found on |
|
47 | * so that we can properly check the modification |
|
48 | * times of the files. |
|
49 | */ |
|
50 | 0 | private Hashtable templatePaths = new Hashtable(); |
51 | ||
52 | 0 | public void init( ExtendedProperties configuration ) |
53 | { |
|
54 | 0 | rsvc.info( "ProjectResourceLoader : initialization starting." ); |
55 | ||
56 | 0 | String separator = System.getProperty( "file.separator" ); |
57 | ||
58 | 0 | String path = System.getProperty( "user.dir" ) + separator + "src" |
59 | + separator + "main" + separator + "resources" + separator; |
|
60 | 0 | |
61 | 0 | rsvc.info( "path :" + path ); |
62 | ||
63 | 0 | paths = new Vector(); |
64 | ||
65 | 0 | paths.add( path ); |
66 | ||
67 | 0 | int sz = paths.size(); |
68 | ||
69 | 0 | for ( int i = 0; i < sz; i++ ) |
70 | { |
|
71 | 0 | rsvc.info( "ProjectResourceLoader : adding path '" + (String) paths.get( i ) + "'" ); |
72 | } |
|
73 | 0 | rsvc.info( "ProjectResourceLoader : initialization complete." ); |
74 | 0 | } |
75 | 0 | |
76 | 0 | /** |
77 | * Get an InputStream so that the Runtime can build a |
|
78 | * template with it. |
|
79 | * |
|
80 | * @param templateName name of template to get |
|
81 | * @return InputStream containing the template |
|
82 | * @throws ResourceNotFoundException if template not found |
|
83 | * in the file template path. |
|
84 | */ |
|
85 | public synchronized InputStream getResourceStream( String templateName ) |
|
86 | throws ResourceNotFoundException |
|
87 | { |
|
88 | /* |
|
89 | * Make sure we have a valid templateName. |
|
90 | */ |
|
91 | 0 | if ( templateName == null || templateName.length() == 0 ) |
92 | { |
|
93 | 0 | /* |
94 | * If we don't get a properly formed templateName then |
|
95 | * there's not much we can do. So we'll forget about |
|
96 | * trying to search any more paths for the template. |
|
97 | */ |
|
98 | 0 | throw new ResourceNotFoundException( |
99 | "Need to specify a file name or file path!" ); |
|
100 | 0 | } |
101 | ||
102 | 0 | String template = StringUtils.normalizePath( templateName ); |
103 | 0 | if ( template == null || template.length() == 0 ) |
104 | 0 | { |
105 | 0 | String msg = "Project Resource loader error : argument " + template |
106 | + " contains .. and may be trying to access " |
|
107 | 0 | + "content outside of template root. Rejected."; |
108 | ||
109 | 0 | rsvc.error( "ProjectResourceLoader : " + msg ); |
110 | ||
111 | 0 | throw new ResourceNotFoundException ( msg ); |
112 | } |
|
113 | 0 | |
114 | /* |
|
115 | * if a / leads off, then just nip that :) |
|
116 | */ |
|
117 | 0 | if ( template.startsWith( "/" ) ) |
118 | { |
|
119 | 0 | template = template.substring( 1 ); |
120 | } |
|
121 | 0 | |
122 | 0 | int size = paths.size(); |
123 | 0 | for ( int i = 0; i < size; i++ ) |
124 | 0 | { |
125 | 0 | String path = (String) paths.get( i ); |
126 | 0 | InputStream inputStream = findTemplate( path, template ); |
127 | 0 | |
128 | 0 | if ( inputStream != null ) |
129 | { |
|
130 | 0 | /* |
131 | * Store the path that this template came |
|
132 | * from so that we can check its modification |
|
133 | * time. |
|
134 | */ |
|
135 | ||
136 | 0 | templatePaths.put( templateName, path ); |
137 | 0 | return inputStream; |
138 | 0 | } |
139 | 0 | } |
140 | ||
141 | /* |
|
142 | * We have now searched all the paths for |
|
143 | * templates and we didn't find anything so |
|
144 | * throw an exception. |
|
145 | */ |
|
146 | 0 | String msg = "ProjectResourceLoader Error: cannot find resource " |
147 | + template; |
|
148 | 0 | |
149 | 0 | throw new ResourceNotFoundException( msg ); |
150 | } |
|
151 | 0 | |
152 | /** |
|
153 | * Try to find a template given a normalized path. |
|
154 | * |
|
155 | * @param path a normalized path |
|
156 | * @return InputStream input stream that will be parsed |
|
157 | * |
|
158 | */ |
|
159 | private InputStream findTemplate( String path, String template ) |
|
160 | { |
|
161 | try |
|
162 | { |
|
163 | 0 | File file = new File( path, template ); |
164 | ||
165 | 0 | if ( file.canRead() ) |
166 | { |
|
167 | 0 | return new BufferedInputStream( |
168 | new FileInputStream( file.getAbsolutePath() ) ); |
|
169 | 0 | } |
170 | else |
|
171 | { |
|
172 | 0 | return null; |
173 | } |
|
174 | 0 | } |
175 | 0 | catch ( FileNotFoundException fnfe ) |
176 | { |
|
177 | 0 | /* |
178 | * log and convert to a general Velocity ResourceNotFoundException |
|
179 | */ |
|
180 | 0 | return null; |
181 | } |
|
182 | 0 | } |
183 | ||
184 | /** |
|
185 | * How to keep track of all the modified times |
|
186 | * across the paths. Note that a file might have |
|
187 | * appeared in a directory which is earlier in the |
|
188 | * path; so we should search the path and see if |
|
189 | * the file we find that way is the same as the one |
|
190 | * that we have cached. |
|
191 | */ |
|
192 | public boolean isSourceModified( Resource resource ) |
|
193 | { |
|
194 | /* |
|
195 | * we assume that the file needs to be reloaded; |
|
196 | * if we find the original file and it's unchanged, |
|
197 | * then we'll flip this. |
|
198 | */ |
|
199 | 0 | boolean modified = true; |
200 | ||
201 | 0 | String fileName = resource.getName(); |
202 | 0 | String path = (String) templatePaths.get( fileName ); |
203 | 0 | File currentFile = null; |
204 | 0 | |
205 | 0 | for ( int i = 0; currentFile == null && i < paths.size(); i++ ) |
206 | { |
|
207 | 0 | String testPath = (String) paths.get( i ); |
208 | 0 | File testFile = new File( testPath, fileName ); |
209 | 0 | if ( testFile.canRead() ) |
210 | 0 | { |
211 | 0 | currentFile = testFile; |
212 | } |
|
213 | 0 | } |
214 | 0 | File file = new File( path, fileName ); |
215 | 0 | if ( currentFile == null || !file.exists() ) |
216 | 0 | { |
217 | 0 | /* |
218 | 0 | * noop: if the file is missing now (either the cached |
219 | * file is gone, or the file can no longer be found) |
|
220 | * then we leave modified alone (it's set to true); a |
|
221 | * reload attempt will be done, which will either use |
|
222 | * a new template or fail with an appropriate message |
|
223 | * about how the file couldn't be found. |
|
224 | */ |
|
225 | } |
|
226 | 0 | else if ( currentFile.equals( file ) && file.canRead() ) |
227 | { |
|
228 | 0 | /* |
229 | * if only if currentFile is the same as file and |
|
230 | * file.lastModified() is the same as |
|
231 | * resource.getLastModified(), then we should use the |
|
232 | * cached version. |
|
233 | */ |
|
234 | 0 | modified = ( file.lastModified() != resource.getLastModified() ); |
235 | } |
|
236 | 0 | |
237 | /* |
|
238 | * rsvc.debug("isSourceModified for " + fileName + ": " + modified); |
|
239 | */ |
|
240 | 0 | return modified; |
241 | } |
|
242 | 0 | |
243 | public long getLastModified( Resource resource ) |
|
244 | { |
|
245 | 0 | String path = (String) templatePaths.get( resource.getName() ); |
246 | 0 | File file = new File( path, resource.getName() ); |
247 | 0 | |
248 | 0 | if ( file.canRead() ) |
249 | { |
|
250 | 0 | return file.lastModified(); |
251 | } |
|
252 | 0 | else |
253 | { |
|
254 | 0 | return 0; |
255 | } |
|
256 | 0 | } |
257 | } |