1 22 package org.jboss.ejb; 23 24 26 import java.lang.reflect.Method ; 27 import java.rmi.RemoteException ; 28 import java.util.HashMap ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import javax.ejb.EJBHome ; 34 import javax.ejb.EJBLocalHome ; 35 import javax.ejb.EJBMetaData ; 36 import javax.ejb.EJBObject ; 37 import javax.ejb.Handle ; 38 import javax.ejb.HomeHandle ; 39 import javax.ejb.RemoveException ; 40 import javax.ejb.TimedObject ; 41 import javax.ejb.Timer ; 42 import javax.management.ObjectName ; 43 44 import org.jboss.invocation.Invocation; 45 import org.jboss.invocation.MarshalledInvocation; 46 import org.jboss.metadata.SessionMetaData; 47 48 59 public abstract class SessionContainer extends Container 60 { 61 65 protected Map homeMapping; 66 67 71 protected Map beanMapping; 72 73 77 protected Interceptor interceptor; 78 79 80 protected Class serviceEndpoint; 81 82 83 protected InstancePool instancePool; 84 85 86 public void setInstancePool(InstancePool ip) 87 { 88 if (ip == null) 89 throw new IllegalArgumentException ("Null pool"); 90 91 this.instancePool = ip; 92 ip.setContainer(this); 93 } 94 95 96 public InstancePool getInstancePool() 97 { 98 return instancePool; 99 } 100 101 102 public LocalProxyFactory getLocalProxyFactory() 103 { 104 return localProxyFactory; 105 } 106 107 108 public void addInterceptor(Interceptor in) 109 { 110 if (interceptor == null) 111 { 112 interceptor = in; 113 } 114 else 115 { 116 Interceptor current = interceptor; 117 while (current.getNext() != null) 118 { 119 current = current.getNext(); 120 } 121 122 current.setNext(in); 123 } 124 } 125 126 127 public Interceptor getInterceptor() 128 { 129 return interceptor; 130 } 131 132 133 public Class getServiceEndpoint() 134 { 135 return serviceEndpoint; 136 } 137 138 140 protected void createService() throws Exception 141 { 142 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 144 SecurityActions.setContextClassLoader(getClassLoader()); 145 146 try 147 { 148 if (metaData.getHome() != null) 150 homeInterface = classLoader.loadClass(metaData.getHome()); 151 if (metaData.getRemote() != null) 152 remoteInterface = classLoader.loadClass(metaData.getRemote()); 153 if (((SessionMetaData) metaData).getServiceEndpoint() != null) 154 { 155 serviceEndpoint = 156 classLoader.loadClass(((SessionMetaData) metaData).getServiceEndpoint()); 157 } 158 159 super.createService(); 161 162 checkCoherency(); 164 165 setupBeanMapping(); 167 168 setupHomeMapping(); 170 171 setupMarshalledInvocationMapping(); 173 174 createInvokers(); 175 176 createInstanceCache(); 177 178 createInstancePool(); 179 180 createPersistenceManager(); 181 182 createInterceptors(); 183 } 184 finally 185 { 186 SecurityActions.setContextClassLoader(oldCl); 188 } 189 } 190 191 194 protected abstract void setupHomeMapping() throws Exception ; 195 196 197 protected void setUpBeanMappingImpl(Map map, Method [] methods, String declaringClass) 198 throws NoSuchMethodException 199 { 200 for (int i = 0; i < methods.length; i++) 201 { 202 Method m = methods[i]; 203 if (m.getDeclaringClass().getName().equals(declaringClass) == false) 204 { 205 try 207 { 208 Method beanMethod = beanClass.getMethod(m.getName(), m.getParameterTypes()); 209 map.put(m, beanMethod); 210 } 211 catch (NoSuchMethodException ex) 212 { 213 throw new NoSuchMethodException ("Not found in bean class: " + m); 214 } 215 216 log.debug("Mapped " + m.getName() + " HASH " + m.hashCode() + "to " + map.get(m)); 217 } 218 else 219 { 220 try 222 { 223 Method containerMethod = getClass().getMethod(m.getName(), new Class []{Invocation.class}); 224 map.put(m, containerMethod); 225 } 226 catch (NoSuchMethodException e) 227 { 228 throw new NoSuchMethodException ("Not found in container class: " + m); 229 } 230 231 log.debug("Mapped Container method " + m.getName() + " HASH " + m.hashCode()); 232 } 233 } 234 } 235 236 237 protected void setupBeanMapping() throws NoSuchMethodException 238 { 239 Map map = new HashMap (); 240 241 if (remoteInterface != null) 242 { 243 Method [] m = remoteInterface.getMethods(); 244 setUpBeanMappingImpl(map, m, "javax.ejb.EJBObject"); 245 } 246 247 if (localInterface != null) 248 { 249 Method [] m = localInterface.getMethods(); 250 setUpBeanMappingImpl(map, m, "javax.ejb.EJBLocalObject"); 251 } 252 253 if (TimedObject .class.isAssignableFrom(beanClass)) 254 { 255 Method [] m = new Method []{TimedObject .class.getMethod("ejbTimeout", new Class []{Timer .class})}; 256 setUpBeanMappingImpl(map, m, "javax.ejb.Timer"); 257 } 258 259 if (serviceEndpoint != null) 260 { 261 Method [] m = serviceEndpoint.getMethods(); 262 setUpBeanMappingImpl(map, m, "java.rmi.Remote"); 263 } 264 265 beanMapping = map; 266 } 267 268 272 273 protected void setupMarshalledInvocationMapping() throws Exception 274 { 275 if (homeInterface != null) 277 { 278 Method [] m = homeInterface.getMethods(); 279 for (int i = 0; i < m.length; i++) 280 { 281 marshalledInvocationMapping.put(new Long (MarshalledInvocation.calculateHash(m[i])), m[i]); 282 } 283 } 284 285 if (remoteInterface != null) 286 { 287 Method [] m = remoteInterface.getMethods(); 288 for (int j = 0; j < m.length; j++) 289 { 290 marshalledInvocationMapping.put(new Long (MarshalledInvocation.calculateHash(m[j])), m[j]); 291 } 292 } 293 Method getEJBObjectMethod = 295 Class.forName("javax.ejb.Handle").getMethod("getEJBObject", 296 new Class [0]); 297 298 marshalledInvocationMapping.put(new Long (MarshalledInvocation.calculateHash(getEJBObjectMethod)), getEJBObjectMethod); 300 } 301 302 protected void checkCoherency() throws Exception 303 { 304 if (metaData.isClustered()) 307 { 308 boolean clusteredProxyFactoryFound = false; 309 Iterator it = proxyFactories.keySet().iterator(); 310 while (it.hasNext()) 311 { 312 String invokerBinding = (String ) it.next(); 313 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 314 if (ci instanceof org.jboss.proxy.ejb.ClusterProxyFactory) 315 clusteredProxyFactoryFound = true; 316 } 317 318 if (!clusteredProxyFactoryFound) 319 { 320 log.warn("*** EJB '" 321 + this.metaData.getEjbName() 322 + "' deployed as CLUSTERED but not a single clustered-invoker is bound to container ***"); 323 } 324 } 325 } 326 327 328 protected void createInstancePool() throws Exception 329 { 330 331 try 333 { 334 ObjectName containerName = super.getJmxName(); 335 Hashtable props = containerName.getKeyPropertyList(); 336 props.put("plugin", "pool"); 337 ObjectName poolName = new ObjectName (containerName.getDomain(), props); 338 server.registerMBean(instancePool, poolName); 339 } 340 catch (Throwable t) 341 { 342 log.debug("Failed to register pool as mbean", t); 343 } 344 instancePool.create(); 346 } 347 348 351 protected void createInstanceCache() throws Exception 352 { 353 } 354 355 356 protected void createInvokers() throws Exception 357 { 358 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 360 { 361 String invokerBinding = (String ) it.next(); 362 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 363 ci.create(); 364 } 365 } 366 367 368 protected void createInterceptors() throws Exception 369 { 370 Interceptor in = interceptor; 371 while (in != null) 372 { 373 in.setContainer(this); 374 in.create(); 375 in = in.getNext(); 376 } 377 } 378 379 382 protected void createPersistenceManager() throws Exception 383 { 384 } 385 386 protected void startService() throws Exception 387 { 388 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 390 SecurityActions.setContextClassLoader(getClassLoader()); 391 392 try 393 { 394 super.startService(); 396 397 startInvokers(); 398 399 startInstanceCache(); 400 401 startInstancePool(); 402 403 startPersistenceManager(); 404 405 startInterceptors(); 406 407 restoreTimers(); 409 } 410 finally 411 { 412 SecurityActions.setContextClassLoader(oldCl); 414 } 415 } 416 417 420 protected void startPersistenceManager() throws Exception 421 { 422 } 423 424 427 protected void startInstanceCache() throws Exception 428 { 429 } 430 431 432 protected void startInvokers() throws Exception 433 { 434 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 435 { 436 String invokerBinding = (String ) it.next(); 437 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 438 ci.start(); 439 } 440 } 441 442 443 protected void startInstancePool() throws Exception 444 { 445 instancePool.start(); 446 } 447 448 449 protected void startInterceptors() throws Exception 450 { 451 Interceptor in = interceptor; 452 while (in != null) 453 { 454 in.start(); 455 in = in.getNext(); 456 } 457 } 458 459 protected void stopService() throws Exception 460 { 461 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 463 SecurityActions.setContextClassLoader(getClassLoader()); 464 465 try 466 { 467 super.stopService(); 469 470 stopInvokers(); 471 472 stopInstanceCache(); 473 474 stopInstancePool(); 475 476 stopPersistenceManager(); 477 478 stopInterceptors(); 479 } 480 finally 481 { 482 SecurityActions.setContextClassLoader(oldCl); 484 } 485 } 486 487 488 protected void stopInterceptors() 489 { 490 Interceptor in = interceptor; 491 while (in != null) 492 { 493 in.stop(); 494 in = in.getNext(); 495 } 496 } 497 498 499 protected void stopPersistenceManager() 500 { 501 } 502 503 504 protected void stopInstancePool() 505 { 506 instancePool.stop(); 507 } 508 509 510 protected void stopInstanceCache() 511 { 512 } 513 514 515 protected void stopInvokers() 516 { 517 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 518 { 519 String invokerBinding = (String ) it.next(); 520 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 521 ci.stop(); 522 } 523 } 524 525 protected void destroyService() throws Exception 526 { 527 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 529 SecurityActions.setContextClassLoader(getClassLoader()); 530 531 try 532 { 533 destroyInvokers(); 534 535 destroyInstanceCache(); 536 537 destroyInstancePool(); 538 539 destroyPersistenceManager(); 540 541 destroyInterceptors(); 542 543 destroyMarshalledInvocationMapping(); 544 545 homeInterface = null; 546 remoteInterface = null; 547 serviceEndpoint = null; 548 beanMapping.clear(); 549 550 super.destroyService(); 552 } 553 finally 554 { 555 SecurityActions.setContextClassLoader(oldCl); 557 } 558 } 559 560 protected void destroyMarshalledInvocationMapping() 561 { 562 MarshalledInvocation.removeHashes(homeInterface); 563 MarshalledInvocation.removeHashes(remoteInterface); 564 } 565 566 protected void destroyInterceptors() 567 { 568 Interceptor in = interceptor; 570 while (in != null) 571 { 572 in.destroy(); 573 in.setContainer(null); 574 in = in.getNext(); 575 } 576 } 577 578 protected void destroyPersistenceManager() 579 { 580 } 581 582 protected void destroyInstancePool() 583 { 584 instancePool.destroy(); 586 instancePool.setContainer(null); 587 try 588 { 589 ObjectName containerName = super.getJmxName(); 590 Hashtable props = containerName.getKeyPropertyList(); 591 props.put("plugin", "pool"); 592 ObjectName poolName = new ObjectName (containerName.getDomain(), props); 593 server.unregisterMBean(poolName); 594 } 595 catch (Throwable ignore) 596 { 597 } 598 } 599 600 protected void destroyInstanceCache() 601 { 602 } 603 604 protected void destroyInvokers() 605 { 606 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 608 { 609 String invokerBinding = (String ) it.next(); 610 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 611 ci.destroy(); 612 ci.setContainer(null); 613 } 614 } 615 616 public Object internalInvokeHome(Invocation mi) throws Exception 617 { 618 Method method = mi.getMethod(); 619 if (method != null && method.getName().equals("remove")) 620 { 621 Object arg = mi.getArguments()[0]; 623 if (arg instanceof Handle ) 624 { 625 if (arg == null) 626 throw new RemoteException ("Null handle"); 627 Handle handle = (Handle ) arg; 628 EJBObject ejbObject = handle.getEJBObject(); 629 ejbObject.remove(); 630 return null; 631 } 632 else 633 throw new RemoveException ("EJBHome.remove(Object) not allowed for session beans"); 634 } 635 return getInterceptor().invokeHome(mi); 636 } 637 638 643 public Object internalInvoke(Invocation mi) throws Exception 644 { 645 return getInterceptor().invoke(mi); 647 } 648 649 651 658 public Handle getHandle(Invocation mi) throws RemoteException 659 { 660 661 return null; 663 } 664 665 public Object getPrimaryKey(Invocation mi) throws RemoteException 666 { 667 return getPrimaryKey(); 668 } 669 670 public Object getPrimaryKey() throws RemoteException 671 { 672 throw new RemoteException ("Call to getPrimaryKey not allowed on session bean"); 673 } 674 675 public EJBHome getEJBHome(Invocation mi) throws RemoteException 676 { 677 EJBProxyFactory ci = getProxyFactory(); 678 if (ci == null) 679 { 680 String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor"; 681 throw new IllegalStateException (msg); 682 } 683 684 return (EJBHome ) ci.getEJBHome(); 685 } 686 687 690 public boolean isIdentical(Invocation mi) throws RemoteException 691 { 692 EJBProxyFactory ci = getProxyFactory(); 693 if (ci == null) 694 { 695 String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor"; 696 throw new IllegalStateException (msg); 697 } 698 699 return ci.isIdentical(this, mi); 700 } 701 702 public EJBMetaData getEJBMetaDataHome(Invocation mi) throws RemoteException 703 { 704 return getEJBMetaDataHome(); 705 } 706 707 public EJBMetaData getEJBMetaDataHome() throws RemoteException 708 { 709 EJBProxyFactory ci = getProxyFactory(); 710 if (ci == null) 711 { 712 String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor"; 713 throw new IllegalStateException (msg); 714 } 715 716 return ci.getEJBMetaData(); 717 } 718 719 public HomeHandle getHomeHandleHome(Invocation mi) throws RemoteException 720 { 721 return getHomeHandleHome(); 722 } 723 724 public HomeHandle getHomeHandleHome() throws RemoteException 725 { 726 EJBProxyFactory ci = getProxyFactory(); 727 if (ci == null) 728 { 729 String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor"; 730 throw new IllegalStateException (msg); 731 } 732 733 EJBHome home = (EJBHome ) ci.getEJBHome(); 734 return home.getHomeHandle(); 735 } 736 737 739 741 public EJBLocalHome getEJBLocalHome(Invocation mi) 742 { 743 return localProxyFactory.getEJBLocalHome(); 744 } 745 746 750 protected Map getHomeMapping() 751 { 752 return homeMapping; 753 } 754 755 759 protected Map getBeanMapping() 760 { 761 return beanMapping; 762 } 763 764 } 765 | Popular Tags |