KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > accurev > AccurevCommandline


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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 /*
38  * Created on 29-Jun-2005 by norru
39  *
40  * Copyright (C) Sony Computer Entertainment Europe
41  * Studio Liverpool Server Group
42  *
43  * Authors:
44  * Nicola Orru' <Nicola_Orru@scee.net>
45  */

46 package net.sourceforge.cruisecontrol.sourcecontrols.accurev;
47
48 import net.sourceforge.cruisecontrol.CruiseControlException;
49 import net.sourceforge.cruisecontrol.sourcecontrols.Accurev;
50 import net.sourceforge.cruisecontrol.util.EnvCommandline;
51 import net.sourceforge.cruisecontrol.util.StreamPumper;
52 import org.apache.log4j.Logger;
53
54 import java.io.BufferedReader JavaDoc;
55 import java.io.File JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58 import java.io.InputStreamReader JavaDoc;
59
60 /**
61  * Allows to build and execute a valid accurev command line.
62  *
63  * @author <a HREF="mailto:Nicola_Orru@scee.net">Nicola Orru'</a>
64  * @author <a HREF="mailto:jason_chown@scee.net">Jason Chown </a>
65  */

66 public class AccurevCommandline extends EnvCommandline implements AccurevInputParser, Runner {
67     private static final Logger LOG = Logger.getLogger(Accurev.class);
68     private boolean verbose;
69     private int runCount;
70     private int returnCode;
71     private AccurevCommand command;
72     private AccurevInputParser inputParser;
73     private static final int SUCCESS = 0;
74     private boolean syntaxError;
75     private Runner runner;
76
77     /**
78      * Creates a new AccurevCommandline.
79      *
80      * @param command
81      */

82     public AccurevCommandline(AccurevCommand command) {
83         super("accurev");
84         this.inputParser = this;
85         this.runner = this;
86         this.command = command;
87         createArgument().setValue(command.toString());
88     }
89
90     /**
91      * Sets the Accurev stream to work in (-s stream)
92      *
93      * @param stream the stream name
94      */

95     public void setStream(String JavaDoc stream) {
96         addOption("-s", stream);
97     }
98
99     /**
100      * Sets the Accurev depot to work in (-d depot)
101      *
102      * @param depot the depot name
103      */

104     public void setDepot(String JavaDoc depot) {
105         addOption("-d", depot);
106     }
107
108     /**
109      * Sets the transaction comment (-c comment)
110      *
111      * @param comment the comment text. Quotes and escapes are not required.
112      */

113     public void setComment(String JavaDoc comment) {
114         addOption("-c", comment);
115     }
116
117     /**
118      * Switches the -i option on
119      */

120     public void setInfoOnly() {
121         addArgument("-i");
122     }
123
124     /**
125      * Selects a transaction range for hist as in (-t), single timespec
126      *
127      * @param time a timespec (can be a DateTimespec, a KeywordTimespec
128      */

129     public void setTransactionRange(Timespec time) {
130         addOption("-t", time.toString());
131     }
132
133     /**
134      * Selects a transaction range for hist as in (-t), timespec span (a-b, a-, -b)
135      */

136     public void setTransactionRange(Timespec begin, Timespec end) {
137         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
138         if (begin != null) {
139             buf.append(begin);
140         }
141         buf.append("-");
142         if (end != null) {
143             buf.append(end);
144         }
145         addOption("-t", buf.toString());
146     }
147
148     /**
149      * Selects a format for hist as in (-f)
150      */

151     public void setFormatExpanded(char format) throws CruiseControlException {
152         if ("evstx".indexOf(format) < 0) {
153             throw new CruiseControlException(
154                     "Invalid format specifier (use one of 'e' 'v' 's' 't' 'x') " + format);
155         }
156         addOption("-f", new String JavaDoc(new char[]{format}));
157     }
158
159     /**
160      * Adds an argument to the command line
161      *
162      * @param argument the argument to add (eg "-i"). Quotes and escape codes are not required.
163      */

164     public void addArgument(String JavaDoc argument) {
165         createArgument().setValue(argument);
166     }
167
168     /**
169      * Sets the input parser, which is the object that handles Accurev's output as its input.
170      */

171     public void setInputParser(AccurevInputParser inputParser) {
172         this.inputParser = inputParser;
173     }
174
175     /**
176      * Adds an option with an argument (eg. -s my_stream)
177      *
178      * @param option the option flag (eg. "-s")
179      * @param optionArgument the option argument ("eg. my_stream"). No need for quotes or escape characters.
180      */

181     public void addOption(String JavaDoc option, String JavaDoc optionArgument) {
182         createArgument().setValue(option);
183         createArgument().setValue(optionArgument);
184     }
185
186     /**
187      * Selects all modified files in keep (as in -m)
188      */

189     public void selectModified() {
190         addArgument("-m");
191     }
192
193     /**
194      * Selects the files to use reading them from the filelist (as in -l filelistName)
195      *
196      * @param filelistName the path of the file containing the list of files to process
197      */

198     public void setFileList(String JavaDoc filelistName) {
199         addOption("-l", filelistName);
200     }
201
202     /**
203      * Selects the workspace to use, specifying its path in the local filesystem.
204      *
205      * @param workspace the workspace path
206      * @throws CruiseControlException if the path does not exist
207      */

208     public void setWorkspaceLocalPath(File JavaDoc workspace) throws CruiseControlException {
209         this.setWorkingDirectory(workspace.getAbsolutePath());
210     }
211
212     /**
213      * Selects the workspace to use, specifying its path in the local filesystem.
214      *
215      * @param workspace the workspace path
216      * @throws CruiseControlException if the path does not exist
217      */

218     public void setWorkspaceLocalPath(String JavaDoc workspace) throws CruiseControlException {
219         this.setWorkingDirectory(workspace);
220     }
221
222     /**
223      * Runs accurev and returns a reference to this.
224      */

225     public void run() {
226         if (verbose) {
227             LOG.info("Accurev: Executing '" + toString() + "'");
228         }
229         this.syntaxError = runner.execute(inputParser);
230         this.returnCode = runner.getReturnCode();
231         runCount++;
232     }
233
234     /**
235      * Runs accurev and parses the output
236      *
237      * @return true if there are no parsing errors.
238      */

239     public boolean execute(AccurevInputParser inputParser) {
240         Process JavaDoc proc;
241         boolean error = false;
242         try {
243             proc = super.execute();
244             StreamPumper errorPumper = new StreamPumper(proc.getErrorStream());
245             new Thread JavaDoc(errorPumper).start();
246             InputStream JavaDoc input = proc.getInputStream();
247             try {
248                 try {
249                     try {
250                         if (inputParser != null) {
251                             error = !inputParser.parseStream(input);
252                         }
253                         returnCode = proc.waitFor();
254                     } finally {
255                         proc.getInputStream().close();
256                     }
257                 } finally {
258                     proc.getOutputStream().close();
259                 }
260             } finally {
261                 proc.getErrorStream().close();
262             }
263         } catch (IOException JavaDoc e) {
264             LOG.error(e);
265             throw new RuntimeException JavaDoc(e.getMessage());
266         } catch (CruiseControlException e) {
267             LOG.error(e);
268             throw new RuntimeException JavaDoc(e.getMessage());
269         } catch (InterruptedException JavaDoc e) {
270             LOG.error(e);
271             throw new RuntimeException JavaDoc(e.getMessage());
272         }
273         return error;
274     }
275
276     protected String JavaDoc[] buildCommandLine() {
277         return null;
278     }
279
280     /**
281      * Default stream parser. It scans Accurev output and detects basic errors.
282      *
283      * @return true if no errors were found in Accurev's output.
284      */

285     public boolean parseStream(InputStream JavaDoc iStream) throws CruiseControlException {
286         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(iStream));
287         boolean badSyntax = false;
288         try {
289             while (true) {
290                 String JavaDoc line = reader.readLine();
291                 if (line == null) {
292                     break;
293                 }
294                 if (line.startsWith("AccuRev was unable to understand your command.")) {
295                     badSyntax = true;
296                 }
297                 if (verbose) {
298                     LOG.info(line);
299                 }
300             }
301         } catch (IOException JavaDoc ex) {
302             throw new CruiseControlException("Error reading input");
303         }
304         return !badSyntax;
305     }
306
307     /**
308      * Gets the last "accurev" exec's return code.
309      *
310      * @return the return code from the command line. Usually 0 is SUCCESS.
311      */

