1 18 19 package org.apache.tools.ant.taskdefs.optional.net; 20 21 import org.apache.commons.net.bsd.RExecClient; 22 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.Calendar ; 27 import java.util.Enumeration ; 28 import java.util.Vector ; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 33 38 39 public class RExecTask extends Task { 40 43 private String userid = null; 44 45 48 private String password = null; 49 50 53 private String command = null; 54 55 58 private String server = null; 59 60 63 private int port = RExecClient.DEFAULT_PORT; 64 65 68 private Vector rexecTasks = new Vector (); 69 70 73 private boolean addCarriageReturn = false; 74 75 79 private Integer defaultTimeout = null; 80 81 85 public class RExecSubTask { 86 protected String taskString = ""; 88 90 95 public void execute(AntRExecClient rexec) 96 throws BuildException { 97 throw new BuildException("Shouldn't be able instantiate a SubTask directly"); 98 } 99 100 104 public void addText(String s) { 105 setString(getProject().replaceProperties(s)); 106 } 107 108 112 public void setString(String s) { 113 taskString += s; 114 } 115 } 116 117 120 public class RExecWrite extends RExecSubTask { 121 private boolean echoString = true; 122 127 public void execute(AntRExecClient rexec) 128 throws BuildException { 129 rexec.sendString(taskString, echoString); 130 } 131 132 137 public void setEcho(boolean b) { 138 echoString = b; 139 } 140 } 141 142 146 public class RExecRead extends RExecSubTask { 147 private Integer timeout = null; 148 153 public void execute(AntRExecClient rexec) 154 throws BuildException { 155 rexec.waitForString(taskString, timeout); 156 } 157 161 public void setTimeout(Integer i) { 162 this.timeout = i; 163 } 164 165 170 public void setDefaultTimeout(Integer defaultTimeout) { 171 if (timeout == null) { 172 timeout = defaultTimeout; 173 } 174 } 175 } 176 177 183 public class AntRExecClient extends RExecClient { 184 189 public void waitForString(String s) { 190 waitForString(s, null); 191 } 192 193 199 public void waitForString(String s, Integer timeout) { 200 InputStream is = this.getInputStream(); 201 try { 202 StringBuffer sb = new StringBuffer (); 203 if (timeout == null || timeout.intValue() == 0) { 204 while (sb.toString().indexOf(s) == -1) { 205 sb.append((char) is.read()); 206 } 207 } else { 208 Calendar endTime = Calendar.getInstance(); 209 endTime.add(Calendar.SECOND, timeout.intValue()); 210 while (sb.toString().indexOf(s) == -1) { 211 while (Calendar.getInstance().before(endTime) 212 && is.available() == 0) { 213 Thread.sleep(250); 214 } 215 if (is.available() == 0) { 216 throw new BuildException( 217 "Response timed-out waiting for \"" + s + '\"', 218 getLocation()); 219 } 220 sb.append((char) is.read()); 221 } 222 } 223 log(sb.toString(), Project.MSG_INFO); 224 } catch (BuildException be) { 225 throw be; 226 } catch (Exception e) { 227 throw new BuildException(e, getLocation()); 228 } 229 } 230 231 236 public void sendString(String s, boolean echoString) { 237 OutputStream os = this.getOutputStream(); 238 try { 239 os.write((s + "\n").getBytes()); 240 if (echoString) { 241 log(s, Project.MSG_INFO); 242 } 243 os.flush(); 244 } catch (Exception e) { 245 throw new BuildException(e, getLocation()); 246 } 247 } 248 253 public void waitForEOF(Integer timeout) { 254 InputStream is = this.getInputStream(); 255 try { 256 StringBuffer sb = new StringBuffer (); 257 if (timeout == null || timeout.intValue() == 0) { 258 int read; 259 while ((read = is.read()) != -1) { 260 char c = (char) read; 261 sb.append(c); 262 if (c == '\n') { 263 log(sb.toString(), Project.MSG_INFO); 264 sb.delete(0, sb.length()); 265 } 266 } 267 } else { 268 Calendar endTime = Calendar.getInstance(); 269 endTime.add(Calendar.SECOND, timeout.intValue()); 270 int read = 0; 271 while (read != -1) { 272 while (Calendar.getInstance().before(endTime) && is.available() == 0) { 273 Thread.sleep(250); 274 } 275 if (is.available() == 0) { 276 log(sb.toString(), Project.MSG_INFO); 277 throw new BuildException( 278 "Response timed-out waiting for EOF", 279 getLocation()); 280 } 281 read = is.read(); 282 if (read != -1) { 283 char c = (char) read; 284 sb.append(c); 285 if (c == '\n') { 286 log(sb.toString(), Project.MSG_INFO); 287 sb.delete(0, sb.length()); 288 } 289 } 290 } 291 } 292 if (sb.length() > 0) { 293 log(sb.toString(), Project.MSG_INFO); 294 } 295 } catch (BuildException be) { 296 throw be; 297 } catch (Exception e) { 298 throw new BuildException(e, getLocation()); 299 } 300 } 301 302 } 303 309 310 public RExecSubTask createRead() { 311 RExecSubTask task = (RExecSubTask) new RExecRead(); 312 rexecTasks.addElement(task); 313 return task; 314 } 315 321 public RExecSubTask createWrite() { 322 RExecSubTask task = (RExecSubTask) new RExecWrite(); 323 rexecTasks.addElement(task); 324 return task; 325 } 326 332 public void execute() throws BuildException { 333 334 if (server == null) { 335 throw new BuildException("No Server Specified"); 336 } 337 340 if (userid == null && password != null) { 341 throw new BuildException("No Userid Specified"); 342 } 343 if (password == null && userid != null) { 344 throw new BuildException("No Password Specified"); 345 } 346 347 348 AntRExecClient rexec = null; 349 try { 350 rexec = new AntRExecClient(); 351 try { 352 rexec.connect(server, port); 353 } catch (IOException e) { 354 throw new BuildException("Can't connect to " + server); 355 } 356 if (userid != null && password != null && command != null 357 && rexecTasks.size() == 0) { 358 rexec.rexec(userid, password, command); 360 } else { 361 handleMultipleTasks(rexec); 363 } 364 365 366 rexec.waitForEOF(defaultTimeout); 367 } catch (IOException e) { 368 throw new BuildException("Error r-executing command", e); 369 } finally { 370 if (rexec != null && rexec.isConnected()) { 371 try { 372 rexec.disconnect(); 373 } catch (IOException e) { 374 throw new BuildException("Error disconnecting from " 375 + server); 376 } 377 } 378 } 379 } 380 384 private void login(AntRExecClient rexec) { 385 if (addCarriageReturn) { 386 rexec.sendString("\n", true); 387 } 388 rexec.waitForString("ogin:"); 389 rexec.sendString(userid, true); 390 rexec.waitForString("assword:"); 391 rexec.sendString(password, false); 392 } 393 397 public void setCommand(String c) { 398 this.command = c; 399 } 400 401 405 public void setInitialCR(boolean b) { 406 this.addCarriageReturn = b; 407 } 408 413 public void setPassword(String p) { 414 this.password = p; 415 } 416 417 421 public void setPort(int p) { 422 this.port = p; 423 } 424 425 429 public void setServer(String m) { 430 this.server = m; 431 } 432 433 438 public void setTimeout(Integer i) { 439 this.defaultTimeout = i; 440 } 441 446 public void setUserid(String u) { 447 this.userid = u; 448 } 449 450 455 private void handleMultipleTasks(AntRExecClient rexec) { 456 457 458 if (userid != null && password != null) { 459 login(rexec); 460 } 461 462 Enumeration tasksToRun = rexecTasks.elements(); 463 while (tasksToRun != null && tasksToRun.hasMoreElements()) { 464 RExecSubTask task = (RExecSubTask) tasksToRun.nextElement(); 465 if (task instanceof RExecRead && defaultTimeout != null) { 466 ((RExecRead) task).setDefaultTimeout(defaultTimeout); 467 } 468 task.execute(rexec); 469 } 470 } 471 } 472 | Popular Tags |