Debian Codegraph @

src/main/java/net/sf/antcontrib/walls/CompileWithWalls.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.walls;
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.Task;
31 import org.apache.tools.ant.taskdefs.Copy;
32 import org.apache.tools.ant.taskdefs.Javac;
33 import org.apache.tools.ant.types.FileSet;
34 import org.apache.tools.ant.types.Path;
35 import org.apache.tools.ant.util.JAXPUtils;
36 import org.xml.sax.HandlerBase;
37 import org.xml.sax.Parser;
38 import org.xml.sax.SAXException;
39 /*
40 * Created on Aug 24, 2003
41 *
42 * To change the template for this generated file go to
43 * Window>Preferences>Java>Code Generation>Code and Comments
44 */
45 /**
46 * FILL IN JAVADOC HERE
47 *
48 * @author Dean Hiller([email protected])
49 */
50 public class CompileWithWalls extends Task {
51 private boolean setWallsTwice = false;
52 private boolean setJavacTwice = false;
53 private Walls walls;
54 private Javac javac;
55 private File wallsFile;
56 private File tempBuildDir;
57
58 private Map packagesNeedingCompiling = new HashMap();
59
60 private SAXException cachedSAXException = null;
61 private IOException cachedIOException = null;
62
63 public void setIntermediaryBuildDir(File f) {
64 tempBuildDir = f;
65 }
66
67 public File getIntermediaryBuildDir() {
68 return tempBuildDir;
69 }
70
71 public void setWalls(File f) {
72 this.wallsFile = f;
73
74 Parser parser = JAXPUtils.getParser();
75 HandlerBase hb = new WallsFileHandler(this, wallsFile);
76 parser.setDocumentHandler(hb);
77 parser.setEntityResolver(hb);
78 parser.setErrorHandler(hb);
79 parser.setDTDHandler(hb);
80 try {
81 log("about to start parsing walls file", Project.MSG_INFO);
82 parser.parse(wallsFile.toURL().toExternalForm());
83 } catch (SAXException e) {
84 cachedSAXException = e;
85 throw new ParsingWallsException("Problem parsing walls file attached:", e);
86 } catch (IOException e) {
87 cachedIOException = e;
88 throw new ParsingWallsException("IOException on walls file attached:", e);
89 }
90 }
91
92 public File getWalls() {
93 return wallsFile;
94 }
95
96
97 public Walls createWalls() {
98 if (walls != null)
99 setWallsTwice = true;
100 walls = new Walls();
101 return walls;
102 }
103 public Javac createJavac() {
104 if (javac != null)
105 setJavacTwice = true;
106 javac = new Javac();
107 return javac;
108 }
109 public void execute() throws BuildException {
110 if(cachedIOException != null)
111 throw new BuildException(cachedIOException, getLocation());
112 else if(cachedSAXException != null)
113 throw new BuildException(cachedSAXException, getLocation());
114 else if(tempBuildDir == null)
115 throw new BuildException(
116 "intermediaryBuildDir attribute must be specified on the compilewithwalls element"
117 , getLocation());
118 else if (javac == null)
119 throw new BuildException(
120 "There must be a nested javac element",
121 getLocation());
122 else if (walls == null)
123 throw new BuildException(
124 "There must be a nested walls element",
125 getLocation());
126 else if (setWallsTwice)
127 throw new BuildException(
128 "compilewithwalls task only supports one nested walls element or one walls attribute",
129 getLocation());
130 else if (setJavacTwice)
131 throw new BuildException(
132 "compilewithwalls task only supports one nested javac element",
133 getLocation());
134
135 getProject().addTaskDefinition("SilentMove", SilentMove.class);
136 getProject().addTaskDefinition("SilentCopy", SilentCopy.class);
137
138 File destDir = javac.getDestdir();
139 Path src = javac.getSrcdir();
140
141 if(src == null)
142 throw new BuildException("Javac inside compilewithwalls must have a srcdir specified");
143
144 String[] list = src.list();
145 File[] tempSrcDirs1 = new File[list.length];
146 for(int i = 0; i < list.length; i++) {
147 tempSrcDirs1[i] = getProject().resolveFile(list[i]);
148 }
149
150 String[] classpaths = new String[0];
151 if(javac.getClasspath() != null)
152 classpaths = javac.getClasspath().list();
153
154 File temp = null;
155 for(int i = 0; i < classpaths.length; i++) {
156 temp = new File(classpaths[i]);
157 if(temp.isDirectory()) {
158
159 for(int n = 0; n < tempSrcDirs1.length; n++) {
160 if(tempSrcDirs1[n].compareTo(temp) == 0)
161 throw new BuildException("The classpath cannot contain any of the\n"
162 +"src directories, but it does.\n"
163 +"srcdir="+tempSrcDirs1[n]);
164 }
165 }
166 }
167
168 //get rid of non-existent srcDirs
169 List srcDirs2 = new ArrayList();
170 for(int i = 0; i < tempSrcDirs1.length; i++) {
171 if(tempSrcDirs1[i].exists())
172 srcDirs2.add(tempSrcDirs1[i]);
173 }
174
175 if (destDir == null)
176 throw new BuildException(
177 "destdir was not specified in nested javac task",
178 getLocation());
179
180 //make sure tempBuildDir is not inside destDir or we are in trouble!!
181 if(file1IsChildOfFile2(tempBuildDir, destDir))
182 throw new BuildException("intermediaryBuildDir attribute cannot be specified\n"
183 +"to be the same as destdir or inside desdir of the javac task.\n"
184 +"This is an intermediary build directory only used by the\n"
185 +"compilewithwalls task, not the class file output directory.\n"
186 +"The class file output directory is specified in javac's destdir attribute", getLocation());
187
188 //create the tempBuildDir if it doesn't exist.
189 if(!tempBuildDir.exists()) {
190 tempBuildDir.mkdirs();
191 log("created direction="+tempBuildDir, Project.MSG_VERBOSE);
192 }
193
194 Iterator iter = walls.getPackagesToCompile();
195 while (iter.hasNext()) {
196 Package toCompile = (Package)iter.next();
197
198 File buildSpace = toCompile.getBuildSpace(tempBuildDir);
199 if(!buildSpace.exists()) {
200 buildSpace.mkdir();
201 log("created directory="+buildSpace, Project.MSG_VERBOSE);
202 }
203
204 FileSet javaIncludes2 =
205 toCompile.getJavaCopyFileSet(getProject(), getLocation());
206
207 for(int i = 0; i < srcDirs2.size(); i++) {
208 File srcDir = (File)srcDirs2.get(i);
209 javaIncludes2.setDir(srcDir);
210 log(toCompile.getPackage()+": sourceDir["+i+"]="+srcDir+" destDir="+buildSpace, Project.MSG_VERBOSE);
211 copyFiles(srcDir, buildSpace, javaIncludes2);
212 }
213
214 Path srcDir2 = toCompile.getSrcPath(tempBuildDir, getProject());
215 Path classPath = toCompile.getClasspath(tempBuildDir, getProject());
216 if(javac.getClasspath() != null)
217 classPath.addExisting(javac.getClasspath());
218
219 //unfortunately, we cannot clear the SrcDir in Javac, so we have to clone
220 //instead of just reusing the other Javac....this means added params in
221 //future releases will be missed unless this task is kept up to date.
222 //need to convert to reflection later so we don't need to keep this up to
223 //date.
224 Javac buildSpaceJavac = new Javac();
225 buildSpaceJavac.setProject(getProject());
226 buildSpaceJavac.setOwningTarget(getOwningTarget());
227 buildSpaceJavac.setTaskName(getTaskName());
228 log(toCompile.getPackage()+": Compiling");
229 log(toCompile.getPackage()+": sourceDir="+srcDir2, Project.MSG_VERBOSE);
230 log(toCompile.getPackage()+": classPath="+classPath, Project.MSG_VERBOSE);
231 log(toCompile.getPackage()+": destDir="+buildSpace, Project.MSG_VERBOSE);
232 buildSpaceJavac.setSrcdir(srcDir2);
233 buildSpaceJavac.setDestdir(buildSpace);
234 //includes not used...ie. ignored
235 //includesfile not used
236 //excludes not used
237 //excludesfile not used
238 buildSpaceJavac.setClasspath(classPath);
239 //sourcepath not used
240 buildSpaceJavac.setBootclasspath(javac.getBootclasspath());
241 //classpath not used..redefined by us
242 //sourcepathref not used...redefined by us.
243 //bootclasspathref was already copied above(see javac and you will understand)
244 buildSpaceJavac.setExtdirs(javac.getExtdirs());
245 buildSpaceJavac.setEncoding(javac.getEncoding());
246 buildSpaceJavac.setNowarn(javac.getNowarn());
247 buildSpaceJavac.setDebug(javac.getDebug());
248 buildSpaceJavac.setDebugLevel(javac.getDebugLevel());
249 buildSpaceJavac.setOptimize(javac.getOptimize());
250 buildSpaceJavac.setDeprecation(javac.getDeprecation());
251 buildSpaceJavac.setTarget(javac.getTarget());
252 buildSpaceJavac.setVerbose(javac.getVerbose());
253 buildSpaceJavac.setDepend(javac.getDepend());
254 buildSpaceJavac.setIncludeantruntime(javac.getIncludeantruntime());
255 buildSpaceJavac.setIncludejavaruntime(javac.getIncludejavaruntime());
256 buildSpaceJavac.setFork(javac.isForkedJavac());
257 buildSpaceJavac.setExecutable(javac.getJavacExecutable());
258 buildSpaceJavac.setMemoryInitialSize(javac.getMemoryInitialSize());
259 buildSpaceJavac.setMemoryMaximumSize(javac.getMemoryMaximumSize());
260 buildSpaceJavac.setFailonerror(javac.getFailonerror());
261 buildSpaceJavac.setSource(javac.getSource());
262 buildSpaceJavac.setCompiler(javac.getCompiler());
263
264 Javac.ImplementationSpecificArgument arg;
265 String[] args = javac.getCurrentCompilerArgs();
266 if(args != null) {
267 for(int i = 0; i < args.length;i++) {
268 arg = buildSpaceJavac.createCompilerArg();
269 arg.setValue(args[i]);
270 }
271 }
272
273 buildSpaceJavac.setProject(getProject());
274 buildSpaceJavac.perform();
275
276 //copy class files to javac's destDir where the user wants the class files
277 copyFiles(buildSpace, destDir, toCompile.getClassCopyFileSet(getProject(), getLocation()));
278 }
279 }
280 /**
281 * @param tempBuildDir
282 * @param destDir
283 * @return
284 */
285 private boolean file1IsChildOfFile2(File tempBuildDir, File destDir) {
286 File parent = tempBuildDir;
287 for(int i = 0; i < 1000; i++) {
288 if(parent.compareTo(destDir) == 0)
289 return true;
290 parent = parent.getParentFile();
291 if(parent == null)
292 return false;
293 }
294
295 throw new RuntimeException("You either have more than 1000 directories in"
296 +"\nyour heirarchy or this is a bug, please report. parent="+parent+" destDir="+destDir);
297 }
298
299 /**
300 * Move java or class files to temp files or moves the temp files
301 * back to java or class files. This must be done because javac
302 * is too nice and sticks already compiled classes and ones depended
303 * on in the classpath destroying the compile time wall. This way,
304 * we can keep the wall up.
305 *
306 * @param srcDir Directory to copy files from/to(Usually the java files dir or the class files dir)
307 * @param fileset The fileset of files to include in the move.
308 * @param moveToTempFile true if moving src files to temp files, false if moving temp files
309 * back to src files.
310 * @param isJavaFiles true if we are moving .java files, false if we are moving .class files.
311 */
312 private void copyFiles(
313 File srcDir,
314 File destDir,
315 FileSet fileset) {
316
317 fileset.setDir(srcDir);
318 if (!srcDir.exists())
319 throw new BuildException(
320 "Directory=" + srcDir + " does not exist",
321 getLocation());
322
323
324 //before we do this, we have to move all files not
325 //in the above fileset to xxx.java.ant-tempfile
326 //so that they don't get dragged into the compile
327 //This way we don't miss anything and all the dependencies
328 //are listed or the compile will break.
329 Copy move = (Copy)getProject().createTask("SilentCopy");
330 move.setProject(getProject());
331 move.setOwningTarget(getOwningTarget());
332 move.setTaskName(getTaskName());
333 move.setLocation(getLocation());
334 move.setTodir(destDir);
335 // move.setOverwrite(true);
336 move.addFileset(fileset);
337 move.perform();
338 }
339
340 public void log(String msg, int level) {
341 super.log(msg, level);
342 }
343
344 //until 1.3 is deprecated, this is a cheat to chain exceptions.
345 private class ParsingWallsException extends RuntimeException {
346
347 private String message;
348
349 public ParsingWallsException(String message, Throwable cause) {
350 super(message);
351
352 this.message = message+"\n";
353
354 StringWriter sw = new StringWriter();
355 PrintWriter pw = new PrintWriter(sw);
356 cause.printStackTrace(pw);
357
358 this.message += sw;
359 }
360
361 public String getMessage() {
362 return message;
363 }
364
365 public String toString() {
366 return getMessage();
367 }
368 }
369 }