1 package org.apache.maven.plugin.resources.remote.stub;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import org.apache.maven.model.Build;
30 import org.codehaus.plexus.util.FileUtils;
31
32
33
34
35 public class MavenProjectBuildStub
36 extends MavenProjectBasicStub
37 {
38 public static final int RESOURCES_FILE = 1;
39
40 public static final int ROOT_FILE = 2;
41
42 public static final int OUTPUT_FILE = 3;
43
44 public static final int SOURCE_FILE = 4;
45
46 protected Build build;
47
48 protected String srcDirectory;
49
50 protected String targetDirectory;
51
52 protected String buildDirectory;
53
54 protected String outputDirectory;
55
56 protected String testOutputDirectory;
57
58 protected String resourcesDirectory;
59
60 protected String testResourcesDirectory;
61
62 protected String targetResourceDirectory;
63
64 protected String targetTestResourcesDirectory;
65
66 protected ArrayList<String> targetClassesList;
67
68 protected ArrayList<String> sourceFileList;
69
70 protected ArrayList<String> resourcesFileList;
71
72 protected ArrayList<String> rootFileList;
73
74 protected ArrayList<String> directoryList;
75
76 protected HashMap<String, String> dataMap;
77
78 public MavenProjectBuildStub( String key )
79 throws Exception
80 {
81 super( key );
82
83 build = new Build();
84 resourcesFileList = new ArrayList<String>();
85 sourceFileList = new ArrayList<String>();
86 rootFileList = new ArrayList<String>();
87 directoryList = new ArrayList<String>();
88 targetClassesList = new ArrayList<String>();
89 dataMap = new HashMap<String, String>();
90 setupBuild();
91 }
92
93 public void addDirectory( String name )
94 {
95 if ( isValidPath( name ) )
96 {
97 directoryList.add( name );
98 }
99 }
100
101 public void setOutputDirectory( String dir )
102 {
103 outputDirectory = buildDirectory + "/" + dir;
104 build.setOutputDirectory( outputDirectory );
105 }
106
107 public void addFile( String name, int type )
108 {
109 if ( isValidPath( name ) )
110 {
111 List<String> list = getList( type );
112
113 list.add( name );
114 }
115 }
116
117 public void addFile( String name, String data, int type )
118 {
119 File fileName = new File( name );
120
121 addFile( name, type );
122 dataMap.put( fileName.getName(), data );
123 }
124
125 public String getOutputDirectory()
126 {
127 return outputDirectory;
128 }
129
130 public String getTestOutputDirectory()
131 {
132 return testOutputDirectory;
133 }
134
135 public String getResourcesDirectory()
136 {
137 return resourcesDirectory;
138 }
139
140 public String getTestResourcesDirectory()
141 {
142 return testResourcesDirectory;
143 }
144
145 public Build getBuild()
146 {
147 return build;
148 }
149
150
151
152
153
154
155
156
157
158
159 private boolean isValidPath( String path )
160 {
161 boolean bRetVal = true;
162
163 if ( path.startsWith( "c:" ) || path.startsWith( ".." ) || path.startsWith( "/" ) || path.startsWith( "\\" ) )
164 {
165 bRetVal = false;
166 }
167
168 return bRetVal;
169 }
170
171 private void setupBuild()
172 {
173
174
175
176 srcDirectory = testRootDir + "/src";
177 buildDirectory = testRootDir + "/target";
178 outputDirectory = buildDirectory + "/classes";
179 testOutputDirectory = buildDirectory + "/test-classes";
180 resourcesDirectory = srcDirectory + "/main/resources/";
181 testResourcesDirectory = srcDirectory + "/test/resources/";
182
183 build.setDirectory( buildDirectory );
184 build.setOutputDirectory( outputDirectory );
185 build.setTestOutputDirectory( testOutputDirectory );
186 }
187
188 public void setupBuildEnvironment()
189 throws Exception
190 {
191
192
193
194 if ( !FileUtils.fileExists( resourcesDirectory ) )
195 {
196 FileUtils.mkdir( resourcesDirectory );
197 }
198
199 if ( !FileUtils.fileExists( testResourcesDirectory ) )
200 {
201 FileUtils.mkdir( testResourcesDirectory );
202 }
203
204 createDirectories( resourcesDirectory, testResourcesDirectory );
205 createFiles( resourcesDirectory, testResourcesDirectory );
206 setupRootFiles();
207
208
209 if ( !FileUtils.fileExists( outputDirectory ) )
210 {
211 FileUtils.mkdir( outputDirectory );
212 }
213
214 if ( !FileUtils.fileExists( testOutputDirectory ) )
215 {
216 FileUtils.mkdir( testOutputDirectory );
217 }
218
219 setupTargetFiles();
220 }
221
222 private void createDirectories( String parent, String testparent )
223 {
224 File currentDirectory;
225
226 for ( int nIndex = 0; nIndex < directoryList.size(); nIndex++ )
227 {
228 currentDirectory = new File( parent, "/" + (String) directoryList.get( nIndex ) );
229
230 if ( !currentDirectory.exists() )
231 {
232 currentDirectory.mkdirs();
233 }
234
235
236 currentDirectory = new File( testparent, "/" + (String) directoryList.get( nIndex ) );
237
238 if ( !currentDirectory.exists() )
239 {
240 currentDirectory.mkdirs();
241 }
242 }
243 }
244
245 private List<String> getList( int type )
246 {
247 ArrayList<String> retVal = null;
248
249 switch ( type )
250 {
251 case SOURCE_FILE :
252 retVal = sourceFileList;
253 break;
254 case OUTPUT_FILE :
255 retVal = targetClassesList;
256 break;
257 case RESOURCES_FILE :
258 retVal = resourcesFileList;
259 break;
260 case ROOT_FILE :
261 retVal = rootFileList;
262 break;
263 }
264
265 return retVal;
266 }
267
268 private void createFiles( String parent, int type )
269 {
270 File currentFile;
271 List<String> list = getList( type );
272
273
274 if ( list == null )
275 {
276 return;
277 }
278
279 for ( int nIndex = 0; nIndex < list.size(); nIndex++ )
280 {
281 currentFile = new File( parent, (String) list.get( nIndex ) );
282
283
284
285 if ( !currentFile.getParentFile().exists() )
286 {
287 currentFile.getParentFile().mkdirs();
288 }
289
290 if ( !currentFile.exists() )
291 {
292 try
293 {
294 currentFile.createNewFile();
295 populateFile( currentFile, RESOURCES_FILE );
296 }
297 catch ( IOException io )
298 {
299
300 }
301 }
302 }
303 }
304
305 private void setupRootFiles()
306 {
307 createFiles( testRootDir, ROOT_FILE );
308 }
309
310 private void setupTargetFiles()
311 {
312 createFiles( getOutputDirectory(), OUTPUT_FILE );
313 }
314
315 private void createFiles( String parent, String testparent )
316 {
317 createFiles( parent, RESOURCES_FILE );
318 createFiles( testparent, RESOURCES_FILE );
319 }
320
321 private void populateFile( File file, int type )
322 {
323 FileOutputStream outputStream;
324 String data = (String) dataMap.get( file.getName() );
325
326 if ( ( data != null ) && file.exists() )
327 {
328 try
329 {
330 outputStream = new FileOutputStream( file );
331 outputStream.write( data.getBytes() );
332 outputStream.flush();
333 outputStream.close();
334 }
335 catch ( IOException ex )
336 {
337
338 }
339 }
340 }
341 }