001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001-2003, 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.publishers;
037:
038: import net.sourceforge.cruisecontrol.CruiseControlException;
039: import net.sourceforge.cruisecontrol.Publisher;
040: import net.sourceforge.cruisecontrol.util.Commandline;
041: import net.sourceforge.cruisecontrol.util.ValidationHelper;
042: import net.sourceforge.cruisecontrol.util.Commandline.Argument;
043: import net.sourceforge.cruisecontrol.util.XMLLogHelper;
044: import org.apache.log4j.Logger;
045: import org.jdom.Element;
046:
047: import java.io.BufferedReader;
048: import java.io.File;
049: import java.io.IOException;
050: import java.io.InputStreamReader;
051:
052: /**
053: * Used to scp a file to a remote location
054: *
055: * @author <a href="orenmnero@sourceforge.net">Oren Miller</a>
056: */
057:
058: public class SCPPublisher implements Publisher {
059:
060: private static final Logger LOG = Logger
061: .getLogger(SCPPublisher.class);
062:
063: private String executableName = "scp";
064: private String sourceUser;
065: private String sourceHost;
066: private String sourceDir = ".";
067: private String targetUser;
068: private String targetHost;
069: private String targetDir = ".";
070: private String ssh = "ssh";
071: private String options;
072: private String file;
073: private String targetSeparator = File.separator;
074: private String sourceSeparator = File.separator;
075:
076: public void setExecutableName(String executableName) {
077: this .executableName = executableName;
078: }
079:
080: public void setSourceUser(String sourceUser) {
081: this .sourceUser = sourceUser;
082: }
083:
084: public void setSourceHost(String sourceHost) {
085: this .sourceHost = sourceHost;
086: }
087:
088: public void setSourceDir(String sourceDir) {
089: this .sourceDir = sourceDir;
090: }
091:
092: public void setTargetUser(String targetUser) {
093: this .targetUser = targetUser;
094: }
095:
096: public void setTargetHost(String targetHost) {
097: this .targetHost = targetHost;
098: }
099:
100: public void setTargetDir(String targetDir) {
101: this .targetDir = targetDir;
102: }
103:
104: public void setSSH(String ssh) {
105: this .ssh = ssh;
106: }
107:
108: public void setOptions(String options) {
109: this .options = options;
110: }
111:
112: public void setFile(String file) {
113: this .file = file;
114: }
115:
116: public void setTargetSeparator(String targetSeparator) {
117: this .targetSeparator = targetSeparator;
118: }
119:
120: public void setSourceSeparator(String sourceSeparator) {
121: this .sourceSeparator = sourceSeparator;
122: }
123:
124: /**
125: * Called after the configuration is read to make sure that all the mandatory parameters
126: * were specified..
127: *
128: * @throws CruiseControlException if there was a configuration error.
129: */
130: public void validate() throws CruiseControlException {
131: ValidationHelper.assertIsSet(executableName, "executableName",
132: this .getClass());
133: ValidationHelper.assertNotEmpty(executableName,
134: "executableName", this .getClass());
135: ValidationHelper.assertFalse(sourceUser == null
136: && sourceHost != null,
137: "'sourceuser' not specified in configuration file");
138:
139: ValidationHelper.assertFalse(sourceHost == null
140: && sourceUser != null,
141: "'sourcehost' not specified in configuration file");
142:
143: ValidationHelper.assertFalse(targetUser == null
144: && targetHost != null,
145: "'targetuser' not specified in configuration file");
146:
147: ValidationHelper.assertFalse(targetHost == null
148: && targetUser != null,
149: "'targethost' not specified in configuration file");
150: }
151:
152: public void publish(Element cruisecontrolLog)
153: throws CruiseControlException {
154: boolean publishCurrentLogFile = file == null;
155: if (publishCurrentLogFile) {
156: file = getLogFileName(cruisecontrolLog);
157: LOG.debug(file);
158: }
159:
160: try {
161: Commandline command = createCommandline(file);
162: executeCommand(command);
163: } finally {
164: if (publishCurrentLogFile) {
165: file = null;
166: }
167: }
168: }
169:
170: protected String getLogFileName(Element cruisecontrolLog)
171: throws CruiseControlException {
172: XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
173: return helper.getLogFileName();
174: }
175:
176: protected void executeCommand(Commandline command)
177: throws CruiseControlException {
178: LOG.info("executing command: " + command);
179: try {
180: Process p = command.execute();
181: LOG.debug("Runtime after.");
182: p.waitFor();
183: LOG
184: .debug("waitfor() ended with exit code "
185: + p.exitValue());
186:
187: try {
188: BufferedReader commandErrorResult = new BufferedReader(
189: new InputStreamReader(p.getErrorStream()));
190: String outputLine;
191: while ((outputLine = commandErrorResult.readLine()) != null) {
192: LOG.warn("Runtime.exec error returned: "
193: + outputLine);
194: }
195:
196: } catch (IOException e) {
197: LOG.warn("Runtime.exec: reading errorStream failed");
198: throw new CruiseControlException(e);
199: }
200:
201: } catch (Exception e) {
202: LOG.warn("Runtime.exec exception.");
203: throw new CruiseControlException(e);
204: }
205: }
206:
207: protected Commandline createCommandline(String file) {
208: String sourcefile = sourceSeparator + file;
209: String targetfile = targetSeparator;
210:
211: Commandline command = new Commandline();
212: command.setExecutable(executableName);
213: command.createArgument().setLine(options);
214: command.createArguments("-S", ssh);
215:
216: createFileArgument(command.createArgument(), sourceUser,
217: sourceHost, sourceDir, sourcefile);
218:
219: createFileArgument(command.createArgument(), targetUser,
220: targetHost, targetDir, targetfile);
221:
222: return command;
223:
224: }
225:
226: private void createFileArgument(Argument arg, String user,
227: String host, String dir, String file) {
228:
229: String argValue = "";
230:
231: if (user != null && host != null) {
232: argValue = user + "@" + host + ":";
233: }
234:
235: argValue += dir;
236: argValue += file;
237: arg.setValue(argValue);
238: }
239:
240: }
|