1 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 ; 49 import java.io.File ; 50 import java.io.IOException ; 51 import java.io.InputStreamReader ; 52 53 58 59 public class SCPPublisher implements Publisher { 60 61 private static final Logger LOG = Logger.getLogger(SCPPublisher.class); 62 63 private String sourceUser; 64 private String sourceHost; 65 private String sourceDir = "."; 66 private String targetUser; 67 private String targetHost; 68 private String targetDir = "."; 69 private String ssh = "ssh"; 70 private String options; 71 private String file; 72 private String targetSeparator = File.separator; 73 private String sourceSeparator = File.separator; 74 75 public void setSourceUser(String sourceUser) { 76 this.sourceUser = sourceUser; 77 } 78 79 public void setSourceHost(String sourceHost) { 80 this.sourceHost = sourceHost; 81 } 82 83 public void setSourceDir(String sourceDir) { 84 this.sourceDir = sourceDir; 85 } 86 87 public void setTargetUser(String targetUser) { 88 this.targetUser = targetUser; 89 } 90 91 public void setTargetHost(String targetHost) { 92 this.targetHost = targetHost; 93 } 94 95 public void setTargetDir(String targetDir) { 96 this.targetDir = targetDir; 97 } 98 99 public void setSSH(String ssh) { 100 this.ssh = ssh; 101 } 102 103 public void setOptions(String options) { 104 this.options = options; 105 } 106 107 public void setFile(String file) { 108 this.file = file; 109 } 110 111 public void setTargetSeparator(String targetSeparator) { 112 this.targetSeparator = targetSeparator; 113 } 114 115 public void setSourceSeparator(String sourceSeparator) { 116 this.sourceSeparator = sourceSeparator; 117 } 118 119 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 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 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 commandErrorResult = new BufferedReader (new InputStreamReader (p.getErrorStream())); 171 String outputLine; 172 while ((outputLine = commandErrorResult.readLine()) != null) { 173 LOG.warn("Runtime.exec error returned: " + outputLine); 174 } 175 176 } catch (IOException e) { 177 LOG.warn("Runtime.exec: reading errorStream failed"); 178 throw new CruiseControlException(e); 179 } 180 181 } catch (Exception e) { 182 LOG.warn("Runtime.exec exception."); 183 throw new CruiseControlException(e); 184 } 185 } 186 187 protected Commandline createCommandline(String file) { 188 String sourcefile = sourceSeparator + file; 189 String 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 user, 218 String host, 219 String dir, 220 String file) { 221 222 String 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 |