View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.ejb.stub;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.model.Build;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Stub
34   */
35  public class MavenProjectBuildStub extends MavenProjectBasicStub {
36      public static final int RESOURCES_FILE = 1;
37  
38      public static final int ROOT_FILE = 2;
39  
40      public static final int OUTPUT_FILE = 3;
41  
42      public static final int SOURCE_FILE = 4;
43  
44      protected Build build;
45  
46      protected String srcDirectory;
47  
48      protected String targetDirectory;
49  
50      protected String buildDirectory;
51  
52      protected String outputDirectory;
53  
54      protected String testOutputDirectory;
55  
56      protected String resourcesDirectory;
57  
58      protected String testResourcesDirectory;
59  
60      protected String targetResourceDirectory;
61  
62      protected String targetTestResourcesDirectory;
63  
64      protected List<String> targetClassesList;
65  
66      protected List<String> sourceFileList;
67  
68      protected List<String> resourcesFileList;
69  
70      protected List<String> rootFileList;
71  
72      protected List<String> directoryList;
73  
74      protected Map<String, String> dataMap;
75  
76      public MavenProjectBuildStub(String key) throws Exception {
77          super(key);
78  
79          this.build = new Build();
80          this.resourcesFileList = new ArrayList<>();
81          this.sourceFileList = new ArrayList<>();
82          this.rootFileList = new ArrayList<>();
83          this.directoryList = new ArrayList<>();
84          this.targetClassesList = new ArrayList<>();
85          this.dataMap = new HashMap<>();
86          setupBuild();
87  
88          model.setBuild(build);
89      }
90  
91      public void addDirectory(String name) {
92          if (isValidPath(name)) {
93              directoryList.add(name);
94          }
95      }
96  
97      public void setOutputDirectory(String dir) {
98          outputDirectory = buildDirectory + "/" + dir;
99          build.setOutputDirectory(outputDirectory);
100     }
101 
102     public void addFile(String name, int type) {
103         if (isValidPath(name)) {
104             List<String> list = getList(type);
105 
106             list.add(name);
107         }
108     }
109 
110     public void addFile(String name, String data, int type) {
111         File fileName = new File(name);
112 
113         addFile(name, type);
114         dataMap.put(fileName.getName(), data);
115     }
116 
117     public String getOutputDirectory() {
118         return outputDirectory;
119     }
120 
121     public String getTestOutputDirectory() {
122         return testOutputDirectory;
123     }
124 
125     public String getResourcesDirectory() {
126         return resourcesDirectory;
127     }
128 
129     public String getTestResourcesDirectory() {
130         return testResourcesDirectory;
131     }
132 
133     public Build getBuild() {
134         return build;
135     }
136 
137     /**
138      * returns true if the path is relative and false if absolute also returns false if it is relative to the parent
139      *
140      * @param path
141      * @return
142      */
143     private boolean isValidPath(String path) {
144         boolean bRetVal = true;
145 
146         if (path.startsWith("c:") || path.startsWith("..") || path.startsWith("/") || path.startsWith("\\")) {
147             bRetVal = false;
148         }
149 
150         return bRetVal;
151     }
152 
153     private void setupBuild() {
154         // check getBasedir method for the exact path
155         // we need to recreate the dir structure in
156         // an isolated environment
157         srcDirectory = testRootDir + "/src";
158         buildDirectory = testRootDir + "/target";
159         outputDirectory = buildDirectory + "/classes";
160         testOutputDirectory = buildDirectory + "/test-classes";
161         resourcesDirectory = srcDirectory + "/main/resources/";
162         testResourcesDirectory = srcDirectory + "/test/resources/";
163 
164         build.setDirectory(buildDirectory);
165         build.setOutputDirectory(outputDirectory);
166         build.setTestOutputDirectory(testOutputDirectory);
167     }
168 
169     public void setupBuildEnvironment() throws Exception {
170         // populate dummy resources and dummy test resources
171 
172         // setup src dir
173         if (!new File(resourcesDirectory).exists()) {
174             FileUtils.mkdir(resourcesDirectory);
175         }
176 
177         if (!new File(testResourcesDirectory).exists()) {
178             FileUtils.mkdir(testResourcesDirectory);
179         }
180 
181         createDirectories(resourcesDirectory, testResourcesDirectory);
182         createFiles(resourcesDirectory, testResourcesDirectory);
183         setupRootFiles();
184 
185         // setup target dir
186         if (!new File(outputDirectory).exists()) {
187             FileUtils.mkdir(outputDirectory);
188         }
189 
190         if (!new File(testOutputDirectory).exists()) {
191             FileUtils.mkdir(testOutputDirectory);
192         }
193 
194         setupTargetFiles();
195     }
196 
197     private void createDirectories(String parent, String testparent) {
198         File currentDirectory;
199 
200         for (Object aDirectoryList : directoryList) {
201             currentDirectory = new File(parent, "/" + aDirectoryList);
202 
203             if (!currentDirectory.exists()) {
204                 currentDirectory.mkdirs();
205             }
206 
207             // duplicate dir structure in test resources
208             currentDirectory = new File(testparent, "/" + aDirectoryList);
209 
210             if (!currentDirectory.exists()) {
211                 currentDirectory.mkdirs();
212             }
213         }
214     }
215 
216     private List<String> getList(int type) {
217         List<String> retVal = null;
218 
219         switch (type) {
220             case SOURCE_FILE:
221                 retVal = sourceFileList;
222                 break;
223             case OUTPUT_FILE:
224                 retVal = targetClassesList;
225                 break;
226             case RESOURCES_FILE:
227                 retVal = resourcesFileList;
228                 break;
229             case ROOT_FILE:
230                 retVal = rootFileList;
231                 break;
232             default:
233             // do nothing
234         }
235 
236         return retVal;
237     }
238 
239     private void createFiles(String parent, int type) {
240         File currentFile;
241         List<String> list = getList(type);
242 
243         // guard
244         if (list == null) {
245             return;
246         }
247 
248         for (String aList : list) {
249             currentFile = new File(parent, aList);
250 
251             // create the necessary parent directories
252             // before we create the files
253             if (!currentFile.getParentFile().exists()) {
254                 currentFile.getParentFile().mkdirs();
255             }
256 
257             if (!currentFile.exists()) {
258                 try {
259                     currentFile.createNewFile();
260                     populateFile(currentFile, RESOURCES_FILE);
261                 } catch (IOException io) {
262                     // TODO: handle exception
263                 }
264             }
265         }
266     }
267 
268     private void setupRootFiles() {
269         createFiles(testRootDir, ROOT_FILE);
270     }
271 
272     private void setupTargetFiles() {
273         createFiles(getOutputDirectory(), OUTPUT_FILE);
274     }
275 
276     private void createFiles(String parent, String testparent) {
277         createFiles(parent, RESOURCES_FILE);
278         createFiles(testparent, RESOURCES_FILE);
279     }
280 
281     private void populateFile(File file, int type) {
282         FileOutputStream outputStream;
283         String data = (String) dataMap.get(file.getName());
284 
285         if ((data != null) && file.exists()) {
286             try {
287                 outputStream = new FileOutputStream(file);
288                 outputStream.write(data.getBytes());
289                 outputStream.flush();
290                 outputStream.close();
291             } catch (IOException ex) {
292                 // TODO: handle exception here
293             }
294         }
295     }
296 }