Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 482   Methods: 36
NCLOC: 210   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SOS.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.optional.sos;
 55   
 
 56   
 import java.io.File;
 57   
 import org.apache.tools.ant.BuildException;
 58   
 import org.apache.tools.ant.Project;
 59   
 import org.apache.tools.ant.Task;
 60   
 import org.apache.tools.ant.taskdefs.Execute;
 61   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 62   
 import org.apache.tools.ant.types.Commandline;
 63   
 import org.apache.tools.ant.types.Path;
 64   
 
 65   
 /**
 66   
  * A base class for creating tasks for executing commands on SourceOffSite.
 67   
  *
 68   
  *  These tasks were inspired by the VSS tasks
 69   
  *
 70   
  * @author Jesse Stockall
 71   
  */
 72   
 
 73   
 public abstract class SOS extends Task implements SOSCmd {
 74   
 
 75   
     private String sosCmdDir = null;
 76   
     private String sosUsername = null;
 77   
     private String sosPassword = "";
 78   
     private String projectPath = null;
 79   
     private String vssServerPath = null;
 80   
     private String sosServerPath = null;
 81   
     private String sosHome = null;
 82   
     private String localPath = null;
 83   
     private String version = null;
 84   
     private String label = null;
 85   
     private String comment = null;
 86   
     private String filename = null;
 87   
 
 88   
     private boolean noCompress = false;
 89   
     private boolean noCache = false;
 90   
     private boolean recursive = false;
 91   
     private boolean verbose = false;
 92   
 
 93   
     /** Commandline to be executed */
 94   
     protected Commandline commandLine;
 95   
 
 96   
     /**
 97   
      * Flag to disable the cache when set;
 98   
      * optional needed if SOSHOME is set as an environment variable., default false
 99   
      *
 100   
      * @param  nocache  True to disable caching.
 101   
      */
 102  0
     public final void setNoCache(boolean nocache) {
 103  0
         noCache = nocache;
 104   
     }
 105   
 
 106   
     /**
 107   
      * Flag that disables compression when set; optional, default false
 108   
      *
 109   
      * @param  nocompress  True to disable compression.
 110   
      */
 111  0
     public final void setNoCompress(boolean nocompress) {
 112  0
         noCompress = nocompress;
 113   
     }
 114   
 
 115   
     /**
 116   
      * Set the directory where soscmd(.exe) is located;
 117   
      * optional, soscmd must be on the path if omitted.
 118   
      *
 119   
      * @param  dir  The new sosCmd value
 120   
      */
 121  0
     public final void setSosCmd(String dir) {
 122  0
         sosCmdDir = Project.translatePath(dir);
 123   
     }
 124   
 
 125   
     /**
 126   
      * Set the SourceSafe username; required.
 127   
      *
 128   
      * @param  username  The new username value
 129   
      */
 130  0
     public final void setUsername(String username) {
 131  0
         sosUsername = username;
 132   
     }
 133   
 
 134   
     /**
 135   
      * Set the SourceSafe password; optional.
 136   
      *
 137   
      * @param  password  The new password value
 138   
      */
 139  0
     public final void setPassword(String password) {
 140  0
         sosPassword = password;
 141   
     }
 142   
 
 143   
     /**
 144   
      * Set the SourceSafe project path; required.
 145   
      *
 146   
      * @param  projectpath  The new projectpath value
 147   
      */
 148  0
     public final void setProjectPath(String projectpath) {
 149  0
         if (projectpath.startsWith(SOSCmd.PROJECT_PREFIX)) {
 150  0
             projectPath = projectpath;
 151   
         } else {
 152  0
             projectPath = SOSCmd.PROJECT_PREFIX + projectpath;
 153   
         }
 154   
     }
 155   
 
 156   
     /**
 157   
      * Set the path to the location of the ss.ini file;
 158   
      * required.
 159   
      *
 160   
      * @param  vssServerPath  The new vssServerPath value
 161   
      */
 162  0
     public final void setVssServerPath(String vssServerPath) {
 163  0
         this.vssServerPath = vssServerPath;
 164   
     }
 165   
 
 166   
     /**
 167   
      * The path to the SourceOffSite home directory
 168   
      *
 169   
      * @param  sosHome  The new sosHome value
 170   
      */
 171  0
     public final void setSosHome(String sosHome) {
 172  0
         this.sosHome = sosHome;
 173   
     }
 174   
 
 175   
     /**
 176   
      * Sets the address and port of SourceOffSite Server,
 177   
      * for example 192.168.0.1:8888.; required.
 178   
      *
 179   
      * @param  sosServerPath  The new sosServerPath value
 180   
      */
 181  0
     public final void setSosServerPath(String sosServerPath) {
 182  0
         this.sosServerPath = sosServerPath;
 183   
     }
 184   
 
 185   
     /**
 186   
      * Override the working directory and get to the specified path; optional.
 187   
      *
 188   
      * @param  path  The new localPath value
 189   
      */
 190  0
     public final void setLocalPath(Path path) {
 191  0
         localPath = path.toString();
 192   
     }
 193   
 
 194   
     /**
 195   
      * Enable verbose output; optional, default false
 196   
      *
 197   
      * @param  verbose  True for verbose output.
 198   
      */
 199  0
     public void setVerbose(boolean verbose) {
 200  0
         this.verbose = verbose;
 201   
     }
 202   
 
 203   
     // Special setters for the sub-classes
 204   
 
 205  0
     protected void setInternalFilename(String file) {
 206  0
         filename = file;
 207   
     }
 208   
 
 209  0
     protected void setInternalRecursive(boolean recurse) {
 210  0
         recursive = recurse;
 211   
     }
 212   
 
 213  0
     protected void setInternalComment(String text) {
 214  0
         comment = text;
 215   
     }
 216   
 
 217  0
     protected void setInternalLabel(String text) {
 218  0
         label = text;
 219   
     }
 220   
 
 221  0
     protected void setInternalVersion(String text) {
 222  0
         version = text;
 223   
     }
 224   
 
 225   
     /**
 226   
      * Get the executable to run. Add the path if it was specifed in the build file
 227   
      *
 228   
      * @return    String the executable to run
 229   
      */
 230  0
     protected String getSosCommand() {
 231  0
         if (sosCmdDir == null) {
 232  0
             return COMMAND_SOS_EXE;
 233   
         } else {
 234  0
             return sosCmdDir + File.separator + COMMAND_SOS_EXE;
 235   
         }
 236   
     }
 237   
 
 238   
     /**
 239   
      * Get the comment
 240   
      * @return String if it was set, null if not
 241   
      */
 242  0
     protected String getComment() {
 243  0
         return comment;
 244   
     }
 245   
 
 246   
     /**
 247   
      * Get the version
 248   
      * @return String if it was set, null if not
 249   
      */
 250  0
     protected String getVersion() {
 251  0
         return version;
 252   
     }
 253   
 
 254   
     /**
 255   
      * Get the label
 256   
      * @return String if it was set, null if not
 257   
      */
 258  0
     protected String getLabel() {
 259  0
         return label;
 260   
     }
 261   
 
 262   
     /**
 263   
      * Get the username
 264   
      * @return String if it was set, null if not
 265   
      */
 266  0
     protected String getUsername() {
 267  0
         return sosUsername;
 268   
     }
 269   
 
 270   
     /**
 271   
      * Get the password
 272   
      * @return String empty string if it wans't set
 273   
      */
 274  0
     protected String getPassword() {
 275  0
         return sosPassword;
 276   
     }
 277   
 
 278   
     /**
 279   
      * Get the project path
 280   
      * @return String if it was set, null if not
 281   
      */
 282  0
     protected String getProjectPath() {
 283  0
         return projectPath;
 284   
     }
 285   
 
 286   
     /**
 287   
      * Get the VSS server path
 288   
      * @return String if it was set, null if not
 289   
      */
 290  0
     protected String getVssServerPath() {
 291  0
         return vssServerPath;
 292   
     }
 293   
 
 294   
     /**
 295   
      * Get the SOS home directory
 296   
      * @return String if it was set, null if not
 297   
      */
 298  0
     protected String getSosHome() {
 299  0
         return sosHome;
 300   
     }
 301   
 
 302   
     /**
 303   
      * Get the SOS serve path
 304   
      * @return String if it was set, null if not
 305   
      */
 306  0
     protected String getSosServerPath() {
 307  0
         return sosServerPath;
 308   
     }
 309   
 
 310   
     /**
 311   
      * Get the filename to be acted upon
 312   
      * @return String if it was set, null if not
 313   
      */
 314  0
     protected String getFilename() {
 315  0
         return filename;
 316   
     }
 317   
 
 318   
     /**
 319   
      * Get the NoCompress flag
 320   
      *
 321   
      * @return    String the 'nocompress' Flag if the attribute was 'true', otherwise an empty string
 322   
      */
 323  0
     protected String getNoCompress() {
 324  0
         return noCompress ? FLAG_NO_COMPRESSION : "";
 325   
     }
 326   
 
 327   
     /**
 328   
      * Get the NoCache flag
 329   
      *
 330   
      * @return    String the 'nocache' Flag if the attribute was 'true', otherwise an empty string
 331   
      */
 332  0
     protected String getNoCache() {
 333  0
         return noCache ? FLAG_NO_CACHE : "";
 334   
     }
 335   
 
 336   
     /**
 337   
      * Get the 'verbose' Flag
 338   
      *
 339   
      * @return    String the 'verbose' Flag if the attribute was 'true', otherwise an empty string
 340   
      */
 341  0
     protected String getVerbose() {
 342  0
         return verbose ? FLAG_VERBOSE : "";
 343   
     }
 344   
 
 345   
     /**
 346   
      * Get the 'recursive' Flag
 347   
      *
 348   
      * @return    String the 'recursive' Flag if the attribute was 'true', otherwise an empty string
 349   
      */
 350  0
     protected String getRecursive() {
 351  0
         return recursive ? FLAG_RECURSION : "";
 352   
     }
 353   
 
 354   
     /**
 355   
      * Builds and returns the working directory.
 356   
      * <p>
 357   
      * The localpath is created if it didn't exist
 358   
      *
 359   
      * @return    String the absolute path of the working directory
 360   
      */
 361  0
     protected String getLocalPath() {
 362  0
         if (localPath == null) {
 363  0
             return getProject().getBaseDir().getAbsolutePath();
 364   
         } else {
 365   
             // make sure localDir exists, create it if it doesn't
 366  0
             File dir = getProject().resolveFile(localPath);
 367  0
             if (!dir.exists()) {
 368  0
                 boolean done = dir.mkdirs();
 369  0
                 if (!done) {
 370  0
                     String msg = "Directory " + localPath + " creation was not " +
 371   
                             "successful for an unknown reason";
 372  0
                     throw new BuildException(msg, getLocation());
 373   
                 }
 374  0
                 getProject().log("Created dir: " + dir.getAbsolutePath());
 375   
             }
 376  0
             return dir.getAbsolutePath();
 377   
         }
 378   
     }
 379   
 
 380   
     /**
 381   
      * Subclasses implement the logic required to construct the command line.
 382   
      *
 383   
      * @return   The command line to execute.
 384   
      */
 385   
     abstract Commandline buildCmdLine();
 386   
 
 387   
 
 388   
     /**
 389   
      * Execute the created command line.
 390   
      *
 391   
      * @throws BuildException
 392   
      */
 393  0
     public void execute()
 394   
         throws BuildException {
 395  0
         int result = 0;
 396  0
         buildCmdLine();
 397  0
         result = run(commandLine);
 398  0
         if (result == 255) {  // This is the exit status
 399  0
             String msg = "Failed executing: " + commandLine.toString();
 400  0
             throw new BuildException(msg, getLocation());
 401   
         }
 402   
     }
 403   
 
 404   
     /**
 405   
      * Execute the created command line.
 406   
      *
 407   
      * @param  cmd              The command line to run.
 408   
      * @return                  int the exit code.
 409   
      * @throws  BuildException
 410   
      */
 411  0
     protected int run(Commandline cmd) {
 412  0
         try {
 413  0
             Execute exe = new Execute(new LogStreamHandler(this,
 414   
                     Project.MSG_INFO,
 415   
                     Project.MSG_WARN));
 416   
 
 417  0
             exe.setAntRun(getProject());
 418  0
             exe.setWorkingDirectory(getProject().getBaseDir());
 419  0
             exe.setCommandline(cmd.getCommandline());
 420  0
             exe.setVMLauncher(false);  // Use the OS VM launcher so we get environment variables
 421  0
             return exe.execute();
 422   
         } catch (java.io.IOException e) {
 423  0
             throw new BuildException(e, getLocation());
 424   
         }
 425   
     }
 426   
 
 427   
     /** Sets the executable and add the required attributes to the command line. */
 428  0
     protected void getRequiredAttributes() {
 429   
         // Get the path to the soscmd(.exe)
 430  0
         commandLine.setExecutable(getSosCommand());
 431   
         // SOS server address is required
 432  0
         if (getSosServerPath() == null) {
 433  0
             throw new BuildException("sosserverpath attribute must be set!", getLocation());
 434   
         }
 435  0
         commandLine.createArgument().setValue(FLAG_SOS_SERVER);
 436  0
         commandLine.createArgument().setValue(getSosServerPath());
 437   
         // Login info is required
 438  0
         if (getUsername() == null) {
 439  0
             throw new BuildException("username attribute must be set!", getLocation());
 440   
         }
 441  0
         commandLine.createArgument().setValue(FLAG_USERNAME);
 442  0
         commandLine.createArgument().setValue(getUsername());
 443   
         // The SOS class knows that the SOS server needs the password flag,
 444   
         // even if there is no password ,so we send a " "
 445  0
         commandLine.createArgument().setValue(FLAG_PASSWORD);
 446  0
         commandLine.createArgument().setValue(getPassword());
 447   
         // VSS Info is required
 448  0
         if (getVssServerPath() == null) {
 449  0
             throw new BuildException("vssserverpath attribute must be set!", getLocation());
 450   
         }
 451  0
         commandLine.createArgument().setValue(FLAG_VSS_SERVER);
 452  0
         commandLine.createArgument().setValue(getVssServerPath());
 453   
         // VSS project is required
 454  0
         if (getProjectPath() == null) {
 455  0
             throw new BuildException("projectpath attribute must be set!", getLocation());
 456   
         }
 457  0
         commandLine.createArgument().setValue(FLAG_PROJECT);
 458  0
         commandLine.createArgument().setValue(getProjectPath());
 459   
     }
 460   
 
 461   
     /** Adds the optional attributes to the command line. */
 462  0
     protected void getOptionalAttributes() {
 463   
         // -verbose
 464  0
         commandLine.createArgument().setValue(getVerbose());
 465   
         // Disable Compression
 466  0
         commandLine.createArgument().setValue(getNoCompress());
 467   
         // Path to the SourceOffSite home directory /home/user/.sos
 468  0
         if (getSosHome() == null) {
 469   
             // If -soshome was not specified then we can look for nocache
 470  0
             commandLine.createArgument().setValue(getNoCache());
 471   
         } else {
 472  0
             commandLine.createArgument().setValue(FLAG_SOS_HOME);
 473  0
             commandLine.createArgument().setValue(getSosHome());
 474   
         }
 475   
         //If a working directory was specified then add it to the command line
 476  0
         if (getLocalPath() != null) {
 477  0
             commandLine.createArgument().setValue(FLAG_WORKING_DIR);
 478  0
             commandLine.createArgument().setValue(getLocalPath());
 479   
         }
 480   
     }
 481   
 }
 482