KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > perforce > P4Base


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  * Portions of this software are based upon public domain software
20  * originally written at the National Center for Supercomputing Applications,
21  * University of Illinois, Urbana-Champaign.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.perforce;
25
26 import java.io.IOException JavaDoc;
27 import org.apache.oro.text.perl.Perl5Util;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.taskdefs.Execute;
31 import org.apache.tools.ant.types.Commandline;
32
33
34 /** Base class for Perforce (P4) ANT tasks. See individual task for example usage.
35  *
36  * @see P4Sync
37  * @see P4Have
38  * @see P4Change
39  * @see P4Edit
40  * @see P4Submit
41  * @see P4Label
42  * @see org.apache.tools.ant.taskdefs.Execute
43  */

44 public abstract class P4Base extends org.apache.tools.ant.Task {
45     // CheckStyle:VisibilityModifier OFF - bc
46
// CheckStyle:MemberNameCheck OFF - bc
47
/**Perl5 regexp in Java - cool eh? */
48     protected Perl5Util util = null;
49     /** The OS shell to use (cmd.exe or /bin/sh) */
50     protected String JavaDoc shell;
51
52     //P4 runtime directives
53
/** Perforce Server Port (eg KM01:1666) */
54     protected String JavaDoc P4Port = "";
55     /** Perforce Client (eg myclientspec) */
56     protected String JavaDoc P4Client = "";
57     /** Perforce User (eg fbloggs) */
58     protected String JavaDoc P4User = "";
59     /** Perforce view for commands. (eg //projects/foobar/main/source/... )*/
60     protected String JavaDoc P4View = "";
61
62     // Perforce task directives
63
/** Keep going or fail on error - defaults to fail. */
64     protected boolean failOnError = true;
65
66     //P4 g-opts and cmd opts (rtfm)
67
/** Perforce 'global' opts.
68      * Forms half of low level API */

69     protected String JavaDoc P4Opts = "";
70     /** Perforce command opts.
71      * Forms half of low level API */

72     protected String JavaDoc P4CmdOpts = "";
73
74     /** Set by the task or a handler to indicate that the task has failed. BuildExceptions
75      * can also be thrown to indicate failure. */

76     private boolean inError = false;
77
78     /** If inError is set, then errorMessage needs to contain the reason why. */
79     private String JavaDoc errorMessage = "";
80
81     // CheckStyle:MemberNameCheck ON
82
// CheckStyle:VisibilityModifier ON
83

84     /**
85      * gets whether or not the task has encountered an error
86      * @return error flag
87      * @since ant 1.6
88      */

89     public boolean getInError() {
90         return inError;
91     }
92
93     /**
94      * sets the error flag on the task
95      * @param inError if true an error has been encountered by the handler
96      * @since ant 1.6
97      */

98     public void setInError(boolean inError) {
99         this.inError = inError;
100     }
101
102     /**
103      * gets the error message recorded by the Perforce handler
104      * @return error message
105      */

106     public String JavaDoc getErrorMessage() {
107         return errorMessage;
108     }
109
110     /**
111      * sets the error message
112      * @param errorMessage line of error output
113      */

114     public void setErrorMessage(String JavaDoc errorMessage) {
115         this.errorMessage = errorMessage;
116     }
117     //Setters called by Ant
118

119     /**
120      * The p4d server and port to connect to;
121      * optional, default "perforce:1666"
122      *
123      * @param p4Port the port one wants to set such as localhost:1666
124      */

125     public void setPort(String JavaDoc p4Port) {
126         this.P4Port = "-p" + p4Port;
127     }
128
129     /**
130      * The p4 client spec to use;
131      * optional, defaults to the current user
132      *
133      * @param p4Client the name of the Perforce client spec
134      */

135     public void setClient(String JavaDoc p4Client) {
136         this.P4Client = "-c" + p4Client;
137     }
138
139     /**
140      * The p4 username;
141      * optional, defaults to the current user
142      *
143      * @param p4User the user name
144      */

145     public void setUser(String JavaDoc p4User) {
146         this.P4User = "-u" + p4User;
147     }
148     /**
149      * Set global P4 options; Used on all
150      * of the Perforce tasks.
151      *
152      * @param p4Opts global options, to use a specific P4Config file for instance
153      */

154     public void setGlobalopts(String JavaDoc p4Opts) {
155         this.P4Opts = p4Opts;
156     }
157     /**
158      * The client, branch or label view to operate upon;
159      * optional default "//...".
160      *
161      * the view is required for the following tasks :
162      * <ul>
163      * <li>p4delete</li>
164      * <li>p4edit</li>
165      * <li>p4reopen</li>
166      * <li>p4resolve</li>
167      * </ul>
168      *
169      * @param p4View the view one wants to use
170      */

171     public void setView(String JavaDoc p4View) {
172         this.P4View = p4View;
173     }
174
175     /**
176      * Set extra command options; only used on some
177      * of the Perforce tasks.
178      *
179      * @param p4CmdOpts command line options going after the particular
180      * Perforce command
181      */

182     public void setCmdopts(String JavaDoc p4CmdOpts) {
183         this.P4CmdOpts = p4CmdOpts;
184     }
185
186     /**
187      * whether to stop the build (true, default)
188      * or keep going if an error is returned from the p4 command
189      * @param fail indicates whether one wants to fail the build if an error comes from the
190      * Perforce command
191      */

192     public void setFailonerror(boolean fail) {
193         failOnError = fail;
194     }
195     /**
196      * sets attributes Port, Client, User from properties
197      * if these properties are defined.
198      * Called automatically by UnknownElement
199      * @see org.apache.tools.ant.UnknownElement
200      * <table>
201      * <tr><th>Property</th><th>Attribute</th></tr>
202      * <tr><td>p4.port</td><td>Port</td></tr>
203      * <tr><td>p4.client</td><td>Client</td></tr>
204      * <tr><td>p4.user</td><td>User</td></tr>
205      * </table>
206      */

207     public void init() {
208
209         util = new Perl5Util();
210
211         //Get default P4 settings from environment - Mark would have done something cool with
212
//introspection here.....:-)
213
String JavaDoc tmpprop;
214         if ((tmpprop = getProject().getProperty("p4.port")) != null) {
215             setPort(tmpprop);
216         }
217         if ((tmpprop = getProject().getProperty("p4.client")) != null) {
218             setClient(tmpprop);
219         }
220         if ((tmpprop = getProject().getProperty("p4.user")) != null) {
221             setUser(tmpprop);
222         }
223     }
224     /**
225     * no usages found for this method
226     * runs a Perforce command without a handler
227     * @param command the command that one wants to execute
228     * @throws BuildException if failonerror is set and the command fails
229     */

230     protected void execP4Command(String JavaDoc command) throws BuildException {
231         execP4Command(command, null);
232     }
233
234     /**
235      * Execute P4 command assembled by subclasses.
236      *
237      * @param command The command to run
238      * @param handler A P4Handler to process any input and output
239      *
240      * @throws BuildException if failonerror has been set to true
241      */

242     protected void execP4Command(String JavaDoc command, P4Handler handler) throws BuildException {
243         try {
244             // reset error flags before executing the command
245
inError = false;
246             errorMessage = "";
247             Commandline commandline = new Commandline();
248             commandline.setExecutable("p4");
249
250             //Check API for these - it's how CVS does it...
251
if (P4Port != null && P4Port.length() != 0) {
252                 commandline.createArgument().setValue(P4Port);
253             }
254             if (P4User != null && P4User.length() != 0) {
255                 commandline.createArgument().setValue(P4User);
256             }
257             if (P4Client != null && P4Client.length() != 0) {
258                 commandline.createArgument().setValue(P4Client);
259             }
260             if (P4Opts != null && P4Opts.length() != 0) {
261                 commandline.createArgument().setLine(P4Opts);
262             }
263             commandline.createArgument().setLine(command);
264
265             log(commandline.describeCommand(), Project.MSG_VERBOSE);
266
267             if (handler == null) {
268                 handler = new SimpleP4OutputHandler(this);
269             }
270
271             Execute exe = new Execute(handler, null);
272
273             exe.setAntRun(getProject());
274
275             exe.setCommandline(commandline.getCommandline());
276
277             try {
278                 exe.execute();
279
280                 if (inError && failOnError) {
281                     throw new BuildException(errorMessage);
282                 }
283             } catch (IOException JavaDoc e) {
284                 throw new BuildException(e);
285             } finally {
286                 try {
287                     handler.stop();
288                 } catch (Exception JavaDoc e) {
289                     log(e.toString(), Project.MSG_ERR);
290                 }
291             }
292
293
294         } catch (Exception JavaDoc e) {
295             String JavaDoc failMsg = "Problem exec'ing P4 command: " + e.getMessage();
296             if (failOnError) {
297                 throw new BuildException(failMsg);
298             } else {
299                 log(failMsg, Project.MSG_ERR);
300             }
301
302         }
303     }
304 }
305
Popular Tags