001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.bootstrappers;
037:
038: import net.sourceforge.cruisecontrol.Bootstrapper;
039: import net.sourceforge.cruisecontrol.CruiseControlException;
040: import net.sourceforge.cruisecontrol.util.Commandline;
041: import net.sourceforge.cruisecontrol.util.ValidationHelper;
042:
043: import org.apache.log4j.Logger;
044:
045: import java.io.File;
046:
047: /**
048: * The SVNBootstrapper will handle updating a single file from Subversion before the build begins.
049: *
050: * @see <a href="http://subversion.tigris.org/">subversion.tigris.org</a>
051: * @author <a href="etienne.studer@canoo.com">Etienne Studer</a>
052: */
053: public class SVNBootstrapper implements Bootstrapper {
054: private static final Logger LOG = Logger
055: .getLogger(SVNBootstrapper.class);
056:
057: /** Configuration parameters */
058: private String fileName;
059: private String localWorkingCopy;
060: private String userName;
061: private String password;
062:
063: /**
064: * @param fileName
065: * the file to update from the Subversion repository.
066: */
067: public void setFile(String fileName) {
068: this .fileName = fileName;
069: }
070:
071: /**
072: * Sets the local working copy to use when making calls to Subversion.
073: *
074: * @param localWorkingCopy
075: * String indicating the relative or absolute path to the local working copy of the Subversion repository
076: * on which to execute the update command.
077: */
078: public void setLocalWorkingCopy(String localWorkingCopy) {
079: this .localWorkingCopy = localWorkingCopy;
080: }
081:
082: /**
083: * @param userName
084: * the username for authentication.
085: */
086: public void setUsername(String userName) {
087: this .userName = userName;
088: }
089:
090: /**
091: * @param password
092: * the password for authentication.
093: */
094: public void setPassword(String password) {
095: this .password = password;
096: }
097:
098: /**
099: * This method validates that at least the filename or the local working copy location has been specified.
100: *
101: * @throws CruiseControlException
102: * Thrown when the repository location and the local working copy location are both null
103: */
104: public void validate() throws CruiseControlException {
105: ValidationHelper
106: .assertTrue(
107: fileName != null || localWorkingCopy != null,
108: "At least 'filename' or 'localWorkingCopy' is a "
109: + "required attribute on the Subversion bootstrapper task");
110:
111: if (localWorkingCopy != null) {
112: File workingDir = new File(localWorkingCopy);
113: ValidationHelper.assertTrue(workingDir.exists()
114: && workingDir.isDirectory(),
115: "'localWorkingCopy' must be an existing directory. Was"
116: + workingDir.getAbsolutePath());
117: }
118: }
119:
120: /**
121: * Update the specified file from the subversion repository.
122: *
123: * @throws CruiseControlException
124: */
125: public void bootstrap() throws CruiseControlException {
126: buildUpdateCommand().executeAndWait(LOG);
127: }
128:
129: /**
130: * Generates the command line for the svn update command.
131: *
132: * For example:
133: *
134: * 'svn update --non-interactive filename'
135: *
136: * @return the command line for the svn update command
137: * @throws CruiseControlException
138: * if the working directory is not valid.
139: */
140: Commandline buildUpdateCommand() throws CruiseControlException {
141: Commandline command = new Commandline();
142: command.setExecutable("svn");
143:
144: if (localWorkingCopy != null) {
145: command.setWorkingDirectory(localWorkingCopy);
146: }
147:
148: command.createArgument("update");
149: command.createArgument("--non-interactive");
150: if (userName != null) {
151: command.createArguments("--username", userName);
152: }
153: if (password != null) {
154: command.createArguments("--password", password);
155: }
156: if (fileName != null) {
157: command.createArgument(fileName);
158: }
159:
160: LOG.debug("SVNBootstrapper: Executing command = " + command);
161:
162: return command;
163: }
164: }
|