1 22 package org.jboss.ejb.plugins; 23 24 import java.rmi.RemoteException ; 25 import java.util.LinkedList ; 26 import java.lang.reflect.UndeclaredThrowableException ; 27 import javax.ejb.EJBException ; 28 import javax.ejb.CreateException ; 29 30 import org.jboss.ejb.Container; 31 import org.jboss.ejb.InstancePool; 32 import org.jboss.ejb.EnterpriseContext; 33 34 import org.jboss.deployment.DeploymentException; 35 import org.jboss.metadata.MetaData; 36 import org.jboss.metadata.XmlLoadable; 37 import org.jboss.system.ServiceMBeanSupport; 38 39 import org.w3c.dom.Element ; 40 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore; 41 42 56 public abstract class AbstractInstancePool 57 extends ServiceMBeanSupport 58 implements AbstractInstancePoolMBean, InstancePool, XmlLoadable 59 { 60 62 67 private FIFOSemaphore strictMaxSize; 68 70 private long strictTimeout = Long.MAX_VALUE; 71 72 protected Container container; 73 74 protected LinkedList pool = new LinkedList (); 75 76 protected int maxSize = 30; 77 78 protected boolean reclaim = false; 79 80 81 83 85 87 93 public void setContainer(Container c) 94 { 95 this.container = c; 96 } 97 98 101 public Container getContainer() 102 { 103 return container; 104 } 105 106 110 public int getCurrentSize() 111 { 112 synchronized (pool) 113 { 114 return this.pool.size(); 115 } 116 } 117 118 122 public int getMaxSize() 123 { 124 return this.maxSize; 125 } 126 127 133 public long getAvailableCount() 134 { 135 long size = Long.MAX_VALUE; 136 if( strictMaxSize != null ) 137 size = strictMaxSize.permits(); 138 return size; 139 } 140 141 148 public EnterpriseContext get() 149 throws Exception 150 { 151 boolean trace = log.isTraceEnabled(); 152 if( trace ) 153 log.trace("Get instance "+this+"#"+pool.size()+"#"+getContainer().getBeanClass()); 154 155 if( strictMaxSize != null ) 156 { 157 boolean acquired = strictMaxSize.attempt(strictTimeout); 159 if( trace ) 160 log.trace("Acquired("+acquired+") strictMaxSize semaphore, remaining="+strictMaxSize.permits()); 161 if( acquired == false ) 162 throw new EJBException ("Failed to acquire the pool semaphore, strictTimeout="+strictTimeout); 163 } 164 165 synchronized (pool) 166 { 167 if ( pool.isEmpty() == false ) 168 { 169 return (EnterpriseContext) pool.removeFirst(); 170 } 171 } 172 173 try 175 { 176 Object instance = container.createBeanClassInstance(); 177 return create(instance); 178 } 179 catch (Throwable e) 180 { 181 if( strictMaxSize != null ) 183 { 184 strictMaxSize.release(); 185 } 186 if( e instanceof CreateException ) 188 throw (CreateException ) e; 189 190 Exception ex = null; 192 if(e instanceof Exception ) 193 { 194 ex = (Exception )e; 195 } else 196 { 197 ex = new UndeclaredThrowableException (e); 198 } 199 throw new EJBException ("Could not instantiate bean", ex); 200 } 201 } 202 203 212 public void free(EnterpriseContext ctx) 213 { 214 if( log.isTraceEnabled() ) 215 { 216 String msg = pool.size() + "/" + maxSize+" Free instance:"+this 217 +"#"+ctx.getId() 218 +"#"+ctx.getTransaction() 219 +"#"+reclaim 220 +"#"+getContainer().getBeanClass(); 221 log.trace(msg); 222 } 223 224 ctx.clear(); 225 226 try 227 { 228 boolean addedToPool = false; 231 232 synchronized (pool) 233 { 234 if (pool.size() < maxSize) 235 { 236 pool.addFirst(ctx); 237 addedToPool = true; 238 } 239 } 240 241 if (addedToPool) 242 { 243 if(strictMaxSize != null) 245 { 246 strictMaxSize.release(); 247 } 248 } 249 else 250 { 251 discard(ctx); 254 } 255 } 256 catch (Exception ignored) 257 { 258 } 259 } 260 261 public void discard(EnterpriseContext ctx) 262 { 263 if( log.isTraceEnabled() ) 264 { 265 String msg = "Discard instance:"+this+"#"+ctx 266 +"#"+ctx.getTransaction() 267 +"#"+reclaim 268 +"#"+getContainer().getBeanClass(); 269 log.trace(msg); 270 } 271 272 if( strictMaxSize != null ) 274 strictMaxSize.release(); 275 276 try 278 { 279 ctx.discard(); 280 } 281 catch (RemoteException e) 282 { 283 if( log.isTraceEnabled() ) 284 log.trace("Ctx.discard error", e); 285 } 286 } 287 288 public void clear() 289 { 290 synchronized (pool) 291 { 292 freeAll(); 293 } 294 } 295 296 299 public void importXml(Element element) throws DeploymentException 300 { 301 String maximumSize = MetaData.getElementContent(MetaData.getUniqueChild(element, "MaximumSize")); 302 try 303 { 304 this.maxSize = Integer.parseInt(maximumSize); 305 } 306 catch (NumberFormatException e) 307 { 308 throw new DeploymentException("Invalid MaximumSize value for instance pool configuration"); 309 } 310 311 String strictValue = MetaData.getElementContent(MetaData.getOptionalChild(element, "strictMaximumSize")); 313 Boolean strictFlag = Boolean.valueOf(strictValue); 314 if( strictFlag == Boolean.TRUE ) 315 this.strictMaxSize = new FIFOSemaphore(this.maxSize); 316 String delay = MetaData.getElementContent(MetaData.getOptionalChild(element, "strictTimeout")); 317 try 318 { 319 if( delay != null ) 320 this.strictTimeout = Long.parseLong(delay); 321 } 322 catch (NumberFormatException e) 323 { 324 throw new DeploymentException("Invalid strictTimeout value for instance pool configuration"); 325 } 326 } 327 328 330 protected abstract EnterpriseContext create(Object instance) 332 throws Exception ; 333 334 protected void destroyService() throws Exception 335 { 336 freeAll(); 337 this.container = null; 338 } 339 340 342 345 private void freeAll() 346 { 347 LinkedList clone = (LinkedList )pool.clone(); 348 for (int i = 0; i < clone.size(); i++) 349 { 350 EnterpriseContext ec = (EnterpriseContext)clone.get(i); 351 ec.clear(); 353 discard(ec); 354 } 355 pool.clear(); 356 } 357 358 360 } 361 | Popular Tags |