KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > clearcase > CCUnlock


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  * TODO:
28  * comment field doesn't include all options yet
29  */

30
31 /**
32  * Performs a ClearCase Unlock command.
33  *
34  * <p>
35  * The following attributes are interpreted:
36  * <table border="1">
37  * <tr>
38  * <th>Attribute</th>
39  * <th>Values</th>
40  * <th>Required</th>
41  * </tr>
42  * <tr>
43  * <td>comment</td>
44  * <td>Specifies how to populate comments fields</td>
45  * <td>No</td>
46  * <tr>
47  * <tr>
48  * <td>pname</td>
49  * <td>Specifies the object pathname to be unlocked.</td>
50  * <td>No</td>
51  * <tr>
52  * <td>objselect</td>
53  * <td>This variable is obsolete. Should use <i>objsel</i> instead.</td>
54  * <td>no</td>
55  * <tr>
56  * <tr>
57  * <td>objsel</td>
58  * <td>Specifies the object(s) to be unlocked.</td>
59  * <td>No</td>
60  * <tr>
61  * <tr>
62  * <td>failonerr</td>
63  * <td>Throw an exception if the command fails. Default is true</td>
64  * <td>No</td>
65  * <tr>
66  *
67  * </table>
68  *
69  */

70 public class CCUnlock extends ClearCase {
71     private String JavaDoc mComment = null;
72     private String JavaDoc mPname = null;
73
74     /**
75      * Executes the task.
76      * <p>
77      * Builds a command line to execute cleartool and then calls Exec's run method
78      * to execute the command line.
79      * @throws BuildException if the command fails and failonerr is set to true
80      */

81     public void execute() throws BuildException {
82         Commandline commandLine = new Commandline();
83         Project aProj = getProject();
84         int result = 0;
85
86         // Default the viewpath to basedir if it is not specified
87
if (getViewPath() == null) {
88             setViewPath(aProj.getBaseDir().getPath());
89         }
90
91         // build the command line from what we got the format is
92
// cleartool lock [options...]
93
// as specified in the CLEARTOOL.EXE help
94
commandLine.setExecutable(getClearToolCommand());
95         commandLine.createArgument().setValue(COMMAND_UNLOCK);
96
97         // Check the command line options
98
checkOptions(commandLine);
99
100         // For debugging
101
// System.out.println(commandLine.toString());
102

103         if (!getFailOnErr()) {
104             getProject().log("Ignoring any errors that occur for: "
105                     + getOpType(), Project.MSG_VERBOSE);
106         }
107         result = run(commandLine);
108         if (Execute.isFailure(result) && getFailOnErr()) {
109             String JavaDoc msg = "Failed executing: " + commandLine.toString();
110             throw new BuildException(msg, getLocation());
111         }
112     }
113
114     /**
115      * Check the command line options.
116      */

117     private void checkOptions(Commandline cmd) {
118         // ClearCase items
119
getCommentCommand(cmd);
120
121         if (getObjSelect() == null && getPname() == null) {
122             throw new BuildException("Should select either an element "
123             + "(pname) or an object (objselect)");
124         }
125         getPnameCommand(cmd);
126         // object selector
127
if (getObjSelect() != null) {
128             cmd.createArgument().setValue(getObjSelect());
129         }
130     }
131
132     /**
133      * Sets how comments should be written
134      * for the event record(s)
135      *
136      * @param comment comment method to use
137      */

138     public void setComment(String JavaDoc comment) {
139         mComment = comment;
140     }
141
142     /**
143      * Get comment method
144      *
145      * @return String containing the desired comment method
146      */

147     public String JavaDoc getComment() {
148         return mComment;
149     }
150
151     /**
152      * Sets the pathname to be locked
153      *
154      * @param pname pathname to be locked
155      */

156     public void setPname(String JavaDoc pname) {
157         mPname = pname;
158     }
159
160     /**
161      * Get the pathname to be locked
162      *
163      * @return String containing the pathname to be locked
164      */

165     public String JavaDoc getPname() {
166         return mPname;
167     }
168
169     /**
170      * Sets the object(s) to be locked
171      *
172      * @param objselect objects to be locked
173      */

174     public void setObjselect(String JavaDoc objselect) {
175         setObjSelect(objselect);
176     }
177
178     /**
179      * Sets the object(s) to be locked
180      *
181      * @param objsel objects to be locked
182      * @since ant 1.6.1
183      */

184     public void setObjSel(String JavaDoc objsel) {
185         setObjSelect(objsel);
186     }
187
188     /**
189      * Get list of objects to be locked
190      *
191      * @return String containing the objects to be locked
192      */

193     public String JavaDoc getObjselect() {
194         return getObjSelect();
195     }
196
197     /**
198      * Get the 'comment' command
199      *
200      * @param cmd containing the command line string with or without the
201      * comment flag and value appended
202      */

203     private void getCommentCommand(Commandline cmd) {
204         if (getComment() == null) {
205             return;
206         } else {
207             /* Had to make two separate commands here because if a space is
208                inserted between the flag and the value, it is treated as a
209                Windows filename with a space and it is enclosed in double
210                quotes ("). This breaks clearcase.
211             */

212             cmd.createArgument().setValue(FLAG_COMMENT);
213             cmd.createArgument().setValue(getComment());
214         }
215     }
216
217     /**
218      * Get the 'pname' command
219      *
220      * @param cmd containing the command line string with or without the
221      * pname flag and value appended
222      */

223     private void getPnameCommand(Commandline cmd) {
224         if (getPname() == null) {
225             return;
226         } else {
227             /* Had to make two separate commands here because if a space is
228                inserted between the flag and the value, it is treated as a
229                Windows filename with a space and it is enclosed in double
230                quotes ("). This breaks clearcase.
231             */

232             cmd.createArgument().setValue(FLAG_PNAME);
233             cmd.createArgument().setValue(getPname());
234         }
235     }
236
237     /**
238      * Return which object/pname is being operated on
239      *
240      * @return String containing the object/pname being worked on
241      */

242     private String JavaDoc getOpType() {
243
244         if (getPname() != null) {
245             return getPname();
246         } else {
247             return getObjSelect();
248         }
249     }
250
251     /**
252      * -comment flag -- method to use for commenting events
253      */

254     public static final String JavaDoc FLAG_COMMENT = "-comment";
255     /**
256      * -pname flag -- pathname to lock
257      */

258     public static final String JavaDoc FLAG_PNAME = "-pname";
259 }
260
261
Popular Tags