source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/perforce/P4Base.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 9.3 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17/*
18 * Portions of this software are based upon public domain software
19 * originally written at the National Center for Supercomputing Applications,
20 * University of Illinois, Urbana-Champaign.
21 */
22
23package org.apache.tools.ant.taskdefs.optional.perforce;
24
25import java.io.IOException;
26import org.apache.oro.text.perl.Perl5Util;
27import org.apache.tools.ant.BuildException;
28import org.apache.tools.ant.Project;
29import org.apache.tools.ant.taskdefs.Execute;
30import org.apache.tools.ant.types.Commandline;
31
32
33/** Base class for Perforce (P4) ANT tasks. See individual task for example usage.
34 *
35 * @see P4Sync
36 * @see P4Have
37 * @see P4Change
38 * @see P4Edit
39 * @see P4Submit
40 * @see P4Label
41 * @see org.apache.tools.ant.taskdefs.Execute
42 */
43public abstract class P4Base extends org.apache.tools.ant.Task {
44
45 /**Perl5 regexp in Java - cool eh? */
46 protected Perl5Util util = null;
47 /** The OS shell to use (cmd.exe or /bin/sh) */
48 protected String shell;
49
50 //P4 runtime directives
51 /** Perforce Server Port (eg KM01:1666) */
52 protected String P4Port = "";
53 /** Perforce Client (eg myclientspec) */
54 protected String P4Client = "";
55 /** Perforce User (eg fbloggs) */
56 protected String P4User = "";
57 /** Perforce view for commands. (eg //projects/foobar/main/source/... )*/
58 protected String P4View = "";
59
60 // Perforce task directives
61 /** Keep going or fail on error - defaults to fail. */
62 protected boolean failOnError = true;
63
64 //P4 g-opts and cmd opts (rtfm)
65 /** Perforce 'global' opts.
66 * Forms half of low level API */
67 protected String P4Opts = "";
68 /** Perforce command opts.
69 * Forms half of low level API */
70 protected String P4CmdOpts = "";
71
72 /** Set by the task or a handler to indicate that the task has failed. BuildExceptions
73 * can also be thrown to indicate failure. */
74 private boolean inError = false;
75
76 /** If inError is set, then errorMessage needs to contain the reason why. */
77 private String errorMessage = "";
78 /**
79 * gets whether or not the task has encountered an error
80 * @return error flag
81 * @since ant 1.6
82 */
83 public boolean getInError() {
84 return inError;
85 }
86
87 /**
88 * sets the error flag on the task
89 * @param inError if true an error has been encountered by the handler
90 * @since ant 1.6
91 */
92 public void setInError(boolean inError) {
93 this.inError = inError;
94 }
95
96 /**
97 * gets the error message recorded by the Perforce handler
98 * @return error message
99 */
100 public String getErrorMessage() {
101 return errorMessage;
102 }
103
104 /**
105 * sets the error message
106 * @param errorMessage line of error output
107 */
108 public void setErrorMessage(String errorMessage) {
109 this.errorMessage = errorMessage;
110 }
111 //Setters called by Ant
112
113 /**
114 * The p4d server and port to connect to;
115 * optional, default "perforce:1666"
116 *
117 * @param P4Port the port one wants to set such as localhost:1666
118 */
119 public void setPort(String P4Port) {
120 this.P4Port = "-p" + P4Port;
121 }
122
123 /**
124 * The p4 client spec to use;
125 * optional, defaults to the current user
126 *
127 * @param P4Client the name of the Perforce client spec
128 */
129 public void setClient(String P4Client) {
130 this.P4Client = "-c" + P4Client;
131 }
132
133 /**
134 * The p4 username;
135 * optional, defaults to the current user
136 *
137 * @param P4User the user name
138 */
139 public void setUser(String P4User) {
140 this.P4User = "-u" + P4User;
141 }
142 /**
143 * Set global P4 options; Used on all
144 * of the Perforce tasks.
145 *
146 * @param P4Opts global options, to use a specific P4Config file for instance
147 */
148 public void setGlobalopts(String P4Opts) {
149 this.P4Opts = P4Opts;
150 }
151 /**
152 * The client, branch or label view to operate upon;
153 * optional default "//...".
154 *
155 * the view is required for the following tasks :
156 * <ul>
157 * <li>p4delete</li>
158 * <li>p4edit</li>
159 * <li>p4reopen</li>
160 * <li>p4resolve</li>
161 * </ul>
162 *
163 * @param P4View the view one wants to use
164 */
165 public void setView(String P4View) {
166 this.P4View = P4View;
167 }
168
169 /**
170 * Set extra command options; only used on some
171 * of the Perforce tasks.
172 *
173 * @param P4CmdOpts command line options going after the particular
174 * Perforce command
175 */
176 public void setCmdopts(String P4CmdOpts) {
177 this.P4CmdOpts = P4CmdOpts;
178 }
179
180 /**
181 * whether to stop the build (true, default)
182 * or keep going if an error is returned from the p4 command
183 * @param fail indicates whether one wants to fail the build if an error comes from the
184 * Perforce command
185 */
186 public void setFailonerror(boolean fail) {
187 failOnError = fail;
188 }
189 /**
190 * sets attributes Port, Client, User from properties
191 * if these properties are defined.
192 * Called automatically by UnknownElement
193 * @see org.apache.tools.ant.UnknownElement
194 * <table>
195 * <tr><th>Property</th><th>Attribute</th></tr>
196 * <tr><td>p4.port</td><td>Port</td></tr>
197 * <tr><td>p4.client</td><td>Client</td></tr>
198 * <tr><td>p4.user</td><td>User</td></tr>
199 * </table>
200 */
201 public void init() {
202
203 util = new Perl5Util();
204
205 //Get default P4 settings from environment - Mark would have done something cool with
206 //introspection here.....:-)
207 String tmpprop;
208 if ((tmpprop = getProject().getProperty("p4.port")) != null) {
209 setPort(tmpprop);
210 }
211 if ((tmpprop = getProject().getProperty("p4.client")) != null) {
212 setClient(tmpprop);
213 }
214 if ((tmpprop = getProject().getProperty("p4.user")) != null) {
215 setUser(tmpprop);
216 }
217 }
218 /**
219 * no usages found for this method
220 * runs a Perforce command without a handler
221 * @param command the command that one wants to execute
222 * @throws BuildException if failonerror is set and the command fails
223 */
224 protected void execP4Command(String command) throws BuildException {
225 execP4Command(command, null);
226 }
227
228 /**
229 * Execute P4 command assembled by subclasses.
230 *
231 * @param command The command to run
232 * @param handler A P4Handler to process any input and output
233 *
234 * @throws BuildException if failonerror has been set to true
235 */
236 protected void execP4Command(String command, P4Handler handler) throws BuildException {
237 try {
238 // reset error flags before executing the command
239 inError = false;
240 errorMessage = "";
241 Commandline commandline = new Commandline();
242 commandline.setExecutable("p4");
243
244 //Check API for these - it's how CVS does it...
245 if (P4Port != null && P4Port.length() != 0) {
246 commandline.createArgument().setValue(P4Port);
247 }
248 if (P4User != null && P4User.length() != 0) {
249 commandline.createArgument().setValue(P4User);
250 }
251 if (P4Client != null && P4Client.length() != 0) {
252 commandline.createArgument().setValue(P4Client);
253 }
254 if (P4Opts != null && P4Opts.length() != 0) {
255 commandline.createArgument().setLine(P4Opts);
256 }
257 commandline.createArgument().setLine(command);
258
259 log(commandline.describeCommand(), Project.MSG_VERBOSE);
260
261 if (handler == null) {
262 handler = new SimpleP4OutputHandler(this);
263 }
264
265 Execute exe = new Execute(handler, null);
266
267 exe.setAntRun(getProject());
268
269 exe.setCommandline(commandline.getCommandline());
270
271 try {
272 exe.execute();
273
274 if (inError && failOnError) {
275 throw new BuildException(errorMessage);
276 }
277 } catch (IOException e) {
278 throw new BuildException(e);
279 } finally {
280 try {
281 handler.stop();
282 } catch (Exception e) {
283 log(e.toString(), Project.MSG_ERR);
284 }
285 }
286
287
288 } catch (Exception e) {
289 String failMsg = "Problem exec'ing P4 command: " + e.getMessage();
290 if (failOnError) {
291 throw new BuildException(failMsg);
292 } else {
293 log(failMsg, Project.MSG_ERR);
294 }
295
296 }
297 }
298}
Note: See TracBrowser for help on using the repository browser.