Home » apache-ant-1.7.1-src » org.apache.tools » ant » taskdefs » optional » clearcase » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.tools.ant.taskdefs.optional.clearcase;
   20   
   21   import org.apache.tools.ant.BuildException;
   22   import org.apache.tools.ant.Project;
   23   import org.apache.tools.ant.taskdefs.Execute;
   24   import org.apache.tools.ant.types.Commandline;
   25   
   26   /**
   27    * Task to perform mklbtype command to ClearCase.
   28    * <p>
   29    * The following attributes are interpreted:
   30    * <table border="1">
   31    *   <tr>
   32    *     <th>Attribute</th>
   33    *     <th>Values</th>
   34    *     <th>Required</th>
   35    *   </tr>
   36    *   <tr>
   37    *      <td>typename</td>
   38    *      <td>Name of the label type to create</td>
   39    *      <td>Yes</td>
   40    *   <tr>
   41    *   <tr>
   42    *      <td>vob</td>
   43    *      <td>Name of the VOB</td>
   44    *      <td>No</td>
   45    *   <tr>
   46    *   <tr>
   47    *      <td>replace</td>
   48    *      <td>Replace an existing label definition of the same type</td>
   49    *      <td>No</td>
   50    *   <tr>
   51    *   <tr>
   52    *      <td>global</td>
   53    *      <td>Either global or ordinary can be specified, not both.
   54    *          Creates a label type that is global to the VOB or to
   55    *          VOBs that use this VOB</td>
   56    *      <td>No</td>
   57    *   <tr>
   58    *   <tr>
   59    *      <td>ordinary</td>
   60    *      <td>Either global or ordinary can be specified, not both.
   61    *          Creates a label type that can be used only in the current
   62    *          VOB. <B>Default</B></td>
   63    *      <td>No</td>
   64    *   <tr>
   65    *   <tr>
   66    *      <td>pbranch</td>
   67    *      <td>Allows the label type to be used once per branch in a given
   68    *          element's version tree</td>
   69    *      <td>No</td>
   70    *   <tr>
   71    *   <tr>
   72    *      <td>shared</td>
   73    *      <td>Sets the way mastership is checked by ClearCase. See ClearCase
   74    *          documentation for details</td>
   75    *      <td>No</td>
   76    *   <tr>
   77    *   <tr>
   78    *      <td>comment</td>
   79    *      <td>Specify a comment. Only one of comment or cfile may be used.</td>
   80    *      <td>No</td>
   81    *   <tr>
   82    *   <tr>
   83    *      <td>commentfile</td>
   84    *      <td>Specify a file containing a comment. Only one of comment or
   85    *          cfile may be used.</td>
   86    *      <td>No</td>
   87    *   <tr>
   88    *   <tr>
   89    *      <td>failonerr</td>
   90    *      <td>Throw an exception if the command fails. Default is true</td>
   91    *      <td>No</td>
   92    *   <tr>
   93    * </table>
   94    *
   95    */
   96   public class CCMklbtype extends ClearCase {
   97       private String mTypeName = null;
   98       private String mVOB = null;
   99       private String mComment = null;
  100       private String mCfile = null;
  101       private boolean mReplace = false;
  102       private boolean mGlobal = false;
  103       private boolean mOrdinary = true;
  104       private boolean mPbranch = false;
  105       private boolean mShared = false;
  106   
  107       /**
  108        * Executes the task.
  109        * <p>
  110        * Builds a command line to execute cleartool and then calls Exec's run method
  111        * to execute the command line.
  112        * @throws BuildException if the command fails and failonerr is set to true
  113        */
  114       public void execute() throws BuildException {
  115           Commandline commandLine = new Commandline();
  116           int result = 0;
  117   
  118           // Check for required attributes
  119           if (getTypeName() == null) {
  120               throw new BuildException("Required attribute TypeName not specified");
  121           }
  122   
  123           // build the command line from what we got. the format is
  124           // cleartool mklbtype [options...] type-selector...
  125           // as specified in the CLEARTOOL help
  126           commandLine.setExecutable(getClearToolCommand());
  127           commandLine.createArgument().setValue(COMMAND_MKLBTYPE);
  128   
  129           checkOptions(commandLine);
  130   
  131           if (!getFailOnErr()) {
  132               getProject().log("Ignoring any errors that occur for: "
  133                       + getTypeSpecifier(), Project.MSG_VERBOSE);
  134           }
  135           result = run(commandLine);
  136           if (Execute.isFailure(result) && getFailOnErr()) {
  137               String msg = "Failed executing: " + commandLine.toString();
  138               throw new BuildException(msg, getLocation());
  139           }
  140       }
  141   
  142   
  143       /**
  144        * Check the command line options.
  145        */
  146       private void checkOptions(Commandline cmd) {
  147           if (getReplace()) {
  148               // -replace
  149               cmd.createArgument().setValue(FLAG_REPLACE);
  150           }
  151   
  152           if (getOrdinary()) {
  153               // -ordinary
  154               cmd.createArgument().setValue(FLAG_ORDINARY);
  155           } else {
  156               if (getGlobal()) {
  157                   // -global
  158                   cmd.createArgument().setValue(FLAG_GLOBAL);
  159               }
  160           }
  161   
  162           if (getPbranch()) {
  163               // -pbranch
  164               cmd.createArgument().setValue(FLAG_PBRANCH);
  165           }
  166   
  167           if (getShared()) {
  168               // -shared
  169               cmd.createArgument().setValue(FLAG_SHARED);
  170           }
  171   
  172           if (getComment() != null) {
  173               // -c
  174               getCommentCommand(cmd);
  175           } else {
  176               if (getCommentFile() != null) {
  177                   // -cfile
  178                   getCommentFileCommand(cmd);
  179               } else {
  180                   cmd.createArgument().setValue(FLAG_NOCOMMENT);
  181               }
  182           }
  183   
  184           // type-name@vob
  185           cmd.createArgument().setValue(getTypeSpecifier());
  186       }
  187   
  188   
  189       /**
  190        * Set type-name string
  191        *
  192        * @param tn the type-name string
  193        */
  194       public void setTypeName(String tn) {
  195           mTypeName = tn;
  196       }
  197   
  198       /**
  199        * Get type-name string
  200        *
  201        * @return String containing the type-name
  202        */
  203       public String getTypeName() {
  204           return mTypeName;
  205       }
  206   
  207       /**
  208        * Set the VOB name
  209        *
  210        * @param vob the VOB name
  211        */
  212       public void setVOB(String vob) {
  213           mVOB = vob;
  214       }
  215   
  216       /**
  217        * Get VOB name
  218        *
  219        * @return String containing VOB name
  220        */
  221       public String getVOB() {
  222           return mVOB;
  223       }
  224   
  225       /**
  226        * Set the replace flag
  227        *
  228        * @param repl the status to set the flag to
  229        */
  230       public void setReplace(boolean repl) {
  231           mReplace = repl;
  232       }
  233   
  234       /**
  235        * Get replace flag status
  236        *
  237        * @return boolean containing status of replace flag
  238        */
  239       public boolean getReplace() {
  240           return mReplace;
  241       }
  242   
  243       /**
  244        * Set the global flag
  245        *
  246        * @param glob the status to set the flag to
  247        */
  248       public void setGlobal(boolean glob) {
  249           mGlobal = glob;
  250       }
  251   
  252       /**
  253        * Get global flag status
  254        *
  255        * @return boolean containing status of global flag
  256        */
  257       public boolean getGlobal() {
  258           return mGlobal;
  259       }
  260   
  261       /**
  262        * Set the ordinary flag
  263        *
  264        * @param ordinary the status to set the flag to
  265        */
  266       public void setOrdinary(boolean ordinary) {
  267           mOrdinary = ordinary;
  268       }
  269   
  270       /**
  271        * Get ordinary flag status
  272        *
  273        * @return boolean containing status of ordinary flag
  274        */
  275       public boolean getOrdinary() {
  276           return mOrdinary;
  277       }
  278   
  279       /**
  280        * Set the pbranch flag
  281        *
  282        * @param pbranch the status to set the flag to
  283        */
  284       public void setPbranch(boolean pbranch) {
  285           mPbranch = pbranch;
  286       }
  287   
  288       /**
  289        * Get pbranch flag status
  290        *
  291        * @return boolean containing status of pbranch flag
  292        */
  293       public boolean getPbranch() {
  294           return mPbranch;
  295       }
  296   
  297       /**
  298        * Set the shared flag
  299        *
  300        * @param shared the status to set the flag to
  301        */
  302       public void setShared(boolean shared) {
  303           mShared = shared;
  304       }
  305   
  306       /**
  307        * Get shared flag status
  308        *
  309        * @return boolean containing status of shared flag
  310        */
  311       public boolean getShared() {
  312           return mShared;
  313       }
  314   
  315       /**
  316        * Set comment string
  317        *
  318        * @param comment the comment string
  319        */
  320       public void setComment(String comment) {
  321           mComment = comment;
  322       }
  323   
  324       /**
  325        * Get comment string
  326        *
  327        * @return String containing the comment
  328        */
  329       public String getComment() {
  330           return mComment;
  331       }
  332   
  333       /**
  334        * Set comment file
  335        *
  336        * @param cfile the path to the comment file
  337        */
  338       public void setCommentFile(String cfile) {
  339           mCfile = cfile;
  340       }
  341   
  342       /**
  343        * Get comment file
  344        *
  345        * @return String containing the path to the comment file
  346        */
  347       public String getCommentFile() {
  348           return mCfile;
  349       }
  350   
  351   
  352       /**
  353        * Get the 'comment' command
  354        *
  355        * @param cmd containing the command line string with or
  356        *        without the comment flag and string appended
  357        */
  358       private void getCommentCommand(Commandline cmd) {
  359           if (getComment() != null) {
  360               /* Had to make two separate commands here because if a space is
  361                  inserted between the flag and the value, it is treated as a
  362                  Windows filename with a space and it is enclosed in double
  363                  quotes ("). This breaks clearcase.
  364               */
  365               cmd.createArgument().setValue(FLAG_COMMENT);
  366               cmd.createArgument().setValue(getComment());
  367           }
  368       }
  369   
  370       /**
  371        * Get the 'commentfile' command
  372        *
  373        * @param cmd containing the command line string with or
  374        *        without the commentfile flag and file appended
  375        */
  376       private void getCommentFileCommand(Commandline cmd) {
  377           if (getCommentFile() != null) {
  378               /* Had to make two separate commands here because if a space is
  379                  inserted between the flag and the value, it is treated as a
  380                  Windows filename with a space and it is enclosed in double
  381                  quotes ("). This breaks clearcase.
  382               */
  383               cmd.createArgument().setValue(FLAG_COMMENTFILE);
  384               cmd.createArgument().setValue(getCommentFile());
  385           }
  386       }
  387   
  388       /**
  389        * Get the type-name specifier
  390        *
  391        * @return the 'type-name-specifier' command if the attribute was
  392        *         specified, otherwise an empty string
  393        */
  394       private String getTypeSpecifier() {
  395           String typenm = null;
  396   
  397           typenm = getTypeName();
  398           if (getVOB() != null) {
  399               typenm += "@" + getVOB();
  400           }
  401   
  402           return typenm;
  403       }
  404   
  405   
  406       /**
  407        * -replace flag -- replace existing label definition of the same type
  408        */
  409       public static final String FLAG_REPLACE = "-replace";
  410       /**
  411        * -global flag -- creates a label type that is global to the VOB or to VOBs that use this VOB
  412        */
  413       public static final String FLAG_GLOBAL = "-global";
  414       /**
  415        * -ordinary flag -- creates a label type that can be used only in the current VOB
  416        */
  417       public static final String FLAG_ORDINARY = "-ordinary";
  418       /**
  419        * -pbranch flag -- allows label type to be used once per branch
  420        */
  421       public static final String FLAG_PBRANCH = "-pbranch";
  422       /**
  423        * -shared flag -- sets the way mastership is checked by ClearCase
  424        */
  425       public static final String FLAG_SHARED = "-shared";
  426       /**
  427        * -c flag -- comment to attach to the file
  428        */
  429       public static final String FLAG_COMMENT = "-c";
  430       /**
  431        * -cfile flag -- file containing a comment to attach to the file
  432        */
  433       public static final String FLAG_COMMENTFILE = "-cfile";
  434       /**
  435        * -nc flag -- no comment is specified
  436        */
  437       public static final String FLAG_NOCOMMENT = "-nc";
  438   
  439   }
  440   

Save This Page
Home » apache-ant-1.7.1-src » org.apache.tools » ant » taskdefs » optional » clearcase » [javadoc | source]