1 package org.apache.maven.plugin.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.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
39
40
41
42 public class ProjectResourceLoader
43 extends ResourceLoader
44 {
45
46
47
48 private List<String> paths = null;
49
50
51
52
53
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
81
82
83
84
85
86
87
88 public synchronized InputStream getResourceStream( String templateName )
89 throws ResourceNotFoundException
90 {
91
92
93
94 if ( templateName == null || templateName.length() == 0 )
95 {
96
97
98
99
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
117
118 if ( template.startsWith( "/" ) )
119 {
120 template = template.substring( 1 );
121 }
122
123
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
134
135
136
137
138 templatePaths.put( templateName, path );
139 return inputStream;
140 }
141 }
142
143
144
145
146
147
148 String msg = "ProjectResourceLoader Error: cannot find resource " + template;
149
150 throw new ResourceNotFoundException( msg );
151 }
152
153
154
155
156
157
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
179
180 return null;
181 }
182 }
183
184
185
186
187
188
189
190
191
192 public boolean isSourceModified( Resource resource )
193 {
194
195
196
197
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
219
220
221
222
223
224
225 }
226 else if ( currentFile.equals( file ) && file.canRead() )
227 {
228
229
230
231
232
233
234 modified = ( file.lastModified() != resource.getLastModified() );
235 }
236
237
238
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 }