KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ant > BaseRedirectorHelperTask


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.catalina.ant;
20
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.PrintStream JavaDoc;
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 /**
34  * Abstract base class to add output redirection support for Catalina
35  * Ant tasks. These tasks require Ant 1.5 or later.
36  * <br>
37  * <strong>WARNING:</strong> due to depends chain, Ant could call a Task
38  * more than once and this can affect the output redirection when configured.
39  * If you are collecting the output in a property, it will collect the output
40  * of only the first run, since Ant properties are immutable and once created
41  * they cannot be changed.
42  * <br>
43  * If you are collecting output in a file the file will be overwritten with the
44  * output of the last run, unless you set append="true", in which case each run
45  * will append it's output to the file.
46  *
47  *
48  * @author Gabriele Garuglieri
49  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
50  * @since 5.5
51  */

52
53 public abstract class BaseRedirectorHelperTask extends Task {
54
55     // ------------------------------------------------------------- Properties
56

57     /** Redirector helper */
58     protected Redirector redirector = new Redirector(this);
59     //protected Redirector redirector = null;
60
/** Redirector element for this task */
61     protected RedirectorElement redirectorElement = null;
62     /** The stream for info output */
63     protected OutputStream JavaDoc redirectOutStream = null;
64     /** The stream for error output */
65     protected OutputStream JavaDoc redirectErrStream = null;
66     /** The print stream for info output */
67     PrintStream JavaDoc redirectOutPrintStream = null;
68     /** The print stream for error output */
69     PrintStream JavaDoc redirectErrPrintStream = null;
70         
71    /**
72      * Whether to fail (with a BuildException) if
73      * ManagerServlet returns an error. The default behavior is
74      * to do so.
75      * <b>
76      * This flag does not control parameters checking. If the task is called
77      * with wrong or invalid parameters, it will throw BuildException
78      * independently from the setting of this flag.
79      */

80     protected boolean failOnError = true;
81     
82     /**
83       * <code>true</code> true when output redirection is requested for this task .
84       * Default is to log on Ant log.
85       */

86     protected boolean redirectOutput = false;
87  
88     /**
89       * will be set to <code>true</code> when the configuration of the Redirector is
90       * complete.
91       */

92     protected boolean redirectorConfigured = false;
93
94     /**
95      * Flag which indicates that, if redirected, output should also be
96      * always sent to the log. Default is that otput is sent only to
97      * redirected streams.
98      */

99     protected boolean alwaysLog = false;
100
101     /**
102      * Whether to fail (with a BuildException) if
103      * ManagerServlet returns an error. The default behavior is
104      * to do so.
105      */

106     public void setFailonerror(boolean fail) {
107         failOnError = fail;
108     }
109
110     /**
111      * Returns the value of the failOnError
112      * property.
113      */

114     public boolean isFailOnError() {
115       return failOnError;
116     }
117         
118
119     /**
120      * File the output of the task is redirected to.
121      *
122      * @param out name of the output file
123      */

124     public void setOutput(File JavaDoc out) {
125         redirector.setOutput(out);
126         redirectOutput = true;
127     }
128
129     /**
130      * File the error output of the task is redirected to.
131      *
132      * @param error name of the error file
133      *
134      */

135     public void setError(File JavaDoc error) {
136         redirector.setError(error);
137         redirectOutput = true;
138     }
139
140     /**
141      * Controls whether error output is logged. This is only useful
142      * when output is being redirected and error output is desired in the
143      * Ant log
144      *
145      * @param logError if true the standard error is sent to the Ant log system
146      * and not sent to output stream.
147      */

148     public void setLogError(boolean logError) {
149         redirector.setLogError(logError);
150         redirectOutput = true;
151     }
152
153     /**
154      * Property name whose value should be set to the output of
155      * the task.
156      *
157      * @param outputProperty property name
158      *
159      */

160     public void setOutputproperty(String JavaDoc outputProperty) {
161         redirector.setOutputProperty(outputProperty);
162         redirectOutput = true;
163     }
164
165     /**
166      * Property name whose value should be set to the error of
167      * the task..
168      *
169      * @param errorProperty property name
170      *
171      */

172     public void setErrorProperty(String JavaDoc errorProperty) {
173         redirector.setErrorProperty(errorProperty);
174         redirectOutput = true;
175     }
176
177     /**
178      * If true, append output to existing file.
179      *
180      * @param append if true, append output to existing file
181      *
182      */

183     public void setAppend(boolean append) {
184         redirector.setAppend(append);
185         redirectOutput = true;
186     }
187
188     /**
189      * If true, (error and non-error) output will be redirected
190      * as specified while being sent to Ant's logging mechanism as if no
191      * redirection had taken place. Defaults to false.
192      * <br>
193      * Actually handled internally, with Ant 1.6.3 it will be handled by
194      * the <code>Redirector</code> itself.
195      * @param alwaysLog <code>boolean</code>
196      */

197     public void setAlwaysLog(boolean alwaysLog) {
198         this.alwaysLog = alwaysLog;
199         //redirector.setAlwaysLog(alwaysLog);
200
redirectOutput = true;
201     }
202
203     /**
204      * Whether output and error files should be created even when empty.
205      * Defaults to true.
206      * @param createEmptyFiles <CODE>boolean</CODE>.
207      */

208     public void setCreateEmptyFiles(boolean createEmptyFiles) {
209         redirector.setCreateEmptyFiles(createEmptyFiles);
210         redirectOutput = true;
211     }
212
213     /**
214      * Add a <CODE>RedirectorElement</CODE> to this task.
215      * @param redirectorElement <CODE>RedirectorElement</CODE>.
216      */

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     /**
226      * Set up properties on the Redirector from RedirectorElement if present.
227      */

228     private void configureRedirector() {
229         if (redirectorElement != null) {
230             redirectorElement.configure(redirector);
231             redirectOutput = true;
232         }
233         /*
234          * Due to depends chain, Ant could call the Task more than once,
235          * this is to prevent that we attempt to configure uselessly
236          * more than once the Redirector.
237          */

238         redirectorConfigured = true;
239     }
240
241     /**
242      * Set up properties on the Redirector and create output streams.
243      */

244     protected void openRedirector() {
245         if (! redirectorConfigured) {
246             configureRedirector();
247         }
248         if (redirectOutput) {
249             redirector.createStreams();
250             redirectOutStream = redirector.getOutputStream();
251             redirectOutPrintStream = new PrintStream JavaDoc(redirectOutStream);
252             redirectErrStream = redirector.getErrorStream();
253             redirectErrPrintStream = new PrintStream JavaDoc(redirectErrStream);
254         }
255    }
256
257     /**
258      * Ask redirector to close all the streams. It is necessary to call this method
259      * before leaving the Task to have the Streams flush their contents. If you are
260      * collecting output in a property, it will be created only if this method is
261      * called, otherwise you'll find it unset.
262      */

263     protected void closeRedirector() {
264         try {
265             if (redirectOutput) {
266                 redirector.complete();
267             }
268         } catch (IOException JavaDoc ioe) {
269             log("Error closing redirector: "
270                 + ioe.getMessage(), Project.MSG_ERR);
271         }
272         /*
273          * Due to depends chain, Ant could call the Task more than once,
274          * this is to prevent that we attempt to reuse the previuosly
275          * closed Streams.
276          */

277         redirectOutStream = null;
278         redirectOutPrintStream = null;
279         redirectErrStream = null;
280         redirectErrPrintStream = null;
281     }
282     
283     /**
284      * Handles output with the INFO priority.
285      *
286      * @param output The output to log. Should not be <code>null</code>.
287      */

288     protected void handleOutput(String JavaDoc 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     /**
303      * Handles output with the INFO priority and flushes the stream.
304      *
305      * @param output The output to log. Should not be <code>null</code>.
306      *
307      */

308     protected void handleFlush(String JavaDoc output) {
309         handleOutput(output);
310         redirectOutPrintStream.flush();
311     }
312
313     /**
314      * Handles error output with the ERR priority.
315      *
316      * @param output The error output to log. Should not be <code>null</code>.
317      */

318     protected void handleErrorOutput(String JavaDoc 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     /**
333      * Handles error output with the ERR priority and flushes the stream.
334      *
335      * @param output The error output to log. Should not be <code>null</code>.
336      *
337      */

338     protected void handleErrorFlush(String JavaDoc output) {
339         handleErrorOutput(output);
340         redirectErrPrintStream.flush();
341     }
342   
343     /**
344      * Handles output with ERR priority to error stream and all other
345      * pritorities to output stream.
346      *
347      * @param output The output to log. Should not be <code>null</code>.
348      */

349     protected void handleOutput(String JavaDoc output, int priority) {
350         if (priority == Project.MSG_ERR) {
351             handleErrorOutput(output);
352         } else {
353             handleOutput(output);
354         }
355     }
356   
357     /**
358      * Handles output with ERR priority to error stream and all other
359      * pritorities to output stream, then flushes the stream.
360      *
361      * @param output The output to log. Should not be <code>null</code>.
362      */

363     protected void handleFlush(String JavaDoc output, int priority) {
364         if (priority == Project.MSG_ERR) {
365             handleErrorFlush(output);
366         } else {
367             handleFlush(output);
368         }
369     }
370
371 }
372
Popular Tags