1 18 package org.apache.tools.ant.taskdefs.optional.sos; 19 20 import java.io.File ; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.taskdefs.Execute; 25 import org.apache.tools.ant.taskdefs.LogStreamHandler; 26 import org.apache.tools.ant.types.Commandline; 27 import org.apache.tools.ant.types.Path; 28 import org.apache.tools.ant.util.FileUtils; 29 30 36 37 public abstract class SOS extends Task implements SOSCmd { 38 39 private String sosCmdDir = null; 40 private String sosUsername = null; 41 private String sosPassword = ""; 42 private String projectPath = null; 43 private String vssServerPath = null; 44 private String sosServerPath = null; 45 private String sosHome = null; 46 private String localPath = null; 47 private String version = null; 48 private String label = null; 49 private String comment = null; 50 private String filename = null; 51 52 private boolean noCompress = false; 53 private boolean noCache = false; 54 private boolean recursive = false; 55 private boolean verbose = false; 56 57 59 protected Commandline commandLine; 60 62 69 public final void setNoCache(boolean nocache) { 70 noCache = nocache; 71 } 72 73 78 public final void setNoCompress(boolean nocompress) { 79 noCompress = nocompress; 80 } 81 82 88 public final void setSosCmd(String dir) { 89 sosCmdDir = FileUtils.translatePath(dir); 90 } 91 92 99 public final void setUsername(String username) { 100 sosUsername = username; 101 } 102 103 108 public final void setPassword(String password) { 109 sosPassword = password; 110 } 111 112 119 public final void setProjectPath(String projectpath) { 120 if (projectpath.startsWith(SOSCmd.PROJECT_PREFIX)) { 121 projectPath = projectpath; 122 } else { 123 projectPath = SOSCmd.PROJECT_PREFIX + projectpath; 124 } 125 } 126 127 134 public final void setVssServerPath(String vssServerPath) { 135 this.vssServerPath = vssServerPath; 136 } 137 138 143 public final void setSosHome(String sosHome) { 144 this.sosHome = sosHome; 145 } 146 147 155 public final void setSosServerPath(String sosServerPath) { 156 this.sosServerPath = sosServerPath; 157 } 158 159 164 public final void setLocalPath(Path path) { 165 localPath = path.toString(); 166 } 167 168 173 public void setVerbose(boolean verbose) { 174 this.verbose = verbose; 175 } 176 177 179 183 protected void setInternalFilename(String file) { 184 filename = file; 185 } 186 187 191 protected void setInternalRecursive(boolean recurse) { 192 recursive = recurse; 193 } 194 195 199 protected void setInternalComment(String text) { 200 comment = text; 201 } 202 203 207 protected void setInternalLabel(String text) { 208 label = text; 209 } 210 211 215 protected void setInternalVersion(String text) { 216 version = text; 217 } 218 219 224 protected String getSosCommand() { 225 if (sosCmdDir == null) { 226 return COMMAND_SOS_EXE; 227 } else { 228 return sosCmdDir + File.separator + COMMAND_SOS_EXE; 229 } 230 } 231 232 236 protected String getComment() { 237 return comment; 238 } 239 240 244 protected String getVersion() { 245 return version; 246 } 247 248 252 protected String getLabel() { 253 return label; 254 } 255 256 260 protected String getUsername() { 261 return sosUsername; 262 } 263 264 268 protected String getPassword() { 269 return sosPassword; 270 } 271 272 276 protected String getProjectPath() { 277 return projectPath; 278 } 279 280 284 protected String getVssServerPath() { 285 return vssServerPath; 286 } 287 288 292 protected String getSosHome() { 293 return sosHome; 294 } 295 296 300 protected String getSosServerPath() { 301 return sosServerPath; 302 } 303 304 308 protected String getFilename() { 309 return filename; 310 } 311 312 318 protected String getNoCompress() { 319 return noCompress ? FLAG_NO_COMPRESSION : ""; 320 } 321 322 327 protected String getNoCache() { 328 return noCache ? FLAG_NO_CACHE : ""; 329 } 330 331 336 protected String getVerbose() { 337 return verbose ? FLAG_VERBOSE : ""; 338 } 339 340 345 protected String getRecursive() { 346 return recursive ? FLAG_RECURSION : ""; 347 } 348 349 356 protected String getLocalPath() { 357 if (localPath == null) { 358 return getProject().getBaseDir().getAbsolutePath(); 359 } else { 360 File dir = getProject().resolveFile(localPath); 362 if (!dir.exists()) { 363 boolean done = dir.mkdirs(); 364 if (!done) { 365 String msg = "Directory " + localPath + " creation was not " 366 + "successful for an unknown reason"; 367 throw new BuildException(msg, getLocation()); 368 } 369 getProject().log("Created dir: " + dir.getAbsolutePath()); 370 } 371 return dir.getAbsolutePath(); 372 } 373 } 374 375 380 abstract Commandline buildCmdLine(); 381 382 383 388 public void execute() 389 throws BuildException { 390 int result = 0; 391 buildCmdLine(); 392 result = run(commandLine); 393 if (result == 255) { String msg = "Failed executing: " + commandLine.toString(); 395 throw new BuildException(msg, getLocation()); 396 } 397 } 398 399 406 protected int run(Commandline cmd) { 407 try { 408 Execute exe = new Execute(new LogStreamHandler(this, 409 Project.MSG_INFO, 410 Project.MSG_WARN)); 411 412 exe.setAntRun(getProject()); 413 exe.setWorkingDirectory(getProject().getBaseDir()); 414 exe.setCommandline(cmd.getCommandline()); 415 exe.setVMLauncher(false); return exe.execute(); 417 } catch (java.io.IOException e) { 418 throw new BuildException(e, getLocation()); 419 } 420 } 421 422 423 protected void getRequiredAttributes() { 424 commandLine.setExecutable(getSosCommand()); 426 if (getSosServerPath() == null) { 428 throw new BuildException("sosserverpath attribute must be set!", getLocation()); 429 } 430 commandLine.createArgument().setValue(FLAG_SOS_SERVER); 431 commandLine.createArgument().setValue(getSosServerPath()); 432 if (getUsername() == null) { 434 throw new BuildException("username attribute must be set!", getLocation()); 435 } 436 commandLine.createArgument().setValue(FLAG_USERNAME); 437 commandLine.createArgument().setValue(getUsername()); 438 commandLine.createArgument().setValue(FLAG_PASSWORD); 441 commandLine.createArgument().setValue(getPassword()); 442 if (getVssServerPath() == null) { 444 throw new BuildException("vssserverpath attribute must be set!", getLocation()); 445 } 446 commandLine.createArgument().setValue(FLAG_VSS_SERVER); 447 commandLine.createArgument().setValue(getVssServerPath()); 448 if (getProjectPath() == null) { 450 throw new BuildException("projectpath attribute must be set!", getLocation()); 451 } 452 commandLine.createArgument().setValue(FLAG_PROJECT); 453 commandLine.createArgument().setValue(getProjectPath()); 454 } 455 456 457 protected void getOptionalAttributes() { 458 commandLine.createArgument().setValue(getVerbose()); 460 commandLine.createArgument().setValue(getNoCompress()); 462 if (getSosHome() == null) { 464 commandLine.createArgument().setValue(getNoCache()); 466 } else { 467 commandLine.createArgument().setValue(FLAG_SOS_HOME); 468 commandLine.createArgument().setValue(getSosHome()); 469 } 470 if (getLocalPath() != null) { 472 commandLine.createArgument().setValue(FLAG_WORKING_DIR); 473 commandLine.createArgument().setValue(getLocalPath()); 474 } 475 } 476 } 477 | Popular Tags |