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<>();
85          sourceFileList = new ArrayList<>();
86          rootFileList = new ArrayList<>();
87          directoryList = new ArrayList<>();
88          targetClassesList = new ArrayList<>();
89          dataMap = new HashMap<>();
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     @Override
146     public Build getBuild()
147     {
148         return build;
149     }
150 
151     /**
152      * returns true if the path is relative
153      * and false if absolute
154      * also returns false if it is relative to
155      * the parent
156      *
157      * @param path
158      * @return
159      */
160     private boolean isValidPath( String path )
161     {
162         boolean bRetVal = true;
163 
164         if ( path.startsWith( "c:" ) || path.startsWith( ".." ) || path.startsWith( "/" ) || path.startsWith( "\\" ) )
165         {
166             bRetVal = false;
167         }
168 
169         return bRetVal;
170     }
171 
172     private void setupBuild()
173     {
174         // check getBasedir method for the exact path
175         // we need to recreate the dir structure in
176         // an isolated environment
177         srcDirectory = testRootDir + "/src";
178         buildDirectory = testRootDir + "/target";
179         outputDirectory = buildDirectory + "/classes";
180         testOutputDirectory = buildDirectory + "/test-classes";
181         resourcesDirectory = srcDirectory + "/main/resources/";
182         testResourcesDirectory = srcDirectory + "/test/resources/";
183 
184         build.setDirectory( buildDirectory );
185         build.setOutputDirectory( outputDirectory );
186         build.setTestOutputDirectory( testOutputDirectory );
187     }
188 
189     public void setupBuildEnvironment()
190         throws Exception
191     {
192         // populate dummy resources and dummy test resources
193 
194         // setup src dir
195         if ( !FileUtils.fileExists( resourcesDirectory ) )
196         {
197             FileUtils.mkdir( resourcesDirectory );
198         }
199 
200         if ( !FileUtils.fileExists( testResourcesDirectory ) )
201         {
202             FileUtils.mkdir( testResourcesDirectory );
203         }
204 
205         createDirectories( resourcesDirectory, testResourcesDirectory );
206         createFiles( resourcesDirectory, testResourcesDirectory );
207         setupRootFiles();
208 
209         // setup target dir
210         if ( !FileUtils.fileExists( outputDirectory ) )
211         {
212             FileUtils.mkdir( outputDirectory );
213         }
214 
215         if ( !FileUtils.fileExists( testOutputDirectory ) )
216         {
217             FileUtils.mkdir( testOutputDirectory );
218         }
219 
220         setupTargetFiles();
221     }
222 
223     private void createDirectories( String parent, String testparent )
224     {
225         File currentDirectory;
226 
227         for ( String aDirectoryList : directoryList )
228         {
229             currentDirectory = new File( parent, "/" + aDirectoryList );
230 
231             if ( !currentDirectory.exists() )
232             {
233                 currentDirectory.mkdirs();
234             }
235 
236             // duplicate dir structure in test resources
237             currentDirectory = new File( testparent, "/" + aDirectoryList );
238 
239             if ( !currentDirectory.exists() )
240             {
241                 currentDirectory.mkdirs();
242             }
243         }
244     }
245 
246     private List<String> getList( int type )
247     {
248         ArrayList<String> retVal = null;
249 
250         switch ( type )
251         {
252             case SOURCE_FILE :
253                 retVal = sourceFileList;
254                 break;
255             case OUTPUT_FILE :
256                 retVal = targetClassesList;
257                 break;
258             case RESOURCES_FILE :
259                 retVal = resourcesFileList;
260                 break;
261             case ROOT_FILE :
262                 retVal = rootFileList;
263                 break;
264         }
265 
266         return retVal;
267     }
268 
269     private void createFiles( String parent, int type )
270     {
271         File currentFile;
272         List<String> list = getList( type );
273 
274         // guard
275         if ( list == null )
276         {
277             return;
278         }
279 
280         for ( String aList : list )
281         {
282             currentFile = new File( parent, aList );
283 
284             // create the necessary parent directories
285             // before we create the files
286             if ( !currentFile.getParentFile().exists() )
287             {
288                 currentFile.getParentFile().mkdirs();
289             }
290 
291             if ( !currentFile.exists() )
292             {
293                 try
294                 {
295                     currentFile.createNewFile();
296                     populateFile( currentFile, RESOURCES_FILE );
297                 }
298                 catch ( IOException io )
299                 {
300                     // TODO: handle exception
301                 }
302             }
303         }
304     }
305 
306     private void setupRootFiles()
307     {
308         createFiles( testRootDir, ROOT_FILE );
309     }
310 
311     private void setupTargetFiles()
312     {
313         createFiles( getOutputDirectory(), OUTPUT_FILE );
314     }
315 
316     private void createFiles( String parent, String testparent )
317     {
318         createFiles( parent, RESOURCES_FILE );
319         createFiles( testparent, RESOURCES_FILE );
320     }
321 
322     private void populateFile( File file, int type )
323     {
324         FileOutputStream outputStream;
325         String data = dataMap.get( file.getName() );
326 
327         if ( ( data != null ) && file.exists() )
328         {
329             try
330             {
331                 outputStream = new FileOutputStream( file );
332                 outputStream.write( data.getBytes() );
333                 outputStream.flush();
334                 outputStream.close();
335             }
336             catch ( IOException ex )
337             {
338                 // TODO: handle exception here
339             }
340         }
341     }
342 }