KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > antcontrib > logic > TryCatchTask


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 Ant-Contrib project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The name Ant-Contrib must not be used to endorse or promote products
26  * derived from this software without prior written permission. For
27  * written permission, please contact
28  * ant-contrib-developers@lists.sourceforge.net.
29  *
30  * 5. Products derived from this software may not be called "Ant-Contrib"
31  * nor may "Ant-Contrib" appear in their names without prior written
32  * permission of the Ant-Contrib project.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS
38  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  * ====================================================================
47  */

48 package net.sf.antcontrib.logic;
49
50 import org.apache.tools.ant.BuildException;
51 import org.apache.tools.ant.Project;
52 import org.apache.tools.ant.Task;
53 import org.apache.tools.ant.taskdefs.Sequential;
54
55 /**
56  * A wrapper that lets you run a set of tasks and optionally run a
57  * different set of tasks if the first set fails and yet another set
58  * after the first one has finished.
59  *
60  * <p>This mirrors Java's try/catch/finally.</p>
61  *
62  * <p>The tasks inside of the required <code>&lt;try&gt;</code>
63  * element will be run. If one of them should throw a {@link
64  * org.apache.tools.ant.BuildException BuildException} several things
65  * can happen:</p>
66  *
67  * <ul>
68  * <li>If there is no <code>&lt;catch&gt;</code> block, the
69  * exception will be passed through to Ant.</li>
70  *
71  * <li>If the property attribute has been set, a property of the
72  * given name will be set to the message of the exception.</li>
73  *
74  * <li>If the reference attribute has been set, a reference of the
75  * given id will be created and point to the exception object.</li>
76  *
77  * <li>If there is a <code>&lt;catch&gt;</code> block, the tasks
78  * nested into it will be run.</li>
79  * </ul>
80  *
81  * <p>If a <code>&lt;finally&gt;</code> block is present, the task
82  * nested into it will be run, no matter whether the first tasks have
83  * thrown an exception or not.</p>
84  *
85  * <p><strong>Attributes:</strong></p>
86  *
87  * <table>
88  * <tr>
89  * <td>Name</td>
90  * <td>Description</td>
91  * <td>Required</td>
92  * </tr>
93  * <tr>
94  * <td>property</td>
95  * <td>Name of a property that will receive the message of the
96  * exception that has been caught (if any)</td>
97  * <td>No</td>
98  * </tr>
99  * <tr>
100  * <td>reference</td>
101  * <td>Id of a reference that will point to the exception object
102  * that has been caught (if any)</td>
103  * <td>No</td>
104  * </tr>
105  * </table>
106  *
107  * <p>Use the following task to define the <code>&lt;trycatch&gt;</code>
108  * task before you use it the first time:</p>
109  *
110  * <pre><code>
111  * &lt;taskdef name="trycatch"
112  * classname="net.sf.antcontrib.logic.TryCatchTask" /&gt;
113  * </code></pre>
114  *
115  * <h3>Crude Example</h3>
116  *
117  * <pre><code>
118  * &lt;trycatch property=&quot;foo&quot; reference=&quot;bar&quot;&gt;
119  * &lt;try&gt;
120  * &lt;fail&gt;Tada!&lt;/fail&gt;
121  * &lt;/try&gt;
122  *
123  * &lt;catch&gt;
124  * &lt;echo&gt;In &amp;lt;catch&amp;gt;.&lt;/echo&gt;
125  * &lt;/catch&gt;
126  *
127  * &lt;finally&gt;
128  * &lt;echo&gt;In &amp;lt;finally&amp;gt;.&lt;/echo&gt;
129  * &lt;/finally&gt;
130  * &lt;/trycatch&gt;
131  *
132  * &lt;echo&gt;As property: ${foo}&lt;/echo&gt;
133  * &lt;property name=&quot;baz&quot; refid=&quot;bar&quot; /&gt;
134  * &lt;echo&gt;From reference: ${baz}&lt;/echo&gt;
135  * </code></pre>
136  *
137  * <p>results in</p>
138  *
139  * <pre><code>
140  * [trycatch] Caught exception: Tada!
141  * [echo] In &lt;catch&gt;.
142  * [echo] In &lt;finally&gt;.
143  * [echo] As property: Tada!
144  * [echo] From reference: Tada!
145  * </code></pre>
146  *
147  * @author <a HREF="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
148  * @author <a HREF="mailto:RITCHED2@Nationwide.com">Dan Ritchey</a>
149  */

150 public class TryCatchTask extends Task {
151
152     private Sequential tryTasks = null;
153     private Sequential catchTasks = null;
154     private Sequential finallyTasks = null;
155     private String JavaDoc property = null;
156     private String JavaDoc reference = null;
157
158     /**
159      * Adds a nested &lt;try&gt; block - one is required, more is
160      * forbidden.
161      */

162     public void addTry(Sequential seq) throws BuildException {
163         if (tryTasks != null) {
164             throw new BuildException("You must not specify more than one <try>");
165         }
166         
167         tryTasks = seq;
168     }
169
170     /**
171      * Adds a nested &lt;catch&gt; block - at most one is allowed.
172      */

173     public void addCatch(Sequential seq) throws BuildException {
174         if (catchTasks != null) {
175             throw new BuildException("You must not specify more than one <catch>");
176         }
177         
178         catchTasks = seq;
179     }
180
181     /**
182      * Adds a nested &lt;finally&gt; block - at most one is allowed.
183      */

184     public void addFinally(Sequential seq) throws BuildException {
185         if (finallyTasks != null) {
186             throw new BuildException("You must not specify more than one <finally>");
187         }
188         
189         finallyTasks = seq;
190     }
191
192     /**
193      * Sets the property attribute.
194      */

195     public void setProperty(String JavaDoc p) {
196         property = p;
197     }
198
199     /**
200      * Sets the reference attribute.
201      */

202     public void setReference(String JavaDoc r) {
203         reference = r;
204     }
205
206     /**
207      * The heart of the task.
208      */

209     public void execute() throws BuildException {
210         if (tryTasks == null) {
211             throw new BuildException("A nested <try> element is required");
212         }
213
214         try {
215             tryTasks.perform();
216         } catch (BuildException e) {
217             if (property != null) {
218                 /*
219                  * Using setProperty instead of setNewProperty to
220                  * be able to compile with Ant < 1.5.
221                  */

222                 project.setProperty(property, e.getMessage());
223             }
224             
225             if (reference != null) {
226                 project.addReference(reference, e);
227             }
228             
229             if (catchTasks == null) {
230                 throw e;
231             } else {
232                 log("Caught exception: "+e.getMessage(), Project.MSG_INFO);
233                 catchTasks.perform();
234             }
235         } finally {
236             if (finallyTasks != null) {
237                 finallyTasks.perform();
238             }
239         }
240     }
241
242 }
243
Popular Tags