| 1 | /* |
| 2 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package net.sf.antcontrib.logic; |
| 17 | |
| 18 | import java.util.Enumeration; |
| 19 | import java.util.Vector; |
| 20 | |
| 21 | import org.apache.tools.ant.BuildException; |
| 22 | import org.apache.tools.ant.Task; |
| 23 | import org.apache.tools.ant.taskdefs.Sequential; |
| 24 | |
| 25 | /** |
| 26 | * A wrapper that lets you run a set of tasks and optionally run a |
| 27 | * different set of tasks if the first set fails and yet another set |
| 28 | * after the first one has finished. |
| 29 | * |
| 30 | * <p>This mirrors Java's try/catch/finally.</p> |
| 31 | * |
| 32 | * <p>The tasks inside of the required <code><try></code> |
| 33 | * element will be run. If one of them should throw a {@link |
| 34 | * org.apache.tools.ant.BuildException BuildException} several things |
| 35 | * can happen:</p> |
| 36 | * |
| 37 | * <ul> |
| 38 | * <li>If there is no <code><catch></code> block, the |
| 39 | * exception will be passed through to Ant.</li> |
| 40 | * |
| 41 | * <li>If the property attribute has been set, a property of the |
| 42 | * given name will be set to the message of the exception.</li> |
| 43 | * |
| 44 | * <li>If the reference attribute has been set, a reference of the |
| 45 | * given id will be created and point to the exception object.</li> |
| 46 | * |
| 47 | * <li>If there is a <code><catch></code> block, the tasks |
| 48 | * nested into it will be run.</li> |
| 49 | * </ul> |
| 50 | * |
| 51 | * <p>If a <code><finally></code> block is present, the task |
| 52 | * nested into it will be run, no matter whether the first tasks have |
| 53 | * thrown an exception or not.</p> |
| 54 | * |
| 55 | * <p><strong>Attributes:</strong></p> |
| 56 | * |
| 57 | * <table> |
| 58 | * <tr> |
| 59 | * <td>Name</td> |
| 60 | * <td>Description</td> |
| 61 | * <td>Required</td> |
| 62 | * </tr> |
| 63 | * <tr> |
| 64 | * <td>property</td> |
| 65 | * <td>Name of a property that will receive the message of the |
| 66 | * exception that has been caught (if any)</td> |
| 67 | * <td>No</td> |
| 68 | * </tr> |
| 69 | * <tr> |
| 70 | * <td>reference</td> |
| 71 | * <td>Id of a reference that will point to the exception object |
| 72 | * that has been caught (if any)</td> |
| 73 | * <td>No</td> |
| 74 | * </tr> |
| 75 | * </table> |
| 76 | * |
| 77 | * <p>Use the following task to define the <code><trycatch></code> |
| 78 | * task before you use it the first time:</p> |
| 79 | * |
| 80 | * <pre><code> |
| 81 | * <taskdef name="trycatch" |
| 82 | * classname="net.sf.antcontrib.logic.TryCatchTask" /> |
| 83 | * </code></pre> |
| 84 | * |
| 85 | * <h3>Crude Example</h3> |
| 86 | * |
| 87 | * <pre><code> |
| 88 | * <trycatch property="foo" reference="bar"> |
| 89 | * <try> |
| 90 | * <fail>Tada!</fail> |
| 91 | * </try> |
| 92 | * |
| 93 | * <catch> |
| 94 | * <echo>In &lt;catch&gt;.</echo> |
| 95 | * </catch> |
| 96 | * |
| 97 | * <finally> |
| 98 | * <echo>In &lt;finally&gt;.</echo> |
| 99 | * </finally> |
| 100 | * </trycatch> |
| 101 | * |
| 102 | * <echo>As property: ${foo}</echo> |
| 103 | * <property name="baz" refid="bar" /> |
| 104 | * <echo>From reference: ${baz}</echo> |
| 105 | * </code></pre> |
| 106 | * |
| 107 | * <p>results in</p> |
| 108 | * |
| 109 | * <pre><code> |
| 110 | * [trycatch] Caught exception: Tada! |
| 111 | * [echo] In <catch>. |
| 112 | * [echo] In <finally>. |
| 113 | * [echo] As property: Tada! |
| 114 | * [echo] From reference: Tada! |
| 115 | * </code></pre> |
| 116 | * |
| 117 | * @author <a href="mailto:[email protected]">Stefan Bodewig</a> |
| 118 | * @author <a href="mailto:[email protected]">Dan Ritchey</a> |
| 119 | */ |
| 120 | public class TryCatchTask extends Task { |
| 121 | |
| 122 | public static final class CatchBlock extends Sequential { |
| 123 | private String throwable = BuildException.class.getName(); |
| 124 | |
| 125 | public CatchBlock() { |
| 126 | super(); |
| 127 | } |
| 128 | |
| 129 | public void setThrowable(String throwable) { |
| 130 | this.throwable = throwable; |
| 131 | } |
| 132 | |
| 133 | public boolean execute(Throwable t) throws BuildException { |
| 134 | try { |
| 135 | Class c = Thread.currentThread().getContextClassLoader().loadClass(throwable); |
| 136 | if (c.isAssignableFrom(t.getClass())) { |
| 137 | execute(); |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | catch (ClassNotFoundException e) { |
| 143 | throw new BuildException(e); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | private Sequential tryTasks = null; |
| 150 | private Vector catchBlocks = new Vector(); |
| 151 | private Sequential finallyTasks = null; |
| 152 | private String property = null; |
| 153 | private String reference = null; |
| 154 | |
| 155 | /** |
| 156 | * Adds a nested <try> block - one is required, more is |
| 157 | * forbidden. |
| 158 | */ |
| 159 | public void addTry(Sequential seq) throws BuildException { |
| 160 | if (tryTasks != null) { |
| 161 | throw new BuildException("You must not specify more than one <try>"); |
| 162 | } |
| 163 | |
| 164 | tryTasks = seq; |
| 165 | } |
| 166 | |
| 167 | public void addCatch(CatchBlock cb) { |
| 168 | catchBlocks.add(cb); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Adds a nested <finally> block - at most one is allowed. |
| 173 | */ |
| 174 | public void addFinally(Sequential seq) throws BuildException { |
| 175 | if (finallyTasks != null) { |
| 176 | throw new BuildException("You must not specify more than one <finally>"); |
| 177 | } |
| 178 | |
| 179 | finallyTasks = seq; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets the property attribute. |
| 184 | */ |
| 185 | public void setProperty(String p) { |
| 186 | property = p; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets the reference attribute. |
| 191 | */ |
| 192 | public void setReference(String r) { |
| 193 | reference = r; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * The heart of the task. |
| 198 | */ |
| 199 | public void execute() throws BuildException { |
| 200 | Throwable thrown = null; |
| 201 | |
| 202 | if (tryTasks == null) { |
| 203 | throw new BuildException("A nested <try> element is required"); |
| 204 | } |
| 205 | |
| 206 | try { |
| 207 | tryTasks.perform(); |
| 208 | } catch (Throwable e) { |
| 209 | if (property != null) { |
| 210 | /* |
| 211 | * Using setProperty instead of setNewProperty to |
| 212 | * be able to compile with Ant < 1.5. |
| 213 | */ |
| 214 | getProject().setProperty(property, e.getMessage()); |
| 215 | } |
| 216 | |
| 217 | if (reference != null) { |
| 218 | getProject().addReference(reference, e); |
| 219 | } |
| 220 | |
| 221 | boolean executed = false; |
| 222 | Enumeration blocks = catchBlocks.elements(); |
| 223 | while (blocks.hasMoreElements() && ! executed) { |
| 224 | CatchBlock cb = (CatchBlock)blocks.nextElement(); |
| 225 | executed = cb.execute(e); |
| 226 | } |
| 227 | |
| 228 | if (! executed) { |
| 229 | thrown = e; |
| 230 | } |
| 231 | } finally { |
| 232 | if (finallyTasks != null) { |
| 233 | finallyTasks.perform(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (thrown != null) { |
| 238 | throw new BuildException(thrown); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | } |