1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.rmi.RemoteException ; 29 import java.util.ArrayList ; 30 31 import javax.ejb.EJBException ; 32 import javax.naming.NamingException ; 33 import javax.transaction.SystemException ; 34 35 import org.objectweb.jonas_ejb.deployment.api.SessionDesc; 36 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 45 public abstract class JSessionFactory extends JFactory { 46 47 int timeout = 0; 49 50 protected JSessionHome home = null; 51 52 protected JSessionLocalHome localhome = null; 53 54 protected boolean isSynchro = false; 55 56 protected boolean isStateful; 58 protected ArrayList sessionList = new ArrayList (); 60 61 66 public JSessionFactory(SessionDesc dd, JContainer cont) { 67 super(dd, cont); 68 if (TraceEjb.isDebugIc()) { 69 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 70 } 71 72 txbeanmanaged = dd.isBeanManagedTransaction(); 73 timeout = dd.getSessionTimeout(); 74 75 Class homeclass = null; 77 String clname = dd.getFullWrpHomeName(); 78 if (clname != null) { 79 try { 80 homeclass = cont.getClassLoader().loadClass(clname); 81 } catch (ClassNotFoundException e) { 82 throw new EJBException (ejbname + " Cannot load " + clname, e); 83 } 84 if (TraceEjb.isDebugIc()) TraceEjb.interp.log(BasicLevel.DEBUG, ejbname + ": " + clname + " loaded"); 85 try { 86 int nbp = 2; 88 Class [] ptype = new Class [nbp]; 89 Object [] pobj = new Object [nbp]; 90 ptype[0] = org.objectweb.jonas_ejb.deployment.api.SessionDesc.class; 91 pobj[0] = (Object ) dd; 92 ptype[1] = org.objectweb.jonas_ejb.container.JSessionFactory.class; 93 pobj[1] = (Object ) this; 94 home = (JSessionHome) homeclass.getConstructor(ptype).newInstance(pobj); 95 } catch (Exception e) { 96 throw new EJBException (ejbname + " Cannot create home ", e); 97 } 98 try { 100 home.register(); 101 } catch (Exception e) { 102 throw new EJBException (ejbname + " Cannot register home ", e); 103 } 104 } 105 106 clname = dd.getFullWrpLocalHomeName(); 108 if (clname != null) { 109 try { 110 homeclass = cont.getClassLoader().loadClass(clname); 111 } catch (ClassNotFoundException e) { 112 throw new EJBException (ejbname + " Cannot load " + clname, e); 113 } 114 if (TraceEjb.isDebugIc()) TraceEjb.interp.log(BasicLevel.DEBUG, ejbname + ": " + clname + " loaded"); 115 try { 116 int nbp = 2; 118 Class [] ptype = new Class [nbp]; 119 Object [] pobj = new Object [nbp]; 120 ptype[0] = org.objectweb.jonas_ejb.deployment.api.SessionDesc.class; 121 pobj[0] = (Object ) dd; 122 ptype[1] = org.objectweb.jonas_ejb.container.JSessionFactory.class; 123 pobj[1] = (Object ) this; 124 localhome = (JSessionLocalHome) homeclass.getConstructor(ptype).newInstance(pobj); 125 } catch (Exception e) { 126 throw new EJBException (ejbname + " Cannot create localhome ", e); 127 } 128 try { 130 localhome.register(); 131 } catch (Exception e) { 132 throw new EJBException (ejbname + " Cannot register localhome ", e); 133 } 134 } 135 136 } 137 138 142 145 public void stop() { 146 if (TraceEjb.isDebugIc()) { 147 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 148 } 149 try { 150 if (home != null) { 151 home.unregister(); 152 } 153 if (localhome != null) { 154 localhome.unregister(); 155 } 156 } catch (NamingException e) { 157 } 158 } 159 160 163 public void sync() { 164 } 165 166 169 public JHome getHome() { 170 return home; 171 } 172 173 176 public JLocalHome getLocalHome() { 177 return localhome; 178 } 179 180 184 188 public synchronized JSessionSwitch createEJB() throws RemoteException { 189 190 if (TraceEjb.isDebugIc()) { 191 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 192 } 193 194 JSessionSwitch bs = null; 195 if (sessionList.size() > 0) { 196 bs = (JSessionSwitch) sessionList.remove(0); 197 JSessionRemote remote = bs.getRemote(); 200 if (remote != null) { 201 if (remote.exportObject() == false) { 202 TraceEjb.logger.log(BasicLevel.ERROR, "bad JSessionSwitch found in pool."); 203 return null; 204 } 205 } 206 } else { 207 bs = createNewSession(); 211 } 212 213 if (timeout > 0) { 215 bs.startTimer(timeout * 1000); 216 } 217 return bs; 218 } 219 220 225 public synchronized void removeEJB(JSessionSwitch bs) { 226 227 if (TraceEjb.isDebugIc()) { 228 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 229 } 230 231 sessionList.add(bs); 232 } 233 234 238 243 public void checkTransaction(RequestCtx rctx) { 244 if (txbeanmanaged) { 245 try { 246 rctx.clientTx = tm.suspend(); 247 if (TraceEjb.isDebugTx()) { 248 TraceEjb.tx.log(BasicLevel.DEBUG, "suspending tx:" + rctx.clientTx); 249 } 250 } catch (SystemException e) { 251 throw new EJBException ("cannot suspend transaction", e); 252 } 253 } else { 254 checkTransactionContainer(rctx); 255 } 256 } 257 258 261 public boolean isSessionSynchro() { 262 return isSynchro; 263 } 264 265 268 public int getTimeout() { 269 return timeout; 270 } 271 272 276 public void setTimeout(int t) { 277 if (TraceEjb.isDebugTx()) { 278 TraceEjb.tx.log(BasicLevel.DEBUG, ""); 279 } 280 timeout = t; 281 } 282 283 288 public boolean isStateful() { 289 return isStateful; 290 } 291 292 295 public abstract JSessionContext getJContext(JSessionSwitch ss); 296 297 301 abstract protected JSessionSwitch createNewSession() throws RemoteException ; 302 } 303 | Popular Tags |