1 4 package org.oddjob.dummy; 5 6 import java.io.File ; 7 import java.util.Enumeration ; 8 import java.util.Locale ; 9 import java.util.Vector ; 10 11 import org.apache.tools.ant.BuildException; 12 import org.apache.tools.ant.Project; 13 import org.apache.tools.ant.Task; 14 import org.apache.tools.ant.types.DummyFileSet; 15 import org.apache.tools.ant.types.EnumeratedAttribute; 16 import org.apache.tools.ant.types.FileSet; 17 import org.apache.tools.ant.types.selectors.FileSelector; 18 19 23 public class DummyFtpTask extends Task { 24 25 protected static final int SEND_FILES = 0; 26 27 protected static final int GET_FILES = 1; 28 29 protected static final int DEL_FILES = 2; 30 31 protected static final int LIST_FILES = 3; 32 33 protected static final int MK_DIR = 4; 34 35 protected static final int CHMOD = 5; 36 37 protected static final int RM_DIR = 6; 38 39 40 private static final int CODE_521 = 521; 41 42 43 public static final int DEFAULT_FTP_PORT = 21; 44 45 private String remotedir; 46 47 private String server; 48 49 private String userid; 50 51 private String password; 52 53 private File listing; 54 55 private boolean binary = true; 56 57 private boolean passive = false; 58 59 private boolean verbose = false; 60 61 private boolean newerOnly = false; 62 63 private boolean timeDiffAuto = false; 64 65 private long timeDiffMillis = 0; 66 67 private int action; 68 69 private Vector filesets = new Vector (); 70 71 private Vector dirCache = new Vector (); 72 73 private int transferred = 0; 74 75 private String remoteFileSep = "/"; 76 77 private int port = DEFAULT_FTP_PORT; 78 79 private boolean skipFailedTransfers = false; 80 81 private int skipped = 0; 82 83 private boolean ignoreNoncriticalErrors = false; 84 85 private boolean preserveLastModified = false; 86 87 private String chmod = null; 88 89 private String umask = null; 90 91 protected static final String [] ACTION_STRS = { "sending", "getting", 92 "deleting", "listing", "making directory", "chmod", "removing" }; 93 94 protected static final String [] COMPLETED_ACTION_STRS = { "sent", 95 "retrieved", "deleted", "listed", "created directory", 96 "mode changed", "removed" }; 97 98 protected static final String [] ACTION_TARGET_STRS = { "files", "files", 99 "files", "files", "directory", "files", "directories" }; 100 101 109 public void setRemotedir(String dir) { 110 this.remotedir = dir; 111 } 112 113 119 public void setServer(String server) { 120 this.server = server; 121 } 122 123 129 public void setPort(int port) { 130 this.port = port; 131 } 132 133 139 public void setUserid(String userid) { 140 this.userid = userid; 141 } 142 143 149 public void setPassword(String password) { 150 this.password = password; 151 } 152 153 159 public void setBinary(boolean binary) { 160 this.binary = binary; 161 } 162 163 171 public void setPassive(boolean passive) { 172 this.passive = passive; 173 } 174 175 181 public void setVerbose(boolean verbose) { 182 this.verbose = verbose; 183 } 184 185 194 public void setNewer(boolean newer) { 195 this.newerOnly = newer; 196 } 197 198 209 public void setTimeDiffMillis(long timeDiffMillis) { 210 this.timeDiffMillis = timeDiffMillis; 211 } 212 213 225 public void setTimeDiffAuto(boolean timeDiffAuto) { 226 this.timeDiffAuto = timeDiffAuto; 227 } 228 229 235 public void setPreserveLastModified(boolean preserveLastModified) { 236 this.preserveLastModified = preserveLastModified; 237 } 238 239 246 public void setDepends(boolean depends) { 247 this.newerOnly = depends; 248 } 249 250 259 public void setSeparator(String separator) { 260 remoteFileSep = separator; 261 } 262 263 269 public void setChmod(String theMode) { 270 this.chmod = theMode; 271 } 272 273 279 public void setUmask(String theUmask) { 280 this.umask = theUmask; 281 } 282 283 290 public void addFileset(DummyFileSet set) { 291 filesets.addElement(set); 292 } 293 294 310 public void setAction(String action) throws BuildException { 311 log("DEPRECATED - The setAction(String) method has been deprecated." 312 + " Use setAction(FTP.Action) instead."); 313 314 Action a = new Action(); 315 316 a.setValue(action); 317 this.action = a.getAction(); 318 } 319 320 330 public void setAction(Action action) throws BuildException { 331 this.action = action.getAction(); 332 } 333 334 341 public void setListing(File listing) { 342 this.listing = listing; 343 } 344 345 352 public void setSkipFailedTransfers(boolean skipFailedTransfers) { 353 this.skipFailedTransfers = skipFailedTransfers; 354 } 355 356 363 public void setIgnoreNoncriticalErrors(boolean ignoreNoncriticalErrors) { 364 this.ignoreNoncriticalErrors = ignoreNoncriticalErrors; 365 } 366 367 373 protected void checkConfiguration() throws BuildException { 374 if (server == null) { 375 throw new BuildException("server attribute must be set!"); 376 } 377 if (userid == null) { 378 throw new BuildException("userid attribute must be set!"); 379 } 380 if (password == null) { 381 throw new BuildException("password attribute must be set!"); 382 } 383 384 if ((action == LIST_FILES) && (listing == null)) { 385 throw new BuildException("listing attribute must be set for list " 386 + "action!"); 387 } 388 389 if (action == MK_DIR && remotedir == null) { 390 throw new BuildException("remotedir attribute must be set for " 391 + "mkdir action!"); 392 } 393 394 if (action == CHMOD && chmod == null) { 395 throw new BuildException("chmod attribute must be set for chmod " 396 + "action!"); 397 } 398 } 399 400 409 protected void transferFiles() 410 throws BuildException { 411 transferred = 0; 412 skipped = 0; 413 414 if (filesets.size() == 0) { 415 throw new BuildException("at least one fileset must be specified."); 416 } else { 417 for (int i = 0; i < filesets.size(); i++) { 419 DummyFileSet fs = (DummyFileSet) filesets.elementAt(i); 420 421 if (fs != null) { 422 log(fs.toString()); 423 } 424 } 425 } 426 427 if (Math.random() > 0.8) { 428 429 throw new BuildException("Pretending FTP Failed Exception"); 430 } 431 432 log(transferred + " " + ACTION_TARGET_STRS[action] + " " 433 + COMPLETED_ACTION_STRS[action]); 434 } 435 436 442 public void execute() throws BuildException { 443 checkConfiguration(); 444 445 try { 446 log("Opening FTP connection to " + server, Project.MSG_VERBOSE); 447 log("...pretending to connect."); 448 log("connected", Project.MSG_VERBOSE); 449 log("logging in to FTP server", Project.MSG_VERBOSE); 450 log("login succeeded", Project.MSG_VERBOSE); 451 452 if (binary) { 453 log("...pretending to change mode to binary"); 454 } else { 455 log("...pretending to change mode to ascii"); 456 } 457 458 if (passive) { 459 log("entering passive mode", Project.MSG_VERBOSE); 460 } 461 462 log("...pretending to do action [" 463 + ACTION_STRS[action] + "]"); 464 467 if (action == MK_DIR) { 468 log("...pretending to make remote dir [" + remotedir + "]."); 469 } else { 470 if (remotedir != null) { 471 log("changing the remote directory", Project.MSG_VERBOSE); 472 log("...pretending to change to remote dir [" + remotedir 473 + "]"); 474 } 475 log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]); 476 transferFiles(); 477 } 478 479 } finally { 480 log("disconnecting", Project.MSG_VERBOSE); 481 } 482 } 483 484 488 public static class Action extends EnumeratedAttribute { 489 490 private static final String [] VALID_ACTIONS = { "send", "put", "recv", 491 "get", "del", "delete", "list", "mkdir", "chmod", "rmdir" }; 492 493 498 public String [] getValues() { 499 return VALID_ACTIONS; 500 } 501 502 507 public int getAction() { 508 String actionL = getValue().toLowerCase(Locale.US); 509 510 if (actionL.equals("send") || actionL.equals("put")) { 511 return SEND_FILES; 512 } else if (actionL.equals("recv") || actionL.equals("get")) { 513 return GET_FILES; 514 } else if (actionL.equals("del") || actionL.equals("delete")) { 515 return DEL_FILES; 516 } else if (actionL.equals("list")) { 517 return LIST_FILES; 518 } else if (actionL.equals("chmod")) { 519 return CHMOD; 520 } else if (actionL.equals("mkdir")) { 521 return MK_DIR; 522 } else if (actionL.equals("rmdir")) { 523 return RM_DIR; 524 } 525 return SEND_FILES; 526 } 527 } 528 529 private void logFileSet(FileSet fs) { 530 StringBuffer text = new StringBuffer (); 531 text.append("FileSelectors:"); 532 Enumeration e = fs.selectorElements(); 533 while (e.hasMoreElements()) { 534 text.append("\n"); 535 FileSelector selector = (FileSelector) e.nextElement(); 536 text.append(selector.toString()); 537 } 538 log(text.toString()); 539 } 540 541 } | Popular Tags |