1 18 23 24 package org.apache.tools.ant.taskdefs.optional.perforce; 25 26 import java.io.IOException ; 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 44 public abstract class P4Base extends org.apache.tools.ant.Task { 45 48 protected Perl5Util util = null; 49 50 protected String shell; 51 52 54 protected String P4Port = ""; 55 56 protected String P4Client = ""; 57 58 protected String P4User = ""; 59 60 protected String P4View = ""; 61 62 64 protected boolean failOnError = true; 65 66 69 protected String P4Opts = ""; 70 72 protected String P4CmdOpts = ""; 73 74 76 private boolean inError = false; 77 78 79 private String errorMessage = ""; 80 81 84 89 public boolean getInError() { 90 return inError; 91 } 92 93 98 public void setInError(boolean inError) { 99 this.inError = inError; 100 } 101 102 106 public String getErrorMessage() { 107 return errorMessage; 108 } 109 110 114 public void setErrorMessage(String errorMessage) { 115 this.errorMessage = errorMessage; 116 } 117 119 125 public void setPort(String p4Port) { 126 this.P4Port = "-p" + p4Port; 127 } 128 129 135 public void setClient(String p4Client) { 136 this.P4Client = "-c" + p4Client; 137 } 138 139 145 public void setUser(String p4User) { 146 this.P4User = "-u" + p4User; 147 } 148 154 public void setGlobalopts(String p4Opts) { 155 this.P4Opts = p4Opts; 156 } 157 171 public void setView(String p4View) { 172 this.P4View = p4View; 173 } 174 175 182 public void setCmdopts(String p4CmdOpts) { 183 this.P4CmdOpts = p4CmdOpts; 184 } 185 186 192 public void setFailonerror(boolean fail) { 193 failOnError = fail; 194 } 195 207 public void init() { 208 209 util = new Perl5Util(); 210 211 String 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 230 protected void execP4Command(String command) throws BuildException { 231 execP4Command(command, null); 232 } 233 234 242 protected void execP4Command(String command, P4Handler handler) throws BuildException { 243 try { 244 inError = false; 246 errorMessage = ""; 247 Commandline commandline = new Commandline(); 248 commandline.setExecutable("p4"); 249 250 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 e) { 284 throw new BuildException(e); 285 } finally { 286 try { 287 handler.stop(); 288 } catch (Exception e) { 289 log(e.toString(), Project.MSG_ERR); 290 } 291 } 292 293 294 } catch (Exception e) { 295 String 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 |