1 29 30 package com.caucho.ejb; 31 32 import com.caucho.config.BuilderProgram; 33 import com.caucho.ejb.protocol.AbstractHandle; 34 import com.caucho.ejb.protocol.EjbProtocolManager; 35 import com.caucho.ejb.protocol.HandleEncoder; 36 import com.caucho.ejb.protocol.SameJVMClientContainer; 37 import com.caucho.ejb.session.SessionServer; 38 import com.caucho.ejb.session.StatelessServer; 39 import com.caucho.ejb.xa.EjbTransactionManager; 40 import com.caucho.ejb.xa.TransactionContext; 41 import com.caucho.loader.DynamicClassLoader; 42 import com.caucho.loader.EnvironmentBean; 43 import com.caucho.loader.EnvironmentClassLoader; 44 import com.caucho.util.L10N; 45 import com.caucho.util.Log; 46 47 import javax.ejb.*; 48 import javax.naming.Context ; 49 import javax.naming.InitialContext ; 50 import javax.naming.NamingException ; 51 import javax.sql.DataSource ; 52 import javax.transaction.UserTransaction ; 53 import java.rmi.RemoteException ; 54 import java.sql.Connection ; 55 import java.sql.SQLException ; 56 import java.util.HashMap ; 57 import java.util.logging.Level ; 58 import java.util.logging.Logger ; 59 60 63 abstract public class AbstractServer implements EnvironmentBean { 64 protected final static Logger log = Log.open(AbstractServer.class); 65 private static final L10N L = new L10N(AbstractServer.class); 66 67 protected String _ejbName; 68 protected String _jndiName; 69 protected String _serverId; 70 protected String _handleServerId; 71 72 private Context _jndiEnv; 73 74 protected EjbServerManager _ejbManager; 75 private BuilderProgram _serverProgram; 76 77 protected HashMap <String ,HandleEncoder> _protocolEncoderMap; 78 protected HandleEncoder _handleEncoder; 79 80 protected DataSource _dataSource; 81 82 protected DynamicClassLoader _loader; 83 84 protected SameJVMClientContainer _jvmClient; 85 86 protected Class _contextImplClass; 88 89 protected Class _remoteHomeClass; 90 protected Class _remoteObjectClass; 91 protected Class _primaryKeyClass; 92 93 protected Class _remoteStubClass; 94 protected Class _homeStubClass; 95 96 protected HomeHandle _homeHandle; 97 protected EJBHome _remoteHome; 98 protected EJBHome _remoteHomeView; 99 protected EJBLocalHome _localHome; 100 protected EJBMetaDataImpl _metaData; 101 102 protected long _transactionTimeout; 103 104 protected BuilderProgram _initProgram; 105 106 111 public AbstractServer(EjbServerManager manager) 112 { 113 _ejbManager = manager; 114 115 _loader = new EnvironmentClassLoader(manager.getClassLoader()); 116 } 117 118 public String getId() 119 { 120 return getEJBName(); 122 } 123 124 127 public void setEJBName(String ejbName) 128 { 129 _ejbName = ejbName; 130 } 131 132 135 public String getEJBName() 136 { 137 return _ejbName; 138 } 139 140 143 public void setJndiName(String jndiName) 144 { 145 if (jndiName == null) 146 return; 147 148 while (jndiName.startsWith("/")) 149 jndiName = jndiName.substring(1); 150 151 while (jndiName.endsWith("/")) 152 jndiName = jndiName.substring(0, jndiName.length() - 1); 153 154 155 _jndiName = jndiName; 156 } 157 158 161 public String getJndiName() 162 { 163 return _jndiName; 164 } 165 166 171 public String getProtocolId() 172 { 173 if (_serverId == null) { 174 String serverId = getJndiName(); 175 176 if (serverId == null) 177 serverId = getEJBName(); 178 179 if (serverId.startsWith("java:comp/env")) 180 serverId = serverId.substring(13); 181 182 if (! serverId.startsWith("/")) 183 serverId = "/" + serverId; 184 185 while (serverId.endsWith("/")) 186 serverId = serverId.substring(0, serverId.length() - 1); 187 188 _serverId = serverId; 189 } 190 191 return _serverId; 192 } 193 194 197 public void setContextImplClass(Class cl) 198 { 199 _contextImplClass = cl; 200 } 201 202 205 public void setRemoteHomeClass(Class cl) 206 { 207 _remoteHomeClass = cl; 208 } 209 210 213 public Class getRemoteHomeClass() 214 { 215 return _remoteHomeClass; 216 } 217 218 221 public void setRemoteObjectClass(Class cl) 222 { 223 _remoteObjectClass = cl; 224 } 225 226 229 public Class getRemoteObjectClass() 230 { 231 return _remoteObjectClass; 232 } 233 234 public HandleEncoder getHandleEncoder(String protocol) 235 { 236 HandleEncoder encoder; 237 238 if (_protocolEncoderMap != null) { 239 encoder = _protocolEncoderMap.get(protocol); 240 241 if (encoder != null) 242 return encoder; 243 } 244 245 try { 246 Class keyClass = getPrimaryKeyClass(); 247 248 encoder = _ejbManager.getProtocolManager().createHandleEncoder(this, keyClass, protocol); 249 } catch (Exception e) { 250 throw EJBExceptionWrapper.createRuntime(e); 251 } 252 253 if (_protocolEncoderMap == null) 254 _protocolEncoderMap = new HashMap <String ,HandleEncoder>(8); 255 256 _protocolEncoderMap.put(protocol, encoder); 257 258 return encoder; 259 } 260 261 264 public String encodeId(Object primaryKey) 265 { 266 return String.valueOf(primaryKey); 267 } 268 269 public HandleEncoder addHandleEncoder(String protocol, String serverId) 270 { 271 HandleEncoder encoder; 272 273 if (_protocolEncoderMap != null) { 274 encoder = _protocolEncoderMap.get(protocol); 275 276 if (encoder != null) 277 return encoder; 278 } 279 280 try { 281 Class keyClass = getPrimaryKeyClass(); 282 283 encoder = new HandleEncoder(this, serverId + _ejbName); 284 } catch (Exception e) { 285 throw EJBExceptionWrapper.createRuntime(e); 286 } 287 288 if (_protocolEncoderMap == null) 289 _protocolEncoderMap = new HashMap <String ,HandleEncoder>(8); 290 291 _protocolEncoderMap.put(protocol, encoder); 292 293 return encoder; 294 } 295 296 public HandleEncoder getHandleEncoder() 297 { 298 return getHandleEncoder(EjbProtocolManager.getThreadProtocol()); 299 } 300 301 public void setHandleEncoder(HandleEncoder encoder) 302 { 303 if (_homeHandle != null) 304 _homeHandle = null; 305 306 _handleEncoder = encoder; 307 } 308 309 public String getHandleServerId() 310 { 311 if (_handleServerId == null) 312 _handleServerId = getHandleEncoder().getServerId(); 313 314 return _handleServerId; 315 } 316 317 320 public Object lookup(String jndiName) 321 { 322 try { 323 if (_jndiEnv == null) 324 _jndiEnv = (Context) new InitialContext ().lookup("java:comp/env"); 325 326 return _jndiEnv.lookup(jndiName); 328 } catch (NamingException e) { 329 throw new IllegalArgumentException (e); 330 } 331 } 332 333 334 public UserTransaction getUserTransaction() 335 { 336 return _ejbManager.getTransactionManager().getUserTransaction(); 337 } 338 339 342 public EjbServerManager getContainer() 343 { 344 return _ejbManager; 345 } 346 347 350 public EjbServerManager getServerManager() 351 { 352 return _ejbManager; 353 } 354 355 358 public void setServerProgram(BuilderProgram serverProgram) 359 { 360 _serverProgram = serverProgram; 361 } 362 363 366 public BuilderProgram getServerProgram() 367 { 368 return _serverProgram; 369 } 370 371 374 public void setTransactionTimeout(long timeout) 375 { 376 _transactionTimeout = timeout; 377 } 378 379 382 public long getTransactionTimeout() 383 { 384 return _transactionTimeout; 385 } 386 387 390 public void invalidateCache() 391 { 392 } 393 394 397 public void remove(AbstractHandle handle) 398 { 399 throw new UnsupportedOperationException (); 400 } 401 402 405 public void remove(Object primaryKey) 406 { 407 throw new UnsupportedOperationException (); 408 } 409 410 413 public DynamicClassLoader getClassLoader() 414 { 415 return _loader; 416 } 417 418 421 public void setClassLoader(DynamicClassLoader loader) 422 { 423 _loader = loader; 424 } 425 426 429 public Class getBeanSkelClass() 430 { 431 return _contextImplClass; 432 } 433 434 public Class getRemoteStubClass() 435 { 436 return _remoteStubClass; 437 } 438 439 public Class getHomeStubClass() 440 { 441 return _homeStubClass; 442 } 443 444 447 public EJBMetaData getEJBMetaData() 448 { 449 if (_metaData == null) { 450 try { 451 _metaData = new EJBMetaDataImpl(getEJBHome(), 452 getRemoteHomeClass(), 453 getRemoteObjectClass(), 454 getPrimaryKeyClass()); 455 } catch (Exception e) { 456 log.log(Level.WARNING, e.toString(), e); 457 } 458 459 if (this instanceof StatelessServer) { 460 _metaData.setSession(true); 461 _metaData.setStatelessSession(true); 462 } 463 else if (this instanceof SessionServer) { 464 _metaData.setSession(true); 465 } 466 } 467 468 return _metaData; 469 } 470 471 474 public HomeHandle getHomeHandle() 475 { 476 if (_homeHandle == null) 477 _homeHandle = getHandleEncoder().createHomeHandle(); 478 479 return _homeHandle; 480 } 481 482 void setEJBHome(EJBHome remoteHome) 483 { 484 _remoteHome = remoteHome; 485 } 486 487 490 public EJBHome getEJBHome() 491 throws RemoteException 492 { 493 return _remoteHomeView; 494 } 495 496 499 EJBHome getClientHome() 500 throws RemoteException 501 { 502 return getEJBHome(); 503 } 504 505 508 public Object getHomeObject() 509 { 510 return _remoteHomeView; 511 } 512 513 516 public Object getRemoteObject() 517 { 518 return getHomeObject(); 519 } 520 521 524 public Object getClientLocalHome() 525 { 526 return _localHome; 527 } 528 529 532 public Object getClientObject() 533 { 534 return getClientLocalHome(); 535 } 536 537 540 public EJBLocalHome getEJBLocalHome() 541 { 542 return _localHome; 543 } 544 545 548 public Class getPrimaryKeyClass() 549 { 550 return _primaryKeyClass; 551 } 552 553 556 EJBLocalObject getEJBLocalObject(AbstractContext context) 557 { 558 throw new UnsupportedOperationException (); 559 } 560 561 public EJBObject getEJBObject(Object key) 562 throws FinderException 563 { 564 return getContext(key).getEJBObject(); 565 } 566 567 public AbstractContext getContext(Object key) 568 throws FinderException 569 { 570 return getContext(key, true); 571 } 572 573 public AbstractContext getContext(long key) 574 throws FinderException 575 { 576 return getContext(new Long (key)); 577 } 578 579 582 abstract public AbstractContext getContext(Object key, boolean forceLoad) 583 throws FinderException; 584 585 588 594 595 600 public EjbTransactionManager getTransactionManager() 601 { 602 return _ejbManager.getTransactionManager(); 603 } 604 605 610 public TransactionContext getTransaction() 611 { 612 return _ejbManager.getTransactionManager().getTransactionContext(); 613 } 614 615 618 public DataSource getDataSource() 619 { 620 return _dataSource; 621 } 622 623 626 public void setDataSource(DataSource dataSource) 627 { 628 _dataSource = dataSource; 629 } 630 631 634 public void setInitProgram(BuilderProgram init) 635 { 636 _initProgram = init; 637 } 638 639 642 public BuilderProgram getInitProgram() 643 { 644 return _initProgram; 645 } 646 647 650 public void initInstance(Object instance) 651 throws Throwable 652 { 653 if (_initProgram != null) { 654 Thread thread = Thread.currentThread(); 655 ClassLoader oldLoader = thread.getContextClassLoader(); 656 657 try { 658 thread.setContextClassLoader(_loader); 659 660 _initProgram.configure(instance); 661 662 } finally { 663 thread.setContextClassLoader(oldLoader); 664 } 665 } 666 } 667 668 public void init() 669 throws Exception 670 { 671 _loader.setId("EnvironmentLoader[ejb:" + getId() + "]"); 672 } 673 674 public void start() 675 throws Exception 676 { 677 } 678 679 682 public boolean isLocal() 683 { 684 return _localHome != null; 685 } 686 687 690 public boolean isRemote() 691 { 692 return _remoteHome != null || _remoteHomeView != null; 693 } 694 695 698 public boolean isDead() 699 { 700 return _ejbManager == null; 701 } 702 703 706 protected void destroy() 707 { 708 _ejbManager = null; 709 } 710 711 public Connection getConnection() 712 throws SQLException 713 { 714 return getDataSource().getConnection(); 715 } 716 717 public String toString() 718 { 719 return getClass().getSimpleName() + "[" + getEJBName() + "," + getJndiName() + "]"; 720 } 721 } 722 | Popular Tags |