KickJava   Java API By Example, From Geeks To Geeks.

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


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

31
32
33
34 /**
35  * Performs a ClearCase Lock command.
36  *
37  * <p>
38  * The following attributes are interpreted:
39  * <table border="1">
40  * <tr>
41  * <th>Attribute</th>
42  * <th>Values</th>
43  * <th>Required</th>
44  * </tr>
45  * <tr>
46  * <td>replace</td>
47  * <td>Specifies replacing an existing lock</td>
48  * <td>No</td>
49  * <tr>
50  * <tr>
51  * <td>nusers</td>
52  * <td>Specifies user(s) who can still modify the object/pname</td>
53  * <td>No</td>
54  * <tr>
55  * <tr>
56  * <td>obsolete</td>
57  * <td>Specifies that the object/pname should be marked obsolete</td>
58  * <td>No</td>
59  * <tr>
60  * <tr>
61  * <td>comment</td>
62  * <td>Specifies how to populate comments fields</td>
63  * <td>No</td>
64  * <tr>
65  * <tr>
66  * <td>pname</td>
67  * <td>Specifies the pathname to be locked.</td>
68  * <td>No</td>
69  * <tr>
70  * <td>objselect</td>
71  * <td>This variable is obsolete. Should use <i>objsel</i> instead.</td>
72  * <td>No</td>
73  * <tr>
74  * <tr>
75  * <td>objsel</td>
76  * <td>Specifies the object(s) to be unlocked.</td>
77  * <td>No</td>
78  * <tr>
79  * <tr>
80  * <td>failonerr</td>
81  * <td>Throw an exception if the command fails. Default is true</td>
82  * <td>No</td>
83  * <tr>
84  * </table>
85  *
86  */

