1 21 package fr.dyade.aaa.agent; 22 23 import java.io.*; 24 import java.util.*; 25 import fr.dyade.aaa.util.*; 26 27 import org.objectweb.util.monolog.api.BasicLevel; 28 import org.objectweb.util.monolog.api.Logger; 29 30 33 class ThreadFinalizer implements Runnable { 34 35 36 Driver driver; 37 38 43 ThreadFinalizer(Driver driver) { 44 this.driver = driver; 45 } 46 47 51 public void run() { 52 try { 53 driver.logmon.log(BasicLevel.DEBUG, 54 driver.getName() + " start"); 55 driver.run(); 56 driver.canStop = false; 57 } catch (ThreadDeath death) { 58 throw death; 60 } catch (Throwable exc) { 61 driver.canStop = false; 62 driver.logmon.log(BasicLevel.ERROR, 63 driver.getName() + " failed", exc); 64 } finally { 65 driver.canStop = false; 66 driver.reset(); 67 if (!Thread.interrupted()) { 68 driver.logmon.log(BasicLevel.DEBUG, driver.getName() + "end"); 69 } 70 driver.end(); 71 } 72 } 73 } 74 75 76 103 public abstract class Driver { 104 105 106 protected Thread thread; 107 108 protected int id; 109 113 public volatile boolean isRunning = false; 114 public volatile boolean canStop = false; 115 116 protected Logger logmon = null; 117 protected String name = null; 118 119 protected static Hashtable drivers = new Hashtable(); 120 121 126 protected Driver(int id) { 127 thread = null; 128 this.id = id; 129 isRunning = true; 130 131 String classname = getClass().getName(); 134 logmon = Debug.getLogger(Debug.A3Proxy + '.' + 135 classname.substring(classname.lastIndexOf('.') +1)); 136 137 this.name = classname + '#' + id; 138 } 139 140 143 protected Driver() { 144 this(0); 145 isRunning =true; 146 } 147 148 152 public String getName() { 153 return name; 154 } 155 156 161 public String toString() { 162 return "(" + getClass().getName() + 163 ",name=" + getName() + 164 ",id=" + id + ")" + 165 ",isRunning=" + isRunning + 166 ",canStop=" + canStop; 167 } 168 169 181 public abstract void run() throws Exception ; 182 183 187 protected void end() {} 188 189 192 public void start() { 193 thread = new Thread (new ThreadFinalizer(this), getName()); 194 thread.setDaemon(true); 195 drivers.put(this, this); 196 thread.start(); 197 } 198 199 202 synchronized void reset() { 203 thread = null; 204 drivers.remove(this); 205 } 206 207 210 public synchronized void stop() { 211 if (thread == null) return; 212 isRunning = false; 213 if (canStop) { 214 thread.interrupt(); 215 close(); 216 } 217 thread = null; 218 } 219 220 public abstract void close(); 221 222 static void stopAll() { 223 Logger logmon = Debug.getLogger(Debug.A3Proxy); 224 if ((drivers == null) || (drivers.size() == 0)) return; 225 Driver[] tab = (Driver[]) (drivers.values().toArray(new Driver[0])); 226 for (int i=0; i<tab.length; i++) { 227 logmon.log(BasicLevel.WARN, tab[i].getName() + " " + tab[i].isRunning + "/" + tab[i].canStop + " stop()"); 228 tab[i].stop(); 229 } 230 } 231 232 242 protected final void sendTo(AgentId to, Notification not) throws IOException { 243 Channel.sendTo(to, not); 244 } 245 } 246 | Popular Tags |