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 paths = null;
49
50
51
52
53
54
55 private Hashtable templatePaths = new Hashtable();
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();
69
70 paths.add( path );
71
72 int sz = paths.size();
73
74 for ( int i = 0; i < sz; i++ )
75 {
76 rsvc.getLog().info( "ProjectResourceLoader : adding path '" + (String) paths.get( i ) + "'" );
77 }
78 rsvc.getLog().info( "ProjectResourceLoader : initialization complete." );
79 }
80
81
82
83
84
85
86
87
88
89
90 public synchronized InputStream getResourceStream( String templateName )
91 throws ResourceNotFoundException
92 {
93
94
95
96 if ( templateName == null || templateName.length() == 0 )
97 {
98
99
100
101
102
103 throw new ResourceNotFoundException( "Need to specify a file name or file path!" );
104 }
105
106 String template = StringUtils.normalizePath( templateName );
107 if ( template == null || template.length() == 0 )
108 {
109 String msg = "Project Resource loader error : argument " + template
110 + " contains .. and may be trying to access " + "content outside of template root. Rejected.";
111
112 rsvc.getLog().error( "ProjectResourceLoader : " + msg );
113
114 throw new ResourceNotFoundException( msg );
115 }
116
117
118
119
120 if ( template.startsWith( "/" ) )
121 {
122 template = template.substring( 1 );
123 }
124
125
126 paths.add( rsvc.getApplicationAttribute( "baseDirectory" ) );
127
128 int size = paths.size();
129 for ( int i = 0; i < size; i++ )
130 {
131 String path = (String) paths.get( i );
132 InputStream inputStream = findTemplate( path, template );
133
134 if ( inputStream != null )
135 {
136
137
138
139
140
141
142 templatePaths.put( templateName, path );
143 return inputStream;
144 }
145 }
146
147
148
149
150
151
152 String msg = "ProjectResourceLoader Error: cannot find resource " + template;
153
154 throw new ResourceNotFoundException( msg );
155 }
156
157
158
159
160
161
162
163
164 private InputStream findTemplate( String path, String template )
165 {
166 try
167 {
168 File file = new File( path, template );
169
170 if ( file.canRead() )
171 {
172 return new BufferedInputStream( new FileInputStream( file.getAbsolutePath() ) );
173 }
174 else
175 {
176 return null;
177 }
178 }
179 catch ( FileNotFoundException fnfe )
180 {
181
182
183
184 return null;
185 }
186 }
187
188
189
190
191
192
193
194
195
196 public boolean isSourceModified( Resource resource )
197 {
198
199
200
201
202
203 boolean modified = true;
204
205 String fileName = resource.getName();
206 String path = (String) templatePaths.get( fileName );
207 File currentFile = null;
208
209 for ( int i = 0; currentFile == null && i < paths.size(); i++ )
210 {
211 String testPath = (String) paths.get( i );
212 File testFile = new File( testPath, fileName );
213 if ( testFile.canRead() )
214 {
215 currentFile = testFile;
216 }
217 }
218 File file = new File( path, fileName );
219 if ( currentFile == null || !file.exists() )
220 {
221
222
223
224
225
226
227
228
229 }
230 else if ( currentFile.equals( file ) && file.canRead() )
231 {
232
233
234
235
236
237
238 modified = ( file.lastModified() != resource.getLastModified() );
239 }
240
241
242
243
244 return modified;
245 }
246
247 public long getLastModified( Resource resource )
248 {
249 String path = (String) templatePaths.get( resource.getName() );
250 File file = new File( path, resource.getName() );
251
252 if ( file.canRead() )
253 {
254 return file.lastModified();
255 }
256 else
257 {
258 return 0;
259 }
260 }
261 }