View Javadoc
1   package org.apache.maven.plugin.ejb.stub;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Stub
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      * returns true if the path is relative and false if absolute also returns false if it is relative to the parent
154      *
155      * @param path
156      * @return
157      */
158     private boolean isValidPath( String path )
159     {
160         boolean bRetVal = true;
161 
162         if ( path.startsWith( "c:" ) || path.startsWith( ".." ) || path.startsWith( "/" ) || path.startsWith( "\\" ) )
163         {
164             bRetVal = false;
165         }
166 
167         return bRetVal;
168     }
169 
170     private void setupBuild()
171     {
172         // check getBasedir method for the exact path
173         // we need to recreate the dir structure in
174         // an isolated environment
175         srcDirectory = testRootDir + "/src";
176         buildDirectory = testRootDir + "/target";
177         outputDirectory = buildDirectory + "/classes";
178         testOutputDirectory = buildDirectory + "/test-classes";
179         resourcesDirectory = srcDirectory + "/main/resources/";
180         testResourcesDirectory = srcDirectory + "/test/resources/";
181 
182         build.setDirectory( buildDirectory );
183         build.setOutputDirectory( outputDirectory );
184         build.setTestOutputDirectory( testOutputDirectory );
185     }
186 
187     public void setupBuildEnvironment()
188         throws Exception
189     {
190         // populate dummy resources and dummy test resources
191 
192         // setup src dir
193         if ( !FileUtils.fileExists( resourcesDirectory ) )
194         {
195             FileUtils.mkdir( resourcesDirectory );
196         }
197 
198         if ( !FileUtils.fileExists( testResourcesDirectory ) )
199         {
200             FileUtils.mkdir( testResourcesDirectory );
201         }
202 
203         createDirectories( resourcesDirectory, testResourcesDirectory );
204         createFiles( resourcesDirectory, testResourcesDirectory );
205         setupRootFiles();
206 
207         // setup target dir
208         if ( !FileUtils.fileExists( outputDirectory ) )
209         {
210             FileUtils.mkdir( outputDirectory );
211         }
212 
213         if ( !FileUtils.fileExists( testOutputDirectory ) )
214         {
215             FileUtils.mkdir( testOutputDirectory );
216         }
217 
218         setupTargetFiles();
219     }
220 
221     private void createDirectories( String parent, String testparent )
222     {
223         File currentDirectory;
224 
225         for ( Object aDirectoryList : directoryList )
226         {
227             currentDirectory = new File( parent, "/" + aDirectoryList );
228 
229             if ( !currentDirectory.exists() )
230             {
231                 currentDirectory.mkdirs();
232             }
233 
234             // duplicate dir structure in test resources
235             currentDirectory = new File( testparent, "/" + aDirectoryList );
236 
237             if ( !currentDirectory.exists() )
238             {
239                 currentDirectory.mkdirs();
240             }
241         }
242     }
243 
244     private List getList( int type )
245     {
246         ArrayList retVal = null;
247 
248         switch ( type )
249         {
250             case SOURCE_FILE:
251                 retVal = sourceFileList;
252                 break;
253             case OUTPUT_FILE:
254                 retVal = targetClassesList;
255                 break;
256             case RESOURCES_FILE:
257                 retVal = resourcesFileList;
258                 break;
259             case ROOT_FILE:
260                 retVal = rootFileList;
261                 break;
262         }
263 
264         return retVal;
265     }
266 
267     private void createFiles( String parent, int type )
268     {
269         File currentFile;
270         ArrayList list = (ArrayList) getList( type );
271 
272         // guard
273         if ( list == null )
274         {
275             return;
276         }
277 
278         for ( Object aList : list )
279         {
280             currentFile = new File( parent, (String) aList );
281 
282             // create the necessary parent directories
283             // before we create the files
284             if ( !currentFile.getParentFile().exists() )
285             {
286                 currentFile.getParentFile().mkdirs();
287             }
288 
289             if ( !currentFile.exists() )
290             {
291                 try
292                 {
293                     currentFile.createNewFile();
294                     populateFile( currentFile, RESOURCES_FILE );
295                 }
296                 catch ( IOException io )
297                 {
298                     // TODO: handle exception
299                 }
300             }
301         }
302     }
303 
304     private void setupRootFiles()
305     {
306         createFiles( testRootDir, ROOT_FILE );
307     }
308 
309     private void setupTargetFiles()
310     {
311         createFiles( getOutputDirectory(), OUTPUT_FILE );
312     }
313 
314     private void setupSourceFiles()
315     {
316         createFiles( srcDirectory, SOURCE_FILE );
317     }
318 
319     private void createFiles( String parent, String testparent )
320     {
321         createFiles( parent, RESOURCES_FILE );
322         createFiles( testparent, RESOURCES_FILE );
323     }
324 
325     private void populateFile( File file, int type )
326     {
327         FileOutputStream outputStream;
328         String data = (String) dataMap.get( file.getName() );
329 
330         if ( ( data != null ) && file.exists() )
331         {
332             try
333             {
334                 outputStream = new FileOutputStream( file );
335                 outputStream.write( data.getBytes() );
336                 outputStream.flush();
337                 outputStream.close();
338             }
339             catch ( IOException ex )
340             {
341                 // TODO: handle exception here
342             }
343         }
344     }
345 }