KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > SCPPublisher


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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.publishers;
38
39 import net.sourceforge.cruisecontrol.CruiseControlException;
40 import net.sourceforge.cruisecontrol.Publisher;
41 import net.sourceforge.cruisecontrol.util.Commandline;
42 import net.sourceforge.cruisecontrol.util.ValidationHelper;
43 import net.sourceforge.cruisecontrol.util.Commandline.Argument;
44 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
45 import org.apache.log4j.Logger;
46 import org.jdom.Element;
47
48 import java.io.BufferedReader JavaDoc;
49 import java.io.File JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.InputStreamReader JavaDoc;
52
53 /**
54  * Used to scp a file to a remote location
55  *
56  * @author <a HREF="orenmnero@sourceforge.net">Oren Miller</a>
57  */

58
59 public class SCPPublisher implements Publisher {
60
61     private static final Logger LOG = Logger.getLogger(SCPPublisher.class);
62
63     private String JavaDoc sourceUser;
64     private String JavaDoc sourceHost;
65     private String JavaDoc sourceDir = ".";
66     private String JavaDoc targetUser;
67     private String JavaDoc targetHost;
68     private String JavaDoc targetDir = ".";
69     private String JavaDoc ssh = "ssh";
70     private String JavaDoc options;
71     private String JavaDoc file;
72     private String JavaDoc targetSeparator = File.separator;
73     private String JavaDoc sourceSeparator = File.separator;
74
75     public void setSourceUser(String JavaDoc sourceUser) {
76         this.sourceUser = sourceUser;
77     }
78
79     public void setSourceHost(String JavaDoc sourceHost) {
80         this.sourceHost = sourceHost;
81     }
82
83     public void setSourceDir(String JavaDoc sourceDir) {
84         this.sourceDir = sourceDir;
85     }
86
87     public void setTargetUser(String JavaDoc targetUser) {
88         this.targetUser = targetUser;
89     }
90
91     public void setTargetHost(String JavaDoc targetHost) {
92         this.targetHost = targetHost;
93     }
94
95     public void setTargetDir(String JavaDoc targetDir) {
96         this.targetDir = targetDir;
97     }
98
99     public void setSSH(String JavaDoc ssh) {
100         this.ssh = ssh;
101     }
102
103     public void setOptions(String JavaDoc options) {
104         this.options = options;
105     }
106
107     public void setFile(String JavaDoc file) {
108         this.file = file;
109     }
110
111     public void setTargetSeparator(String JavaDoc targetSeparator) {
112         this.targetSeparator = targetSeparator;
113     }
114     
115     public void setSourceSeparator(String JavaDoc sourceSeparator) {
116         this.sourceSeparator = sourceSeparator;
117     }
118
119     /**
120      * Called after the configuration is read to make sure that all the mandatory parameters
121      * were specified..
122      *
123      * @throws CruiseControlException if there was a configuration error.
124      */

125     public void validate() throws CruiseControlException {
126         ValidationHelper.assertFalse(sourceUser == null && sourceHost != null,
127             "'sourceuser' not specified in configuration file");
128
129         ValidationHelper.assertFalse(sourceHost == null && sourceUser != null,
130             "'sourcehost' not specified in configuration file");
131
132         ValidationHelper.assertFalse(targetUser == null && targetHost != null,
133             "'targetuser' not specified in configuration file");
134
135         ValidationHelper.assertFalse(targetHost == null && targetUser != null,
136             "'targethost' not specified in configuration file");
137     }
138
139     public void publish(Element cruisecontrolLog) throws CruiseControlException {
140         boolean publishCurrentLogFile = file == null;
141         if (publishCurrentLogFile) {
142             file = getLogFileName(cruisecontrolLog);
143             LOG.debug(file);
144         }
145
146         try {
147             Commandline command = createCommandline(file);
148             executeCommand(command);
149         } finally {
150             if (publishCurrentLogFile) {
151                 file = null;
152             }
153         }
154     }
155
156     protected String JavaDoc getLogFileName(Element cruisecontrolLog) throws CruiseControlException {
157         XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
158         return helper.getLogFileName();
159     }
160
161     protected void executeCommand(Commandline command) throws CruiseControlException {
162         LOG.info("executing command: " + command);
163         try {
164             Process JavaDoc p = command.execute();;
165             LOG.debug("Runtime after.");
166             p.waitFor();
167             LOG.debug("waitfor() ended with exit code " + p.exitValue());
168
169             try {
170                 BufferedReader JavaDoc commandErrorResult = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
171                 String JavaDoc outputLine;
172                 while ((outputLine = commandErrorResult.readLine()) != null) {
173                     LOG.warn("Runtime.exec error returned: " + outputLine);
174                 }
175
176             } catch (IOException JavaDoc e) {
177                 LOG.warn("Runtime.exec: reading errorStream failed");
178                 throw new CruiseControlException(e);
179             }
180
181         } catch (Exception JavaDoc e) {
182             LOG.warn("Runtime.exec exception.");
183             throw new CruiseControlException(e);
184         }
185     }
186
187     protected Commandline createCommandline(String JavaDoc file) {
188         String JavaDoc sourcefile = sourceSeparator + file;
189         String JavaDoc targetfile = targetSeparator;
190
191         Commandline command = new Commandline();
192         command.setExecutable("scp");
193         command.createArgument().setLine(options);
194         command.createArgument().setValue("-S");
195         command.createArgument().setValue(ssh);
196
197         createFileArgument(
198             command.createArgument(),
199             sourceUser,
200             sourceHost,
201             sourceDir,
202             sourcefile);
203
204         createFileArgument(
205             command.createArgument(),
206             targetUser,
207             targetHost,
208             targetDir,
209             targetfile);
210
211         return command;
212
213     }
214
215     private void createFileArgument(
216         Argument arg,
217         String JavaDoc user,
218         String JavaDoc host,
219         String JavaDoc dir,
220         String JavaDoc file) {
221
222         String JavaDoc argValue = "";
223
224         if (user != null && host != null) {
225             argValue = user + "@" + host + ":";
226         }
227
228         argValue += dir;
229         argValue += file;
230         arg.setValue(argValue);
231     }
232
233 }
234
Popular Tags