View Javadoc

1   package org.apache.maven.plugin.resources.remote.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<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      * returns true if the path is relative
152      * and false if absolute
153      * also returns false if it is relative to
154      * the parent
155      *
156      * @param path
157      * @return
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         // check getBasedir method for the exact path
174         // we need to recreate the dir structure in 
175         // an isolated environment
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         // populate dummy resources and dummy test resources
192 
193         // setup src dir
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         // setup target dir        
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             // duplicate dir structure in test resources
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         // guard
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             // create the necessary parent directories
284             // before we create the files
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                     //TODO: handle exception
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                 // TODO: handle exception here
338             }
339         }
340     }
341 }