1 | /*
|
---|
2 | * The Apache Software License, Version 1.1
|
---|
3 | *
|
---|
4 | * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
|
---|
5 | * reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * 1. Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | *
|
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
15 | * notice, this list of conditions and the following disclaimer in
|
---|
16 | * the documentation and/or other materials provided with the
|
---|
17 | * distribution.
|
---|
18 | *
|
---|
19 | * 3. The end-user documentation included with the redistribution, if
|
---|
20 | * any, must include the following acknowlegement:
|
---|
21 | * "This product includes software developed by the
|
---|
22 | * Apache Software Foundation (http://www.apache.org/)."
|
---|
23 | * Alternately, this acknowlegement may appear in the software itself,
|
---|
24 | * if and wherever such third-party acknowlegements normally appear.
|
---|
25 | *
|
---|
26 | * 4. The names "The Jakarta Project", "Ant", and "Apache Software
|
---|
27 | * Foundation" must not be used to endorse or promote products derived
|
---|
28 | * from this software without prior written permission. For written
|
---|
29 | * permission, please contact [email protected].
|
---|
30 | *
|
---|
31 | * 5. Products derived from this software may not be called "Apache"
|
---|
32 | * nor may "Apache" appear in their names without prior written
|
---|
33 | * permission of the Apache Group.
|
---|
34 | *
|
---|
35 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
---|
36 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
37 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
38 | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
---|
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
41 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
---|
42 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
43 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
44 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
45 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
46 | * SUCH DAMAGE.
|
---|
47 | * ====================================================================
|
---|
48 | *
|
---|
49 | * This software consists of voluntary contributions made by many
|
---|
50 | * individuals on behalf of the Apache Software Foundation. For more
|
---|
51 | * information on the Apache Software Foundation, please see
|
---|
52 | * <http://www.apache.org/>.
|
---|
53 | */
|
---|
54 | package ise.antelope.tasks;
|
---|
55 |
|
---|
56 | import java.io.PrintWriter;
|
---|
57 | import java.io.StringWriter;
|
---|
58 | import java.util.Enumeration;
|
---|
59 | import java.util.Vector;
|
---|
60 | import org.apache.tools.ant.BuildException;
|
---|
61 | import org.apache.tools.ant.Task;
|
---|
62 | import org.apache.tools.ant.TaskContainer;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Try is a container task - it can contain other Ant tasks. The nested tasks
|
---|
66 | * are simply executed in sequence. Try's primary use is to support the
|
---|
67 | * try/catch-like execution of a set of tasks. If any of the child tasks fail
|
---|
68 | * (that is, they throw a build exception), the exception is caught and the
|
---|
69 | * build can continue. This is sometimes useful for tasks that can fail, but it
|
---|
70 | * isn't necessary to fail the build if they do. For example, the "mail" task
|
---|
71 | * may fail if the server is unavailable, but not sending the message may not be
|
---|
72 | * critical to the build continuing.
|
---|
73 | *
|
---|
74 | * @author Dale Anson, [email protected]
|
---|
75 | * @version $Revision: 1.1 $
|
---|
76 | * @since Ant 1.5
|
---|
77 | * @ant.task category="control"
|
---|
78 | */
|
---|
79 | public class TryTask extends Task implements TaskContainer {
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Vector to hold the nested tasks
|
---|
83 | */
|
---|
84 | private Vector tasks = new Vector();
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * support for a nested CatchTask
|
---|
88 | */
|
---|
89 | private Task catchTask = null;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * support for a nested FinallyTask
|
---|
93 | */
|
---|
94 | private Task finallyTask = null;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * should the try block exit on the first failure? Default is true.
|
---|
98 | */
|
---|
99 | private boolean doBreak = true;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * should the error message of an exception be logged? Default is true.
|
---|
103 | */
|
---|
104 | private boolean doPrintMessage = true;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * the error message of the exception can be stored as a property
|
---|
108 | */
|
---|
109 | private String messageProperty = null;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * should the stack trace of an exception be logged? Default is false.
|
---|
113 | */
|
---|
114 | private boolean doPrintStackTrace = false;
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * the stack trace can be stored as a property
|
---|
119 | */
|
---|
120 | private String stackTraceProperty = null;
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * make sure dependent tasks are loaded
|
---|
125 | */
|
---|
126 | public void init() {
|
---|
127 | getProject().addTaskDefinition( "catch", CatchTask.class );
|
---|
128 | getProject().addTaskDefinition( "finally", FinallyTask.class );
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Override {@link org.apache.tools.ant.Task#maybeConfigure maybeConfigure}
|
---|
134 | * in a way that leaves the nested tasks unconfigured until they get
|
---|
135 | * executed.
|
---|
136 | *
|
---|
137 | * @exception BuildException Description of Exception
|
---|
138 | * @since Ant 1.5
|
---|
139 | */
|
---|
140 | public void maybeConfigure() throws BuildException {
|
---|
141 | if ( isInvalid() ) {
|
---|
142 | super.maybeConfigure();
|
---|
143 | } else {
|
---|
144 | getRuntimeConfigurableWrapper().maybeConfigure( getProject(), false );
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Add a nested task to Try.
|
---|
151 | *
|
---|
152 | * @param task Nested task to try to execute
|
---|
153 | */
|
---|
154 | public void addTask( Task task ) {
|
---|
155 | if ( task instanceof CatchTask ) {
|
---|
156 | if ( catchTask == null ) {
|
---|
157 | catchTask = task;
|
---|
158 | return ;
|
---|
159 | } else {
|
---|
160 | throw new BuildException( "Only one Catch allowed per Try." );
|
---|
161 | }
|
---|
162 | } else if ( task instanceof FinallyTask ) {
|
---|
163 | if ( finallyTask == null ) {
|
---|
164 | finallyTask = task;
|
---|
165 | return ;
|
---|
166 | } else {
|
---|
167 | throw new BuildException( "Only one Finally allowed per Try." );
|
---|
168 | }
|
---|
169 | }
|
---|
170 | tasks.addElement( task );
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void addCatch( CatchTask task ) {
|
---|
174 | addTask( task );
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void addFinally( FinallyTask task ) {
|
---|
178 | addTask( task );
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * A try block may contain several tasks. This parameter determines whether
|
---|
183 | * the block should continue executing tasks following a failed task. The
|
---|
184 | * default is false, and the try block will exit on the first failure. Note
|
---|
185 | * that if set to false and more than one task fails, the "catch" target will
|
---|
186 | * execute for each failed task.
|
---|
187 | *
|
---|
188 | * @param b if set to false, the try block will execute all tasks in the
|
---|
189 | * block, regardless of failure of an individual task.
|
---|
190 | */
|
---|
191 | public void setBreak( boolean b ) {
|
---|
192 | doBreak = b;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * If printstacktrace is set to true, this is ignored as the error message is
|
---|
198 | * printed as part of the stack trace. Default is to print the message.
|
---|
199 | *
|
---|
200 | * @param b Should the error message of a failed task be logged?
|
---|
201 | */
|
---|
202 | public void setPrintmessage( boolean b ) {
|
---|
203 | doPrintMessage = b;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * the error message of the exception can be stored as a property
|
---|
209 | */
|
---|
210 | public void setMessageproperty(String name) {
|
---|
211 | messageProperty = name;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Default is to not print the stack trace.
|
---|
217 | *
|
---|
218 | * @param b Should the stack trace of a failed task be logged?
|
---|
219 | */
|
---|
220 | public void setPrintstacktrace( boolean b ) {
|
---|
221 | doPrintStackTrace = b;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * the stack trace can be stored as a property
|
---|
226 | */
|
---|
227 | public void setStacktraceproperty( String name ) {
|
---|
228 | stackTraceProperty = name;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Try to execute all tasks.
|
---|
234 | *
|
---|
235 | * @exception BuildException Description of Exception
|
---|
236 | */
|
---|
237 | public void execute() throws BuildException {
|
---|
238 | Throwable be = null;
|
---|
239 | for ( Enumeration e = tasks.elements(); e.hasMoreElements(); ) {
|
---|
240 | Task task = ( Task ) e.nextElement();
|
---|
241 | try {
|
---|
242 | task.perform();
|
---|
243 | } catch ( Throwable throwable ) {
|
---|
244 | be = throwable;
|
---|
245 | if (messageProperty != null){
|
---|
246 | getProject().setProperty(messageProperty, throwable.getMessage());
|
---|
247 | }
|
---|
248 | if (stackTraceProperty != null) {
|
---|
249 | StringWriter stacktrace = new StringWriter();
|
---|
250 | PrintWriter writer = new PrintWriter( stacktrace, true );
|
---|
251 | throwable.printStackTrace( writer );
|
---|
252 | getProject().setProperty( stackTraceProperty, stacktrace.toString() );
|
---|
253 | }
|
---|
254 | if ( doPrintStackTrace ) {
|
---|
255 | try {
|
---|
256 | // log a message
|
---|
257 | log( "Task '" + task.getTaskName() + "' in target '" +
|
---|
258 | ( task.getOwningTarget() == null ?
|
---|
259 | "unknown" :
|
---|
260 | task.getOwningTarget().getName() ) +
|
---|
261 | "' failed, task stack trace follows:" );
|
---|
262 |
|
---|
263 | // send the stack trace to the log
|
---|
264 | StringWriter stacktrace = new StringWriter();
|
---|
265 | PrintWriter writer = new PrintWriter( stacktrace, true );
|
---|
266 | throwable.printStackTrace( writer );
|
---|
267 | log( stacktrace.toString() );
|
---|
268 | } catch ( Exception ignored ) {
|
---|
269 | // don't fail on any exception
|
---|
270 | }
|
---|
271 | }
|
---|
272 | else if ( doPrintMessage ) {
|
---|
273 | try {
|
---|
274 | // log a message
|
---|
275 | log( "Task '" + task.getTaskName() + "' in target '" +
|
---|
276 | ( task.getOwningTarget() == null ?
|
---|
277 | "unknown" :
|
---|
278 | task.getOwningTarget().getName() ) +
|
---|
279 | "' failed, error message is: " + throwable.getMessage() );
|
---|
280 | } catch ( Exception ignored ) {
|
---|
281 | // don't fail on any exception
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | // check if there is a nested CatchTask to execute. If the tasks
|
---|
286 | // in the catch throw an exception, catch it so the 'finally' can
|
---|
287 | // execute.
|
---|
288 |
|
---|
289 | if ( catchTask != null ) {
|
---|
290 | try {
|
---|
291 | catchTask.perform();
|
---|
292 | } catch ( Throwable t ) {
|
---|
293 | if ( finallyTask != null ) {
|
---|
294 | try {
|
---|
295 | finallyTask.perform();
|
---|
296 | } catch ( Exception eeee ) {
|
---|
297 | throw new BuildException( be.getMessage(), new BuildException( eeee.getMessage(), t ) );
|
---|
298 | }
|
---|
299 | } else {
|
---|
300 | throw new BuildException( be.getMessage(), t );
|
---|
301 | }
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | // check if remaining tasks should execute
|
---|
306 | if ( doBreak ) {
|
---|
307 | break;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | // do the finally
|
---|
313 | if ( finallyTask != null ) {
|
---|
314 | finallyTask.perform();
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|