JSessionFactory.java

00001 
00026 package org.objectweb.jonas_ejb.container;
00027 
00028 import java.rmi.RemoteException;
00029 import java.util.ArrayList;
00030 
00031 import javax.ejb.EJBException;
00032 import javax.naming.NamingException;
00033 import javax.transaction.SystemException;
00034 
00035 import org.objectweb.jonas_ejb.deployment.api.SessionDesc;
00036 
00037 import org.objectweb.util.monolog.api.BasicLevel;
00038 
00045 public abstract class JSessionFactory extends JFactory {
00046 
00047     // Timeout for this session
00048     int timeout = 0;
00049 
00050     protected JSessionHome home = null;
00051 
00052     protected JSessionLocalHome localhome = null;
00053 
00054     protected boolean isSynchro = false;
00055 
00056     protected boolean isStateful; // set after constructor call
00057 
00058     // Pool of JSessionSwitch
00059     protected ArrayList sessionList = new ArrayList();
00060 
00066     public JSessionFactory(SessionDesc dd, JContainer cont) {
00067         super(dd, cont);
00068         TraceEjb.interp.log(BasicLevel.DEBUG, "");
00069 
00070         txbeanmanaged = dd.isBeanManagedTransaction();
00071         timeout = dd.getSessionTimeout();
00072 
00073         // Create the Home if defined in DD
00074         Class homeclass = null;
00075         String clname = dd.getFullWrpHomeName();
00076         if (clname != null) {
00077             try {
00078                 homeclass = cont.getClassLoader().loadClass(clname);
00079             } catch (ClassNotFoundException e) {
00080                 throw new EJBException(ejbname + " Cannot load " + clname, e);
00081             }
00082             if (TraceEjb.isDebugIc()) TraceEjb.interp.log(BasicLevel.DEBUG, ejbname + ": " + clname + " loaded");
00083             try {
00084                 // new JSessionHome(dd, this)
00085                 int nbp = 2;
00086                 Class[] ptype = new Class[nbp];
00087                 Object[] pobj = new Object[nbp];
00088                 ptype[0] = org.objectweb.jonas_ejb.deployment.api.SessionDesc.class;
00089                 pobj[0] = (Object) dd;
00090                 ptype[1] = org.objectweb.jonas_ejb.container.JSessionFactory.class;
00091                 pobj[1] = (Object) this;
00092                 home = (JSessionHome) homeclass.getConstructor(ptype).newInstance(pobj);
00093             } catch (Exception e) {
00094                 throw new EJBException(ejbname + " Cannot create home ", e);
00095             }
00096             // register it in JNDI
00097             try {
00098                 home.register();
00099             } catch (Exception e) {
00100                 throw new EJBException(ejbname + " Cannot register home ", e);
00101             }
00102         }
00103 
00104         // Create the LocalHome if defined in DD
00105         clname = dd.getFullWrpLocalHomeName();
00106         if (clname != null) {
00107             try {
00108                 homeclass = cont.getClassLoader().loadClass(clname);
00109             } catch (ClassNotFoundException e) {
00110                 throw new EJBException(ejbname + " Cannot load " + clname, e);
00111             }
00112             if (TraceEjb.isDebugIc()) TraceEjb.interp.log(BasicLevel.DEBUG, ejbname + ": " + clname + " loaded");
00113             try {
00114                 // new JSessionLocalHome(dd, this)
00115                 int nbp = 2;
00116                 Class[] ptype = new Class[nbp];
00117                 Object[] pobj = new Object[nbp];
00118                 ptype[0] = org.objectweb.jonas_ejb.deployment.api.SessionDesc.class;
00119                 pobj[0] = (Object) dd;
00120                 ptype[1] = org.objectweb.jonas_ejb.container.JSessionFactory.class;
00121                 pobj[1] = (Object) this;
00122                 localhome = (JSessionLocalHome) homeclass.getConstructor(ptype).newInstance(pobj);
00123             } catch (Exception e) {
00124                 throw new EJBException(ejbname + " Cannot create localhome ", e);
00125             }
00126             // register it in JNDI
00127             try {
00128                 localhome.register();
00129             } catch (Exception e) {
00130                 throw new EJBException(ejbname + " Cannot register localhome ", e);
00131             }
00132         }
00133 
00134     }
00135 
00136     // ---------------------------------------------------------------
00137     // BeanFactory implementation
00138     // ---------------------------------------------------------------
00139 
00143     public void stop() {
00144         TraceEjb.interp.log(BasicLevel.DEBUG, "");
00145         try {
00146             if (home != null) {
00147                 home.unregister();
00148             }
00149             if (localhome != null) {
00150                 localhome.unregister();
00151             }
00152         } catch (NamingException e) {
00153         }
00154     }
00155 
00159     public void sync() {
00160     }
00161 
00165     public JHome getHome() {
00166         return home;
00167     }
00168 
00172     public JLocalHome getLocalHome() {
00173         return localhome;
00174     }
00175 
00176     // ---------------------------------------------------------------
00177     // JSessionSwitch pool management
00178     // ---------------------------------------------------------------
00179 
00184     public synchronized JSessionSwitch createEJB() throws RemoteException {
00185 
00186         TraceEjb.interp.log(BasicLevel.DEBUG, "");
00187 
00188         JSessionSwitch bs = null;
00189         if (sessionList.size() > 0) {
00190             bs = (JSessionSwitch) sessionList.remove(0);
00191             // must re-export the remote object in the Orb since EJBObjects
00192             // should be un exported when put in the pool.
00193             JSessionRemote remote = bs.getRemote();
00194             if (remote != null) {
00195                 if (remote.exportObject() == false) {
00196                     TraceEjb.logger.log(BasicLevel.ERROR, "bad JSessionSwitch found in pool.");
00197                     return null;
00198                 }
00199             }
00200         } else {
00201             // This depend on subclass (stateless or stateful)
00202             // no need to export the EJBObject because it is basically a
00203             // RemoteObject
00204             bs = createNewSession();
00205         }
00206 
00207         // Start a timer for this new session
00208         if (timeout > 0) {
00209             bs.startTimer(timeout * 1000);
00210         }
00211         return bs;
00212     }
00213 
00219     public synchronized void removeEJB(JSessionSwitch bs) {
00220 
00221         TraceEjb.interp.log(BasicLevel.DEBUG, "");
00222 
00223         sessionList.add(bs);
00224     }
00225 
00226     // ---------------------------------------------------------------
00227     // other public methods
00228     // ---------------------------------------------------------------
00229 
00235     public void checkTransaction(RequestCtx rctx) {
00236         if (txbeanmanaged) {
00237             try {
00238                 rctx.clientTx = tm.suspend();
00239                 TraceEjb.tx.log(BasicLevel.DEBUG, "suspending tx:" + rctx.clientTx);
00240             } catch (SystemException e) {
00241                 throw new EJBException("cannot suspend transaction");
00242             }
00243         } else {
00244             checkTransactionContainer(rctx);
00245         }
00246     }
00247 
00251     public boolean isSessionSynchro() {
00252         return isSynchro;
00253     }
00254 
00258     public int getTimeout() {
00259         return timeout;
00260     }
00261 
00266     public void setTimeout(int t) {
00267         TraceEjb.tx.log(BasicLevel.DEBUG, "");
00268         timeout = t;
00269     }
00270 
00276     public boolean isStateful() {
00277         return isStateful;
00278     }
00279 
00283     public abstract JSessionContext getJContext(JSessionSwitch ss);
00284 
00285     // ---------------------------------------------------------------
00286     // private methods
00287     // ---------------------------------------------------------------
00288 
00289     abstract protected JSessionSwitch createNewSession() throws RemoteException;
00290 }

Generated on Tue Feb 15 15:05:42 2005 for JOnAS by  doxygen 1.3.9.1