1 8 9 package foxtrot; 10 11 import java.security.AccessControlContext ; 12 import java.security.AccessController ; 13 14 import javax.swing.SwingUtilities ; 15 16 36 public abstract class Task 37 { 38 private Object result; 39 private Throwable throwable; 40 private boolean completed; 41 private AccessControlContext securityContext; 42 43 46 protected Task() 47 { 48 securityContext = AccessController.getContext(); 49 } 50 51 56 public abstract Object run() throws Exception ; 57 58 66 protected final synchronized Object getResultOrThrow() throws Exception 67 { 68 Throwable t = getThrowable(); 69 if (t != null) 70 { 71 if (t instanceof Exception ) 72 throw (Exception )t; 73 else 74 throw (Error )t; 75 } 76 return getResult(); 77 } 78 79 85 private final synchronized Object getResult() 86 { 87 return result; 88 } 89 90 98 final synchronized void setResult(Object result) 99 { 100 this.result = result; 101 } 102 103 108 final synchronized Throwable getThrowable() 109 { 110 return throwable; 111 } 112 113 120 final synchronized void setThrowable(Throwable x) 121 { 122 throwable = x; 123 } 124 125 128 public final synchronized boolean isCompleted() 129 { 130 return completed; 133 } 134 135 142 final synchronized void setCompleted(boolean value) 143 { 144 completed = value; 145 if (value) notifyAll(); 146 } 147 148 155 final synchronized AccessControlContext getSecurityContext() 156 { 157 return securityContext; 158 } 159 160 167 final synchronized void reset() 168 { 169 setResult(null); 170 setThrowable(null); 171 setCompleted(false); 172 } 173 174 178 void postRun() 179 { 180 SwingUtilities.invokeLater(new Runnable () 186 { 187 public final void run() 188 { 189 if (AbstractWorker.debug) System.out.println("[Task] Run completed for task " + Task.this); 190 } 191 }); 192 } 193 } 194 | Popular Tags |