312     public int getReturnCode() {
313         return returnCode;
314     }
315
316     /**
317      * Returns the accurev subcommand to be run by this command line object
318      * (eg. keep, synctime, update). The subcommand can be selected by the
319      * {@link #AccurevCommandline(AccurevCommand command)} constructor.
320      *
321      * @return the command
322      */

323     public AccurevCommand getCommand() {
324         return command;
325     }
326
327     /**
328      * Enables/disables verbose logging
329      *
330      * @param verbose if true, verbose logging is enabled.
331      */

332     public void setVerbose(boolean verbose) {
333         this.verbose = verbose;
334     }
335
336     /**
337      * Returns the verbose flag
338      *
339      * @return true if verbose logging is enabled
340      */

341     public boolean isVerbose() {
342         return verbose;
343     }
344
345     /**
346      * Returns the run status
347      *
348      * @return true if the last command executed successfully (that is, returnCode == success and the
349      * parser didn't detect errors). It returns false if the command has not been run yet.
350      */

351     public boolean isSuccess() {
352         return (runCount > 0) && (!syntaxError) && (returnCode == SUCCESS);
353     }
354
355     /**
356      * Throws a CruiseControlException if the last command was not executed successfully.
357      *
358      * @throws CruiseControlException if the command was not executed successfully
359      */

360     public void assertSuccess() throws CruiseControlException {
361         if (!isSuccess()) {
362             throw new CruiseControlException("Error running " + toString());
363         }
364     }
365
366     /**
367      * Sets the runner
368      *
369      * @param runner
370      * the object that is in charge for provide some input to the parser
371      */

372     public void setRunner(Runner runner) {
373     this.runner = runner;
374   }
375 }
376
Popular Tags