1 18 19 package org.apache.tools.ant.taskdefs.optional.net; 20 21 import org.apache.commons.net.telnet.TelnetClient; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.Calendar ; 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Task; 31 32 36 37 public class TelnetTask extends Task { 38 41 private String userid = null; 42 43 46 private String password = null; 47 48 51 private String server = null; 52 53 56 private int port = 23; 57 58 61 private Vector telnetTasks = new Vector (); 62 63 66 private boolean addCarriageReturn = false; 67 68 72 private Integer defaultTimeout = null; 73 74 80 public void execute() throws BuildException { 81 82 if (server == null) { 83 throw new BuildException("No Server Specified"); 84 } 85 88 if (userid == null && password != null) { 89 throw new BuildException("No Userid Specified"); 90 } 91 if (password == null && userid != null) { 92 throw new BuildException("No Password Specified"); 93 } 94 95 96 AntTelnetClient telnet = null; 97 try { 98 telnet = new AntTelnetClient(); 99 try { 100 telnet.connect(server, port); 101 } catch (IOException e) { 102 throw new BuildException("Can't connect to " + server); 103 } 104 105 if (userid != null && password != null) { 106 login(telnet); 107 } 108 109 Enumeration tasksToRun = telnetTasks.elements(); 110 while (tasksToRun != null && tasksToRun.hasMoreElements()) { 111 TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement(); 112 if (task instanceof TelnetRead && defaultTimeout != null) { 113 ((TelnetRead) task).setDefaultTimeout(defaultTimeout); 114 } 115 task.execute(telnet); 116 } 117 } finally { 118 if (telnet != null && telnet.isConnected()) { 119 try { 120 telnet.disconnect(); 121 } catch (IOException e) { 122 throw new BuildException("Error disconnecting from " 123 + server); 124 } 125 } 126 } 127 } 128 129 133 private void login(AntTelnetClient telnet) { 134 if (addCarriageReturn) { 135 telnet.sendString("\n", true); 136 } 137 telnet.waitForString("ogin:"); 138 telnet.sendString(userid, true); 139 telnet.waitForString("assword:"); 140 telnet.sendString(password, false); 141 } 142 143 148 public void setUserid(String u) { 149 this.userid = u; 150 } 151 152 157 public void setPassword(String p) { 158 this.password = p; 159 } 160 161 165 public void setServer(String m) { 166 this.server = m; 167 } 168 169 173 public void setPort(int p) { 174 this.port = p; 175 } 176 177 181 public void setInitialCR(boolean b) { 182 this.addCarriageReturn = b; 183 } 184 185 190 public void setTimeout(Integer i) { 191 this.defaultTimeout = i; 192 } 193 194 200 201 public TelnetSubTask createRead() { 202 TelnetSubTask task = (TelnetSubTask) new TelnetRead(); 203 telnetTasks.addElement(task); 204 return task; 205 } 206 207 213 public TelnetSubTask createWrite() { 214 TelnetSubTask task = (TelnetSubTask) new TelnetWrite(); 215 telnetTasks.addElement(task); 216 return task; 217 } 218 219 223 public class TelnetSubTask { 224 protected String taskString = ""; 226 232 public void execute(AntTelnetClient telnet) 233 throws BuildException { 234 throw new BuildException("Shouldn't be able instantiate a SubTask directly"); 235 } 236 237 241 public void addText(String s) { 242 setString(getProject().replaceProperties(s)); 243 } 244 245 249 public void setString(String s) { 250 taskString += s; 251 } 252 } 253 254 257 public class TelnetWrite extends TelnetSubTask { 258 private boolean echoString = true; 259 264 public void execute(AntTelnetClient telnet) 265 throws BuildException { 266 telnet.sendString(taskString, echoString); 267 } 268 269 274 public void setEcho(boolean b) { 275 echoString = b; 276 } 277 } 278 279 283 public class TelnetRead extends TelnetSubTask { 284 private Integer timeout = null; 285 290 public void execute(AntTelnetClient telnet) 291 throws BuildException { 292 telnet.waitForString(taskString, timeout); 293 } 294 298 public void setTimeout(Integer i) { 299 this.timeout = i; 300 } 301 302 307 public void setDefaultTimeout(Integer defaultTimeout) { 308 if (timeout == null) { 309 timeout = defaultTimeout; 310 } 311 } 312 } 313 314 320 public class AntTelnetClient extends TelnetClient { 321 326 public void waitForString(String s) { 327 waitForString(s, null); 328 } 329 330 336 public void waitForString(String s, Integer timeout) { 337 InputStream is = this.getInputStream(); 338 try { 339 StringBuffer sb = new StringBuffer (); 340 if (timeout == null || timeout.intValue() == 0) { 341 while (sb.toString().indexOf(s) == -1) { 342 sb.append((char) is.read()); 343 } 344 } else { 345 Calendar endTime = Calendar.getInstance(); 346 endTime.add(Calendar.SECOND, timeout.intValue()); 347 while (sb.toString().indexOf(s) == -1) { 348 while (Calendar.getInstance().before(endTime) 349 && is.available() == 0) { 350 Thread.sleep(250); 351 } 352 if (is.available() == 0) { 353 log("Read before running into timeout: " 354 + sb.toString(), Project.MSG_DEBUG); 355 throw new BuildException( 356 "Response timed-out waiting for \"" + s + '\"', 357 getLocation()); 358 } 359 sb.append((char) is.read()); 360 } 361 } 362 log(sb.toString(), Project.MSG_INFO); 363 } catch (BuildException be) { 364 throw be; 365 } catch (Exception e) { 366 throw new BuildException(e, getLocation()); 367 } 368 } 369 370 375 public void sendString(String s, boolean echoString) { 376 OutputStream os = this.getOutputStream(); 377 try { 378 os.write((s + "\n").getBytes()); 379 if (echoString) { 380 log(s, Project.MSG_INFO); 381 } 382 os.flush(); 383 } catch (Exception e) { 384 throw new BuildException(e, getLocation()); 385 } 386 } 387 } 388 } 389 | Popular Tags |