FileDocCategorySizeDatePackage
SimpleStatefulCache.javaAPI DocJBoss 4.2.114605Fri Jul 13 20:53:48 BST 2007org.jboss.ejb3.cache.simple

SimpleStatefulCache

public class SimpleStatefulCache extends Object implements org.jboss.ejb3.cache.StatefulCache
Comment
author
Bill Burke
version
$Revision: 62768 $

Fields Summary
private Logger
log
private org.jboss.ejb3.Pool
pool
private CacheMap
cacheMap
private int
maxSize
private StatefulSessionPersistenceManager
pm
private long
sessionTimeout
private long
removalTimeout
private SessionTimeoutTask
timeoutTask
private RemovalTimeoutTask
removalTask
private boolean
running
protected int
createCount
protected int
passivatedCount
protected int
removeCount
Constructors Summary
public SimpleStatefulCache()

   
Methods Summary
public org.jboss.ejb3.stateful.StatefulBeanContextcreate()

      StatefulBeanContext ctx = null;
      try
      {
         ctx = (StatefulBeanContext) pool.get();
         
         if (log.isTraceEnabled())
         {
            log.trace("Caching context " + ctx.getId() + " of type " + ctx.getClass());
         }
         synchronized (cacheMap)
         {
            cacheMap.put(ctx.getId(), ctx);
         }
         ctx.setInUse(true);
         ctx.lastUsed = System.currentTimeMillis();
         ++createCount;
      }
      catch (EJBException e)
      {
         e.printStackTrace();
         throw e;
      }
      catch (Exception e)
      {
         e.printStackTrace();
         throw new EJBException(e);
      }
      return ctx;
   
public org.jboss.ejb3.stateful.StatefulBeanContextcreate(java.lang.Class[] initTypes, java.lang.Object[] initValues)

      StatefulBeanContext ctx = null;
      try
      {
         ctx = (StatefulBeanContext) pool.get(initTypes, initValues);
         if (log.isTraceEnabled())
         {
            log.trace("Caching context " + ctx.getId() + " of type " + ctx.getClass());
         }
         synchronized (cacheMap)
         {
            cacheMap.put(ctx.getId(), ctx);
         }
         ctx.setInUse(true);
         ctx.lastUsed = System.currentTimeMillis();
         ++createCount;
      }
      catch (EJBException e)
      {
         e.printStackTrace();
         throw e;
      }
      catch (Exception e)
      {
         e.printStackTrace();
         throw new EJBException(e);
      }
      return ctx;
   
public voidfinished(org.jboss.ejb3.stateful.StatefulBeanContext ctx)

      synchronized (ctx)
      {
         ctx.setInUse(false);
         ctx.lastUsed = System.currentTimeMillis();
         if (ctx.markedForPassivation)
         {
            passivate(ctx);
         }
      }
   
public org.jboss.ejb3.stateful.StatefulBeanContextget(java.lang.Object key)

      return get(key, true);
   
public org.jboss.ejb3.stateful.StatefulBeanContextget(java.lang.Object key, boolean markInUse)

      StatefulBeanContext entry = null;
      synchronized (cacheMap)
      {
         entry = (StatefulBeanContext) cacheMap.get(key);
      }
      if (entry == null)
      {
         entry = (StatefulBeanContext) pm.activateSession(key);
         if (entry == null)
         {
            throw new NoSuchEJBException("Could not find stateful bean: " + key);
         }
         --passivatedCount;
          
         // We cache the entry even if we will throw an exception below
         // as we may still need it for its children and XPC references
         if (log.isTraceEnabled())
         {
            log.trace("Caching activated context " + entry.getId() + " of type " + entry.getClass());
         }

         synchronized (cacheMap)
         {
            cacheMap.put(key, entry);
         }
      }
      
      // Now we know entry isn't null
      if (markInUse)
      { 
         if (entry.isRemoved())
         {
            throw new NoSuchEJBException("Could not find stateful bean: " + key +
                                         " (bean was marked as removed");
         }      
      
         entry.setInUse(true);
         entry.lastUsed = System.currentTimeMillis();
      }
      
      return entry;
   
public intgetAvailableCount()

      return -1;
   
public intgetCacheSize()

	   return cacheMap.size();
   
public intgetCreateCount()

	   return createCount;
   
public intgetCurrentSize()

      return cacheMap.size();
   
public intgetMaxSize()

      return maxSize;
   
public intgetPassivatedCount()

	   return passivatedCount;
   
public intgetRemoveCount()

      return removeCount;
   
public intgetTotalSize()

      return cacheMap.size() + pm.getNumPassivatedBeans();
   
public voidinitialize(org.jboss.ejb3.Container container)

      Advisor advisor = (Advisor) container;
      this.pool = container.getPool();
      cacheMap = new CacheMap();
      PersistenceManager pmConfig = (PersistenceManager) advisor.resolveAnnotation(PersistenceManager.class);
      this.pm = (StatefulSessionPersistenceManager) pmConfig.value().newInstance();
      pm.initialize(container);
      CacheConfig config = (CacheConfig) advisor.resolveAnnotation(CacheConfig.class);
      maxSize = config.maxSize();
      sessionTimeout = config.idleTimeoutSeconds();
      removalTimeout = config.removalTimeoutSeconds();
      log = Logger.getLogger(getClass().getName() + "." + container.getEjbName());
      log.debug("Initializing SimpleStatefulCache with maxSize: " +maxSize + " timeout: " +sessionTimeout +
              " for " +container.getObjectName().getCanonicalName() );
      timeoutTask = new SessionTimeoutTask("SFSB Passivation Thread - " + container.getObjectName().getCanonicalName());
      
      if (removalTimeout > 0)
         removalTask = new RemovalTimeoutTask("SFSB Removal Thread - " + container.getObjectName().getCanonicalName());
   
protected voidpassivate(org.jboss.ejb3.stateful.StatefulBeanContext ctx)

      ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
      try
      {
         Thread.currentThread().setContextClassLoader(((EJBContainer) ctx.getContainer()).getClassloader());
         pm.passivateSession(ctx);
         ++passivatedCount;
      }
      finally
      {
         Thread.currentThread().setContextClassLoader(oldCl);
      }
   
public voidremove(java.lang.Object key)

      StatefulBeanContext ctx = null;
      synchronized (cacheMap)
      {
         ctx = (StatefulBeanContext) cacheMap.get(key);
      }
      if (ctx != null) 
      {
         if (!ctx.isRemoved())
            pool.remove(ctx);
         
         ++removeCount;
         
         if (ctx.getCanRemoveFromCache())
         {
            synchronized (cacheMap)
            {
               cacheMap.remove(key);
            }
         }
      }
   
public voidstart()

      running = true;
      timeoutTask.start();
      
      if (removalTask != null)
         removalTask.start();
   
public voidstop()

      synchronized (cacheMap)
      {
         running = false;
         timeoutTask.interrupt();
         if (removalTask != null)
            removalTask.interrupt();
         cacheMap.clear();
         try
         {
            pm.destroy();
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }
      }