1 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 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 property = null; 156 private String reference = null; 157 158 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 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 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 195 public void setProperty(String p) { 196 property = p; 197 } 198 199 202 public void setReference(String r) { 203 reference = r; 204 } 205 206 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 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 |