01: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.utils.threading;
07:
08: /**
09: * A convenience base task class for subclasses.
10: * Introduces an execute() method which declares <code>throws Exception</code>,
11: * which subclasses may override. Any Exception thrown from <code>execute</code>
12: * will be caught and stored, to be accessible through <code>getException</code>
13: * NOTE: Throwable is not caught to avoid catching <code>Error</code>s
14: * @author Aaron Hamid (arh14 at cornell dot edu)
15: */
16:
17: public abstract class BaseTask implements Task {
18: protected Exception exception;
19:
20: /**
21: * Run implementation which delegates to <code>execute()</code>.
22: * Catches and stores any exception <code>execute()</code> throws.
23: * @see #execute()
24: */
25: public void run() {
26: try {
27: execute();
28: } catch (Exception e) {
29: setException(e);
30: }
31: }
32:
33: /**
34: * Only subclasses are allowed to use this
35: * @param e exception to set
36: */
37: protected void setException(Exception e) {
38: this .exception = e;
39: }
40:
41: /**
42: * Subclasses should implement this method
43: * NOTE: not declaring throws Throwable. We shouldn't really catch Errors, should we?
44: * @throws Exception
45: */
46: public abstract void execute() throws Exception;
47:
48: /**
49: * Returns the exception that was thrown during execution, if any
50: * @return the exception that was thrown during execution, if any
51: */
52: public Exception getException() {
53: return this.exception;
54: }
55: }
|