1 17 18 19 package org.apache.catalina.ant; 20 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.PrintStream ; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.Task; 29 import org.apache.tools.ant.taskdefs.Redirector; 30 import org.apache.tools.ant.types.RedirectorElement; 31 32 33 52 53 public abstract class BaseRedirectorHelperTask extends Task { 54 55 57 58 protected Redirector redirector = new Redirector(this); 59 61 protected RedirectorElement redirectorElement = null; 62 63 protected OutputStream redirectOutStream = null; 64 65 protected OutputStream redirectErrStream = null; 66 67 PrintStream redirectOutPrintStream = null; 68 69 PrintStream redirectErrPrintStream = null; 70 71 80 protected boolean failOnError = true; 81 82 86 protected boolean redirectOutput = false; 87 88 92 protected boolean redirectorConfigured = false; 93 94 99 protected boolean alwaysLog = false; 100 101 106 public void setFailonerror(boolean fail) { 107 failOnError = fail; 108 } 109 110 114 public boolean isFailOnError() { 115 return failOnError; 116 } 117 118 119 124 public void setOutput(File out) { 125 redirector.setOutput(out); 126 redirectOutput = true; 127 } 128 129 135 public void setError(File error) { 136 redirector.setError(error); 137 redirectOutput = true; 138 } 139 140 148 public void setLogError(boolean logError) { 149 redirector.setLogError(logError); 150 redirectOutput = true; 151 } 152 153 160 public void setOutputproperty(String outputProperty) { 161 redirector.setOutputProperty(outputProperty); 162 redirectOutput = true; 163 } 164 165 172 public void setErrorProperty(String errorProperty) { 173 redirector.setErrorProperty(errorProperty); 174 redirectOutput = true; 175 } 176 177 183 public void setAppend(boolean append) { 184 redirector.setAppend(append); 185 redirectOutput = true; 186 } 187 188 197 public void setAlwaysLog(boolean alwaysLog) { 198 this.alwaysLog = alwaysLog; 199 redirectOutput = true; 201 } 202 203 208 public void setCreateEmptyFiles(boolean createEmptyFiles) { 209 redirector.setCreateEmptyFiles(createEmptyFiles); 210 redirectOutput = true; 211 } 212 213 217 public void addConfiguredRedirector(RedirectorElement redirectorElement) { 218 if (this.redirectorElement != null) { 219 throw new BuildException("Cannot have > 1 nested <redirector>s"); 220 } else { 221 this.redirectorElement = redirectorElement; 222 } 223 } 224 225 228 private void configureRedirector() { 229 if (redirectorElement != null) { 230 redirectorElement.configure(redirector); 231 redirectOutput = true; 232 } 233 238 redirectorConfigured = true; 239 } 240 241 244 protected void openRedirector() { 245 if (! redirectorConfigured) { 246 configureRedirector(); 247 } 248 if (redirectOutput) { 249 redirector.createStreams(); 250 redirectOutStream = redirector.getOutputStream(); 251 redirectOutPrintStream = new PrintStream (redirectOutStream); 252 redirectErrStream = redirector.getErrorStream(); 253 redirectErrPrintStream = new PrintStream (redirectErrStream); 254 } 255 } 256 257 263 protected void closeRedirector() { 264 try { 265 if (redirectOutput) { 266 redirector.complete(); 267 } 268 } catch (IOException ioe) { 269 log("Error closing redirector: " 270 + ioe.getMessage(), Project.MSG_ERR); 271 } 272 277 redirectOutStream = null; 278 redirectOutPrintStream = null; 279 redirectErrStream = null; 280 redirectErrPrintStream = null; 281 } 282 283 288 protected void handleOutput(String output) { 289 if (redirectOutput) { 290 if (redirectOutPrintStream == null) { 291 openRedirector(); 292 } 293 redirectOutPrintStream.println(output); 294 if (alwaysLog) { 295 log(output, Project.MSG_INFO); 296 } 297 } else { 298 log(output, Project.MSG_INFO); 299 } 300 } 301 302 308 protected void handleFlush(String output) { 309 handleOutput(output); 310 redirectOutPrintStream.flush(); 311 } 312 313 318 protected void handleErrorOutput(String output) { 319 if (redirectOutput) { 320 if (redirectErrPrintStream == null) { 321 openRedirector(); 322 } 323 redirectErrPrintStream.println(output); 324 if (alwaysLog) { 325 log(output, Project.MSG_ERR); 326 } 327 } else { 328 log(output, Project.MSG_ERR); 329 } 330 } 331 332 338 protected void handleErrorFlush(String output) { 339 handleErrorOutput(output); 340 redirectErrPrintStream.flush(); 341 } 342 343 349 protected void handleOutput(String output, int priority) { 350 if (priority == Project.MSG_ERR) { 351 handleErrorOutput(output); 352 } else { 353 handleOutput(output); 354 } 355 } 356 357 363 protected void handleFlush(String output, int priority) { 364 if (priority == Project.MSG_ERR) { 365 handleErrorFlush(output); 366 } else { 367 handleFlush(output); 368 } 369 } 370 371 } 372 | Popular Tags |