1 22 package org.jboss.ejb3.cache; 23 24 import java.util.HashMap ; 25 import javax.ejb.EJBException ; 26 import javax.ejb.NoSuchEJBException ; 27 import org.jboss.ejb3.Container; 28 import org.jboss.ejb3.Pool; 29 import org.jboss.ejb3.stateful.StatefulBeanContext; 30 import org.jboss.util.id.GUID; 31 32 38 public class NoPassivationCache implements StatefulCache 39 { 40 private Pool pool; 41 private HashMap cacheMap; 42 43 public void initialize(Container container) throws Exception 44 { 45 this.pool = container.getPool(); 46 cacheMap = new HashMap (); 47 } 48 49 public NoPassivationCache() 50 { 51 } 52 53 public void start() 54 { 55 } 56 57 public void stop() 58 { 59 synchronized (cacheMap) 60 { 61 cacheMap.clear(); 62 } 63 } 64 65 public StatefulBeanContext create() 66 { 67 StatefulBeanContext ctx = null; 68 try 69 { 70 ctx = (StatefulBeanContext) pool.get(); 71 synchronized (cacheMap) 72 { 73 cacheMap.put(ctx.getId(), ctx); 74 } 75 } 76 catch (EJBException e) 77 { 78 e.printStackTrace(); 79 throw e; 80 } 81 catch (Exception e) 82 { 83 e.printStackTrace(); 84 throw new EJBException (e); 85 } 86 return ctx; 87 } 88 89 public StatefulBeanContext create(Class [] initTypes, Object [] initValues) 90 { 91 StatefulBeanContext ctx = null; 92 try 93 { 94 ctx = (StatefulBeanContext) pool.get(initTypes, initValues); 95 synchronized (cacheMap) 96 { 97 cacheMap.put(ctx.getId(), ctx); 98 } 99 } 100 catch (EJBException e) 101 { 102 throw e; 103 } 104 catch (Exception e) 105 { 106 throw new EJBException (e); 107 } 108 return ctx; 109 } 110 111 public StatefulBeanContext get(Object key) throws EJBException 112 { 113 StatefulBeanContext entry = null; 114 synchronized (cacheMap) 115 { 116 entry = (StatefulBeanContext) cacheMap.get(key); 117 } 118 if (entry == null) 119 { 120 throw new NoSuchEJBException ("Could not find Stateful bean: " + key); 121 } 122 entry.inUse = true; 123 entry.lastUsed = System.currentTimeMillis(); 124 return entry; 125 } 126 127 public void finished(StatefulBeanContext ctx) 128 { 129 synchronized (ctx) 130 { 131 ctx.inUse = false; 132 ctx.lastUsed = System.currentTimeMillis(); 133 } 134 } 135 136 public void remove(Object key) 137 { 138 StatefulBeanContext ctx = null; 139 synchronized (cacheMap) 140 { 141 ctx = (StatefulBeanContext) cacheMap.remove(key); 142 } 143 if (ctx != null) pool.remove(ctx); 144 } 145 146 147 } 148 | Popular Tags |