KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > frontend > ant > TaskControl


1 /* TaskControl.java */
2
3 package org.quilt.frontend.ant;
4
5 import java.io.File JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 import org.apache.tools.ant.BuildException;
10 import org.apache.tools.ant.Project;
11 import org.apache.tools.ant.Task;
12 import org.apache.tools.ant.taskdefs.ExecuteWatchdog;
13 import org.apache.tools.ant.taskdefs.LogOutputStream;
14 import org.apache.tools.ant.types.Commandline;
15 import org.apache.tools.ant.types.CommandlineJava;
16 import org.apache.tools.ant.types.Environment;
17 import org.apache.tools.ant.types.Path;
18
19 import org.quilt.cl.QuiltClassLoader;
20 import org.quilt.framework.QuiltTest;
21 import org.quilt.reports.FmtSelector;
22 import org.quilt.runner.Runner;
23
24 /////////////////////////////////////////////////////////////////////
25
// NEEDS A LOT OF ATTENTION
26
/////////////////////////////////////////////////////////////////////
27

28 /**
29  * Run control parameters from the Ant build.xml file.
30  */

31 public class TaskControl {
32
33     // set by constructor or created at construction ////////////////
34
/** Command line, clone of which is used to fork tests. */
35     private CommandlineJava commandline
36                                         = new CommandlineJava();
37     /** Run-time environment for command line. */
38     private Environment env = new Environment();
39
40     /** The running Task. */
41     private QuiltTask task;
42
43     // values assigned by Ant using add/create/set //////////////////
44
private File JavaDoc dir = null;
45     private Vector JavaDoc formatters = new Vector JavaDoc();
46     private boolean includeAntRuntime = true;
47     private boolean mockExec = false;
48     private boolean newEnvironment = false;
49     private boolean showOutput = false;
50     private boolean summary = false; // printsummary
51
private Vector JavaDoc tests = new Vector JavaDoc();
52     private Long JavaDoc timeout = null;
53
54     // ancillary variables //////////////////////////////////////////
55
private Path antRuntimeClasses = null;
56     private Runner runner = null;
57     private String JavaDoc summaryValue = "";
58    
59     // Quilt-specific, not available from Ant ///////////////////////
60
protected QuiltClassLoader loader = null;
61     
62     /////////////////////////////////////////////////////////////////
63
/** No-arg constructor for clone() */
64     private TaskControl () { }
65
66     /**
67      * Constructor.
68      * @param t The QuiltTask being executed.
69      */

70     public TaskControl (QuiltTask t) {
71         task = t;
72     }
73     // //////////////////////////////////////////////////////////////
74
// ADD/CREATE/SET METHODS. Please keep in
75
// alphabetical order by the name of the variable being set (so
76
// addXYZ sorts after createABC).
77
// //////////////////////////////////////////////////////////////
78
public void setAntRuntimeClasses (Path p) {
79         antRuntimeClasses = p;
80     }
81     public Path createClasspath() {
82         return commandline.createClasspath(task.getProject()).createPath();
83     }
84     public void setCommandline (CommandlineJava cmd) {
85         commandline = cmd;
86     }
87     public void setDir(File JavaDoc dir) {
88         this.dir = dir;
89     }
90     public void addEnv(Environment.Variable var) {
91         env.addVariable(var);
92     }
93     
94     public void addFormatter(FmtSelector fs) {
95         formatters.addElement(fs);
96     }
97     public void setFormatters (Vector JavaDoc v) {
98         formatters = v;
99     }
100
101     public void setIncludeAntRuntime(boolean b) {
102         includeAntRuntime = b;
103     }
104     public void setJvm(String JavaDoc value) {
105         commandline.setVm(value);
106     }
107     public Commandline.Argument createJvmarg() {
108         return commandline.createVmArgument();
109     }
110     // not for use from Ant
111
public void setLoader (QuiltClassLoader qcl) {
112         loader = qcl;
113     }
114     public void setMaxmemory(String JavaDoc max) {
115         commandline.setMaxmemory(max);
116     }
117     public void setMockExec(boolean b) {
118         mockExec = b;
119     }
120     public void setNewEnvironment(boolean newenv) {
121         newEnvironment = newenv;
122     }
123     
124     public void setPrintsummary(SummaryAttribute value) {
125         summaryValue = value.getValue();
126         summary = value.asBoolean();
127     }
128     public void setRunner (Runner r) {
129         runner = r;
130     }
131     public void setShowOutput (boolean b) {
132         showOutput = b;
133     }
134     public void setSummary(boolean b) {
135         summary = b;
136     }
137     public void setSummaryValue (String JavaDoc s) {
138         summaryValue = s;
139     }
140
141     public void addSysproperty(Environment.Variable sysp) {
142         commandline.addSysproperty(sysp);
143     }
144     public void addTest(QuiltTest t) {
145         tests.addElement(t);
146     }
147     public void setTests (Vector JavaDoc v) {
148         tests = v;
149     }
150     public void setTask(QuiltTask t) {
151         task = t;
152     }
153     public void setTimeout(Long JavaDoc value) {
154         timeout = value;
155     }
156     // GET METHODS //////////////////////////////////////////////////
157
public CommandlineJava getCommandline() { return commandline; }
158     public File JavaDoc getDir() { return dir; }
159     public Environment getEnv() { return env; }
160     public Vector JavaDoc getFormatters() { return formatters; }
161     public boolean getIncludeAntRuntime() { return includeAntRuntime;}
162     public QuiltClassLoader getLoader() { return loader; }
163     public boolean getMockExec() { return mockExec; }
164     public boolean getNewEnvironment() { return newEnvironment; }
165     public boolean getSummary() { return summary; }
166     public Task getTask() { return task; }
167     public Vector JavaDoc getTests() { return tests; }
168     public Long JavaDoc getTimeout() { return timeout; }
169
170     public Path getAntRuntimeClasses() { return antRuntimeClasses;}
171     public Runner getRunner() { return runner; }
172     public String JavaDoc getSummaryValue() { return summaryValue; }
173
174     // I/O HANDLING /////////////////////////////////////////////////
175
void handleOutput(String JavaDoc line) {
176         if (runner != null) {
177             runner.handleOutput(line);
178             if (showOutput) {
179                 task.handleTheOutput(line);
180             }
181         } else {
182             task.handleTheOutput(line);
183         }
184     }
185     void handleFlush(String JavaDoc line) {
186         if (runner != null) {
187             runner.handleFlush(line);
188             if (showOutput) {
189                 task.handleTheFlush(line);
190             }
191         } else {
192             task.handleTheFlush(line);
193         }
194     }
195     void handleErrorOutput(String JavaDoc line) {
196         if (runner != null) {
197             runner.handleErrorOutput(line);
198             if (showOutput) {
199                 task.handleTheErrorOutput(line);
200             }
201         } else {
202             task.handleTheErrorOutput(line);
203         }
204     }
205     void handleErrorFlush(String JavaDoc line) {
206         if (runner != null) {
207             runner.handleErrorFlush(line);
208             if (showOutput) {
209                 task.handleTheErrorFlush(line);
210             }
211         } else {
212             task.handleTheErrorFlush(line);
213         }
214     }
215
216     OutputStream JavaDoc getDefaultOutput(){
217         return new LogOutputStream(task, Project.MSG_INFO);
218     }
219     FmtSelector[] mergeSelectors(QuiltTest test){
220         Vector JavaDoc fsVector = (Vector JavaDoc) getFormatters().clone();
221         test.addFormattersTo(fsVector);
222         FmtSelector[] fsArray =
223                             new FmtSelector[fsVector.size()];
224         fsVector.copyInto(fsArray);
225         return fsArray;
226     }
227     File JavaDoc getOutput(FmtSelector fs, QuiltTest test){
228         if (fs.getUseFile()) {
229             String JavaDoc filename = test.getOutfile() + fs.getExtension();
230             File JavaDoc destFile = new File JavaDoc(test.getTodir(), filename);
231             String JavaDoc absFilename = destFile.getAbsolutePath();
232             return task.getProject().resolveFile(absFilename);
233         }
234         return null;
235     }
236     ExecuteWatchdog createWatchdog() throws BuildException {
237         if (timeout == null){
238             return null;
239         }
240         return new ExecuteWatchdog(timeout.longValue());
241     }
242
243     // EQUALS() /////////////////////////////////////////////////////
244
public boolean equals( TaskControl tc2 ) {
245         if (this == tc2)
246             return true;
247         else
248             return
249                 formatters. equals( tc2.getFormatters() ) &&
250                 tests. equals( tc2.getTests() ) &&
251                 
252                 task == tc2.getTask() &&
253                 commandline == tc2.getCommandline() &&
254                 dir == tc2.getDir() &&
255                 includeAntRuntime == tc2.getIncludeAntRuntime() &&
256                 loader == tc2.getLoader() &&
257                 mockExec == tc2.getMockExec() &&
258                 newEnvironment == tc2.getNewEnvironment() &&
259                 summary == tc2.getSummary() &&
260                 timeout == tc2.getTimeout() &&
261                 antRuntimeClasses == tc2.getAntRuntimeClasses() &&
262                 runner == tc2.getRunner() &&
263                 summaryValue.equals(tc2.getSummaryValue());
264     }
265     // CLONE() //////////////////////////////////////////////////////
266
public Object JavaDoc clone() {
267         TaskControl tc2 = new TaskControl();
268         tc2.setTask(task);
269         tc2.setCommandline(commandline);
270
271         // the Vectors
272
tc2.setFormatters((Vector JavaDoc)formatters.clone());
273         tc2.setTests((Vector JavaDoc)tests.clone());
274
275         tc2.setDir(dir);
276         tc2.setIncludeAntRuntime(includeAntRuntime);
277         tc2.setLoader(loader);
278         tc2.setMockExec(mockExec);
279         tc2.setNewEnvironment(newEnvironment);
280         tc2.setSummary(summary);
281         tc2.setTimeout(timeout);
282         tc2.setAntRuntimeClasses(antRuntimeClasses);
283         tc2.setRunner(runner);
284         tc2.setSummaryValue(summaryValue);
285         return tc2;
286     }
287     // TOSTRING () //////////////////////////////////////////////////
288
public String JavaDoc toString() {
289         // the Vectors
290
String JavaDoc f = "";
291         for (int i = 0; i < formatters.size(); i++)
292             f += formatters.elementAt(i) + " ";
293         
294         String JavaDoc s =
295                 " task: " + task.getTaskName()
296             + "\n antRuntimeClasses: " + antRuntimeClasses
297             + "\n commandline: "
298             + "\n as string: " + commandline
299             + "\n classname: " + commandline.getClassname()
300             + "\n classpath: " + commandline.getClasspath()
301             + "\n dir: " + dir
302             + "\n includeAntRuntime: " + includeAntRuntime
303             // loader ignored
304
+ "\n mockExec: " + mockExec
305             + "\n newEnvironment: " + newEnvironment
306             + "\n runner: " + runner
307             + "\n summary: " + summary
308             + "\n summaryValue: " + summaryValue
309             + "\n timeout: " + timeout
310             + "\n formatters: " + f
311             + "\n ---------------------------------------------\n";
312         return s;
313     }
314 }
315
Popular Tags