1 29 30 package com.caucho.ejb; 31 32 import com.caucho.amber.cfg.EntityIntrospector; 33 import com.caucho.amber.manager.PersistenceEnvironmentListener; 34 import com.caucho.config.ConfigException; 35 import com.caucho.config.types.FileSetType; 36 import com.caucho.config.types.JndiBuilder; 37 import com.caucho.config.types.PathPatternType; 38 import com.caucho.config.types.Period; 39 import com.caucho.ejb.cfg.EjbMethod; 40 import com.caucho.ejb.metadata.Bean; 41 import com.caucho.ejb.protocol.ProtocolContainer; 42 import com.caucho.loader.Environment; 43 import com.caucho.loader.EnvironmentBean; 44 import com.caucho.loader.EnvironmentClassLoader; 45 import com.caucho.loader.EnvironmentListener; 46 import com.caucho.loader.EnvironmentLocal; 47 import com.caucho.log.Log; 48 import com.caucho.naming.Jndi; 49 import com.caucho.util.L10N; 50 import com.caucho.vfs.JarPath; 51 import com.caucho.vfs.MergePath; 52 import com.caucho.vfs.Path; 53 import com.caucho.vfs.Vfs; 54 55 import javax.annotation.PostConstruct; 56 import javax.jms.ConnectionFactory ; 57 import javax.naming.NameNotFoundException ; 58 import javax.naming.NamingException ; 59 import javax.sql.DataSource ; 60 import java.util.ArrayList ; 61 import java.util.Iterator ; 62 import java.util.logging.Level ; 63 import java.util.logging.Logger ; 64 65 70 public class EJBServer 71 implements EnvironmentListener, EJBServerInterface, EnvironmentBean 72 { 73 static final L10N L = new L10N(EJBServer.class); 74 protected static final Logger log = Log.open(EJBServer.class); 75 76 private static EnvironmentLocal<EJBServer> _localServer 77 = new EnvironmentLocal<EJBServer>("caucho.ejb-server"); 78 79 private static EnvironmentLocal<EjbServerManager> _localManager 80 = new EnvironmentLocal<EjbServerManager>(); 81 82 protected static EnvironmentLocal<String > _localURL = 83 new EnvironmentLocal<String >("caucho.url"); 84 85 private String _localJndiPrefix; private String _remoteJndiPrefix; 88 private String _entityManagerJndiName = "java:comp/EntityManager"; 89 90 private EjbServerManager _ejbManager; 91 92 private ArrayList <Path> _descriptors; 93 private ArrayList <Path> _ejbJars = new ArrayList <Path>(); 94 95 private EntityIntrospector _entityIntrospector; 96 97 private MergePath _mergePath; 98 99 private String _urlPrefix; 100 101 private ArrayList <FileSetType> _configFileSetList = 102 new ArrayList <FileSetType>(); 103 104 private DataSource _dataSource; 105 private boolean _createDatabaseSchema; 106 private boolean _validateDatabaseSchema = true; 107 108 private boolean _entityLoadLazyOnTransaction = true; 109 110 private String _resinIsolation; 111 private String _jdbcIsolation; 112 113 private ConnectionFactory _jmsConnectionFactory; 114 115 private int _entityCacheSize = 32 * 1024; 116 private long _entityCacheTimeout = 5000; 117 118 private boolean _forbidJVMCall; 119 private boolean _autoCompile = true; 120 private boolean _isAllowPOJO = false; 121 122 private String _startupMode; 123 124 private long _transactionTimeout = 0; 125 126 129 public EJBServer() 130 throws ConfigException 131 { 132 _ejbManager = new EjbServerManager(); 133 134 _urlPrefix = _localURL.get(); 135 136 _mergePath = new MergePath(); 137 _mergePath.addMergePath(Vfs.lookup()); 138 _mergePath.addClassPath(); 139 140 169 170 172 Environment.addChildEnvironmentListener(new PersistenceEnvironmentListener()); 173 } 174 175 public void addJarUrls(EnvironmentClassLoader loader, Path root) 176 throws java.io.IOException 177 { 178 Iterator <String > it = root.iterator(); 179 180 while (it.hasNext()) { 181 182 String s = it.next(); 183 184 Path path = root.lookup(s); 185 186 if (path.isDirectory()) { 187 addJarUrls(loader, path); 188 } 189 else if (s.endsWith(".jar")) { 190 JarPath jarPath = JarPath.create(path); 191 192 loader.addURL(jarPath); 193 } 194 } 195 } 196 197 200 206 207 210 public EnvironmentClassLoader getClassLoader() 211 { 212 return _ejbManager.getClassLoader(); 213 } 214 215 218 public void setEnvironmentClassLoader(EnvironmentClassLoader env) 219 { 220 } 221 222 225 public void setName(String name) 226 { 227 setJndiName(name); 228 } 229 230 233 public void setJndiName(String name) 234 { 235 setJndiLocalPrefix(name); 236 } 237 238 241 public void setJndiLocalPrefix(String name) 242 { 243 _localJndiPrefix = name; 244 } 245 246 249 public String getLocalJndiPrefix() 250 { 251 return _localJndiPrefix; 252 } 253 254 257 public void setJndiRemotePrefix(String name) 258 { 259 _remoteJndiPrefix = name; 260 } 261 262 265 public String getRemoteJndiPrefix() 266 { 267 return _remoteJndiPrefix; 268 } 269 270 273 public void setEntityManagerJndiName(String name) 274 { 275 _entityManagerJndiName = name; 276 } 277 278 281 public String getEntityManagerJndiName() 282 { 283 return _entityManagerJndiName; 284 } 285 286 289 public void setURLPrefix(String urlPrefix) 290 { 291 _urlPrefix = urlPrefix; 292 } 293 294 297 public String getURLPrefix() 298 { 299 return _urlPrefix; 300 } 301 302 305 public void setConfigDirectory(Path dir) 306 throws ConfigException 307 { 308 FileSetType fileSet = new FileSetType(); 309 310 fileSet.setDir(dir); 311 312 fileSet.addInclude(new PathPatternType("**/*.ejb")); 313 314 Path pwd = Vfs.lookup(); 315 316 String dirPath = dir.getPath(); 317 String pwdPath = pwd.getPath(); 318 319 if (dirPath.startsWith(pwdPath)) { 320 String prefix = dirPath.substring(pwdPath.length()); 321 322 fileSet.setUserPathPrefix(prefix); 323 } 324 325 _ejbManager.addConfigFiles(fileSet); 326 } 327 328 331 public void addEJBDescriptor(String ejbDescriptor) 332 { 333 if (_descriptors == null) 334 _descriptors = new ArrayList <Path>(); 335 336 Path path = _mergePath.lookup(ejbDescriptor); 337 338 _descriptors.add(path); 339 } 340 341 344 public void addEJBJar(Path ejbJar) 345 throws ConfigException 346 { 347 if (! ejbJar.canRead() || ! ejbJar.isFile()) 348 throw new ConfigException(L.l("<ejb-jar> {0} must refer to a valid jar file.", 349 ejbJar.getURL())); 350 351 _ejbJars.add(ejbJar); 352 } 353 354 357 public Bean createBean() 358 { 359 return new Bean(_ejbManager); 360 } 361 362 365 public void addBean(Bean bean) 366 { 367 } 368 369 372 public void setDataSource(DataSource dataSource) 373 throws ConfigException 374 { 375 _dataSource = dataSource; 376 377 if (_dataSource == null) 378 throw new ConfigException(L.l("<ejb-server> data-source must be a valid DataSource.")); 379 380 _ejbManager.setDataSource(_dataSource); 381 } 382 383 386 public void setReadDataSource(DataSource dataSource) 387 throws ConfigException 388 { 389 _ejbManager.setReadDataSource(dataSource); 390 } 391 392 395 public void setXADataSource(DataSource dataSource) 396 throws ConfigException 397 { 398 _ejbManager.setXADataSource(dataSource); 399 } 400 401 404 public void setCreateDatabaseSchema(boolean create) 405 { 406 _createDatabaseSchema = create; 407 _ejbManager.getAmberContainer().setCreateDatabaseTables(create); 408 _ejbManager.getAmberManager().setCreateDatabaseTables(create); 409 } 410 411 414 public boolean getCreateDatabaseSchema() 415 { 416 return _ejbManager.getAmberContainer().getCreateDatabaseTables(); 417 } 418 419 422 public void setValidateDatabaseSchema(boolean validate) 423 { 424 _validateDatabaseSchema = validate; 425 } 426 427 430 public boolean getValidateDatabaseSchema() 431 { 432 return _validateDatabaseSchema; 433 } 434 435 438 public void setLoadLazyOnTransaction(boolean isLazy) 439 { 440 _ejbManager.setEntityLoadLazyOnTransaction(isLazy); 441 } 442 443 446 public void setJMSConnectionFactory(JndiBuilder factory) 447 throws ConfigException, NamingException 448 { 449 Object obj = factory.getObject(); 450 451 if (! (obj instanceof ConnectionFactory )) 452 throw new ConfigException(L.l("`{0}' must be a JMS ConnectionFactory.", obj)); 453 454 _jmsConnectionFactory = (ConnectionFactory ) obj; 455 } 456 457 460 public ConnectionFactory getConnectionFactory() 461 { 462 return _jmsConnectionFactory; 463 } 464 465 468 public void setMessageConsumerMax(int consumerMax) 469 throws ConfigException, NamingException 470 { 471 _ejbManager.setMessageConsumerMax(consumerMax); 472 } 473 474 477 public int getEntityCacheSize() 478 { 479 return _entityCacheSize; 480 } 481 482 485 public void setCacheSize(int size) 486 { 487 _entityCacheSize = size; 488 } 489 490 493 public long getEntityCacheTimeout() 494 { 495 return _entityCacheTimeout; 496 } 497 498 501 public void setCacheTimeout(Period timeout) 502 { 503 _entityCacheTimeout = timeout.getPeriod(); 504 } 505 506 509 public long getTransactionTimeout() 510 { 511 return _transactionTimeout; 512 } 513 514 517 public void setTransactionTimeout(Period timeout) 518 { 519 _transactionTimeout = timeout.getPeriod(); 520 } 521 522 525 public String getResinIsolation() 526 { 527 return _resinIsolation; 528 } 529 530 533 public void setResinIsolation(String resinIsolation) 534 { 535 _resinIsolation = resinIsolation; 536 } 537 538 541 public String getJdbcIsolation() 542 { 543 return _jdbcIsolation; 544 } 545 546 549 public void setJdbcIsolation(String jdbcIsolation) 550 { 551 _jdbcIsolation = jdbcIsolation; 552 } 553 554 557 public void setForbidJvmCall(boolean forbid) 558 { 559 _forbidJVMCall = forbid; 560 } 561 562 565 public boolean isAutoCompile() 566 { 567 return _autoCompile; 568 } 569 570 573 public void setAutoCompile(boolean autoCompile) 574 { 575 _autoCompile = autoCompile; 576 } 577 578 581 public boolean isAllowPOJO() 582 { 583 return _isAllowPOJO; 584 } 585 586 589 public void setAllowPOJO(boolean allowPOJO) 590 { 591 _isAllowPOJO = allowPOJO; 592 } 593 594 597 public void setStartupMode(String startupMode) 598 { 599 _startupMode = startupMode; 600 } 601 602 public static EJBServer getLocal() 603 { 604 return _localServer.get(); 605 } 606 607 public static EjbServerManager getLocalManager() 608 { 609 return _localManager.get(); 610 } 611 612 615 @PostConstruct 616 public void init() 617 throws Exception 618 { 619 627 628 if (_localServer.getLevel() == null 629 || "java:comp/env/cmp".equals(_localJndiPrefix)) { 630 _localServer.set(this); 631 _localManager.set(_ejbManager); 632 } 633 634 try { 635 if (_localJndiPrefix != null) 636 Jndi.bindDeepShort(_localJndiPrefix + "/resin-ejb-server", _ejbManager); 637 } catch (NamingException e) { 638 log.log(Level.WARNING, e.toString(), e); 639 } 640 641 try { 642 if (_localJndiPrefix != null) 643 Jndi.bindDeepShort(_localJndiPrefix + "/caucho-ejb-admin", _ejbManager); 644 } catch (NamingException e) { 645 log.log(Level.WARNING, e.toString(), e); 646 } 647 648 try { 649 if (_entityManagerJndiName != null) { 650 Jndi.rebindDeepShort(_entityManagerJndiName, 651 _ejbManager.getAmberManager().getEntityManager()); 652 } 653 } catch (NamingException e) { 654 log.log(Level.FINER, e.toString(), e); 655 } 656 657 if ("manual".equals(_startupMode)) 658 return; 659 660 manualInit(); 661 } 662 663 666 public void manualInit() 667 throws Exception 668 { 669 try { 670 log.fine("Initializing ejb-server : local-jndi=" + _localJndiPrefix 671 + " remote-jndi=" + _remoteJndiPrefix); 672 673 ProtocolContainer protocol = new ProtocolContainer(); 674 if (_urlPrefix != null) 675 protocol.setURLPrefix(_urlPrefix); 676 677 protocol.setServerManager(_ejbManager); 679 _ejbManager.getProtocolManager().setProtocolContainer(protocol); 680 _ejbManager.setLocalJndiPrefix(_localJndiPrefix); 681 _ejbManager.setRemoteJndiPrefix(_remoteJndiPrefix); 682 683 _ejbManager.setValidateDatabaseSchema(_validateDatabaseSchema); 686 _ejbManager.setJMSConnectionFactory(_jmsConnectionFactory); 687 _ejbManager.setCacheSize(_entityCacheSize); 688 _ejbManager.setCacheTimeout(_entityCacheTimeout); 689 _ejbManager.setTransactionTimeout(_transactionTimeout); 690 _ejbManager.setAllowJVMCall(! _forbidJVMCall); 691 _ejbManager.setAutoCompile(_autoCompile); 692 _ejbManager.setAllowPOJO(isAllowPOJO()); 693 694 int resinIsolation = -1; 695 696 if (_resinIsolation == null) { 697 } 698 else if (_resinIsolation.equals("row-locking")) 699 resinIsolation = EjbMethod.RESIN_ROW_LOCKING; 700 else if (_resinIsolation.equals("database")) 701 resinIsolation = EjbMethod.RESIN_DATABASE; 702 else { 703 throw new ConfigException(L.l("resin-isolation may only be `row-locking' or `database' in EJBServer, not `{0}'", _resinIsolation)); 704 } 705 706 _ejbManager.setResinIsolation(resinIsolation); 707 708 int jdbcIsolation = -1; 709 710 if (_jdbcIsolation == null) { 711 } 712 else if (_jdbcIsolation.equals("none")) 713 jdbcIsolation = java.sql.Connection.TRANSACTION_NONE; 714 else if (_jdbcIsolation.equals("read-committed")) 715 jdbcIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED; 716 else if (_jdbcIsolation.equals("read-uncommitted")) 717 jdbcIsolation = java.sql.Connection.TRANSACTION_READ_UNCOMMITTED; 718 else if (_jdbcIsolation.equals("repeatable-read")) 719 jdbcIsolation = java.sql.Connection.TRANSACTION_REPEATABLE_READ; 720 else if (_jdbcIsolation.equals("serializable")) 721 jdbcIsolation = java.sql.Connection.TRANSACTION_SERIALIZABLE; 722 else 723 throw new ConfigException(L.l("unknown value for jdbc-isolation at `{0}'", 724 _jdbcIsolation)); 725 726 _ejbManager.setJDBCIsolation(jdbcIsolation); 727 728 730 initAllEjbs(); 731 732 _ejbManager.init(); 733 734 741 742 Environment.addEnvironmentListener(this); 743 } catch (Exception e) { 744 log.log(Level.WARNING, e.toString(), e); 745 746 throw e; 747 } 748 } 749 750 754 public void initEJBs() 755 throws Exception 756 { 757 manualInit(); 758 } 759 760 764 private void initAllEjbs() 765 throws Exception 766 { 767 addEJBJars(); 768 769 if (_descriptors != null) { 770 for (int i = 0; i < _descriptors.size(); i++) { 771 Path path = _descriptors.get(i); 772 773 _ejbManager.addEJBPath(path, path); 775 } 776 } 777 } 778 779 private void addEJBJars() 780 throws Exception 781 { 782 for (int i = 0; i < _ejbJars.size(); i++) { 783 Path path = _ejbJars.get(i); 784 785 Environment.addDependency(path); 786 787 JarPath jar = JarPath.create(path); 788 789 _ejbManager.addEJBJar(jar); 790 } 791 } 792 793 798 public AbstractServer getServer(Path path, String ejbName) 799 throws NameNotFoundException 800 { 801 return _ejbManager.getServer(path, ejbName); 802 } 803 804 807 public void environmentStart(EnvironmentClassLoader loader) 808 { 809 } 810 811 814 public void environmentStop(EnvironmentClassLoader loader) 815 { 816 close(); 817 } 818 819 public void close() 820 { 821 _ejbManager.destroy(); 822 } 823 824 } 825 826 | Popular Tags |