1 29 30 package com.caucho.ejb; 31 32 import com.caucho.amber.entity.AmberEntityHome; 33 import com.caucho.amber.manager.AmberPersistenceUnit; 34 import com.caucho.bytecode.JClassLoader; 35 import com.caucho.config.ConfigException; 36 import com.caucho.ejb.admin.EJBAdmin; 37 import com.caucho.ejb.cfg.EjbConfig; 38 import com.caucho.ejb.entity.EntityKey; 39 import com.caucho.ejb.entity.EntityServer; 40 import com.caucho.ejb.entity.QEntityContext; 41 import com.caucho.ejb.protocol.EjbProtocolManager; 42 import com.caucho.ejb.protocol.ProtocolContainer; 43 import com.caucho.ejb.xa.EjbTransactionManager; 44 import com.caucho.java.WorkDir; 45 import com.caucho.lifecycle.Lifecycle; 46 import com.caucho.loader.Environment; 47 import com.caucho.loader.EnvironmentClassLoader; 48 import com.caucho.loader.EnvironmentListener; 49 import com.caucho.loader.SimpleLoader; 50 import com.caucho.util.L10N; 51 import com.caucho.util.Log; 52 import com.caucho.util.LruCache; 53 import com.caucho.vfs.Path; 54 55 import java.util.ArrayList ; 56 import java.util.Collections ; 57 import java.util.Comparator ; 58 import java.util.HashMap ; 59 import java.util.Hashtable ; 60 import java.util.Iterator ; 61 import java.util.logging.Level ; 62 import java.util.logging.Logger ; 63 64 67 public class EnvServerManager implements EnvironmentListener { 68 private static final L10N L = new L10N(EnvServerManager.class); 69 protected static final Logger log = Log.open(EnvServerManager.class); 70 71 75 76 private EnvironmentClassLoader _classLoader; 77 78 private Path _workPath; 79 80 private int _entityCacheSize = 32 * 1024; 81 82 private long _entityCacheTimeout = 5000L; 83 84 private ConfigException _initException; 85 86 private EJBAdmin _ejbAdmin; 87 88 private AmberPersistenceUnit _amberPersistenceUnit; 89 90 private EjbTransactionManager _ejbTransactionManager; 91 92 private EjbProtocolManager _protocolManager; 93 94 private ArrayList <EjbConfig> _ejbConfigList = new ArrayList <EjbConfig>(); 95 96 private Hashtable <String ,AbstractServer> _serverMap 97 = new Hashtable <String ,AbstractServer>(); 98 99 protected ProtocolContainer _protocolContainer; 101 protected HashMap <String ,ProtocolContainer> _protocolMap 102 = new HashMap <String ,ProtocolContainer>(); 103 104 private LruCache<EntityKey,QEntityContext> _entityCache; 105 106 private EntityKey _entityKey = new EntityKey(); 107 108 private final Lifecycle _lifecycle = new Lifecycle(log, "ejb-manager"); 109 110 113 EnvServerManager(AmberPersistenceUnit amberPersistenceUnit) 114 { 115 try { 116 _amberPersistenceUnit = amberPersistenceUnit; 117 _amberPersistenceUnit.initLoaders(); 118 _amberPersistenceUnit.setTableCacheTimeout(_entityCacheTimeout); 119 120 _classLoader = (EnvironmentClassLoader) Thread.currentThread().getContextClassLoader(); 121 _workPath = WorkDir.getLocalWorkDir(_classLoader).lookup("ejb"); 122 _classLoader.addLoader(new SimpleLoader(_workPath)); 123 124 try { 125 _ejbTransactionManager = new EjbTransactionManager(this); 126 } catch (Throwable e) { 127 log.info("transactions are not available to EJB server"); 128 129 log.log(Level.FINE, e.toString(), e); 130 } 131 132 _ejbAdmin = new EJBAdmin(this); 133 134 _protocolManager = new EjbProtocolManager(this); 135 } catch (RuntimeException e) { 136 throw e; 137 } catch (Exception e) { 138 throw new RuntimeException (e); 139 } 140 } 141 142 145 151 152 155 170 171 174 public EnvironmentClassLoader getClassLoader() 175 { 176 return _classLoader; 177 } 178 179 182 public EjbProtocolManager getProtocolManager() 183 { 184 return _protocolManager; 185 } 186 187 190 public EjbTransactionManager getTransactionManager() 191 { 192 return _ejbTransactionManager; 193 } 194 195 198 public void setResinIsolation(int resinIsolation) 199 { 200 _ejbTransactionManager.setResinIsolation(resinIsolation); 201 } 202 203 206 public int getResinIsolation() 207 { 208 return _ejbTransactionManager.getResinIsolation(); 209 } 210 211 214 public void setJDBCIsolation(int jdbcIsolation) 215 { 216 _ejbTransactionManager.setJDBCIsolation(jdbcIsolation); 217 } 218 219 222 public int getJDBCIsolation() 223 { 224 return _ejbTransactionManager.getJDBCIsolation(); 225 } 226 227 230 public long getTransactionTimeout() 231 { 232 return _ejbTransactionManager.getTransactionTimeout(); 233 } 234 235 238 public void setTransactionTimeout(long transactionTimeout) 239 { 240 _ejbTransactionManager.setTransactionTimeout(transactionTimeout); 241 } 242 243 246 public Path getWorkPath() 247 { 248 return _workPath; 249 } 250 251 254 public void setWorkPath(Path workPath) 255 { 256 _workPath = workPath; 257 } 258 259 262 public long getCacheTimeout() 263 { 264 return _entityCacheTimeout; 265 } 266 267 270 public void setCacheTimeout(long cacheTimeout) 271 { 272 _entityCacheTimeout = cacheTimeout; 273 } 275 276 279 public int getCacheSize() 280 { 281 return _entityCacheSize; 282 } 283 284 287 public void setCacheSize(int cacheSize) 288 { 289 _entityCacheSize = cacheSize; 290 } 291 292 295 public EJBAdmin getAdmin() 296 { 297 return _ejbAdmin; 298 } 299 300 303 void addEjbConfig(EjbConfig ejbConfig) 304 { 305 _ejbConfigList.add(ejbConfig); 306 } 307 308 311 public void init() 312 throws Exception 313 { 314 build(); 315 316 Environment.addEnvironmentListener(this); 317 } 318 319 322 public void build() 323 throws ConfigException 324 { 325 try { 326 _amberPersistenceUnit.init(); 327 328 332 333 _entityCache = new LruCache<EntityKey,QEntityContext>(_entityCacheSize); 334 335 _protocolManager.init(); 336 } catch (Exception e) { 337 throw new ConfigException(e); 338 } 339 } 340 341 public void start() 342 throws Exception 343 { 344 for (EjbConfig cfg : _ejbConfigList) 345 cfg.deploy(); 346 347 Thread thread = Thread.currentThread(); 348 ClassLoader oldLoader = thread.getContextClassLoader(); 349 350 for (AbstractServer server : _serverMap.values()) { 351 try { 352 thread.setContextClassLoader(server.getClassLoader()); 353 354 log.fine(server + " starting"); 355 356 server.start(); 357 } finally { 358 thread.setContextClassLoader(oldLoader); 359 } 360 } 361 } 362 363 public AmberEntityHome getAmberEntityHome(String name) 364 { 365 return _amberPersistenceUnit.getEntityHome(name); 366 } 367 368 public AmberPersistenceUnit getAmberManager() 369 { 370 return _amberPersistenceUnit; 371 } 372 373 public JClassLoader getJClassLoader() 374 { 375 return getAmberManager().getJClassLoader(); 376 } 377 378 381 public void invalidateCache() 382 { 383 } 384 385 388 public void addServer(AbstractServer server) 389 { 390 _serverMap.put(server.getEJBName(), server); 391 392 try { 393 _protocolManager.addServer(server); 394 } catch (Throwable e) { 395 log.log(Level.WARNING, e.toString(), e); 396 } 397 } 398 399 400 403 public AbstractServer getServer(Path path, String ejbName) 404 { 405 407 AbstractServer server = _serverMap.get(ejbName); 408 409 return server; 410 } 411 412 415 public QEntityContext getEntity(EntityServer server, Object key) 416 { 417 synchronized (_entityKey) { 418 _entityKey.init(server, key); 419 420 return _entityCache.get(_entityKey); 421 } 422 } 423 424 427 public QEntityContext putEntityIfNew(EntityServer server, Object key, 428 QEntityContext context) 429 { 430 return _entityCache.putIfNew(new EntityKey(server, key), context); 431 } 432 433 436 public void removeEntity(EntityServer server, Object key) 437 { 438 synchronized (_entityKey) { 439 _entityKey.init(server, key); 440 _entityCache.remove(_entityKey); 441 } 442 } 443 444 447 public void removeBeans(ArrayList <QEntityContext> beans, EntityServer server) 448 { 449 synchronized (_entityCache) { 450 Iterator <LruCache.Entry<EntityKey,QEntityContext>> iter; 451 452 iter = _entityCache.iterator(); 453 454 while (iter.hasNext()) { 455 LruCache.Entry<EntityKey,QEntityContext> entry = iter.next(); 456 457 beans.add(entry.getValue()); 458 459 iter.remove(); 460 } 461 } 462 } 463 464 467 public void environmentStart(EnvironmentClassLoader loader) 468 throws Exception 469 { 470 start(); 471 } 472 473 476 public void environmentStop(EnvironmentClassLoader loader) 477 { 478 } 479 480 483 public void destroy() 484 { 485 if (! _lifecycle.toDestroy()) 486 return; 487 488 try { 489 ArrayList <AbstractServer> servers; 490 servers = new ArrayList <AbstractServer>(_serverMap.values()); 491 _serverMap.clear(); 492 493 497 498 Collections.sort(servers, new ServerCmp()); 500 501 for (AbstractServer server : servers) { 502 try { 503 _protocolManager.removeServer(server); 504 } catch (Throwable e) { 505 log.log(Level.WARNING, e.toString(), e); 506 } 507 } 508 509 for (AbstractServer server : servers) { 510 try { 511 server.destroy(); 512 } catch (Throwable e) { 513 log.log(Level.WARNING, e.toString(), e); 514 } 515 } 516 517 _serverMap = null; 518 _protocolManager.destroy(); 519 _protocolManager = null; 520 _ejbTransactionManager.destroy(); 521 _ejbTransactionManager = null; 522 _amberPersistenceUnit = null; 523 } catch (Throwable e) { 524 log.log(Level.WARNING, e.toString(), e); 525 } 526 } 527 528 532 static class ServerCmp implements Comparator <AbstractServer> { 533 public int compare(AbstractServer a, AbstractServer b) 534 { 535 return a.getEJBName().compareTo(b.getEJBName()); 536 } 537 } 538 } 539 540 | Popular Tags |