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