1 28 29 package com.caucho.jca; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.loader.CloseListener; 33 import com.caucho.loader.DynamicClassLoader; 34 import com.caucho.loader.Environment; 35 import com.caucho.loader.EnvironmentLocal; 36 import com.caucho.log.Log; 37 import com.caucho.util.L10N; 38 39 import javax.naming.Context ; 40 import javax.naming.InitialContext ; 41 import javax.resource.spi.BootstrapContext ; 42 import javax.resource.spi.ResourceAdapter ; 43 import javax.resource.spi.XATerminator ; 44 import javax.resource.spi.work.WorkManager ; 45 import java.lang.ref.SoftReference ; 46 import java.util.ArrayList ; 47 import java.util.Timer ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 54 public class ResourceManagerImpl implements BootstrapContext { 55 private static final L10N L = new L10N(ResourceManagerImpl.class); 56 private static final Logger log = Log.open(ResourceManagerImpl.class); 57 58 private static EnvironmentLocal<ResourceManagerImpl> _localManager = 59 new EnvironmentLocal<ResourceManagerImpl>(); 60 61 private UserTransactionProxy _tm; 62 63 private ArrayList <ResourceAdapter > _resources = 64 new ArrayList <ResourceAdapter >(); 65 66 private ArrayList <ConnectionPool> _connectionManagers = 67 new ArrayList <ConnectionPool>(); 68 69 private ArrayList <SoftReference <Timer >> _timers = 70 new ArrayList <SoftReference <Timer >>(); 71 72 private WorkManagerImpl _workManager; 73 74 private boolean _isInit; 75 private boolean _isClosed; 76 77 80 private ResourceManagerImpl() 81 { 82 Environment.addClassLoaderListener(new CloseListener(this)); 83 84 try { 85 Context ic = new InitialContext (); 86 87 _tm = (UserTransactionProxy) ic.lookup("java:comp/UserTransaction"); 88 } catch (Exception e) { 89 log.log(Level.WARNING, e.toString(), e); 90 91 throw new RuntimeException (e); 92 } 93 } 94 95 98 public static ResourceManagerImpl createLocalManager() 99 { 100 ResourceManagerImpl rm; 101 102 synchronized (_localManager) { 103 rm = _localManager.getLevel(); 104 105 if (rm == null) { 106 rm = new ResourceManagerImpl(); 107 _localManager.set(rm); 108 } 109 } 110 111 return rm; 112 } 113 114 117 public static ResourceManagerImpl getLocalManager() 118 { 119 return _localManager.getLevel(); 120 } 121 122 125 public static void addResource(ResourceAdapter resource) 126 throws ConfigException 127 { 128 ResourceManagerImpl rm = createLocalManager(); 129 130 rm.addResourceImpl(resource); 131 } 132 133 136 private void addResourceImpl(ResourceAdapter resource) 137 { 138 try { 139 resource.start(this); 140 } catch (Throwable e) { 141 log.log(Level.WARNING, e.toString(), e); 142 } 143 144 _resources.add(resource); 145 } 146 147 150 public ConnectionPool createConnectionPool() 151 { 152 ConnectionPool cm = new ConnectionPool(); 153 154 cm.setTransactionManager(_tm); 155 156 _connectionManagers.add(cm); 157 158 return cm; 159 } 160 161 164 public WorkManager getWorkManager() 165 { 166 synchronized (this) { 167 if (_workManager == null) 168 _workManager = new WorkManagerImpl(); 169 } 170 171 return _workManager; 172 } 173 174 178 public XATerminator getXATerminator() 179 { 180 return null; 181 } 182 183 186 public Timer createTimer() 187 { 188 TimerImpl timer = new TimerImpl(this); 189 190 synchronized (_timers) { 191 SoftReference <Timer > timerRef = new SoftReference <Timer >(timer); 192 193 _timers.add(timerRef); 194 } 195 196 return timer; 197 } 198 199 202 void removeTimer(Timer timer) 203 { 204 if (_timers == null) 205 return; 206 207 synchronized (_timers) { 208 for (int i = _timers.size(); i >= 0; i--) { 209 SoftReference <Timer > timerRef = _timers.get(i); 210 Timer oldTimer = timerRef.get(); 211 212 if (oldTimer == null) 213 _timers.remove(i); 214 else if (oldTimer == timer) 215 _timers.remove(i); 216 } 217 } 218 } 219 220 223 public void classLoaderInit(DynamicClassLoader loader) 224 { 225 } 226 227 230 public void classLoaderDestroy(DynamicClassLoader loader) 231 { 232 destroy(); 233 } 234 235 238 public void destroy() 239 { 240 ArrayList <ConnectionPool> connectionManagers; 241 ArrayList <ResourceAdapter > resources; 242 ArrayList <SoftReference <Timer >> timers; 243 244 synchronized (this) { 245 if (_isClosed) 246 return; 247 _isClosed = true; 248 249 connectionManagers = _connectionManagers; 250 _connectionManagers = null; 251 252 resources = _resources; 253 _resources = null; 254 255 timers = _timers; 256 _timers = null; 257 } 258 259 for (int i = 0; i < timers.size(); i++) { 261 SoftReference <Timer > timerRef = timers.get(i); 262 Timer timer = timerRef.get(); 263 264 try { 265 if (timer != null) 266 timer.cancel(); 267 } catch (Throwable e) { 268 log.log(Level.WARNING, e.toString(), e); 269 } 270 } 271 272 if (_workManager != null) 274 _workManager.destroy(); 275 276 for (int i = 0; i < connectionManagers.size(); i++) { 277 ConnectionPool connectionManager = connectionManagers.get(i); 278 279 try { 280 connectionManager.destroy(); 281 } catch (Throwable e) { 282 log.log(Level.WARNING, e.toString(), e); 283 } 284 } 285 286 for (int i = 0; i < resources.size(); i++) { 288 ResourceAdapter resource = resources.get(i); 289 290 try { 291 resource.stop(); 292 } catch (Throwable e) { 293 log.log(Level.WARNING, e.toString(), e); 294 } 295 } 296 } 297 298 public class TimerImpl extends Timer { 299 private ResourceManagerImpl _rm; 300 301 TimerImpl(ResourceManagerImpl rm) 302 { 303 super(true); 304 305 _rm = rm; 306 } 307 308 public void cancel() 309 { 310 _rm.removeTimer(this); 311 312 super.cancel(); 313 } 314 } 315 } 316 | Popular Tags |