1 45 46 47 package org.openejb.core.stateless; 48 49 50 import java.lang.reflect.Method ; 51 import java.rmi.RemoteException ; 52 import java.util.HashMap ; 53 import java.util.Properties ; 54 55 import javax.ejb.EJBHome ; 56 import javax.ejb.EJBLocalHome ; 57 import javax.ejb.EJBLocalObject ; 58 import javax.ejb.EJBObject ; 59 import javax.ejb.EnterpriseBean ; 60 import javax.ejb.SessionBean ; 61 62 import org.openejb.Container; 63 import org.openejb.DeploymentInfo; 64 import org.openejb.OpenEJB; 65 import org.openejb.OpenEJBException; 66 import org.openejb.ProxyInfo; 67 import org.openejb.core.EnvProps; 68 import org.openejb.core.Operations; 69 import org.openejb.core.ThreadContext; 70 import org.openejb.core.transaction.TransactionContainer; 71 import org.openejb.core.transaction.TransactionContext; 72 import org.openejb.core.transaction.TransactionPolicy; 73 import org.openejb.util.SafeProperties; 74 import org.openejb.util.SafeToolkit; 75 76 83 public class StatelessContainer implements org.openejb.RpcContainer, TransactionContainer{ 84 85 86 StatelessInstanceManager instanceManager; 87 88 89 HashMap deploymentRegistry; 90 91 92 Object containerID = null; 93 94 95 96 98 99 110 public void init(Object id, HashMap registry, Properties properties) 111 throws org.openejb.OpenEJBException{ 112 containerID = id; 113 deploymentRegistry = registry; 114 115 if(properties == null)properties = new Properties (); 116 117 118 SafeToolkit toolkit = SafeToolkit.getToolkit("StatelessContainer"); 119 SafeProperties safeProps = toolkit.getSafeProperties(properties); 120 try{ 121 String className = safeProps.getProperty(EnvProps.IM_CLASS_NAME, "org.openejb.core.stateless.StatelessInstanceManager"); 122 ClassLoader cl = OpenEJB.getContextClassLoader(); 123 instanceManager =(StatelessInstanceManager)Class.forName(className, true, cl).newInstance(); 124 }catch(Exception e){ 125 throw new org.openejb.SystemException("Initialization of InstanceManager for the \""+containerID+"\" stateful container failed",e); 126 } 127 instanceManager.init(properties); 128 129 130 132 138 org.openejb.DeploymentInfo [] deploys = this.deployments(); 139 for(int x = 0; x < deploys.length; x++){ 140 org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)deploys[x]; 141 di.setContainer(this); 142 } 143 144 } 145 146 150 156 public DeploymentInfo [] deployments(){ 157 return (DeploymentInfo [])deploymentRegistry.values().toArray(new DeploymentInfo[deploymentRegistry.size()]); 158 } 159 160 168 public DeploymentInfo getDeploymentInfo(Object deploymentID){ 169 return (DeploymentInfo)deploymentRegistry.get(deploymentID); 170 } 171 176 public int getContainerType( ){ 177 return Container.STATELESS; 178 } 179 180 185 public Object getContainerID(){ 186 return containerID; 187 } 188 189 197 public void deploy(Object deploymentID, DeploymentInfo info) throws OpenEJBException { 198 HashMap registry = (HashMap )deploymentRegistry.clone(); 199 registry.put(deploymentID, info); 200 deploymentRegistry = registry; 201 } 202 203 215 public Object invoke(Object deployID, Method callMethod,Object [] args,Object primKey, Object securityIdentity) 216 throws org.openejb.OpenEJBException { 217 try{ 218 219 org.openejb.core.DeploymentInfo deployInfo = (org.openejb.core.DeploymentInfo)this.getDeploymentInfo(deployID); 220 221 ThreadContext callContext = ThreadContext.getThreadContext(); 222 callContext.set(deployInfo, primKey, securityIdentity); 223 224 226 boolean authorized = OpenEJB.getSecurityService().isCallerAuthorized(securityIdentity, deployInfo.getAuthorizedRoles(callMethod)); 227 if(!authorized) 228 throw new org.openejb.ApplicationException(new RemoteException ("Unauthorized Access by Principal Denied")); 229 230 Class declaringClass = callMethod.getDeclaringClass(); 232 if(EJBHome .class.isAssignableFrom(declaringClass) || EJBLocalHome .class.isAssignableFrom(declaringClass) ){ 233 if(callMethod.getName().equals("create")){ 234 return createEJBObject(deployInfo, callMethod); 235 }else 236 return null; } else if(EJBObject .class == declaringClass || EJBLocalObject .class == declaringClass) { 238 return null; } 240 241 242 SessionBean bean = null; 243 244 bean = (SessionBean )instanceManager.getInstance(callContext); 246 247 callContext.setCurrentOperation(Operations.OP_BUSINESS); 249 250 Method runMethod = deployInfo.getMatchingBeanMethod(callMethod); 252 253 Object retValue = invoke(callMethod, runMethod, args, bean, callContext) ; 254 instanceManager.poolInstance(callContext,bean); 255 256 return deployInfo.convertIfLocalReference(callMethod, retValue); 258 259 }finally{ 260 272 ThreadContext.setThreadContext(null); 273 } 274 } 275 279 280 284 public StatelessInstanceManager getInstanceManager( ){ 285 return instanceManager; 286 } 287 288 protected Object invoke(Method callMethod, Method runMethod, Object [] args, EnterpriseBean bean, ThreadContext callContext) 289 throws org.openejb.OpenEJBException{ 290 291 TransactionPolicy txPolicy = callContext.getDeploymentInfo().getTransactionPolicy( callMethod ); 292 TransactionContext txContext = new TransactionContext(); 293 txContext.callContext = callContext; 294 295 txPolicy.beforeInvoke( bean, txContext ); 297 298 300 Object returnValue = null; 301 try{ 302 returnValue = runMethod.invoke(bean, args); 304 }catch(java.lang.reflect.InvocationTargetException ite){ if ( ite.getTargetException() instanceof RuntimeException ) { 306 307 txPolicy.handleSystemException( ite.getTargetException(), bean, txContext ); 309 } else { 310 311 instanceManager.poolInstance(callContext,bean); 312 txPolicy.handleApplicationException( ite.getTargetException(), txContext ); 314 } 315 }catch(Throwable re){ 324 txPolicy.handleSystemException( re, bean, txContext ); 325 }finally{ 326 txPolicy.afterInvoke( bean, txContext ); 328 } 329 330 return returnValue; 331 } 332 333 340 protected ProxyInfo createEJBObject(org.openejb.core.DeploymentInfo deploymentInfo, Method callMethod) { 341 Class callingClass = callMethod.getDeclaringClass(); 342 boolean isLocalInterface = EJBLocalHome .class.isAssignableFrom(callingClass); 343 return new ProxyInfo(deploymentInfo, null, isLocalInterface, this); 344 } 345 346 350 public void discardInstance(EnterpriseBean instance, ThreadContext context) { 351 instanceManager.discardInstance( context, instance ); 352 } 353 354 } 355 | Popular Tags |