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: * Performs a ClearCase Update command.
028: *
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>No</td>
041: * <tr>
042: * <tr>
043: * <td>graphical</td>
044: * <td>Displays a graphical dialog during the update</td>
045: * <td>No</td>
046: * <tr>
047: * <tr>
048: * <td>log</td>
049: * <td>Specifies a log file for ClearCase to write to</td>
050: * <td>No</td>
051: * <tr>
052: * <tr>
053: * <td>overwrite</td>
054: * <td>Specifies whether to overwrite hijacked files or not</td>
055: * <td>No</td>
056: * <tr>
057: * <tr>
058: * <td>rename</td>
059: * <td>Specifies that hijacked files should be renamed with a .keep extension</td>
060: * <td>No</td>
061: * <tr>
062: * <tr>
063: * <td>currenttime</td>
064: * <td>Specifies that modification time should be written as the current
065: * time. Either currenttime or preservetime can be specified.</td>
066: * <td>No</td>
067: * <tr>
068: * <tr>
069: * <td>preservetime</td>
070: * <td>Specifies that modification time should preserved from the VOB
071: * time. Either currenttime or preservetime can be specified.</td>
072: * <td>No</td>
073: * <tr>
074: * <tr>
075: * <td>failonerr</td>
076: * <td>Throw an exception if the command fails. Default is true</td>
077: * <td>No</td>
078: * <tr>
079: * </table>
080: *
081: */
082: public class CCUpdate extends ClearCase {
083: private boolean mGraphical = false;
084: private boolean mOverwrite = false;
085: private boolean mRename = false;
086: private boolean mCtime = false;
087: private boolean mPtime = false;
088: private String mLog = null;
089:
090: /**
091: * Executes the task.
092: * <p>
093: * Builds a command line to execute cleartool and then calls Exec's run method
094: * to execute the command line.
095: * @throws BuildException if the command fails and failonerr is set to true
096: */
097: public void execute() throws BuildException {
098: Commandline commandLine = new Commandline();
099: Project aProj = getProject();
100: int result = 0;
101:
102: // Default the viewpath to basedir if it is not specified
103: if (getViewPath() == null) {
104: setViewPath(aProj.getBaseDir().getPath());
105: }
106:
107: // build the command line from what we got the format is
108: // cleartool update [options...] [viewpath ...]
109: // as specified in the CLEARTOOL.EXE help
110: commandLine.setExecutable(getClearToolCommand());
111: commandLine.createArgument().setValue(COMMAND_UPDATE);
112:
113: // Check the command line options
114: checkOptions(commandLine);
115:
116: // For debugging
117: getProject().log(commandLine.toString(), Project.MSG_DEBUG);
118:
119: if (!getFailOnErr()) {
120: getProject().log(
121: "Ignoring any errors that occur for: "
122: + getViewPathBasename(),
123: Project.MSG_VERBOSE);
124: }
125: result = run(commandLine);
126: if (Execute.isFailure(result) && getFailOnErr()) {
127: String msg = "Failed executing: " + commandLine.toString();
128: throw new BuildException(msg, getLocation());
129: }
130: }
131:
132: /**
133: * Check the command line options.
134: */
135: private void checkOptions(Commandline cmd) {
136: // ClearCase items
137: if (getGraphical()) {
138: // -graphical
139: cmd.createArgument().setValue(FLAG_GRAPHICAL);
140: } else {
141: if (getOverwrite()) {
142: // -overwrite
143: cmd.createArgument().setValue(FLAG_OVERWRITE);
144: } else {
145: if (getRename()) {
146: // -rename
147: cmd.createArgument().setValue(FLAG_RENAME);
148: } else {
149: // -noverwrite
150: cmd.createArgument().setValue(FLAG_NOVERWRITE);
151: }
152: }
153:
154: if (getCurrentTime()) {
155: // -ctime
156: cmd.createArgument().setValue(FLAG_CURRENTTIME);
157: } else {
158: if (getPreserveTime()) {
159: // -ptime
160: cmd.createArgument().setValue(FLAG_PRESERVETIME);
161: }
162: }
163:
164: // -log logname
165: getLogCommand(cmd);
166: }
167:
168: // viewpath
169: cmd.createArgument().setValue(getViewPath());
170: }
171:
172: /**
173: * If true, displays a graphical dialog during the update.
174: *
175: * @param graphical the status to set the flag to
176: */
177: public void setGraphical(boolean graphical) {
178: mGraphical = graphical;
179: }
180:
181: /**
182: * Get graphical flag status
183: *
184: * @return boolean containing status of graphical flag
185: */
186: public boolean getGraphical() {
187: return mGraphical;
188: }
189:
190: /**
191: * If true, overwrite hijacked files.
192: *
193: * @param ow the status to set the flag to
194: */
195: public void setOverwrite(boolean ow) {
196: mOverwrite = ow;
197: }
198:
199: /**
200: * Get overwrite hijacked files status
201: *
202: * @return boolean containing status of overwrite flag
203: */
204: public boolean getOverwrite() {
205: return mOverwrite;
206: }
207:
208: /**
209: * If true, hijacked files are renamed with a .keep extension.
210: *
211: * @param ren the status to set the flag to
212: */
213: public void setRename(boolean ren) {
214: mRename = ren;
215: }
216:
217: /**
218: * Get rename hijacked files status
219: *
220: * @return boolean containing status of rename flag
221: */
222: public boolean getRename() {
223: return mRename;
224: }
225:
226: /**
227: * If true, modification time should be written as the current time.
228: * Either currenttime or preservetime can be specified.
229: *
230: * @param ct the status to set the flag to
231: */
232: public void setCurrentTime(boolean ct) {
233: mCtime = ct;
234: }
235:
236: /**
237: * Get current time status
238: *
239: * @return boolean containing status of current time flag
240: */
241: public boolean getCurrentTime() {
242: return mCtime;
243: }
244:
245: /**
246: * If true, modification time should be preserved from the VOB time.
247: * Either currenttime or preservetime can be specified.
248: *
249: * @param pt the status to set the flag to
250: */
251: public void setPreserveTime(boolean pt) {
252: mPtime = pt;
253: }
254:
255: /**
256: * Get preserve time status
257: *
258: * @return boolean containing status of preserve time flag
259: */
260: public boolean getPreserveTime() {
261: return mPtime;
262: }
263:
264: /**
265: * Sets the log file where cleartool records
266: * the status of the command.
267: *
268: * @param log the path to the log file
269: */
270: public void setLog(String log) {
271: mLog = log;
272: }
273:
274: /**
275: * Get log file
276: *
277: * @return String containing the path to the log file
278: */
279: public String getLog() {
280: return mLog;
281: }
282:
283: /**
284: * Get the 'log' command
285: *
286: * @param cmd containing the command line string with or without the log flag and path appended
287: */
288: private void getLogCommand(Commandline cmd) {
289: if (getLog() == null) {
290: return;
291: } else {
292: /* Had to make two separate commands here because if a space is
293: inserted between the flag and the value, it is treated as a
294: Windows filename with a space and it is enclosed in double
295: quotes ("). This breaks clearcase.
296: */
297: cmd.createArgument().setValue(FLAG_LOG);
298: cmd.createArgument().setValue(getLog());
299: }
300: }
301:
302: /**
303: * -graphical flag -- display graphical dialog during update operation
304: */
305: public static final String FLAG_GRAPHICAL = "-graphical";
306: /**
307: * -log flag -- file to log status to
308: */
309: public static final String FLAG_LOG = "-log";
310: /**
311: * -overwrite flag -- overwrite hijacked files
312: */
313: public static final String FLAG_OVERWRITE = "-overwrite";
314: /**
315: * -noverwrite flag -- do not overwrite hijacked files
316: */
317: public static final String FLAG_NOVERWRITE = "-noverwrite";
318: /**
319: * -rename flag -- rename hijacked files with .keep extension
320: */
321: public static final String FLAG_RENAME = "-rename";
322: /**
323: * -ctime flag -- modified time is written as the current time
324: */
325: public static final String FLAG_CURRENTTIME = "-ctime";
326: /**
327: * -ptime flag -- modified time is written as the VOB time
328: */
329: public static final String FLAG_PRESERVETIME = "-ptime";
330:
331: }
|