001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.clearcase;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.taskdefs.Execute;
024: import org.apache.tools.ant.types.Commandline;
025:
026: /**
027: * TODO:
028: * comment field doesn't include all options yet
029: */
030:
031: /**
032: * Performs a ClearCase Unlock command.
033: *
034: * <p>
035: * The following attributes are interpreted:
036: * <table border="1">
037: * <tr>
038: * <th>Attribute</th>
039: * <th>Values</th>
040: * <th>Required</th>
041: * </tr>
042: * <tr>
043: * <td>comment</td>
044: * <td>Specifies how to populate comments fields</td>
045: * <td>No</td>
046: * <tr>
047: * <tr>
048: * <td>pname</td>
049: * <td>Specifies the object pathname to be unlocked.</td>
050: * <td>No</td>
051: * <tr>
052: * <td>objselect</td>
053: * <td>This variable is obsolete. Should use <i>objsel</i> instead.</td>
054: * <td>no</td>
055: * <tr>
056: * <tr>
057: * <td>objsel</td>
058: * <td>Specifies the object(s) to be unlocked.</td>
059: * <td>No</td>
060: * <tr>
061: * <tr>
062: * <td>failonerr</td>
063: * <td>Throw an exception if the command fails. Default is true</td>
064: * <td>No</td>
065: * <tr>
066: *
067: * </table>
068: *
069: */
070: public class CCUnlock extends ClearCase {
071: private String mComment = null;
072: private String mPname = null;
073:
074: /**
075: * Executes the task.
076: * <p>
077: * Builds a command line to execute cleartool and then calls Exec's run method
078: * to execute the command line.
079: * @throws BuildException if the command fails and failonerr is set to true
080: */
081: public void execute() throws BuildException {
082: Commandline commandLine = new Commandline();
083: Project aProj = getProject();
084: int result = 0;
085:
086: // Default the viewpath to basedir if it is not specified
087: if (getViewPath() == null) {
088: setViewPath(aProj.getBaseDir().getPath());
089: }
090:
091: // build the command line from what we got the format is
092: // cleartool lock [options...]
093: // as specified in the CLEARTOOL.EXE help
094: commandLine.setExecutable(getClearToolCommand());
095: commandLine.createArgument().setValue(COMMAND_UNLOCK);
096:
097: // Check the command line options
098: checkOptions(commandLine);
099:
100: // For debugging
101: // System.out.println(commandLine.toString());
102:
103: if (!getFailOnErr()) {
104: getProject().log(
105: "Ignoring any errors that occur for: "
106: + getOpType(), Project.MSG_VERBOSE);
107: }
108: result = run(commandLine);
109: if (Execute.isFailure(result) && getFailOnErr()) {
110: String msg = "Failed executing: " + commandLine.toString();
111: throw new BuildException(msg, getLocation());
112: }
113: }
114:
115: /**
116: * Check the command line options.
117: */
118: private void checkOptions(Commandline cmd) {
119: // ClearCase items
120: getCommentCommand(cmd);
121:
122: if (getObjSelect() == null && getPname() == null) {
123: throw new BuildException("Should select either an element "
124: + "(pname) or an object (objselect)");
125: }
126: getPnameCommand(cmd);
127: // object selector
128: if (getObjSelect() != null) {
129: cmd.createArgument().setValue(getObjSelect());
130: }
131: }
132:
133: /**
134: * Sets how comments should be written
135: * for the event record(s)
136: *
137: * @param comment comment method to use
138: */
139: public void setComment(String comment) {
140: mComment = comment;
141: }
142:
143: /**
144: * Get comment method
145: *
146: * @return String containing the desired comment method
147: */
148: public String getComment() {
149: return mComment;
150: }
151:
152: /**
153: * Sets the pathname to be locked
154: *
155: * @param pname pathname to be locked
156: */
157: public void setPname(String pname) {
158: mPname = pname;
159: }
160:
161: /**
162: * Get the pathname to be locked
163: *
164: * @return String containing the pathname to be locked
165: */
166: public String getPname() {
167: return mPname;
168: }
169:
170: /**
171: * Sets the object(s) to be locked
172: *
173: * @param objselect objects to be locked
174: */
175: public void setObjselect(String objselect) {
176: setObjSelect(objselect);
177: }
178:
179: /**
180: * Sets the object(s) to be locked
181: *
182: * @param objsel objects to be locked
183: * @since ant 1.6.1
184: */
185: public void setObjSel(String objsel) {
186: setObjSelect(objsel);
187: }
188:
189: /**
190: * Get list of objects to be locked
191: *
192: * @return String containing the objects to be locked
193: */
194: public String getObjselect() {
195: return getObjSelect();
196: }
197:
198: /**
199: * Get the 'comment' command
200: *
201: * @param cmd containing the command line string with or without the
202: * comment flag and value appended
203: */
204: private void getCommentCommand(Commandline cmd) {
205: if (getComment() == null) {
206: return;
207: } else {
208: /* Had to make two separate commands here because if a space is
209: inserted between the flag and the value, it is treated as a
210: Windows filename with a space and it is enclosed in double
211: quotes ("). This breaks clearcase.
212: */
213: cmd.createArgument().setValue(FLAG_COMMENT);
214: cmd.createArgument().setValue(getComment());
215: }
216: }
217:
218: /**
219: * Get the 'pname' command
220: *
221: * @param cmd containing the command line string with or without the
222: * pname flag and value appended
223: */
224: private void getPnameCommand(Commandline cmd) {
225: if (getPname() == null) {
226: return;
227: } else {
228: /* Had to make two separate commands here because if a space is
229: inserted between the flag and the value, it is treated as a
230: Windows filename with a space and it is enclosed in double
231: quotes ("). This breaks clearcase.
232: */
233: cmd.createArgument().setValue(FLAG_PNAME);
234: cmd.createArgument().setValue(getPname());
235: }
236: }
237:
238: /**
239: * Return which object/pname is being operated on
240: *
241: * @return String containing the object/pname being worked on
242: */
243: private String getOpType() {
244:
245: if (getPname() != null) {
246: return getPname();
247: } else {
248: return getObjSelect();
249: }
250: }
251:
252: /**
253: * -comment flag -- method to use for commenting events
254: */
255: public static final String FLAG_COMMENT = "-comment";
256: /**
257: * -pname flag -- pathname to lock
258: */
259: public static final String FLAG_PNAME = "-pname";
260: }
|