87 public class CCLock extends ClearCase {
88     private boolean mReplace = false;
89     private boolean mObsolete = false;
90     private String JavaDoc mComment = null;
91     private String JavaDoc mNusers = null;
92     private String JavaDoc mPname = null;
93     private String JavaDoc mObjselect = null;
94
95     /**
96      * Executes the task.
97      * <p>
98      * Builds a command line to execute cleartool and then calls Exec's run method
99      * to execute the command line.
100      * @throws BuildException if the command fails and failonerr is set to true
101      */

102     public void execute() throws BuildException {
103         Commandline commandLine = new Commandline();
104         Project aProj = getProject();
105         int result = 0;
106
107         // Default the viewpath to basedir if it is not specified
108
if (getViewPath() == null) {
109             setViewPath(aProj.getBaseDir().getPath());
110         }
111
112         // build the command line from what we got the format is
113
// cleartool lock [options...]
114
// as specified in the CLEARTOOL.EXE help
115
commandLine.setExecutable(getClearToolCommand());
116         commandLine.createArgument().setValue(COMMAND_LOCK);
117
118         // Check the command line options
119
checkOptions(commandLine);
120
121         // For debugging
122
// System.out.println(commandLine.toString());
123

124         if (!getFailOnErr()) {
125             getProject().log("Ignoring any errors that occur for: "
126                     + getOpType(), Project.MSG_VERBOSE);
127         }
128         result = run(commandLine);
129         if (Execute.isFailure(result) && getFailOnErr()) {
130             String JavaDoc msg = "Failed executing: " + commandLine.toString();
131             throw new BuildException(msg, getLocation());
132         }
133     }
134
135     /**
136      * Check the command line options.
137      */

138 private void checkOptions(Commandline cmd) {
139         // ClearCase items
140
if (getReplace()) {
141             // -replace
142
cmd.createArgument().setValue(FLAG_REPLACE);
143         }
144         if (getObsolete()) {
145             // -obsolete
146
cmd.createArgument().setValue(FLAG_OBSOLETE);
147         } else {
148             getNusersCommand(cmd);
149         }
150         getCommentCommand(cmd);
151
152         if (getObjselect() == null && getPname() == null) {
153             throw new BuildException("Should select either an element "
154             + "(pname) or an object (objselect)");
155         }
156         getPnameCommand(cmd);
157         // object selector
158
if (getObjselect() != null) {
159             cmd.createArgument().setValue(getObjselect());
160         }
161 }
162
163     /**
164      * If true, replace an existing lock.
165      *
166      * @param replace the status to set the flag to
167      */

168     public void setReplace(boolean replace) {
169         mReplace = replace;
170     }
171
172     /**
173      * Get replace flag status
174      *
175      * @return boolean containing status of replace flag
176      */

177     public boolean getReplace() {
178         return mReplace;
179     }
180
181     /**
182      * If true, mark object as obsolete.
183      *
184      * @param obsolete the status to set the flag to
185      */

186     public void setObsolete(boolean obsolete) {
187         mObsolete = obsolete;
188     }
189
190     /**
191      * Get obsolete flag status
192      *
193      * @return boolean containing status of obsolete flag
194      */

195     public boolean getObsolete() {
196         return mObsolete;
197     }
198
199     /**
200      * Sets the users who may continue to
201      * edit the object while it is locked.
202      *
203      * @param nusers users excluded from lock
204      */

205     public void setNusers(String JavaDoc nusers) {
206         mNusers = nusers;
207     }
208
209     /**
210      * Get nusers list
211      *
212      * @return String containing the list of users excluded from lock
213      */

214     public String JavaDoc getNusers() {
215         return mNusers;
216     }
217
218     /**
219      * Sets how comments should be written
220      * for the event record(s)
221      *
222      * @param comment comment method to use
223      */

224     public void setComment(String JavaDoc comment) {
225         mComment = comment;
226     }
227
228     /**
229      * Get comment method
230      *
231      * @return String containing the desired comment method
232      */

233     public String JavaDoc getComment() {
234         return mComment;
235     }
236
237     /**
238      * Sets the pathname to be locked
239      *
240      * @param pname pathname to be locked
241      */

242     public void setPname(String JavaDoc pname) {
243         mPname = pname;
244     }
245
246     /**
247      * Get the pathname to be locked
248      *
249      * @return String containing the pathname to be locked
250      */

251     public String JavaDoc getPname() {
252         return mPname;
253     }
254
255     /**
256      * Sets the object(s) to be locked
257      *
258      * @param objsel objects to be locked
259      * @since ant 1.6.1
260      */

261     public void setObjSel(String JavaDoc objsel) {
262         mObjselect = objsel;
263     }
264
265     /**
266      * Sets the object(s) to be locked
267      *
268      * @param objselect objects to be locked
269      */

270     public void setObjselect(String JavaDoc objselect) {
271         mObjselect = objselect;
272     }
273
274     /**
275      * Get list of objects to be locked
276      *
277      * @return String containing the objects to be locked
278      */

279     public String JavaDoc getObjselect() {
280         return mObjselect;
281     }
282
283     /**
284      * Get the 'nusers' command
285      *
286      * @param cmd containing the command line string with or
287      * without the nusers flag and value appended
288      */

289     private void getNusersCommand(Commandline cmd) {
290         if (getNusers() == null) {
291             return;
292         } else {
293             /* Had to make two separate commands here because if a space is
294                inserted between the flag and the value, it is treated as a
295                Windows filename with a space and it is enclosed in double
296                quotes ("). This breaks clearcase.
297             */

298             cmd.createArgument().setValue(FLAG_NUSERS);
299             cmd.createArgument().setValue(getNusers());
300         }
301     }
302
303     /**
304      * Get the 'comment' command
305      *
306      * @param cmd containing the command line string with or without the
307      * comment flag and value appended
308      */

309     private void getCommentCommand(Commandline cmd) {
310         if (getComment() == null) {
311             return;
312         } else {
313             /* Had to make two separate commands here because if a space is
314                inserted between the flag and the value, it is treated as a
315                Windows filename with a space and it is enclosed in double
316                quotes ("). This breaks clearcase.
317             */

318             cmd.createArgument().setValue(FLAG_COMMENT);
319             cmd.createArgument().setValue(getComment());
320         }
321     }
322
323     /**
324      * Get the 'pname' command
325      *
326      * @param cmd containing the command line string with or
327      * without the pname flag and value appended
328      */

329     private void getPnameCommand(Commandline cmd) {
330         if (getPname() == null) {
331             return;
332         } else {
333             /* Had to make two separate commands here because if a space is
334                inserted between the flag and the value, it is treated as a
335                Windows filename with a space and it is enclosed in double
336                quotes ("). This breaks clearcase.
337             */

338             cmd.createArgument().setValue(FLAG_PNAME);
339             cmd.createArgument().setValue(getPname());
340         }
341     }
342
343     /**
344      * Return which object/pname is being operated on
345      *
346      * @return String containing the object/pname being worked on
347      */

348     private String JavaDoc getOpType() {
349
350         if (getPname() != null) {
351             return getPname();
352         } else {
353             return getObjselect();
354         }
355     }
356
357     /**
358      * -replace flag -- replace existing lock on object(s)
359      */

360     public static final String JavaDoc FLAG_REPLACE = "-replace";
361     /**
362      * -nusers flag -- list of users to exclude from lock
363      */

364     public static final String JavaDoc FLAG_NUSERS = "-nusers";
365     /**
366      * -obsolete flag -- mark locked object as obsolete
367      */

368     public static final String JavaDoc FLAG_OBSOLETE = "-obsolete";
369     /**
370      * -comment flag -- method to use for commenting events
371      */

372     public static final String JavaDoc FLAG_COMMENT = "-comment";
373     /**
374      * -pname flag -- pathname to lock
375      */

376     public static final String JavaDoc FLAG_PNAME = "-pname";
377 }
378
379
Popular Tags