KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > builders > AntScript


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.builders;
38
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43
44 import net.sourceforge.cruisecontrol.CruiseControlException;
45 import net.sourceforge.cruisecontrol.util.Commandline;
46
47 /**
48  * Ant script class.
49  *
50  * Contains all the details related to running a Ant based build via
51  * either a batch script or inprocess.
52  * @author <a HREF="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
53  */

54 public class AntScript implements Script {
55     private Map JavaDoc buildProperties;
56
57     private boolean isWindows;
58     private String JavaDoc antScript;
59     private List JavaDoc args;
60     private String JavaDoc loggerClassName;
61     private String JavaDoc tempFileName = "log.xml";
62     private boolean useScript;
63     private boolean useLogger;
64     private boolean useQuiet;
65     private boolean useDebug;
66     private String JavaDoc buildFile = "build.xml";
67     private List JavaDoc properties;
68     private String JavaDoc target = "";
69     private String JavaDoc systemClassPath;
70     private int exitCode;
71
72
73     /**
74      * construct the command that we're going to execute.
75      *
76      * @return Commandline holding command to be executed
77      * @throws CruiseControlException on unquotable attributes
78      */

79     public Commandline buildCommandline() throws CruiseControlException {
80         Commandline cmdLine = new Commandline();
81
82         if (useScript) {
83             cmdLine.setExecutable(antScript);
84         } else {
85             if (isWindows) {
86                 cmdLine.setExecutable("java.exe");
87             } else {
88                 cmdLine.setExecutable("java");
89             }
90             for (Iterator JavaDoc argsIterator = args.iterator(); argsIterator.hasNext(); ) {
91                 String JavaDoc arg = ((AntBuilder.JVMArg) argsIterator.next()).getArg();
92                 // empty args may break the command line
93
if (arg != null && arg.length() > 0) {
94                     cmdLine.createArgument().setValue(arg);
95                 }
96             }
97
98             cmdLine.createArgument().setValue("-classpath");
99             cmdLine.createArgument().setValue(getAntLauncherJarLocation(systemClassPath, isWindows));
100             cmdLine.createArgument().setValue("org.apache.tools.ant.launch.Launcher");
101             cmdLine.createArgument().setValue("-lib");
102             cmdLine.createArgument().setValue(systemClassPath);
103         }
104
105         if (useLogger) {
106             cmdLine.createArgument().setValue("-logger");
107             cmdLine.createArgument().setValue(getLoggerClassName());
108             cmdLine.createArgument().setValue("-logfile");
109             cmdLine.createArgument().setValue(tempFileName);
110         } else {
111             cmdLine.createArgument().setValue("-listener");
112             cmdLine.createArgument().setValue(getLoggerClassName());
113             cmdLine.createArgument().setValue("-DXmlLogger.file=" + tempFileName);
114         }
115
116         // -debug and -quiet only affect loggers, not listeners: when we use the loggerClassName as
117
// a listener, they will affect the default logger that writes to the console
118
if (useDebug) {
119             cmdLine.createArgument().setValue("-debug");
120         } else if (useQuiet) {
121             cmdLine.createArgument().setValue("-quiet");
122         }
123
124         for (Iterator JavaDoc propertiesIter = buildProperties.entrySet().iterator(); propertiesIter.hasNext(); ) {
125             Map.Entry JavaDoc property = (Map.Entry JavaDoc) propertiesIter.next();
126             String JavaDoc value = (String JavaDoc) property.getValue();
127             if (!"".equals(value)) {
128                 cmdLine.createArgument().setValue("-D" + property.getKey() + "=" + value);
129             }
130         }
131
132         for (Iterator JavaDoc antPropertiesIterator = properties.iterator(); antPropertiesIterator.hasNext(); ) {
133             Property property = (Property) antPropertiesIterator.next();
134             cmdLine.createArgument().setValue("-D" + property.getName() + "=" + property.getValue());
135         }
136
137         cmdLine.createArgument().setValue("-buildfile");
138         cmdLine.createArgument().setValue(buildFile);
139
140         StringTokenizer JavaDoc targets = new StringTokenizer JavaDoc(target);
141         while (targets.hasMoreTokens()) {
142             cmdLine.createArgument().setValue(targets.nextToken());
143         }
144         return cmdLine;
145     }
146
147
148     /**
149      * @return the path to ant-launcher*.jar taken from the given path
150      */

151     String JavaDoc getAntLauncherJarLocation(String JavaDoc path, boolean isWindows) throws CruiseControlException {
152         String JavaDoc separator = isWindows ? ";" : ":";
153         StringTokenizer JavaDoc pathTokenizer = new StringTokenizer JavaDoc(path, separator);
154         while (pathTokenizer.hasMoreTokens()) {
155             String JavaDoc pathElement = pathTokenizer.nextToken();
156             if (pathElement.indexOf("ant-launcher") != -1 && pathElement.endsWith(".jar")) {
157                 return pathElement;
158             }
159         }
160         throw new CruiseControlException("Couldn't find path to ant-launcher jar in this classpath: '" + path + "'");
161     }
162     
163     /**
164      * @param buildProperties The buildProperties to set.
165      */

166     public void setBuildProperties(Map JavaDoc buildProperties) {
167         this.buildProperties = buildProperties;
168     }
169
170     /**
171      * @return Returns the loggerClassName.
172      */

173     public String JavaDoc getLoggerClassName() {
174         return loggerClassName;
175     }
176     /**
177      * @param loggerClassName The loggerClassName to set.
178      */

179     public void setLoggerClassName(String JavaDoc loggerClassName) {
180         this.loggerClassName = loggerClassName;
181     }
182     /**
183      * @param antScript The antScript to set.
184      */

185     public void setAntScript(String JavaDoc antScript) {
186         this.antScript = antScript;
187     }
188     /**
189      * @param args The args to set.
190      */

191     public void setArgs(List JavaDoc args) {
192         this.args = args;
193     }
194     /**
195      * @param isWindows The isWindows to set.
196      */

197     public void setWindows(boolean isWindows) {
198         this.isWindows = isWindows;
199     }
200     /**
201      * @param buildFile The buildFile to set.
202      */

203     public void setBuildFile(String JavaDoc buildFile) {
204         this.buildFile = buildFile;
205     }
206     /**
207      * @param tempFileName The tempFileName to set.
208      */

209     public void setTempFileName(String JavaDoc tempFileName) {
210         this.tempFileName = tempFileName;
211     }
212     /**
213      * @param useDebug The useDebug to set.
214      */

215     public void setUseDebug(boolean useDebug) {
216         this.useDebug = useDebug;
217     }
218     /**
219      * @param useLogger The useLogger to set.
220      */

221     public void setUseLogger(boolean useLogger) {
222         this.useLogger = useLogger;
223     }
224     /**
225      * @param useQuiet The useQuiet to set.
226      */

227     public void setUseQuiet(boolean useQuiet) {
228         this.useQuiet = useQuiet;
229     }
230     /**
231      * @param useScript The useScript to set.
232      */

233     public void setUseScript(boolean useScript) {
234         this.useScript = useScript;
235     }
236     /**
237      * @param systemClassPath The systemClassPath to set.
238      */

239     public void setSystemClassPath(String JavaDoc systemClassPath) {
240         this.systemClassPath = systemClassPath;
241     }
242     /**
243      * @param properties The properties to set.
244      */

245     public void setProperties(List JavaDoc properties) {
246         this.properties = properties;
247     }
248     /**
249      * @param target The target to set.
250      */

251     public void setTarget(String JavaDoc target) {
252         this.target = target;
253     }
254     /**
255      * @return Returns the exitCode.
256      */

257     public int getExitCode() {
258         return exitCode;
259     }
260     /**
261      * @param exitCode The exitCode to set.
262      */

263     public void setExitCode(int exitCode) {
264         this.exitCode = exitCode;
265     }
266 }
267
Popular Tags