1 22 package org.jboss.ejb; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.net.URL ; 27 import java.security.AccessController ; 28 import java.security.Policy ; 29 import java.security.PrivilegedActionException ; 30 import java.security.PrivilegedExceptionAction ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.rmi.MarshalException ; 37 38 import javax.ejb.EJBException ; 39 import javax.ejb.EJBObject ; 40 import javax.ejb.TimedObject ; 41 import javax.ejb.Timer ; 42 import javax.ejb.TimerService ; 43 import javax.ejb.spi.HandleDelegate ; 44 import javax.management.MBeanException ; 45 import javax.management.MalformedObjectNameException ; 46 import javax.management.ObjectName ; 47 import javax.naming.Context ; 48 import javax.naming.InitialContext ; 49 import javax.naming.LinkRef ; 50 import javax.naming.NamingException ; 51 import javax.naming.Reference ; 52 import javax.naming.StringRefAddr ; 53 import javax.transaction.TransactionManager ; 54 import javax.xml.soap.SOAPMessage ; 55 56 import org.jboss.deployers.spi.deployer.DeploymentUnit; 57 import org.jboss.deployment.DeploymentException; 58 import org.jboss.deployment.DeploymentInfo; 59 import org.jboss.ejb.plugins.local.BaseLocalProxyFactory; 60 import org.jboss.ejb.txtimer.EJBTimerService; 61 import org.jboss.invocation.*; 62 import org.jboss.logging.Logger; 63 import org.jboss.metadata.ApplicationMetaData; 64 import org.jboss.metadata.BeanMetaData; 65 import org.jboss.metadata.EjbLocalRefMetaData; 66 import org.jboss.metadata.EjbRefMetaData; 67 import org.jboss.metadata.EnvEntryMetaData; 68 import org.jboss.metadata.MessageDestinationMetaData; 69 import org.jboss.metadata.MessageDestinationRefMetaData; 70 import org.jboss.metadata.ResourceEnvRefMetaData; 71 import org.jboss.metadata.ResourceRefMetaData; 72 import org.jboss.mx.util.ObjectNameConverter; 73 import org.jboss.mx.util.ObjectNameFactory; 74 import org.jboss.naming.ENCThreadLocalKey; 75 import org.jboss.naming.NonSerializableFactory; 76 import org.jboss.naming.Util; 77 import org.jboss.security.AnybodyPrincipal; 78 import org.jboss.security.AuthenticationManager; 79 import org.jboss.security.AuthorizationManager; 80 import org.jboss.security.RealmMapping; 81 import org.jboss.system.ServiceMBeanSupport; 82 import org.jboss.util.NestedError; 83 import org.jboss.util.NestedRuntimeException; 84 import org.jboss.webservice.ServiceRefHandler; 85 import org.jboss.webservice.ServiceRefHandlerFactory; 86 import org.omg.CORBA.ORB ; 87 88 112 public abstract class Container extends ServiceMBeanSupport 113 implements ContainerMBean, AllowedOperationsFlags 114 { 115 public final static String BASE_EJB_CONTAINER_NAME = 116 "jboss.j2ee:service=EJB"; 117 118 public final static ObjectName ORB_NAME = ObjectNameFactory.create("jboss:service=CorbaORB"); 119 120 public final static ObjectName EJB_CONTAINER_QUERY_NAME = 121 ObjectNameFactory.create(BASE_EJB_CONTAINER_NAME + ",*"); 122 123 protected static final Method EJBOBJECT_REMOVE; 124 125 protected static final Method EJB_TIMEOUT; 126 127 128 protected EjbModule ejbModule; 129 130 135 protected ClassLoader classLoader; 136 137 138 protected ClassLoader webClassLoader; 139 140 143 private DeploymentUnit di; 144 145 150 protected BeanMetaData metaData; 151 152 153 protected Class beanClass; 154 155 156 protected Class homeInterface; 157 158 159 protected Class remoteInterface; 160 161 162 protected Class localHomeInterface; 163 164 165 protected Class localInterface; 166 167 168 protected TransactionManager tm; 169 170 171 protected AuthenticationManager sm; 172 173 176 protected AuthorizationManager authorizationManager; 177 178 179 protected RealmMapping rm; 180 181 182 protected Object securityProxy; 183 184 185 protected BeanLockManager lockManager; 186 187 188 protected LocalProxyFactory localProxyFactory = 189 new BaseLocalProxyFactory(); 190 191 192 private HashMap methodPermissionsCache = new HashMap (); 193 194 195 protected Map marshalledInvocationMapping = new HashMap (); 196 197 198 private ObjectName jmxName; 199 200 protected HashMap proxyFactories = new HashMap (); 201 202 private MBeanServerAction serverAction = new MBeanServerAction(); 203 204 208 protected ThreadLocal proxyFactoryTL = new ThreadLocal (); 209 210 211 protected long createCount; 212 213 protected long removeCount; 214 215 protected InvocationStatistics invokeStats = new InvocationStatistics(); 216 217 218 protected String jaccContextID; 219 220 223 protected boolean isJaccEnabled = false; 224 225 static 226 { 227 try 228 { 229 EJBOBJECT_REMOVE = EJBObject .class.getMethod("remove", new Class [0]); 230 EJB_TIMEOUT = TimedObject .class.getMethod("ejbTimeout", new Class []{Timer .class}); 231 } 232 catch (Throwable t) 233 { 234 throw new NestedRuntimeException(t); 235 } 236 } 237 238 240 public Class getLocalClass() 241 { 242 return localInterface; 243 } 244 245 public Class getLocalHomeClass() 246 { 247 return localHomeInterface; 248 } 249 250 public Class getRemoteClass() 251 { 252 return remoteInterface; 253 } 254 255 259 public Class getHomeClass() 260 { 261 return homeInterface; 262 } 263 264 269 public boolean isCallByValue() 270 { 271 if (ejbModule.isCallByValue()) 272 return true; 273 return metaData.isCallByValue(); 274 } 275 276 283 public void setTransactionManager(final TransactionManager tm) 284 { 285 this.tm = tm; 286 } 287 288 293 public TransactionManager getTransactionManager() 294 { 295 return tm; 296 } 297 298 public void setSecurityManager(AuthenticationManager sm) 299 { 300 this.sm = sm; 301 } 302 303 public AuthenticationManager getSecurityManager() 304 { 305 return sm; 306 } 307 308 313 public AuthorizationManager getAuthorizationManager() 314 { 315 return authorizationManager; 316 } 317 318 323 public void setAuthorizationManager(AuthorizationManager authorizationManager) 324 { 325 this.authorizationManager = authorizationManager; 326 } 327 328 public BeanLockManager getLockManager() 329 { 330 return lockManager; 331 } 332 333 public void setLockManager(final BeanLockManager lockManager) 334 { 335 this.lockManager = lockManager; 336 lockManager.setContainer(this); 337 } 338 339 public void addProxyFactory(String invokerBinding, EJBProxyFactory factory) 340 { 341 proxyFactories.put(invokerBinding, factory); 342 } 343 344 public void setRealmMapping(final RealmMapping rm) 345 { 346 this.rm = rm; 347 } 348 349 public RealmMapping getRealmMapping() 350 { 351 return rm; 352 } 353 354 public void setSecurityProxy(Object proxy) 355 { 356 this.securityProxy = proxy; 357 } 358 359 public Object getSecurityProxy() 360 { 361 return securityProxy; 362 } 363 364 public EJBProxyFactory getProxyFactory() 365 { 366 EJBProxyFactory factory = (EJBProxyFactory)proxyFactoryTL.get(); 367 if (factory == null && remoteInterface != null) 374 { 375 Iterator i = proxyFactories.values().iterator(); 376 if (i.hasNext()) 377 factory = (EJBProxyFactory) i.next(); 378 } 379 return factory; 380 } 381 382 public void setProxyFactory(Object factory) 383 { 384 proxyFactoryTL.set(factory); 385 } 386 387 public EJBProxyFactory lookupProxyFactory(String binding) 388 { 389 return (EJBProxyFactory) proxyFactories.get(binding); 390 } 391 392 397 public final DeploymentInfo getDeploymentInfo() 398 { 399 return null; 400 } 401 406 public final void setDeploymentInfo(DeploymentInfo di) 407 { 408 } 409 410 public final DeploymentUnit getDeploymentUnit() 411 { 412 return di; 413 } 414 public final void setDeploymentUnit(DeploymentUnit di) 415 { 416 this.di = di; 417 } 418 419 425 public void setEjbModule(EjbModule app) 426 { 427 ejbModule = app; 428 } 429 430 public String getJaccContextID() 431 { 432 return jaccContextID; 433 } 434 public void setJaccContextID(String id) 435 { 436 jaccContextID = id; 437 } 438 439 443 public boolean isJaccEnabled() 444 { 445 return isJaccEnabled; 446 } 447 448 453 public void setJaccEnabled(boolean isJaccEnabled) 454 { 455 this.isJaccEnabled = isJaccEnabled; 456 } 457 458 463 public EjbModule getEjbModule() 464 { 465 return ejbModule; 466 } 467 468 472 public long getCreateCount() 473 { 474 return createCount; 475 } 476 477 481 public long getRemoveCount() 482 { 483 return removeCount; 484 } 485 486 489 public InvocationStatistics getInvokeStats() 490 { 491 return invokeStats; 492 } 493 494 500 public void setClassLoader(ClassLoader cl) 501 { 502 this.classLoader = cl; 503 } 504 505 510 public ClassLoader getClassLoader() 511 { 512 return classLoader; 513 } 514 515 517 public ClassLoader getWebClassLoader() 518 { 519 return webClassLoader; 520 } 521 522 524 public void setWebClassLoader(final ClassLoader webClassLoader) 525 { 526 this.webClassLoader = webClassLoader; 527 } 528 529 535 public void setBeanMetaData(final BeanMetaData metaData) 536 { 537 this.metaData = metaData; 538 } 539 540 544 public Context getEnvContext() throws NamingException 545 { 546 ClassLoader ccl = SecurityActions.getContextClassLoader(); 547 try 548 { 549 SecurityActions.setContextClassLoader(classLoader); 551 return (Context )new InitialContext ().lookup("java:comp/env"); 552 } 553 finally 554 { 555 SecurityActions.setContextClassLoader(ccl); 556 } 557 } 558 559 565 public BeanMetaData getBeanMetaData() 566 { 567 return metaData; 568 } 569 570 575 public Set getMethodPermissions(Method m, InvocationType iface) 576 { 577 Set permissions; 578 579 if (methodPermissionsCache.containsKey(m)) 580 { 581 permissions = (Set ) methodPermissionsCache.get(m); 582 } 583 else if( m.equals(EJB_TIMEOUT) ) 584 { 585 permissions = new HashSet (); 587 permissions.add(AnybodyPrincipal.ANYBODY_PRINCIPAL); 588 methodPermissionsCache.put(m, permissions); 589 } 590 else 591 { 592 String name = m.getName(); 593 Class [] sig = m.getParameterTypes(); 594 permissions = getBeanMetaData().getMethodPermissions(name, sig, iface); 595 methodPermissionsCache.put(m, permissions); 596 } 597 598 return permissions; 599 } 600 601 606 public Class getBeanClass() 607 { 608 return beanClass; 609 } 610 611 622 public Object createBeanClassInstance() throws Exception 623 { 624 return getBeanClass().newInstance(); 625 } 626 627 639 650 654 public ObjectName getJmxName() 655 { 656 if (jmxName == null) 657 { 658 BeanMetaData beanMetaData = getBeanMetaData(); 659 if (beanMetaData == null) 660 { 661 throw new IllegalStateException ("Container metaData is null"); 662 } 663 664 String jndiName = beanMetaData.getContainerObjectNameJndiName(); 665 if (jndiName == null) 666 { 667 throw new IllegalStateException ("Container jndiName is null"); 668 } 669 670 String name = BASE_EJB_CONTAINER_NAME + ",jndiName=" + jndiName; 672 try 673 { 674 jmxName = ObjectNameConverter.convert(name); 675 } 676 catch (MalformedObjectNameException e) 677 { 678 throw new RuntimeException ("Failed to create ObjectName, msg=" + e.getMessage()); 679 } 680 } 681 return jmxName; 682 } 683 684 696 public TimerService getTimerService(Object pKey) 697 throws IllegalStateException 698 { 699 if (this instanceof StatefulSessionContainer) 700 throw new IllegalStateException ("Statefull Session Beans are not allowed to access the TimerService"); 701 702 Class beanClass = this.getBeanClass(); 704 if( TimedObject .class.isAssignableFrom(beanClass) == false ) 705 { 706 String msg = this.getBeanMetaData().getEjbName() 707 +" requested getTimerService but "+beanClass 708 +" does not implement javax.ejb.TimedObject"; 709 throw new IllegalStateException (msg); 710 } 711 712 TimerService timerService = null; 713 try 714 { 715 EJBTimerService service = (EJBTimerService)SecurityActions.getMBeanProxy(EJBTimerService.class, EJBTimerService.OBJECT_NAME, server); 716 timerService = service.createTimerService(getJmxName(), pKey, this); 717 } 718 catch (Exception e) 719 { 720 throw new EJBException ("Could not create timer service", e); 721 } 722 return timerService; 723 } 724 725 733 public void removeTimerService(Object pKey) 734 throws IllegalStateException 735 { 736 try 737 { 738 EJBTimerService service = (EJBTimerService)SecurityActions.getMBeanProxy(EJBTimerService.class, EJBTimerService.OBJECT_NAME, server); 739 if (pKey != null) 740 { 741 service.removeTimerService(getJmxName(), pKey); 743 } 744 else 745 { 746 service.removeTimerService(getJmxName(), getBeanMetaData().getTimerPersistence()); 749 } 750 } 751 catch (Exception e) 752 { 753 log.error("Could not remove timer service", e); 754 } 755 } 756 757 760 protected void restoreTimers() 761 { 762 try 763 { 764 server.invoke( 766 EJBTimerService.OBJECT_NAME, 767 "restoreTimers", 768 new Object [] { getServiceName(), getClassLoader() }, 769 new String [] { "javax.management.ObjectName" , "java.lang.ClassLoader" } ); 770 } 771 catch (Exception e) 772 { 773 log.warn("Could not restore ejb timers", e); 774 } 775 } 776 777 789 protected void createService() throws Exception 790 { 791 beanClass = classLoader.loadClass(metaData.getEjbClass()); 793 794 if (metaData.getLocalHome() != null) 795 localHomeInterface = classLoader.loadClass(metaData.getLocalHome()); 796 if (metaData.getLocal() != null) 797 localInterface = classLoader.loadClass(metaData.getLocal()); 798 799 localProxyFactory.setContainer(this); 800 localProxyFactory.create(); 801 if (localHomeInterface != null) 802 ejbModule.addLocalHome(this, localProxyFactory.getEJBLocalHome()); 803 ejbModule.createMissingPermissions(this, metaData); 804 Policy.getPolicy().refresh(); 806 } 807 808 820 protected void startService() throws Exception 821 { 822 setupEnvironment(); 824 825 localProxyFactory.start(); 826 } 827 828 833 protected void stopService() throws Exception 834 { 835 localProxyFactory.stop(); 836 removeTimerService(null); 837 teardownEnvironment(); 838 } 839 840 845 protected void destroyService() throws Exception 846 { 847 localProxyFactory.destroy(); 848 ejbModule.removeLocalHome(this); 849 850 beanClass = null; 851 homeInterface = null; 852 remoteInterface = null; 853 localHomeInterface = null; 854 localInterface = null; 855 methodPermissionsCache.clear(); 856 invokeStats.resetStats(); 857 marshalledInvocationMapping.clear(); 858 } 859 860 870 public abstract Object internalInvokeHome(Invocation mi) 871 throws Exception ; 872 873 878 public abstract Object internalInvoke(Invocation mi) 879 throws Exception ; 880 881 abstract Interceptor createContainerInterceptor(); 882 883 public abstract void addInterceptor(Interceptor in); 884 885 893 public Object invoke(Invocation mi) 894 throws Exception 895 { 896 ClassLoader callerClassLoader = SecurityActions.getContextClassLoader(); 897 long start = System.currentTimeMillis(); 898 Method m = null; 899 900 boolean setCl = false; 901 Object type = null; 902 String contextID = getJaccContextID(); 903 try 904 { 905 if(!this.classLoader.equals(callerClassLoader)) 906 { 907 setCl = true; 908 SecurityActions.setContextClassLoader(this.classLoader); 909 } 910 911 mi.setValue(InvocationKey.JACC_CONTEXT_ID, contextID); 913 contextID = SecurityActions.setContextID(contextID); 914 if( mi.getType() != InvocationType.SERVICE_ENDPOINT ) 916 { 917 EJBArgsPolicyContextHandler.setArgs(mi.getArguments()); 918 } 919 else 920 { 921 SOAPMessage msg = (SOAPMessage ) mi.getValue(InvocationKey.SOAP_MESSAGE); 922 SOAPMsgPolicyContextHandler.setMessage(msg); 923 } 924 BeanMetaDataPolicyContextHandler.setMetaData(this.getBeanMetaData()); 926 927 type = mi.getType(); 930 931 this.invokeStats.callIn(); 933 934 if (type == InvocationType.REMOTE || 935 type == InvocationType.LOCAL || 936 type == InvocationType.SERVICE_ENDPOINT) 938 { 939 if (mi instanceof MarshalledInvocation) 940 { 941 ((MarshalledInvocation) mi).setMethodMap( 942 marshalledInvocationMapping); 943 944 if (log.isTraceEnabled()) 945 { 946 log.trace("METHOD REMOTE INVOKE " + 947 mi.getObjectName() + "||" + 948 mi.getMethod().getName() + "||"); 949 } 950 } 951 952 m = mi.getMethod(); 953 954 Object obj = internalInvoke(mi); 955 return obj; 956 } 957 else if (type == InvocationType.HOME || 958 type == InvocationType.LOCALHOME) 959 { 960 if (mi instanceof MarshalledInvocation) 961 { 962 963 ((MarshalledInvocation) mi).setMethodMap( 964 marshalledInvocationMapping); 965 966 if (log.isTraceEnabled()) 967 { 968 log.trace("METHOD HOME INVOKE " + 969 mi.getObjectName() + "||" + 970 mi.getMethod().getName() + "||" + 971 mi.getArguments().toString()); 972 } 973 } 974 m = mi.getMethod(); 975 976 Object obj = internalInvokeHome(mi); 977 return obj; 978 } 979 else 980 { 981 throw new MBeanException (new IllegalArgumentException ( 982 "Unknown invocation type: " + type)); 983 } 984 } 985 990 catch (JBossLazyUnmarshallingException e) 991 { 992 InvocationType calltype = mi.getType(); 993 boolean isLocal = 994 calltype == InvocationType.LOCAL || 995 calltype == InvocationType.LOCALHOME; 996 997 if (isLocal) 1000 { 1001 throw new EJBException ("UnmarshalException", e); 1002 } 1003 else 1004 { 1005 throw new MarshalException ("MarshalException", e); 1006 } 1007 } 1008 finally 1009 { 1010 if (m != null) 1011 { 1012 long end = System.currentTimeMillis(); 1013 long elapsed = end - start; 1014 this.invokeStats.updateStats(m, elapsed); 1015 } 1016 1017 this.invokeStats.callOut(); 1019 1020 if(setCl) 1022 { 1023 SecurityActions.setContextClassLoader(callerClassLoader); 1024 } 1025 contextID = SecurityActions.setContextID(contextID); 1027 1028 if (mi.getType() == InvocationType.SERVICE_ENDPOINT) 1029 { 1030 SOAPMsgPolicyContextHandler.setMessage(null); 1032 } 1033 else 1034 { 1035 EJBArgsPolicyContextHandler.setArgs(null); 1037 } 1038 } 1039 } 1040 1041 1043 1048 private void setupEnvironment() throws Exception 1049 { 1050 BeanMetaData beanMetaData = getBeanMetaData(); 1051 1052 log.debug("Begin java:comp/env for EJB: " + beanMetaData.getEjbName()); 1054 ClassLoader tcl = SecurityActions.getContextClassLoader(); 1055 log.debug("TCL: " + tcl); 1056 1057 ORB orb = null; 1058 HandleDelegate hd = null; 1059 try 1060 { 1061 orb = (ORB ) server.getAttribute(ORB_NAME, "ORB"); 1062 hd = (HandleDelegate ) server.getAttribute(ORB_NAME, "HandleDelegate"); 1063 } 1064 catch (Throwable t) 1065 { 1066 log.debug("Unable to retrieve orb" + t.toString()); 1067 } 1068 1069 Context ctx = (Context ) new InitialContext ().lookup("java:comp"); 1072 1073 if (orb != null) 1075 { 1076 NonSerializableFactory.rebind(ctx, "ORB", orb); 1077 log.debug("Bound java:comp/ORB for EJB: " + getBeanMetaData().getEjbName()); 1078 1079 NonSerializableFactory.rebind(ctx, "HandleDelegate", hd); 1080 log.debug("Bound java:comp:/HandleDelegate for EJB: " + getBeanMetaData().getEjbName()); 1081 } 1082 1083 Context envCtx = ctx.createSubcontext("env"); 1084 1085 { 1087 Iterator i = beanMetaData.getEnvironmentEntries(); 1088 while (i.hasNext()) 1089 { 1090 EnvEntryMetaData entry = (EnvEntryMetaData) i.next(); 1091 log.debug("Binding env-entry: " + entry.getName() + " of type: " + 1092 entry.getType() + " to value:" + entry.getValue()); 1093 1094 EnvEntryMetaData.bindEnvEntry(envCtx, entry); 1095 } 1096 } 1097 1098 { 1100 Iterator i = beanMetaData.getEjbReferences(); 1101 while (i.hasNext()) 1102 { 1103 EjbRefMetaData ref = (EjbRefMetaData) i.next(); 1104 log.debug("Binding an EJBReference " + ref.getName()); 1105 1106 if (ref.getLink() != null) 1107 { 1108 String linkName = ref.getLink(); 1110 String jndiName = EjbUtil.findEjbLink(server, di, linkName); 1111 log.debug("Binding " + ref.getName() + 1112 " to ejb-link: " + linkName + " -> " + jndiName); 1113 1114 if (jndiName == null) 1115 { 1116 String msg = "Failed to resolve ejb-link: " + linkName 1117 + " make by ejb-name: " + ref.getName(); 1118 throw new DeploymentException(msg); 1119 } 1120 1121 Util.bind(envCtx, 1122 ref.getName(), 1123 new LinkRef (jndiName)); 1124 1125 } 1126 else 1127 { 1128 Iterator it = beanMetaData.getInvokerBindings(); 1130 Reference reference = null; 1131 while (it.hasNext()) 1132 { 1133 String invokerBinding = (String ) it.next(); 1134 String name = ref.getInvokerBinding(invokerBinding); 1136 if (name == null) 1138 name = ref.getJndiName(); 1139 if (name == null) 1140 { 1141 throw new DeploymentException 1142 ("ejb-ref " + ref.getName() + 1143 ", expected either ejb-link in ejb-jar.xml or " + 1144 "jndi-name in jboss.xml"); 1145 } 1146 1147 StringRefAddr addr = new StringRefAddr (invokerBinding, name); 1148 log.debug("adding " + invokerBinding + ":" + name + 1149 " to Reference"); 1150 1151 if (reference == null) 1152 { 1153 reference = new Reference ("javax.naming.LinkRef", 1154 ENCThreadLocalKey.class.getName(), 1155 null); 1156 } 1157 reference.add(addr); 1158 } 1159 1160 if (reference != null) 1162 { 1163 if (ref.getJndiName() != null) 1164 { 1165 StringRefAddr addr = 1167 new StringRefAddr ("default", ref.getJndiName()); 1168 reference.add(addr); 1169 } 1170 if (reference.size() == 1 && reference.get("default") == null) 1171 { 1172 1176 StringRefAddr addr = (StringRefAddr ) reference.get(0); 1177 String target = (String ) addr.getContent(); 1178 StringRefAddr addr1 = new StringRefAddr ("default", target); 1179 reference.add(addr1); 1180 } 1181 Util.bind(envCtx, ref.getName(), reference); 1182 } 1183 else 1184 { 1185 if (ref.getJndiName() == null) 1187 { 1188 throw new DeploymentException("ejb-ref " + ref.getName() + 1189 ", expected either ejb-link in ejb-jar.xml " + 1190 "or jndi-name in jboss.xml"); 1191 } 1192 Util.bind(envCtx, 1193 ref.getName(), 1194 new LinkRef (ref.getJndiName())); 1195 } 1196 } 1197 } 1198 } 1199 1200 { 1202 Iterator i = beanMetaData.getEjbLocalReferences(); 1203 while (i.hasNext()) 1204 { 1205 EjbLocalRefMetaData ref = (EjbLocalRefMetaData) i.next(); 1206 String refName = ref.getName(); 1207 log.debug("Binding an EJBLocalReference " + ref.getName()); 1208 1209 if (ref.getLink() != null) 1210 { 1211 log.debug("Binding " + refName + " to bean source: " + ref.getLink()); 1213 1214 String jndiName = EjbUtil.findLocalEjbLink(server, di, 1215 ref.getLink()); 1216 1217 Util.bind(envCtx, 1218 ref.getName(), 1219 new LinkRef (jndiName)); 1220 } 1221 else 1222 { 1223 if (ref.getJndiName() == null) 1225 { 1226 throw new DeploymentException("ejb-local-ref " + ref.getName() + 1227 ", expected either ejb-link in ejb-jar.xml " + 1228 "or local-jndi-name in jboss.xml"); 1229 } 1230 Util.bind(envCtx, 1231 ref.getName(), 1232 new LinkRef (ref.getJndiName())); 1233 } 1234 } 1235 } 1236 1237 Iterator serviceRefs = metaData.getServiceReferences().values().iterator(); 1239 ServiceRefHandler refHandler = ServiceRefHandlerFactory.newInstance(); 1240 if (refHandler != null && serviceRefs.hasNext()) 1241 refHandler.setupServiceRefEnvironment(envCtx, serviceRefs, di); 1242 1243 { 1245 Iterator i = beanMetaData.getResourceReferences(); 1246 1247 ApplicationMetaData application = 1249 beanMetaData.getApplicationMetaData(); 1250 1251 while (i.hasNext()) 1252 { 1253 ResourceRefMetaData ref = (ResourceRefMetaData) i.next(); 1254 1255 String resourceName = ref.getResourceName(); 1256 String finalName = application.getResourceByName(resourceName); 1257 String resType = ref.getType(); 1258 if (finalName == null) 1261 finalName = ref.getJndiName(); 1262 1263 if (finalName == null && resType.equals("java.net.URL") == false) 1264 { 1265 1268 if (ref.getType().equals("javax.sql.DataSource")) 1269 { 1270 Context dsCtx = new InitialContext (); 1272 try 1273 { 1274 dsCtx.lookup("java:/DefaultDS"); 1276 finalName = "java:/DefaultDS"; 1277 } 1278 catch (Exception e) 1279 { 1280 log.debug("failed to lookup DefaultDS; ignoring", e); 1281 } 1282 finally 1283 { 1284 dsCtx.close(); 1285 } 1286 } 1287 1288 if (finalName == null) 1291 { 1292 log.warn("No resource manager found for " + 1293 ref.getResourceName()); 1294 continue; 1295 } 1296 } 1297 1298 if (resType.equals("java.net.URL")) 1299 { 1300 if( ref.getResURL() != null ) 1302 { 1303 log.debug("Binding URL: " + ref.getRefName() + 1305 " to JDNI ENC as: " + ref.getResURL()); 1306 URL resURL = new URL (ref.getResURL()); 1307 Util.bind(envCtx, ref.getRefName(), resURL); 1308 } 1309 else 1310 { 1311 log.debug("Binding URL: " + ref.getRefName() + " to: " + finalName); 1312 Object bind = null; 1313 if( ref.getJndiName() != null ) 1314 { 1315 bind = new LinkRef (finalName); 1317 } 1318 else 1319 { 1320 bind = new URL (finalName); 1322 } 1323 Util.bind(envCtx, ref.getRefName(), bind); 1324 } 1325 } 1326 else 1327 { 1328 log.debug("Binding resource manager: " + ref.getRefName() + 1330 " to JDNI ENC as: " + finalName); 1331 Util.bind(envCtx, ref.getRefName(), new LinkRef (finalName)); 1332 } 1333 } 1334 } 1335 1336 { 1338 Iterator i = beanMetaData.getResourceEnvReferences(); 1339 while (i.hasNext()) 1340 { 1341 ResourceEnvRefMetaData resRef = 1342 (ResourceEnvRefMetaData) i.next(); 1343 String encName = resRef.getRefName(); 1344 String jndiName = resRef.getJndiName(); 1345 log.debug("Binding env resource: " + encName + 1347 " to JDNI ENC as: " + jndiName); 1348 Util.bind(envCtx, encName, new LinkRef (jndiName)); 1349 } 1350 } 1351 1352 { 1354 Iterator i = beanMetaData.getMessageDestinationReferences(); 1355 1356 while (i.hasNext()) 1357 { 1358 MessageDestinationRefMetaData ref = (MessageDestinationRefMetaData) i.next(); 1359 1360 String refName = ref.getRefName(); 1361 String jndiName = ref.getJNDIName(); 1362 String link = ref.getLink(); 1363 if (link != null) 1364 { 1365 if (jndiName == null) 1366 { 1367 MessageDestinationMetaData messageDestination = getMessageDestination(link); 1368 if (messageDestination == null) 1369 throw new DeploymentException("message-destination-ref '" + refName + 1370 "' message-destination-link '" + link + "' not found and no jndi-name in jboss.xml"); 1371 else 1372 { 1373 String linkJNDIName = messageDestination.getJNDIName(); 1374 if (linkJNDIName == null) 1375 log.warn("message-destination '" + link + "' has no jndi-name in jboss.xml"); 1376 else 1377 jndiName = linkJNDIName; 1378 } 1379 } 1380 else 1381 log.warn("message-destination-ref '" + refName + 1382 "' ignoring message-destination-link '" + link + "' because it has a jndi-name in jboss.xml"); 1383 } 1384 else if (jndiName == null) 1385 throw new DeploymentException("message-destination-ref '" + refName + 1386 "' has no message-destination-link in ejb-jar.xml and no jndi-name in jboss.xml"); 1387 Util.bind(envCtx, refName, new LinkRef (jndiName)); 1388 } 1389 } 1390 1391 1395 String securityDomain = 1396 metaData.getContainerConfiguration().getSecurityDomain(); 1397 if (securityDomain == null) 1398 securityDomain = metaData.getApplicationMetaData().getSecurityDomain(); 1399 if (securityDomain != null) 1400 { 1401 log.debug("Binding securityDomain: " + securityDomain + 1402 " to JDNI ENC as: security/security-domain"); 1403 1404 Util.bind( envCtx, "security/security-domain", new LinkRef (securityDomain)); 1405 Util.bind( envCtx, "security/subject", new LinkRef (securityDomain + "/subject")); 1406 Util.bind(envCtx, "security/realmMapping", new LinkRef (securityDomain + "/realmMapping")); 1407 Util.bind(envCtx, "security/authorizationMgr", new LinkRef (securityDomain+"/authorizationMgr")); 1408 } 1409 1410 log.debug("End java:comp/env for EJB: " + beanMetaData.getEjbName()); 1411 } 1412 1413 public MessageDestinationMetaData getMessageDestination(String link) 1414 { 1415 return EjbUtil.findMessageDestination(server, di, link); 1416 } 1417 1418 1425 private void teardownEnvironment() throws Exception 1426 { 1427 Context ctx = (Context ) new InitialContext ().lookup("java:comp"); 1428 ctx.unbind("env"); 1429 log.debug("Removed bindings from java:comp/env for EJB: " + getBeanMetaData().getEjbName()); 1430 try 1431 { 1432 NonSerializableFactory.unbind("ORB"); 1433 log.debug("Unbound java:comp/ORB for EJB: " + getBeanMetaData().getEjbName()); 1434 1435 NonSerializableFactory.unbind("HandleDelegate"); 1436 log.debug("Unbound java:comp/HandleDelegate for EJB: " + getBeanMetaData().getEjbName()); 1437 } 1438 catch (NamingException ignored) 1439 { 1440 } 1441 } 1442 1443 1450 protected abstract class AbstractContainerInterceptor 1451 implements Interceptor 1452 { 1453 protected final Logger log = Logger.getLogger(this.getClass()); 1454 1455 public void setContainer(Container con) 1456 { 1457 } 1458 1459 public void setNext(Interceptor interceptor) 1460 { 1461 } 1462 1463 public Interceptor getNext() 1464 { 1465 return null; 1466 } 1467 1468 public void create() 1469 { 1470 } 1471 1472 public void start() 1473 { 1474 } 1475 1476 public void stop() 1477 { 1478 } 1479 1480 public void destroy() 1481 { 1482 } 1483 1484 protected void rethrow(Exception e) 1485 throws Exception 1486 { 1487 if (e instanceof IllegalAccessException ) 1488 { 1489 throw new EJBException (e); 1491 } 1492 else if (e instanceof InvocationTargetException ) 1493 { 1494 Throwable t = ((InvocationTargetException ) e).getTargetException(); 1495 1496 if (t instanceof EJBException ) 1497 { 1498 throw (EJBException ) t; 1499 } 1500 else if (t instanceof Exception ) 1501 { 1502 throw (Exception ) t; 1503 } 1504 else if (t instanceof Error ) 1505 { 1506 throw (Error ) t; 1507 } 1508 else 1509 { 1510 throw new NestedError("Unexpected Throwable", t); 1511 } 1512 } 1513 1514 throw e; 1515 } 1516 1517 1519 public void sample(Object s) 1520 { 1521 } 1523 1524 public Map retrieveStatistic() 1525 { 1526 return null; 1527 } 1528 1529 public void resetStatistic() 1530 { 1531 } 1532 } 1533 1534 1537 class MBeanServerAction implements PrivilegedExceptionAction 1538 { 1539 private ObjectName target; 1540 String method; 1541 Object [] args; 1542 String [] sig; 1543 1544 MBeanServerAction() 1545 { 1546 } 1547 MBeanServerAction(ObjectName target, String method, Object [] args, String [] sig) 1548 { 1549 this.target = target; 1550 this.method = method; 1551 this.args = args; 1552 this.sig = sig; 1553 } 1554 1555 public Object run() throws Exception 1556 { 1557 Object rtnValue = server.invoke(target, method, args, sig); 1558 return rtnValue; 1559 } 1560 Object invoke(ObjectName target, String method, Object [] args, String [] sig) 1561 throws Exception 1562 { 1563 SecurityManager sm = System.getSecurityManager(); 1564 Object rtnValue = null; 1565 if( sm == null ) 1566 { 1567 rtnValue = server.invoke(target, method, args, sig); 1569 } 1570 else 1571 { 1572 try 1573 { 1574 MBeanServerAction action = new MBeanServerAction(target, method, args, sig); 1576 rtnValue = AccessController.doPrivileged(action); 1577 } 1578 catch (PrivilegedActionException e) 1579 { 1580 Exception ex = e.getException(); 1581 throw ex; 1582 } 1583 } 1584 return rtnValue; 1585 } 1586 } 1587} 1588 | Popular Tags |