1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.util.TeeOutputStream; 24 import org.apache.tools.ant.util.KeepAliveOutputStream; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.io.StringReader ; 31 32 import com.jcraft.jsch.ChannelExec; 33 import com.jcraft.jsch.JSchException; 34 import com.jcraft.jsch.Session; 35 36 40 public class SSHExec extends SSHBase { 41 42 43 private String command = null; 44 45 46 private long maxwait = 0; 47 48 49 private Thread thread = null; 50 51 private String outputProperty = null; private File outputFile = null; private boolean append = false; 55 private static final String TIMEOUT_MESSAGE = 56 "Timeout period exceeded, connection dropped."; 57 58 61 public SSHExec() { 62 super(); 63 } 64 65 70 public void setCommand(String command) { 71 this.command = command; 72 } 73 74 81 public void setTimeout(long timeout) { 82 maxwait = timeout; 83 } 84 85 90 public void setOutput(File output) { 91 outputFile = output; 92 } 93 94 101 public void setAppend(boolean append) { 102 this.append = append; 103 } 104 105 111 public void setOutputproperty(String property) { 112 outputProperty = property; 113 } 114 115 120 public void execute() throws BuildException { 121 if (getHost() == null) { 122 throw new BuildException("Host is required."); 123 } 124 if (getUserInfo().getName() == null) { 125 throw new BuildException("Username is required."); 126 } 127 if (getUserInfo().getKeyfile() == null 128 && getUserInfo().getPassword() == null) { 129 throw new BuildException("Password or Keyfile is required."); 130 } 131 if (command == null) { 132 throw new BuildException("Command is required."); 133 } 134 135 ByteArrayOutputStream out = new ByteArrayOutputStream (); 136 TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out)); 137 138 Session session = null; 139 try { 140 session = openSession(); 142 session.setTimeout((int) maxwait); 143 final ChannelExec channel = (ChannelExec) session.openChannel("exec"); 144 channel.setCommand(command); 145 channel.setOutputStream(tee); 146 channel.setExtOutputStream(tee); 147 channel.connect(); 148 149 thread = 151 new Thread () { 152 public void run() { 153 while (!channel.isEOF()) { 154 if (thread == null) { 155 return; 156 } 157 try { 158 sleep(500); 159 } catch (Exception e) { 160 } 162 } 163 } 164 }; 165 166 thread.start(); 167 thread.join(maxwait); 168 169 if (thread.isAlive()) { 170 thread = null; 172 if (getFailonerror()) { 173 throw new BuildException(TIMEOUT_MESSAGE); 174 } else { 175 log(TIMEOUT_MESSAGE, Project.MSG_ERR); 176 } 177 } else { 178 if (outputProperty != null) { 180 getProject().setProperty(outputProperty, out.toString()); 181 } 182 if (outputFile != null) { 183 writeToFile(out.toString(), append, outputFile); 184 } 185 186 int ec = channel.getExitStatus(); 189 if (ec != 0) { 190 String msg = "Remote command failed with exit status " + ec; 191 if (getFailonerror()) { 192 throw new BuildException(msg); 193 } else { 194 log(msg, Project.MSG_ERR); 195 } 196 } 197 } 198 } catch (BuildException e) { 199 throw e; 200 } catch (JSchException e) { 201 if (e.getMessage().indexOf("session is down") >= 0) { 202 if (getFailonerror()) { 203 throw new BuildException(TIMEOUT_MESSAGE, e); 204 } else { 205 log(TIMEOUT_MESSAGE, Project.MSG_ERR); 206 } 207 } else { 208 if (getFailonerror()) { 209 throw new BuildException(e); 210 } else { 211 log("Caught exception: " + e.getMessage(), 212 Project.MSG_ERR); 213 } 214 } 215 } catch (Exception e) { 216 if (getFailonerror()) { 217 throw new BuildException(e); 218 } else { 219 log("Caught exception: " + e.getMessage(), Project.MSG_ERR); 220 } 221 } finally { 222 if (session != null && session.isConnected()) { 223 session.disconnect(); 224 } 225 } 226 } 227 228 229 238 private void writeToFile(String from, boolean append, File to) 239 throws IOException { 240 FileWriter out = null; 241 try { 242 out = new FileWriter (to.getAbsolutePath(), append); 243 StringReader in = new StringReader (from); 244 char[] buffer = new char[8192]; 245 int bytesRead; 246 while (true) { 247 bytesRead = in.read(buffer); 248 if (bytesRead == -1) { 249 break; 250 } 251 out.write(buffer, 0, bytesRead); 252 } 253 out.flush(); 254 } finally { 255 if (out != null) { 256 out.close(); 257 } 258 } 259 } 260 261 } 262 | Popular Tags |