1 package org.apache.maven.plugins.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 import java.util.Map;
29
30 import org.apache.maven.model.Build;
31 import org.codehaus.plexus.util.FileUtils;
32
33
34
35
36 public class MavenProjectBuildStub
37 extends MavenProjectBasicStub
38 {
39 public static final int RESOURCES_FILE = 1;
40
41 public static final int ROOT_FILE = 2;
42
43 public static final int OUTPUT_FILE = 3;
44
45 public static final int SOURCE_FILE = 4;
46
47 protected Build build;
48
49 protected String srcDirectory;
50
51 protected String targetDirectory;
52
53 protected String buildDirectory;
54
55 protected String outputDirectory;
56
57 protected String testOutputDirectory;
58
59 protected String resourcesDirectory;
60
61 protected String testResourcesDirectory;
62
63 protected String targetResourceDirectory;
64
65 protected String targetTestResourcesDirectory;
66
67 protected List<String> targetClassesList;
68
69 protected List<String> sourceFileList;
70
71 protected List<String> resourcesFileList;
72
73 protected List<String> rootFileList;
74
75 protected List<String> directoryList;
76
77 protected Map<String, String> dataMap;
78
79 public MavenProjectBuildStub( String key )
80 throws Exception
81 {
82 super( key );
83
84 this.build = new Build();
85 this.resourcesFileList = new ArrayList<String>();
86 this.sourceFileList = new ArrayList<String>();
87 this.rootFileList = new ArrayList<String>();
88 this.directoryList = new ArrayList<String>();
89 this.targetClassesList = new ArrayList<String>();
90 this.dataMap = new HashMap<String, String>();
91 setupBuild();
92
93 model.setBuild( build );
94 }
95
96 public void addDirectory( String name )
97 {
98 if ( isValidPath( name ) )
99 {
100 directoryList.add( name );
101 }
102 }
103
104 public void setOutputDirectory( String dir )
105 {
106 outputDirectory = buildDirectory + "/" + dir;
107 build.setOutputDirectory( outputDirectory );
108 }
109
110 public void addFile( String name, int type )
111 {
112 if ( isValidPath( name ) )
113 {
114 List<String> list = getList( type );
115
116 list.add( name );
117 }
118 }
119
120 public void addFile( String name, String data, int type )
121 {
122 File fileName = new File( name );
123
124 addFile( name, type );
125 dataMap.put( fileName.getName(), data );
126 }
127
128 public String getOutputDirectory()
129 {
130 return outputDirectory;
131 }
132
133 public String getTestOutputDirectory()
134 {
135 return testOutputDirectory;
136 }
137
138 public String getResourcesDirectory()
139 {
140 return resourcesDirectory;
141 }
142
143 public String getTestResourcesDirectory()
144 {
145 return testResourcesDirectory;
146 }
147
148 public Build getBuild()
149 {
150 return build;
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 ( Object aDirectoryList : directoryList )
227 {
228 currentDirectory = new File( parent, "/" + aDirectoryList );
229
230 if ( !currentDirectory.exists() )
231 {
232 currentDirectory.mkdirs();
233 }
234
235
236 currentDirectory = new File( testparent, "/" + aDirectoryList );
237
238 if ( !currentDirectory.exists() )
239 {
240 currentDirectory.mkdirs();
241 }
242 }
243 }
244
245 private List<String> getList( int type )
246 {
247 List<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 ( String aList : list )
280 {
281 currentFile = new File( parent, aList );
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 }