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 java.io.File;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.Task;
025: import org.apache.tools.ant.taskdefs.ExecTask;
026: import org.apache.tools.ant.taskdefs.Execute;
027: import org.apache.tools.ant.taskdefs.LogStreamHandler;
028: import org.apache.tools.ant.types.Commandline;
029: import org.apache.tools.ant.util.FileUtils;
030:
031: /**
032: * A base class for creating tasks for executing commands on ClearCase.
033: * <p>
034: * The class extends the 'exec' task as it operates by executing the cleartool program
035: * supplied with ClearCase. By default the task expects the cleartool executable to be
036: * in the path, * you can override this be specifying the cleartooldir attribute.
037: * </p>
038: * <p>
039: * This class provides set and get methods for the 'viewpath' and 'objselect'
040: * attribute. It also contains constants for the flags that can be passed to
041: * cleartool.
042: * </p>
043: *
044: */
045: public abstract class ClearCase extends Task {
046: private String mClearToolDir = "";
047: private String mviewPath = null;
048: private String mobjSelect = null;
049: private static int pcnt = 0;
050: private boolean mFailonerr = true;
051:
052: /**
053: * Set the directory where the cleartool executable is located.
054: *
055: * @param dir the directory containing the cleartool executable
056: */
057: public final void setClearToolDir(String dir) {
058: mClearToolDir = FileUtils.translatePath(dir);
059: }
060:
061: /**
062: * Builds and returns the command string to execute cleartool
063: *
064: * @return String containing path to the executable
065: */
066: protected final String getClearToolCommand() {
067: String toReturn = mClearToolDir;
068: if (!toReturn.equals("") && !toReturn.endsWith("/")) {
069: toReturn += "/";
070: }
071:
072: toReturn += CLEARTOOL_EXE;
073:
074: return toReturn;
075: }
076:
077: /**
078: * Set the path to the item in a ClearCase view to operate on.
079: *
080: * @param viewPath Path to the view directory or file
081: */
082: public final void setViewPath(String viewPath) {
083: mviewPath = viewPath;
084: }
085:
086: /**
087: * Get the path to the item in a clearcase view
088: *
089: * @return mviewPath
090: */
091: public String getViewPath() {
092: return mviewPath;
093: }
094:
095: /**
096: * Get the basename path of the item in a clearcase view
097: *
098: * @return basename
099: */
100: public String getViewPathBasename() {
101: return (new File(mviewPath)).getName();
102: }
103:
104: /**
105: * Set the object to operate on.
106: *
107: * @param objSelect object to operate on
108: */
109: public final void setObjSelect(String objSelect) {
110: mobjSelect = objSelect;
111: }
112:
113: /**
114: * Get the object to operate on
115: *
116: * @return mobjSelect
117: */
118: public String getObjSelect() {
119: return mobjSelect;
120: }
121:
122: /**
123: * Execute the given command are return success or failure
124: * @param cmd command line to execute
125: * @return the exit status of the subprocess or <code>INVALID</code>
126: */
127: protected int run(Commandline cmd) {
128: try {
129: Project aProj = getProject();
130: Execute exe = new Execute(new LogStreamHandler(this ,
131: Project.MSG_INFO, Project.MSG_WARN));
132: exe.setAntRun(aProj);
133: exe.setWorkingDirectory(aProj.getBaseDir());
134: exe.setCommandline(cmd.getCommandline());
135: return exe.execute();
136: } catch (java.io.IOException e) {
137: throw new BuildException(e, getLocation());
138: }
139: }
140:
141: /**
142: * Execute the given command, and return it's output
143: * @param cmdline command line to execute
144: * @return output of the command line
145: */
146: protected String runS(Commandline cmdline) {
147: String outV = "opts.cc.runS.output" + pcnt++;
148: ExecTask exe = new ExecTask(this );
149: Commandline.Argument arg = exe.createArg();
150:
151: exe.setExecutable(cmdline.getExecutable());
152: arg.setLine(Commandline.toString(cmdline.getArguments()));
153: exe.setOutputproperty(outV);
154: exe.execute();
155:
156: return getProject().getProperty(outV);
157: }
158:
159: /**
160: * If true, command will throw an exception on failure.
161: *
162: * @param failonerr the status to set the flag to
163: * @since ant 1.6.1
164: */
165: public void setFailOnErr(boolean failonerr) {
166: mFailonerr = failonerr;
167: }
168:
169: /**
170: * Get failonerr flag status
171: *
172: * @return boolean containing status of failonerr flag
173: * @since ant 1.6.1
174: */
175: public boolean getFailOnErr() {
176: return mFailonerr;
177: }
178:
179: /**
180: * Constant for the thing to execute
181: */
182: private static final String CLEARTOOL_EXE = "cleartool";
183: /**
184: * The 'Update' command
185: */
186: public static final String COMMAND_UPDATE = "update";
187: /**
188: * The 'Checkout' command
189: */
190: public static final String COMMAND_CHECKOUT = "checkout";
191: /**
192: * The 'Checkin' command
193: */
194: public static final String COMMAND_CHECKIN = "checkin";
195: /**
196: * The 'UndoCheckout' command
197: */
198: public static final String COMMAND_UNCHECKOUT = "uncheckout";
199: /**
200: * The 'Lock' command
201: */
202: public static final String COMMAND_LOCK = "lock";
203: /**
204: * The 'Unlock' command
205: */
206: public static final String COMMAND_UNLOCK = "unlock";
207: /**
208: * The 'Mkbl' command
209: */
210: public static final String COMMAND_MKBL = "mkbl";
211: /**
212: * The 'Mklabel' command
213: */
214: public static final String COMMAND_MKLABEL = "mklabel";
215: /**
216: * The 'Mklbtype' command
217: */
218: public static final String COMMAND_MKLBTYPE = "mklbtype";
219: /**
220: * The 'Rmtype' command
221: */
222: public static final String COMMAND_RMTYPE = "rmtype";
223: /**
224: * The 'LsCheckout' command
225: */
226: public static final String COMMAND_LSCO = "lsco";
227: /**
228: * The 'Mkelem' command
229: */
230: public static final String COMMAND_MKELEM = "mkelem";
231: /**
232: * The 'Mkattr' command
233: */
234: public static final String COMMAND_MKATTR = "mkattr";
235: /**
236: * The 'Mkdir' command
237: */
238: public static final String COMMAND_MKDIR = "mkdir";
239:
240: }
|