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.EnterpriseBean ; 56 import javax.ejb.SessionBean ; 57 58 import org.apache.log4j.Category; 59 import org.openejb.OpenEJBException; 60 import org.openejb.SystemException; 61 import org.openejb.core.DeploymentInfo; 62 import org.openejb.core.EnvProps; 63 import org.openejb.core.Operations; 64 import org.openejb.core.ThreadContext; 65 import org.openejb.util.LinkedListStack; 66 import org.openejb.util.SafeProperties; 67 import org.openejb.util.SafeToolkit; 68 import org.openejb.util.Stack; 69 70 79 80 public class StatelessInstanceManager { 81 82 protected java.util.HashMap poolMap = new HashMap (); 83 protected int poolLimit = 0; 84 protected int beanCount = 0; 85 protected boolean strictPooling = false; 86 87 protected PoolQueue poolQueue= null; 88 89 protected final SafeToolkit toolkit = SafeToolkit.getToolkit("StatefulInstanceManager"); 90 protected final static Category logger = Category.getInstance("OpenEJB"); 91 92 95 public StatelessInstanceManager( ){ 96 } 97 98 public void init(Properties props) 99 throws OpenEJBException{ 100 101 102 SafeProperties safeProps = toolkit.getSafeProperties(props); 103 104 poolLimit = safeProps.getPropertyAsInt(EnvProps.IM_POOL_SIZE, 10); 105 strictPooling = safeProps.getPropertyAsBoolean(EnvProps.IM_STRICT_POOLING,new Boolean (false)).booleanValue(); 106 if(strictPooling){ 107 int waitTime = safeProps.getPropertyAsInt(EnvProps.IM_TIME_OUT, 0); 108 poolQueue = new PoolQueue(waitTime); 109 } 110 } 111 public EnterpriseBean getInstance(ThreadContext callContext) 112 throws OpenEJBException{ 113 SessionBean bean = null; 114 Object deploymentId = callContext.getDeploymentInfo().getDeploymentID(); 115 Stack pool = (Stack)poolMap.get(deploymentId); 116 if(pool==null){ 117 pool = new LinkedListStack(poolLimit); 118 poolMap.put(deploymentId,pool); 119 }else 120 bean = (SessionBean )pool.pop(); 121 122 while(strictPooling && bean == null && pool.size() >= poolLimit){ 124 poolQueue.waitForAvailableInstance(); 125 bean = (SessionBean )pool.pop(); 126 } 127 128 if(bean==null){ 130 try{ 131 Class beanClass = callContext.getDeploymentInfo().getBeanClass(); 132 bean = (SessionBean )toolkit.newInstance(beanClass); 133 }catch(OpenEJBException oee){ 134 throw (SystemException)oee; 135 } 136 137 byte originalOperation = callContext.getCurrentOperation(); 138 try{ 139 callContext.setCurrentOperation(Operations.OP_SET_CONTEXT); 141 DeploymentInfo deploymentInfo = callContext.getDeploymentInfo(); 142 bean.setSessionContext((javax.ejb.SessionContext )deploymentInfo.getEJBContext()); 143 callContext.setCurrentOperation(Operations.OP_CREATE); 145 Method createMethod = deploymentInfo.getCreateMethod(); 146 createMethod.invoke(bean, null); 147 }catch(Throwable e){ 148 if(e instanceof java.lang.reflect.InvocationTargetException ) { 149 e = ((java.lang.reflect.InvocationTargetException )e).getTargetException(); 150 } 151 String t = "The bean instance "+bean+" threw a system exception:"+e; 152 logger.error(t, e); 153 throw new org.openejb.ApplicationException(new RemoteException ("Can not obtain a free instance.")); 154 } finally { 155 callContext.setCurrentOperation( originalOperation ); 156 } 157 } 158 return bean; 159 } 160 public void poolInstance(ThreadContext callContext, EnterpriseBean bean) 161 throws OpenEJBException{ 162 if(bean == null) 163 throw new SystemException("Invalid arguments"); 164 Object deploymentId = callContext.getDeploymentInfo().getDeploymentID(); 165 Stack pool = (Stack)poolMap.get(deploymentId); 166 if(strictPooling){ 167 pool.push(bean); 168 poolQueue.notifyWaitingThreads(); 169 }else{ 170 if(pool.size() > poolLimit) 171 freeInstance(callContext,bean); 172 else 173 pool.push(bean); 174 } 175 176 } 177 178 public void freeInstance(ThreadContext callContext, EnterpriseBean bean){ 179 try{ 180 callContext.setCurrentOperation(Operations.OP_REMOVE); 181 ((SessionBean )bean).ejbRemove(); 182 }catch(Throwable re){ 183 logger.error("The bean instance "+bean+" threw a system exception:"+re, re); 185 } 186 187 } 189 190 195 public void discardInstance(ThreadContext callContext, EnterpriseBean bean){ 196 } 198 199 static class PoolQueue{ 200 private final long waitPeriod; 201 public PoolQueue(long time){waitPeriod = time;} 202 public synchronized void waitForAvailableInstance( ) 203 throws org.openejb.InvalidateReferenceException{ 204 try{ 205 wait(waitPeriod); 206 }catch(InterruptedException ie){ 207 throw new org.openejb.InvalidateReferenceException(new RemoteException ("No instance avaiable to service request")); 208 } 209 } 210 public synchronized void notifyWaitingThreads(){ 211 notify(); 212 } 213 } 214 215 } 216 | Popular Tags |