1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.io.BufferedInputStream ; 29 import java.io.BufferedOutputStream ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileNotFoundException ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.rmi.RemoteException ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 import javax.ejb.EJBException ; 40 import javax.ejb.SessionBean ; 41 import javax.ejb.TimerService ; 42 43 import org.objectweb.jonas_ejb.deployment.api.SessionDesc; 44 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc; 45 import org.objectweb.jonas_ejb.lib.EJBInvocation; 46 47 import org.objectweb.util.monolog.api.BasicLevel; 48 49 53 public class JStatefulFactory extends JSessionFactory { 54 55 59 protected HashMap statefulList = new HashMap (); 60 61 64 protected int cacheSize = 0; 65 66 69 private int sessionCount = 0; 70 71 76 public JStatefulFactory(SessionStatefulDesc dd, JContainer cont) { 77 super((SessionDesc) dd, cont); 78 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 79 isSynchro = javax.ejb.SessionSynchronization .class.isAssignableFrom(beanclass); 80 isStateful = true; 81 } 82 83 87 90 public int getPoolSize() { 91 return sessionList.size(); 92 } 93 94 97 public void reduceCache() { 98 } 99 100 103 public void initInstancePool() { 104 } 105 106 110 116 public RequestCtx preInvoke(int txa) { 117 if (TraceEjb.isDebugIc()) { 118 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 119 } 120 RequestCtx rctx = super.preInvoke(txa); 121 return rctx; 122 } 123 124 129 public void checkSecurity(EJBInvocation ejbInv) { 130 if (TraceEjb.isDebugIc()) { 131 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 132 } 133 super.checkSecurity(ejbInv); 134 } 135 136 141 public void postInvoke(RequestCtx rctx) { 142 if (TraceEjb.isDebugIc()) { 143 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 144 } 145 super.postInvoke(rctx); 146 } 147 148 152 156 public TimerService getTimerService() { 157 throw new EJBException ("No TimerService for Stateful Session beans"); 158 } 159 160 164 public JSessionSwitch createNewSession() throws RemoteException { 165 JStatefulSwitch bs = new JStatefulSwitch(this); 166 return bs; 167 } 168 169 public synchronized int getNewSessionId(JStatefulSwitch jss) { 170 int sid = sessionCount++; 172 if (TraceEjb.isDebugSsfpool()) { 173 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "#" + sid); 174 } 175 while (maxCacheSize > 0 && cacheSize >= maxCacheSize) { 177 long maxtime = Long.MAX_VALUE; 179 JStatefulSwitch victim = null; 180 for (Iterator i = statefulList.values().iterator(); i.hasNext(); ) { 181 JStatefulSwitch ss = (JStatefulSwitch) i.next(); 182 long time = ss.getLastAccessTime(); 183 if (time < maxtime) { 184 if (ss.canPassivate()) { 185 victim = ss; 186 } 187 } 188 } 189 if (victim != null) { 190 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "try to passivate a bean"); 191 if (victim.passivate()) { 192 cacheSize--; 193 } 194 } else { 195 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Could not find enough beans to passivate"); 196 break; 197 } 198 } 199 statefulList.put(new Integer (sid), jss); 201 cacheSize++; 202 return sid; 203 } 204 205 private FileOutputStream getFileOutputStream(int sid) throws IOException { 206 File file = new File (passivationDir, String.valueOf(sid) + ".ssf"); 207 file.createNewFile(); 208 return new FileOutputStream (file); 209 } 210 211 private FileInputStream getFileInputStream(int sid) throws IOException { 212 File file = new File (passivationDir, String.valueOf(sid) + ".ssf"); 213 return new FileInputStream (file); 214 } 215 216 public boolean passivateStateful(JStatefulSwitch jss) { 217 int sid = jss.getSessionId(); 218 if (TraceEjb.isDebugSsfpool()) { 219 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "#" + sid); 220 } 221 JStatefulContext ctx = jss.getStatefulContext(); 222 try { 223 SessionBean instance = ctx.getInstance(); 224 instance.ejbPassivate(); 225 JStatefulOutputStream out = 226 new JStatefulOutputStream(new BufferedOutputStream (getFileOutputStream(sid))); 227 out.writeObject(instance); 228 out.close(); 229 } catch (Exception e) { 230 TraceEjb.ssfpool.log(BasicLevel.WARN, "Cannot passivate instance:" + e); 231 return false; 232 } 233 return true; 234 } 235 236 public JStatefulContext activateStateful(JStatefulSwitch jss) { 237 int sid = jss.getSessionId(); 238 if (TraceEjb.isDebugSsfpool()) { 239 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "#" + sid); 240 } 241 JStatefulContext bctx = (JStatefulContext) jss.getStatefulContext(); 243 try { 244 JStatefulInputStream in = 245 new JStatefulInputStream(new BufferedInputStream (getFileInputStream(sid)), jss); 246 Object obj = in.readObject(); 247 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Deserialized object:" + obj); 248 SessionBean sb = (SessionBean ) obj; 249 in.close(); 250 bctx.setInstance(sb); 251 sb.ejbActivate(); 252 } catch (Exception e) { 254 TraceEjb.ssfpool.log(BasicLevel.WARN, "Cannot activate instance:" + e); 255 return null; 256 } 257 return bctx; 258 } 259 260 public synchronized void removeStateful(int sid) { 261 if (TraceEjb.isDebugSsfpool()) { 262 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "#" + sid); 263 } 264 JStatefulSwitch jss = (JStatefulSwitch) statefulList.remove(new Integer (sid)); 266 if (! jss.isPassivated()) { 267 cacheSize--; 268 } 269 if (TraceEjb.isDebugSsfpool()) { 270 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "statefulList size = " + statefulList.size()); 271 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "cache size = " + cacheSize); 272 } 273 } 274 275 280 public JSessionContext getJContext(JSessionSwitch ss) { 281 if (TraceEjb.isDebugIc()) { 282 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 283 } 284 JStatefulContext bctx = null; 285 try { 286 bctx = createNewInstance(ss); 287 } catch (Exception e) { 288 throw new EJBException ("Cannot create a new instance", e); 289 } 290 return bctx; 291 } 292 293 297 300 private JStatefulContext createNewInstance(JSessionSwitch ss) throws Exception { 301 if (TraceEjb.isDebugIc()) { 302 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 303 } 304 SessionBean bean = null; 306 try { 307 bean = (SessionBean ) beanclass.newInstance(); 308 } catch (InstantiationException e) { 309 TraceEjb.logger.log(BasicLevel.ERROR, ejbname + " cannot instantiate session bean"); 310 throw e; 311 } catch (IllegalAccessException e) { 312 TraceEjb.logger.log(BasicLevel.ERROR, ejbname + " cannot instantiate session bean"); 313 throw e; 314 } 315 JStatefulContext bctx = new JStatefulContext(this, bean, isSynchro); 317 bean.setSessionContext(bctx); 318 bctx.setState(1); 319 return bctx; 320 } 321 322 } 323 | Popular Tags |