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: import org.apache.tools.ant.taskdefs.condition.Os;
026:
027: /**
028: * Task to perform mkattr command to ClearCase.
029: * <p>
030: * The following attributes are interpreted:
031: * <table border="1">
032: * <tr>
033: * <th>Attribute</th>
034: * <th>Values</th>
035: * <th>Required</th>
036: * </tr>
037: * <tr>
038: * <td>viewpath</td>
039: * <td>Path to the ClearCase view file or directory that the command will operate on</td>
040: * <td>Yes</td>
041: * <tr>
042: * <tr>
043: * <td>replace</td>
044: * <td>Replace the value of the attribute if it already exists</td>
045: * <td>No</td>
046: * <tr>
047: * <tr>
048: * <td>recurse</td>
049: * <td>Process each subdirectory under viewpath</td>
050: * <td>No</td>
051: * <tr>
052: * <tr>
053: * <td>version</td>
054: * <td>Identify a specific version to attach the attribute to</td>
055: * <td>No</td>
056: * <tr>
057: * <tr>
058: * <td>typename</td>
059: * <td>Name of the attribute type</td>
060: * <td>Yes</td>
061: * <tr>
062: * <tr>
063: * <td>typevalue</td>
064: * <td>Value to attach to the attribute type</td>
065: * <td>Yes</td>
066: * <tr>
067: * <tr>
068: * <td>comment</td>
069: * <td>Specify a comment. Only one of comment or cfile may be used.</td>
070: * <td>No</td>
071: * <tr>
072: * <tr>
073: * <td>commentfile</td>
074: * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
075: * <td>No</td>
076: * <tr>
077: * <tr>
078: * <td>failonerr</td>
079: * <td>Throw an exception if the command fails. Default is true</td>
080: * <td>No</td>
081: * <tr>
082: * </table>
083: *
084: */
085: public class CCMkattr extends ClearCase {
086: private boolean mReplace = false;
087: private boolean mRecurse = false;
088: private String mVersion = null;
089: private String mTypeName = null;
090: private String mTypeValue = null;
091: private String mComment = null;
092: private String mCfile = null;
093:
094: /**
095: * Executes the task.
096: * <p>
097: * Builds a command line to execute cleartool and then calls Exec's run method
098: * to execute the command line.
099: * @throws BuildException if the command fails and failonerr is set to true
100: */
101: public void execute() throws BuildException {
102: Commandline commandLine = new Commandline();
103: Project aProj = getProject();
104: int result = 0;
105:
106: // Check for required attributes
107: if (getTypeName() == null) {
108: throw new BuildException(
109: "Required attribute TypeName not specified");
110: }
111: if (getTypeValue() == null) {
112: throw new BuildException(
113: "Required attribute TypeValue not specified");
114: }
115: // Default the viewpath to basedir if it is not specified
116: if (getViewPath() == null) {
117: setViewPath(aProj.getBaseDir().getPath());
118: }
119:
120: // build the command line from what we got. the format is
121: // cleartool mkattr [options...] [viewpath ...]
122: // as specified in the CLEARTOOL help
123: commandLine.setExecutable(getClearToolCommand());
124: commandLine.createArgument().setValue(COMMAND_MKATTR);
125:
126: checkOptions(commandLine);
127:
128: if (!getFailOnErr()) {
129: getProject().log(
130: "Ignoring any errors that occur for: "
131: + getViewPathBasename(),
132: Project.MSG_VERBOSE);
133: }
134:
135: // For debugging
136: // System.out.println(commandLine.toString());
137:
138: result = run(commandLine);
139: if (Execute.isFailure(result) && getFailOnErr()) {
140: String msg = "Failed executing: " + commandLine.toString();
141: throw new BuildException(msg, getLocation());
142: }
143: }
144:
145: /**
146: * Check the command line options.
147: */
148: private void checkOptions(Commandline cmd) {
149: if (getReplace()) {
150: // -replace
151: cmd.createArgument().setValue(FLAG_REPLACE);
152: }
153:
154: if (getRecurse()) {
155: // -recurse
156: cmd.createArgument().setValue(FLAG_RECURSE);
157: }
158:
159: if (getVersion() != null) {
160: // -version
161: getVersionCommand(cmd);
162: }
163:
164: if (getComment() != null) {
165: // -c
166: getCommentCommand(cmd);
167: } else {
168: if (getCommentFile() != null) {
169: // -cfile
170: getCommentFileCommand(cmd);
171: } else {
172: cmd.createArgument().setValue(FLAG_NOCOMMENT);
173: }
174: }
175:
176: if (getTypeName() != null) {
177: // type
178: getTypeCommand(cmd);
179: }
180: if (getTypeValue() != null) {
181: // type value
182: getTypeValueCommand(cmd);
183: }
184: // viewpath
185: cmd.createArgument().setValue(getViewPath());
186: }
187:
188: /**
189: * Set the replace flag
190: *
191: * @param replace the status to set the flag to
192: */
193: public void setReplace(boolean replace) {
194: mReplace = replace;
195: }
196:
197: /**
198: * Get replace flag status
199: *
200: * @return boolean containing status of replace flag
201: */
202: public boolean getReplace() {
203: return mReplace;
204: }
205:
206: /**
207: * Set recurse flag
208: *
209: * @param recurse the status to set the flag to
210: */
211: public void setRecurse(boolean recurse) {
212: mRecurse = recurse;
213: }
214:
215: /**
216: * Get recurse flag status
217: *
218: * @return boolean containing status of recurse flag
219: */
220: public boolean getRecurse() {
221: return mRecurse;
222: }
223:
224: /**
225: * Set the version flag
226: *
227: * @param version the status to set the flag to
228: */
229: public void setVersion(String version) {
230: mVersion = version;
231: }
232:
233: /**
234: * Get version flag status
235: *
236: * @return boolean containing status of version flag
237: */
238: public String getVersion() {
239: return mVersion;
240: }
241:
242: /**
243: * Set comment string
244: *
245: * @param comment the comment string
246: */
247: public void setComment(String comment) {
248: mComment = comment;
249: }
250:
251: /**
252: * Get comment string
253: *
254: * @return String containing the comment
255: */
256: public String getComment() {
257: return mComment;
258: }
259:
260: /**
261: * Set comment file
262: *
263: * @param cfile the path to the comment file
264: */
265: public void setCommentFile(String cfile) {
266: mCfile = cfile;
267: }
268:
269: /**
270: * Get comment file
271: *
272: * @return String containing the path to the comment file
273: */
274: public String getCommentFile() {
275: return mCfile;
276: }
277:
278: /**
279: * Set the attribute type-name
280: *
281: * @param tn the type name
282: */
283: public void setTypeName(String tn) {
284: mTypeName = tn;
285: }
286:
287: /**
288: * Get attribute type-name
289: *
290: * @return String containing type name
291: */
292: public String getTypeName() {
293: return mTypeName;
294: }
295:
296: /**
297: * Set the attribute type-value
298: *
299: * @param tv the type value
300: */
301: public void setTypeValue(String tv) {
302: mTypeValue = tv;
303: }
304:
305: /**
306: * Get the attribute type-value
307: *
308: * @return String containing type value
309: */
310: public String getTypeValue() {
311: return mTypeValue;
312: }
313:
314: /**
315: * Get the 'version' command
316: *
317: * @param cmd CommandLine containing the command line string with or
318: * without the version flag and string appended
319: */
320: private void getVersionCommand(Commandline cmd) {
321: if (getVersion() != null) {
322: /* Had to make two separate commands here because if a space is
323: inserted between the flag and the value, it is treated as a
324: Windows filename with a space and it is enclosed in double
325: quotes ("). This breaks clearcase.
326: */
327: cmd.createArgument().setValue(FLAG_VERSION);
328: cmd.createArgument().setValue(getVersion());
329: }
330: }
331:
332: /**
333: * Get the 'comment' command
334: *
335: * @param cmd containing the command line string with or
336: * without the comment flag and string appended
337: */
338: private void getCommentCommand(Commandline cmd) {
339: if (getComment() != null) {
340: /* Had to make two separate commands here because if a space is
341: inserted between the flag and the value, it is treated as a
342: Windows filename with a space and it is enclosed in double
343: quotes ("). This breaks clearcase.
344: */
345: cmd.createArgument().setValue(FLAG_COMMENT);
346: cmd.createArgument().setValue(getComment());
347: }
348: }
349:
350: /**
351: * Get the 'commentfile' command
352: *
353: * @param cmd containing the command line string with or
354: * without the commentfile flag and file appended
355: */
356: private void getCommentFileCommand(Commandline cmd) {
357: if (getCommentFile() != null) {
358: /* Had to make two separate commands here because if a space is
359: inserted between the flag and the value, it is treated as a
360: Windows filename with a space and it is enclosed in double
361: quotes ("). This breaks clearcase.
362: */
363: cmd.createArgument().setValue(FLAG_COMMENTFILE);
364: cmd.createArgument().setValue(getCommentFile());
365: }
366: }
367:
368: /**
369: * Get the attribute type-name
370: *
371: * @param cmd containing the command line string with or
372: * without the type-name
373: */
374: private void getTypeCommand(Commandline cmd) {
375: String typenm = getTypeName();
376:
377: if (typenm != null) {
378: cmd.createArgument().setValue(typenm);
379: }
380: }
381:
382: /**
383: * Get the attribute type-value
384: *
385: * @param cmd containing the command line string with or
386: * without the type-value
387: */
388: private void getTypeValueCommand(Commandline cmd) {
389: String typevl = getTypeValue();
390:
391: if (typevl != null) {
392: if (Os.isFamily("windows")) {
393: typevl = "\\\"" + typevl + "\\\""; // Windows quoting of the value
394: } else {
395: typevl = "\"" + typevl + "\"";
396: }
397: cmd.createArgument().setValue(typevl);
398: }
399: }
400:
401: /**
402: * -replace flag -- replace the existing value of the attribute
403: */
404: public static final String FLAG_REPLACE = "-replace";
405: /**
406: * -recurse flag -- process all subdirectories
407: */
408: public static final String FLAG_RECURSE = "-recurse";
409: /**
410: * -version flag -- attach attribute to specified version
411: */
412: public static final String FLAG_VERSION = "-version";
413: /**
414: * -c flag -- comment to attach to the element
415: */
416: public static final String FLAG_COMMENT = "-c";
417: /**
418: * -cfile flag -- file containing a comment to attach to the file
419: */
420: public static final String FLAG_COMMENTFILE = "-cfile";
421: /**
422: * -nc flag -- no comment is specified
423: */
424: public static final String FLAG_NOCOMMENT = "-nc";
425: }
|