KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.Task;
25 import org.apache.tools.ant.taskdefs.ExecTask;
26 import org.apache.tools.ant.taskdefs.Execute;
27 import org.apache.tools.ant.taskdefs.LogStreamHandler;
28 import org.apache.tools.ant.types.Commandline;
29 import org.apache.tools.ant.util.FileUtils;
30
31
32
33 /**
34  * A base class for creating tasks for executing commands on ClearCase.
35  * <p>
36  * The class extends the 'exec' task as it operates by executing the cleartool program
37  * supplied with ClearCase. By default the task expects the cleartool executable to be
38  * in the path, * you can override this be specifying the cleartooldir attribute.
39  * </p>
40  * <p>
41  * This class provides set and get methods for the 'viewpath' and 'objselect'
42  * attribute. It also contains constants for the flags that can be passed to
43  * cleartool.
44  * </p>
45  *
46  */

47 public abstract class ClearCase extends Task {
48     private String JavaDoc mClearToolDir = "";
49     private String JavaDoc mviewPath = null;
50     private String JavaDoc mobjSelect = null;
51     private static int pcnt = 0;
52     private boolean mFailonerr = true;
53     /**
54      * Set the directory where the cleartool executable is located.
55      *
56      * @param dir the directory containing the cleartool executable
57      */

58     public final void setClearToolDir(String JavaDoc dir) {
59         mClearToolDir = FileUtils.translatePath(dir);
60     }
61
62     /**
63      * Builds and returns the command string to execute cleartool
64      *
65      * @return String containing path to the executable
66      */

67     protected final String JavaDoc getClearToolCommand() {
68         String JavaDoc toReturn = mClearToolDir;
69         if (!toReturn.equals("") && !toReturn.endsWith("/")) {
70             toReturn += "/";
71         }
72
73         toReturn += CLEARTOOL_EXE;
74
75         return toReturn;
76     }
77
78     /**
79      * Set the path to the item in a ClearCase view to operate on.
80      *
81      * @param viewPath Path to the view directory or file
82      */

83     public final void setViewPath(String JavaDoc viewPath) {
84         mviewPath = viewPath;
85     }
86
87     /**
88      * Get the path to the item in a clearcase view
89      *
90      * @return mviewPath
91      */

92     public String JavaDoc getViewPath() {
93         return mviewPath;
94     }
95
96     /**
97      * Get the basename path of the item in a clearcase view
98      *
99      * @return basename
100      */

101     public String JavaDoc getViewPathBasename() {
102         return (new File JavaDoc(mviewPath)).getName();
103     }
104
105     /**
106      * Set the object to operate on.
107      *
108      * @param objSelect object to operate on
109      */

110     public final void setObjSelect(String JavaDoc objSelect) {
111         mobjSelect = objSelect;
112     }
113
114     /**
115      * Get the object to operate on
116      *
117      * @return mobjSelect
118      */

119     public String JavaDoc getObjSelect() {
120         return mobjSelect;
121     }
122
123     /**
124      * Execute the given command are return success or failure
125      * @param cmd command line to execute
126      * @return the exit status of the subprocess or <code>INVALID</code>
127      */

128     protected int run(Commandline cmd) {
129         try {
130             Project aProj = getProject();
131             Execute exe
132                 = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
133             exe.setAntRun(aProj);
134             exe.setWorkingDirectory(aProj.getBaseDir());
135             exe.setCommandline(cmd.getCommandline());
136             return exe.execute();
137         } catch (java.io.IOException JavaDoc e) {
138             throw new BuildException(e, getLocation());
139         }
140     }
141
142     /**
143      * Execute the given command, and return it's output
144      * @param cmdline command line to execute
145      * @return output of the command line
146      */

147     protected String JavaDoc runS(Commandline cmdline) {
148         String JavaDoc outV = "opts.cc.runS.output" + pcnt++;
149         ExecTask exe = new ExecTask(this);
150         Commandline.Argument arg = exe.createArg();
151
152         exe.setExecutable(cmdline.getExecutable());
153         arg.setLine(Commandline.toString(cmdline.getArguments()));
154         exe.setOutputproperty(outV);
155         exe.execute();
156
157         return getProject().getProperty(outV);
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 JavaDoc CLEARTOOL_EXE = "cleartool";
183     /**
184      * The 'Update' command
185      */

186     public static final String JavaDoc COMMAND_UPDATE = "update";
187     /**
188      * The 'Checkout' command
189      */

190     public static final String JavaDoc COMMAND_CHECKOUT = "checkout";
191     /**
192      * The 'Checkin' command
193      */

194     public static final String JavaDoc COMMAND_CHECKIN = "checkin";
195     /**
196      * The 'UndoCheckout' command
197      */

198     public static final String JavaDoc COMMAND_UNCHECKOUT = "uncheckout";
199     /**
200      * The 'Lock' command
201      */

202     public static final String JavaDoc COMMAND_LOCK = "lock";
203     /**
204      * The 'Unlock' command
205      */

206     public static final String JavaDoc COMMAND_UNLOCK = "unlock";
207     /**
208      * The 'Mkbl' command
209      */

210     public static final String JavaDoc COMMAND_MKBL = "mkbl";
211     /**
212      * The 'Mklabel' command
213      */

214     public static final String JavaDoc COMMAND_MKLABEL = "mklabel";
215     /**
216      * The 'Mklbtype' command
217      */

218     public static final String JavaDoc COMMAND_MKLBTYPE = "mklbtype";
219     /**
220      * The 'Rmtype' command
221      */

222     public static final String JavaDoc COMMAND_RMTYPE = "rmtype";
223     /**
224      * The 'LsCheckout' command
225      */

226     public static final String JavaDoc COMMAND_LSCO = "lsco";
227     /**
228      * The 'Mkelem' command
229      */

230     public static final String JavaDoc COMMAND_MKELEM = "mkelem";
231     /**
232      * The 'Mkattr' command
233      */

234     public static final String JavaDoc COMMAND_MKATTR = "mkattr";
235     /**
236      * The 'Mkdir' command
237      */

238     public static final String JavaDoc COMMAND_MKDIR = "mkdir";
239
240 }
241
242
Popular Tags