1 22 package org.jboss.ejb.plugins; 23 24 import org.jboss.ejb.EnterpriseContext; 25 import org.jboss.ejb.Container; 26 import org.jboss.ejb.EntityContainer; 27 import org.jboss.ejb.EntityEnterpriseContext; 28 import org.jboss.ejb.BeanLock; 29 import org.jboss.ejb.EntityCache; 30 import org.jboss.tm.TransactionLocal; 31 import org.jboss.logging.Logger; 32 33 import java.rmi.RemoteException ; 34 import java.rmi.NoSuchObjectException ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 38 46 public class PerTxEntityInstanceCache 47 implements EntityCache, PerTxEntityInstanceCacheMBean 48 { 49 private static final Logger log = Logger.getLogger(PerTxEntityInstanceCache.class); 50 51 private final TransactionLocal txLocalCache = new TransactionLocal() 53 { 54 protected Object initialValue() 55 { 56 return new HashMap (); 57 } 58 }; 59 60 private EntityContainer container; 61 62 64 public Object createCacheKey(Object id) 65 { 66 return id; 67 } 68 69 public EnterpriseContext get(Object id) throws RemoteException , NoSuchObjectException 70 { 71 if(id == null) throw new IllegalArgumentException ("Can't get an object with a null key"); 72 73 Map cache = getLocalCache(); 74 EntityEnterpriseContext instance = (EntityEnterpriseContext) cache.get(id); 75 if(instance == null) 76 { 77 try 78 { 79 instance = (EntityEnterpriseContext) container.getInstancePool().get(); 81 instance.setId(id); 83 instance.setCacheKey(id); 84 container.getPersistenceManager().activateEntity(instance); 86 cache.put(id, instance); 88 } 89 catch(Throwable x) 90 { 91 throw new NoSuchObjectException (x.getMessage()); 92 } 93 } 94 return instance; 95 } 96 97 public void insert(EnterpriseContext instance) 98 { 99 if(instance == null) throw new IllegalArgumentException ("Can't insert a null object in the cache"); 100 101 EntityEnterpriseContext entity = (EntityEnterpriseContext) instance; 102 getLocalCache().put(entity.getCacheKey(), instance); 103 } 104 105 public void release(EnterpriseContext instance) 106 { 107 if(instance == null) throw new IllegalArgumentException ("Can't release a null object"); 108 109 tryToPassivate(instance); 110 } 111 112 public void remove(Object id) 113 { 114 getLocalCache().remove(id); 115 } 116 117 public boolean isActive(Object id) 118 { 119 return getLocalCache().containsKey(id); 120 } 121 122 public long getCacheSize() 123 { 124 return 0; 125 } 126 127 public void flush() 128 { 129 } 130 131 133 public void setContainer(Container con) 134 { 135 this.container = (EntityContainer) con; 136 } 137 138 140 public void create() throws Exception 141 { 142 } 143 144 public void start() throws Exception 145 { 146 } 147 148 public void stop() 149 { 150 } 151 152 public void destroy() 153 { 154 } 155 156 158 protected void tryToPassivate(EnterpriseContext instance) 159 { 160 Object id = instance.getId(); 161 if(id != null) 162 { 163 BeanLock lock = container.getLockManager().getLock(id); 164 try 165 { 166 lock.sync(); 167 if(canPassivate(instance)) 168 { 169 try 170 { 171 remove(id); 172 EntityEnterpriseContext entity = (EntityEnterpriseContext) instance; 173 container.getPersistenceManager().passivateEntity(entity); 174 container.getInstancePool().free(instance); 175 } 176 catch(Exception ignored) 177 { 178 log.warn("failed to passivate, id=" + id, ignored); 179 } 180 } 181 else 182 { 183 log.warn("Unable to passivate due to ctx lock, id=" + id); 184 } 185 } 186 finally 187 { 188 lock.releaseSync(); 189 container.getLockManager().removeLockRef(id); 190 } 191 } 192 } 193 194 protected boolean canPassivate(EnterpriseContext ctx) 195 { 196 if(ctx.isLocked()) 197 { 198 return false; 200 } 201 202 if(ctx.getTransaction() != null) 203 { 204 return false; 205 } 206 207 Object key = ((EntityEnterpriseContext) ctx).getCacheKey(); 208 return container.getLockManager().canPassivate(key); 209 } 210 211 213 private Map getLocalCache() 214 { 215 return (Map ) txLocalCache.get(); 216 } 217 } 218 | Popular Tags |