KickJava   Java API By Example, From Geeks To Geeks.

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


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.StreamPumper;
43 import net.sourceforge.cruisecontrol.util.ValidationHelper;
44
45 import org.apache.log4j.Logger;
46
47 import java.io.File JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.PrintWriter JavaDoc;
50
51 /**
52  * The SVNBootstrapper will handle updating a single file from Subversion
53  * before the build begins.
54  *
55  * @see <a HREF="http://subversion.tigris.org/">subversion.tigris.org</a>
56  * @author <a HREF="etienne.studer@canoo.com">Etienne Studer</a>
57  */

58 public class SVNBootstrapper implements Bootstrapper {
59     private static final Logger LOG = Logger.getLogger(SVNBootstrapper.class);
60
61     /** Configuration parameters */
62     private String JavaDoc fileName;
63     private String JavaDoc localWorkingCopy;
64     private String JavaDoc userName;
65     private String JavaDoc password;
66
67     /**
68      * Sets the file to update from the Subversion repository.
69      */

70     public void setFile(String JavaDoc fileName) {
71         this.fileName = fileName;
72     }
73
74     /**
75      * Sets the local working copy to use when making calls to Subversion.
76      *
77      * @param localWorkingCopy String indicating the relative or absolute path
78      * to the local working copy of the Subversion
79      * repository on which to execute the update command.
80      */

81     public void setLocalWorkingCopy(String JavaDoc localWorkingCopy) {
82         this.localWorkingCopy = localWorkingCopy;
83     }
84
85     /**
86      * Sets the username for authentication.
87      */

88     public void setUsername(String JavaDoc userName) {
89         this.userName = userName;
90     }
91
92     /**
93      * Sets the password for authentication.
94      */

95     public void setPassword(String JavaDoc password) {
96         this.password = password;
97     }
98
99     /**
100      * This method validates that at least the filename or the local working
101      * copy location has been specified.
102      *
103      * @throws CruiseControlException Thrown when the repository location and
104      * the local working copy location are both
105      * null
106      */

107     public void validate() throws CruiseControlException {
108         ValidationHelper.assertTrue(fileName != null || localWorkingCopy != null,
109             "At least 'filename' or 'localWorkingCopy' is a "
110                     + "required attribute on the Subversion bootstrapper task");
111
112         if (localWorkingCopy != null) {
113             File JavaDoc workingDir = new File JavaDoc(localWorkingCopy);
114             ValidationHelper.assertTrue(workingDir.exists() && workingDir.isDirectory(),
115                 "'localWorkingCopy' must be an existing " + "directory.");
116         }
117     }
118
119     /**
120      * Update the specified file from the subversion repository.
121      */

122     public void bootstrap() {
123         try {
124             Commandline commandLine = buildUpdateCommand();
125             execUpdateCommand(commandLine);
126         } catch (Exception JavaDoc e) {
127             LOG.error("Error executing svn update command", e);
128         }
129     }
130
131     /**
132      * Generates the command line for the svn update command.
133      *
134      * For example:
135      *
136      * 'svn update --non-interactive filename'
137      */

138     Commandline buildUpdateCommand() throws CruiseControlException {
139         Commandline command = new Commandline();
140         command.setExecutable("svn");
141
142         if (localWorkingCopy != null) {
143             command.setWorkingDirectory(localWorkingCopy);
144         }
145
146         command.createArgument().setValue("update");
147         command.createArgument().setValue("--non-interactive");
148         if (userName != null) {
149             command.createArgument().setValue("--username");
150             command.createArgument().setValue(userName);
151         }
152         if (password != null) {
153             command.createArgument().setValue("--password");
154             command.createArgument().setValue(password);
155         }
156         if (fileName != null) {
157             command.createArgument().setValue(fileName);
158         }
159
160         LOG.debug("SVNBootstrapper: Executing command = " + command);
161
162         return command;
163     }
164
165     private void execUpdateCommand(Commandline command)
166         throws IOException JavaDoc, InterruptedException JavaDoc {
167             
168         Process JavaDoc p = command.execute();
169
170         logErrorStream(p);
171         logOutStream(p);
172
173         p.waitFor();
174         p.getInputStream().close();
175         p.getOutputStream().close();
176         p.getErrorStream().close();
177     }
178
179     private void logErrorStream(Process JavaDoc p) {
180         StreamPumper errorPumper =
181             new StreamPumper(p.getErrorStream(), new PrintWriter JavaDoc(System.err, true));
182         new Thread JavaDoc(errorPumper).start();
183     }
184
185     private void logOutStream(Process JavaDoc p) {
186         StreamPumper outPumper =
187             new StreamPumper(p.getInputStream(), new PrintWriter JavaDoc(System.out, true));
188         new Thread JavaDoc(outPumper).start();
189     }
190 }
191
Popular Tags