1 22 package org.jboss.ejb.plugins; 23 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Iterator ; 27 import java.rmi.RemoteException ; 28 import javax.transaction.Status ; 29 import javax.transaction.SystemException ; 30 31 import org.jboss.ejb.Container; 32 import org.jboss.ejb.StatefulSessionContainer; 33 import org.jboss.ejb.EnterpriseContext; 34 import org.jboss.ejb.StatefulSessionEnterpriseContext; 35 import org.jboss.ejb.StatefulSessionPersistenceManager; 36 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 37 38 45 public class StatefulSessionInstanceCache extends AbstractInstanceCache 46 { 47 49 51 52 private StatefulSessionContainer m_container; 53 54 57 private ConcurrentReaderHashMap passivatedIDs = new ConcurrentReaderHashMap(); 58 59 60 private HashSet activating = new HashSet (); 61 62 63 private StringBuffer buffer = new StringBuffer (); 64 65 67 69 71 75 public long getPassivatedCount() 76 { 77 return passivatedIDs.size(); 78 } 79 80 81 public void setContainer(Container c) 82 { 83 m_container = (StatefulSessionContainer) c; 84 } 85 86 public void destroy() 87 { 88 synchronized (this) 89 { 90 this.m_container = null; 91 } 92 passivatedIDs.clear(); 93 super.destroy(); 94 } 95 96 98 100 protected synchronized Container getContainer() 101 { 102 return m_container; 103 } 104 105 protected void passivate(EnterpriseContext ctx) throws RemoteException 106 { 107 m_container.getPersistenceManager().passivateSession((StatefulSessionEnterpriseContext) ctx); 108 passivatedIDs.put(ctx.getId(), new Long (System.currentTimeMillis())); 109 } 110 111 protected void activate(EnterpriseContext ctx) throws RemoteException 112 { 113 m_container.getPersistenceManager().activateSession((StatefulSessionEnterpriseContext) ctx); 114 passivatedIDs.remove(ctx.getId()); 115 } 116 117 protected boolean doActivate(EnterpriseContext ctx) throws RemoteException 118 { 119 Object id = ctx.getId(); 120 synchronized (activating) 121 { 122 if (activating.contains(id)) 124 return false; 125 activating.add(id); 126 } 127 try 128 { 129 return super.doActivate(ctx); 130 } 131 finally 132 { 133 synchronized (activating) 134 { 135 activating.remove(id); 136 } 137 } 138 } 139 140 protected EnterpriseContext acquireContext() throws Exception 141 { 142 return m_container.getInstancePool().get(); 143 } 144 145 protected void freeContext(EnterpriseContext ctx) 146 { 147 m_container.getInstancePool().free(ctx); 148 } 149 150 protected Object getKey(EnterpriseContext ctx) 151 { 152 return ctx.getId(); 153 } 154 155 protected void setKey(Object id, EnterpriseContext ctx) 156 { 157 ctx.setId(id); 158 } 159 160 protected boolean canPassivate(EnterpriseContext ctx) 161 { 162 if (ctx.isLocked()) 163 { 164 return false; 166 } 167 else if (m_container.getLockManager().canPassivate(ctx.getId()) == false) 168 { 169 return false; 170 } 171 else 172 { 173 if (ctx.getTransaction() != null) 174 { 175 try 176 { 177 return (ctx.getTransaction().getStatus() == Status.STATUS_NO_TRANSACTION); 178 } 179 catch (SystemException e) 180 { 181 return false; 183 } 184 } 185 } 186 return true; 187 } 188 189 193 protected void removePassivated(long maxLifeAfterPassivation) 194 { 195 StatefulSessionPersistenceManager store = m_container.getPersistenceManager(); 196 long now = System.currentTimeMillis(); 197 log.debug("removePassivated, now="+now+", maxLifeAfterPassivation="+maxLifeAfterPassivation); 198 boolean trace = log.isTraceEnabled(); 199 Iterator entries = passivatedIDs.entrySet().iterator(); 200 while (entries.hasNext()) 201 { 202 Map.Entry entry = (Map.Entry ) entries.next(); 203 Object key = entry.getKey(); 204 Long value = (Long ) entry.getValue(); 205 if (value != null) 206 { 207 long passivationTime = value.longValue(); 208 if (now - passivationTime > maxLifeAfterPassivation) 209 { 210 preRemovalPreparation(key); 211 store.removePassivated(key); 212 if (trace) 213 log(key, passivationTime); 214 entries.remove(); 216 postRemovalCleanup(key); 217 } 218 } 219 } 220 } 221 222 224 protected void preRemovalPreparation(Object key) 225 { 226 } 228 229 protected void postRemovalCleanup(Object key) 230 { 231 } 233 234 236 private void log(Object key, long passivationTime) 237 { 238 if (log.isTraceEnabled()) 239 { 240 buffer.setLength(0); 241 buffer.append("Removing from storage bean '"); 242 buffer.append(m_container.getBeanMetaData().getEjbName()); 243 buffer.append("' with id = "); 244 buffer.append(key); 245 buffer.append(", passivationTime="); 246 buffer.append(passivationTime); 247 log.trace(buffer.toString()); 248 } 249 } 250 251 253 } | Popular Tags |