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