1 29 30 package com.caucho.ejb; 31 32 import com.caucho.amber.entity.AmberEntityHome; 33 import com.caucho.amber.manager.AmberContainer; 34 import com.caucho.amber.manager.AmberPersistenceUnit; 35 import com.caucho.bytecode.ByteCodeClassMatcher; 36 import com.caucho.bytecode.ByteCodeClassScanner; 37 import com.caucho.bytecode.JClassLoader; 38 import com.caucho.config.ConfigException; 39 import com.caucho.config.types.FileSetType; 40 import com.caucho.ejb.admin.EJBAdmin; 41 import com.caucho.ejb.cfg.EjbConfig; 42 import com.caucho.ejb.entity.EntityKey; 43 import com.caucho.ejb.entity.EntityServer; 44 import com.caucho.ejb.entity.QEntityContext; 45 import com.caucho.ejb.metadata.Bean; 46 import com.caucho.ejb.protocol.EjbProtocolManager; 47 import com.caucho.ejb.xa.EjbTransactionManager; 48 import com.caucho.lifecycle.Lifecycle; 49 import com.caucho.loader.EnvironmentClassLoader; 50 import com.caucho.loader.EnvironmentListener; 51 import com.caucho.management.j2ee.EJBModule; 52 import com.caucho.management.j2ee.J2EEManagedObject; 53 import com.caucho.util.CharBuffer; 54 import com.caucho.util.L10N; 55 import com.caucho.vfs.JarPath; 56 import com.caucho.vfs.Path; 57 import com.caucho.vfs.TempBuffer; 58 59 import javax.jms.ConnectionFactory ; 60 import javax.sql.DataSource ; 61 import java.io.ByteArrayOutputStream ; 62 import java.io.IOException ; 63 import java.io.InputStream ; 64 import java.util.ArrayList ; 65 import java.util.Comparator ; 66 import java.util.LinkedHashMap ; 67 import java.util.Map ; 68 import java.util.logging.Level ; 69 import java.util.logging.Logger ; 70 import java.util.zip.ZipEntry ; 71 import java.util.zip.ZipInputStream ; 72 73 76 public class EjbServerManager 77 implements EJBServerInterface, EnvironmentListener 78 { 79 private static final L10N L = new L10N(EjbServerManager.class); 80 protected static final Logger log = Logger.getLogger(EjbServerManager.class.getName()); 81 82 private EnvironmentClassLoader _classLoader; 83 84 private EnvServerManager _envServerManager; 85 86 private boolean _autoCompile = true; 87 88 private boolean _entityLoadLazyOnTransaction = true; 89 90 protected boolean _allowJVMCall = true; 91 protected boolean _allowReferenceCall = true; 92 93 protected boolean _createDatabaseSchema; 94 protected boolean _validateDatabaseSchema; 95 96 private boolean _hasInitJdbc; 97 private ConfigException _initException; 98 99 private EjbConfig _ejbConfig; 100 101 private AmberContainer _amberContainer; 102 private AmberPersistenceUnit _amberPersistenceUnit; 103 104 protected ConnectionFactory _jmsConnectionFactory; 105 private int _messageConsumerMax = 5; 106 107 private EntityKey _entityKey = new EntityKey(); 108 109 private final Lifecycle _lifecycle = new Lifecycle(log, "ejb-manager"); 110 111 private Map <String , J2EEManagedObject> _ejbModuleManagedObjectMap 112 = new LinkedHashMap <String , J2EEManagedObject>(); 113 114 117 EjbServerManager() 118 { 119 try { 120 _amberContainer = AmberContainer.getLocalContainer(); 121 122 _amberPersistenceUnit = AmberContainer.getLocalContainer().createPersistenceUnit("resin-ejb"); 123 _amberPersistenceUnit.setBytecodeGenerator(false); 124 125 _envServerManager = new EnvServerManager(_amberPersistenceUnit); 126 127 _ejbConfig = new EjbConfig(this); 128 129 _envServerManager.addEjbConfig(_ejbConfig); 130 } catch (RuntimeException e) { 131 throw e; 132 } catch (Exception e) { 133 throw new RuntimeException (e); 134 } 135 } 136 137 140 public EnvironmentClassLoader getClassLoader() 141 { 142 return _envServerManager.getClassLoader(); 143 } 144 145 148 public EnvServerManager getEnvServerManager() 149 { 150 return _envServerManager; 151 } 152 153 156 public EjbProtocolManager getProtocolManager() 157 { 158 return _envServerManager.getProtocolManager(); 159 } 160 161 164 public EjbTransactionManager getTransactionManager() 165 { 166 return _envServerManager.getTransactionManager(); 167 } 168 169 public AmberContainer getAmberContainer() 170 { 171 return _amberContainer; 172 } 173 174 public void setLocalJndiPrefix(String localJndiPrefix) 175 { 176 getProtocolManager().setLocalJndiPrefix(localJndiPrefix); 177 } 178 179 public String getLocalJndiPrefix() 180 { 181 return getProtocolManager().getLocalJndiPrefix(); 182 } 183 184 public void setRemoteJndiPrefix(String remoteJndiPrefix) 185 { 186 getProtocolManager().setRemoteJndiPrefix(remoteJndiPrefix); 187 } 188 189 public String getRemoteJndiPrefix() 190 { 191 return getProtocolManager().getRemoteJndiPrefix(); 192 } 193 194 197 public void setDataSource(DataSource dataSource) 198 { 199 _amberContainer.setDataSource(dataSource); 200 _amberPersistenceUnit.setDataSource(dataSource); 201 } 202 203 206 public DataSource getDataSource() 207 { 208 return _amberContainer.getDataSource(); 209 } 210 211 214 public void setReadDataSource(DataSource dataSource) 215 { 216 _amberContainer.setReadDataSource(dataSource); 217 } 218 219 222 public DataSource getReadDataSource() 223 { 224 return _amberContainer.getReadDataSource(); 225 } 226 227 230 public void setXADataSource(DataSource dataSource) 231 { 232 _amberContainer.setXADataSource(dataSource); 233 } 234 235 238 public DataSource getXADataSource() 239 { 240 return _amberContainer.getXADataSource(); 241 } 242 243 246 public void setResinIsolation(int resinIsolation) 247 { 248 getTransactionManager().setResinIsolation(resinIsolation); 249 } 250 251 254 public int getResinIsolation() 255 { 256 return getTransactionManager().getResinIsolation(); 257 } 258 259 262 public void setJDBCIsolation(int jdbcIsolation) 263 { 264 getTransactionManager().setJDBCIsolation(jdbcIsolation); 265 } 266 267 270 public int getJDBCIsolation() 271 { 272 return getTransactionManager().getJDBCIsolation(); 273 } 274 275 278 public long getTransactionTimeout() 279 { 280 return getTransactionManager().getTransactionTimeout(); 281 } 282 283 286 public void setTransactionTimeout(long transactionTimeout) 287 { 288 getTransactionManager().setTransactionTimeout(transactionTimeout); 289 } 290 291 294 public boolean isEntityLoadLazyOnTransaction() 295 { 296 return _entityLoadLazyOnTransaction; 297 } 298 299 302 public void setEntityLoadLazyOnTransaction(boolean isLazy) 303 { 304 _entityLoadLazyOnTransaction = isLazy; 305 } 306 307 310 public void setJMSConnectionFactory(ConnectionFactory factory) 311 { 312 _jmsConnectionFactory = factory; 313 } 314 315 318 public void setMessageConsumerMax(int consumerMax) 319 { 320 _messageConsumerMax = consumerMax; 321 } 322 323 326 public int getMessageConsumerMax() 327 { 328 return _messageConsumerMax; 329 } 330 331 334 public ConnectionFactory getConnectionFactory() 335 { 336 return _jmsConnectionFactory; 337 } 338 339 342 public Path getWorkPath() 343 { 344 return _envServerManager.getWorkPath(); 345 } 346 347 350 public void setWorkPath(Path workPath) 351 { 352 _envServerManager.setWorkPath(workPath); 353 } 355 356 359 public boolean isAutoCompile() 360 { 361 return _autoCompile; 362 } 363 364 367 public void setAutoCompile(boolean autoCompile) 368 { 369 _autoCompile = autoCompile; 370 } 371 372 375 public boolean isAllowPOJO() 376 { 377 return _ejbConfig.isAllowPOJO(); 378 } 379 380 383 public void setAllowPOJO(boolean allowPOJO) 384 { 385 _ejbConfig.setAllowPOJO(allowPOJO); 386 } 387 388 391 public void setAllowJVMCall(boolean allowJVMCall) 392 { 393 _allowJVMCall = allowJVMCall; 394 } 395 396 400 public void setAllowReferenceCall(boolean allowReferenceCall) 401 { 402 _allowReferenceCall = allowReferenceCall; 403 } 404 405 408 public void setCreateDatabaseSchema(boolean create) 409 { 410 _createDatabaseSchema = create; 411 412 _amberPersistenceUnit.setCreateDatabaseTables(create); 413 } 414 415 418 public boolean getCreateDatabaseSchema() 419 { 420 return _createDatabaseSchema; 421 } 422 423 426 public void setValidateDatabaseSchema(boolean validate) 427 { 428 _validateDatabaseSchema = validate; 429 430 _amberPersistenceUnit.setValidateDatabaseTables(validate); 431 } 432 433 436 public boolean getValidateDatabaseSchema() 437 { 438 return _validateDatabaseSchema; 439 } 440 441 444 public long getCacheTimeout() 445 { 446 return _envServerManager.getCacheTimeout(); 447 } 448 449 452 public void setCacheTimeout(long cacheTimeout) 453 { 454 _envServerManager.setCacheTimeout(cacheTimeout); 455 } 456 457 460 public int getCacheSize() 461 { 462 return _envServerManager.getCacheSize(); 463 } 464 465 468 public void setCacheSize(int cacheSize) 469 { 470 _envServerManager.setCacheSize(cacheSize); 471 } 472 473 476 public EJBAdmin getAdmin() 477 { 478 return _envServerManager.getAdmin(); 479 } 480 481 484 public EjbConfig getConfig() 485 { 486 return _ejbConfig; 487 } 488 489 492 public void addConfigFiles(FileSetType fileSet) 493 { 494 _ejbConfig.addFileSet(fileSet); 495 } 496 497 500 public void addEJBJar(Path path) 501 throws Exception 502 { 503 JarPath jar; 504 505 if (path instanceof JarPath) 506 jar = (JarPath) path; 507 else 508 jar = JarPath.create(path); 509 510 introspectJar(jar.getContainer()); 511 512 Path descriptorPath = jar.lookup("META-INF/ejb-jar.xml"); 513 514 if (descriptorPath.exists()) { 515 addEJBPath(path, descriptorPath); 516 } 517 518 descriptorPath = jar.lookup("META-INF/resin-ejb-jar.xml"); 519 520 if (descriptorPath.exists()) { 521 addEJBPath(path, descriptorPath); 522 } 523 524 Path metaInf = jar.lookup("META-INF"); 525 if (metaInf.isDirectory()) { 526 String []metaList = metaInf.list(); 527 for (int j = 0; j < metaList.length; j++) { 528 String metaName = metaList[j]; 529 if (metaName.endsWith(".ejb")) { 530 Path metaPath = metaInf.lookup(metaName); 531 532 addEJBPath(path, metaPath); 533 } 534 } 535 } 536 } 537 538 private void introspectJar(Path path) 539 { 540 try { 541 InputStream is = path.openRead(); 542 543 try { 544 ZipInputStream zipIs = new ZipInputStream (is); 545 546 ZipEntry entry; 547 TempBuffer tbuf = TempBuffer.allocate(); 548 byte []buffer = tbuf.getBuffer(); 549 550 while ((entry = zipIs.getNextEntry()) != null) { 551 String classFileName = entry.getName(); 552 553 if (! classFileName.endsWith(".class")) 554 continue; 555 556 String className 557 = classFileName.substring(0, classFileName.length() - 6); 558 className = className.replace('/', '.'); 559 560 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 561 562 int size = 0; 563 int sublen; 564 565 while ((sublen = zipIs.read(buffer, 0, buffer.length)) > 0) { 566 bos.write(buffer, 0, sublen); 567 568 size += size; 569 } 570 571 bos.close(); 572 573 byte []classBytes = bos.toByteArray(); 574 ByteCodeClassMatcher matcher = new EjbClassMatcher(); 575 ByteCodeClassScanner scanner 576 = new ByteCodeClassScanner(className, 577 classBytes, 0, classBytes.length, 578 matcher); 579 580 if (scanner.scan()) { 581 try { 582 Bean bean = new Bean(this); 583 584 bean.setType(className); 585 586 bean.init(); 587 } catch (ConfigException e) { 588 throw e; 589 } catch (Exception e) { 590 throw new ConfigException(e); 591 } 592 } 593 } 594 595 zipIs.close(); 596 } finally { 597 is.close(); 598 } 599 } catch (IOException e) { 600 log.log(Level.FINE, e.toString(), e); 601 } 602 } 603 604 public void addEJBModule(String ejbModuleName) 606 { 607 J2EEManagedObject ejbModuleManagedObject = _ejbModuleManagedObjectMap.get(ejbModuleName); 608 609 if (ejbModuleManagedObject == null) { 610 ejbModuleManagedObject = J2EEManagedObject.register(new EJBModule(ejbModuleName)); 611 _ejbModuleManagedObjectMap.put(ejbModuleName, ejbModuleManagedObject); 612 } 613 } 614 615 618 public void addEJBPath(Path ejbModulePath, Path path) 619 throws ConfigException 620 { 621 _ejbConfig.addEJBPath(ejbModulePath, path); 622 } 623 624 627 public void initEJBs() 628 throws Exception 629 { 630 init(); 631 } 632 633 636 public void init() 637 throws Exception 638 { 639 _envServerManager.init(); 640 641 build(); 642 } 643 644 647 public void build() 648 throws ConfigException 649 { 650 try { 651 _amberPersistenceUnit.init(); 652 653 _ejbConfig.configure(); 654 655 } catch (ConfigException e) { 657 throw e; 658 } catch (Exception e) { 659 throw new ConfigException(e); 660 } 661 } 662 663 public void start() 664 throws Exception 665 { 666 _envServerManager.start(); 667 } 668 669 674 public AbstractServer getServer(Path path, String ejbName) 675 { 676 return _envServerManager.getServer(path, ejbName); 677 } 678 679 public AmberEntityHome getAmberEntityHome(String name) 680 { 681 return _amberPersistenceUnit.getEntityHome(name); 682 } 683 684 public AmberPersistenceUnit getAmberManager() 685 { 686 return _amberPersistenceUnit; 687 } 688 689 public JClassLoader getJClassLoader() 690 { 691 return getAmberManager().getJClassLoader(); 692 } 693 694 697 public void invalidateCache() 698 { 699 } 700 701 704 public void addServer(AbstractServer server) 705 { 706 _envServerManager.addServer(server); 707 } 708 709 712 public QEntityContext getEntity(EntityServer server, Object key) 713 { 714 return _envServerManager.getEntity(server, key); 715 } 716 717 720 public QEntityContext putEntityIfNew(EntityServer server, Object key, 721 QEntityContext context) 722 { 723 return _envServerManager.putEntityIfNew(server, key, context); 724 } 725 726 729 public void removeEntity(EntityServer server, Object key) 730 { 731 _envServerManager.removeEntity(server, key); 732 } 733 734 737 public void removeBeans(ArrayList <QEntityContext> beans, EntityServer server) 738 { 739 EnvServerManager manager = _envServerManager; 740 741 if (manager != null) 742 manager.removeBeans(beans, server); 743 } 744 745 748 public void environmentStart(EnvironmentClassLoader loader) 749 throws Throwable 750 { 751 start(); 752 } 753 754 757 public void environmentStop(EnvironmentClassLoader loader) 758 { 759 } 760 761 764 public void destroy() 765 { 766 if (! _lifecycle.toDestroy()) 767 return; 768 769 ArrayList <J2EEManagedObject> ejbModuleManagedObjects; 770 EnvServerManager envServerManager; 771 772 _ejbModuleManagedObjectMap.clear(); 774 775 envServerManager = _envServerManager; 776 779 _ejbConfig = null; 781 782 try { 783 envServerManager.destroy(); 784 } catch (Throwable e) { 785 log.log(Level.WARNING, e.toString(), e); 786 } 787 } 788 789 class EjbClassMatcher implements ByteCodeClassMatcher { 790 public boolean isClassMatch(String className) 791 { 792 return false; 793 } 794 795 public boolean isMatch(CharBuffer annotationName) 796 { 797 if (annotationName.matches("javax.ejb.Stateless")) 798 return true; 799 else if (annotationName.matches("javax.ejb.Stateful")) 800 return true; 801 else if (annotationName.matches("javax.ejb.Session")) 802 return true; 803 else 804 return false; 805 } 806 } 807 808 812 static class ServerCmp implements Comparator <AbstractServer> { 813 public int compare(AbstractServer a, AbstractServer b) 814 { 815 return a.getEJBName().compareTo(b.getEJBName()); 816 } 817 } 818 } 819 820 | Popular Tags |