KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > bootstrappers > CVSBootstrapper


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 package net.sourceforge.cruisecontrol.bootstrappers;
38
39 import net.sourceforge.cruisecontrol.Bootstrapper;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import net.sourceforge.cruisecontrol.util.Commandline;
42 import net.sourceforge.cruisecontrol.util.StreamConsumer;
43 import net.sourceforge.cruisecontrol.util.StreamPumper;
44 import net.sourceforge.cruisecontrol.util.ValidationHelper;
45
46 import org.apache.log4j.Logger;
47
48 import java.io.File JavaDoc;
49
50 /**
51  * Since we rely on our build.xml to handle updating our source code, there has
52  * always been a problem with what happens when the build.xml file itself
53  * changes. Previous workarounds have included writing a wrapper build.xml that
54  * will check out the "real" build.xml. This class is a substitute for that
55  * practice.
56  *
57  * The CVSBootstrapper will handle updating a single file from CVS before the
58  * build begins.
59  *
60  * Usage:
61  *
62  * <cvsbootstrapper cvsroot="" file=""/>
63  *
64  */

65 public class CVSBootstrapper implements Bootstrapper {
66
67     private static final Logger LOG = Logger.getLogger(CVSBootstrapper.class);
68
69     private String JavaDoc localWorkingCopy;
70     private String JavaDoc filename;
71     private String JavaDoc cvsroot;
72     private boolean resetStickyTags = false;
73     private boolean overwriteChanges = false;
74
75     public void setCvsroot(String JavaDoc cvsroot) {
76         this.cvsroot = cvsroot;
77     }
78
79     public void setFile(String JavaDoc filename) {
80         this.filename = filename;
81     }
82
83     /**
84      * Sets the local working copy to use when making calls to CVS.
85      *
86      *@param local String relative or absolute path to the local
87      * working copy of the CVS module which contains the target file.
88      */

89     public void setLocalWorkingCopy(String JavaDoc local) {
90         localWorkingCopy = local;
91     }
92
93     /**
94      * Update the specified file.
95      */

96     public void bootstrap() {
97         try {
98             Commandline commandLine = buildUpdateCommand();
99             Process JavaDoc p = commandLine.execute();
100             StreamConsumer infoConsumer = new StreamConsumer() {
101                 public void consumeLine(String JavaDoc line) {
102                     LOG.warn(line);
103                 }
104             };
105             StreamConsumer warnConsumer = new StreamConsumer() {
106                 public void consumeLine(String JavaDoc line) {
107                     LOG.warn(line);
108                 }
109             };
110             StreamPumper errorPumper =
111                 new StreamPumper(p.getErrorStream(), null, warnConsumer);
112             StreamPumper outPumper = new StreamPumper(p.getInputStream(), null, infoConsumer);
113             Thread JavaDoc errorPumperThread = new Thread JavaDoc(errorPumper);
114             Thread JavaDoc outPumperThread = new Thread JavaDoc(outPumper);
115             errorPumperThread.start();
116             outPumperThread.start();
117             p.waitFor();
118             errorPumperThread.join();
119             outPumperThread.join();
120             p.getInputStream().close();
121             p.getOutputStream().close();
122             p.getErrorStream().close();
123         } catch (Exception JavaDoc e) {
124             LOG.error("Error executing CVS update command", e);
125         }
126     }
127
128     public void validate() throws CruiseControlException {
129         ValidationHelper.assertTrue(filename != null || cvsroot != null || localWorkingCopy != null,
130             "at least one of 'file', 'cvsroot' or 'localworkingcopy' is required as an attribute for CVSBootstrapper");
131
132         if (localWorkingCopy != null) {
133             File JavaDoc workingDir = new File JavaDoc(localWorkingCopy);
134
135             ValidationHelper.assertTrue(workingDir.exists(),
136                         "'localWorkingCopy' must be an existing directory. Was <"
137                         + localWorkingCopy + ">");
138             ValidationHelper.assertTrue(workingDir.isDirectory(),
139                         "'localWorkingCopy' must be an existing directory, not a file. Was <"
140                         + localWorkingCopy + ">");
141         }
142     }
143
144     protected Commandline buildUpdateCommand() throws CruiseControlException {
145         Commandline commandLine = new Commandline();
146
147         if (localWorkingCopy != null) {
148             commandLine.setWorkingDirectory(localWorkingCopy);
149         }
150
151         commandLine.setExecutable("cvs");
152
153         if (cvsroot != null) {
154             commandLine.createArgument().setValue("-d");
155             commandLine.createArgument().setValue(cvsroot);
156         }
157         commandLine.createArgument().setValue("update");
158         
159         StringBuffer JavaDoc flags = new StringBuffer JavaDoc("-dP");
160         if (resetStickyTags) {
161             flags.append("A");
162         }
163         if (overwriteChanges) {
164             flags.append("C");
165         }
166         commandLine.createArgument().setValue(flags.toString());
167
168         if (filename != null) {
169             commandLine.createArgument().setValue(filename);
170         }
171
172         return commandLine;
173     }
174
175     public void setResetStickyTags(boolean reset) {
176         resetStickyTags = reset;
177     }
178
179     public void setOverwriteChanges(boolean overwrite) {
180       overwriteChanges = overwrite;
181     }
182
183 }
184
Popular Tags