JStatefulContext.java

00001 
00026 package org.objectweb.jonas_ejb.container;
00027 
00028 import java.rmi.RemoteException;
00029 import java.util.List;
00030 
00031 import javax.ejb.RemoveException;
00032 import javax.ejb.SessionBean;
00033 import javax.ejb.SessionSynchronization;
00034 import javax.ejb.TimerService;
00035 import javax.naming.Context;
00036 import javax.naming.NamingException;
00037 import javax.transaction.Status;
00038 import javax.transaction.Synchronization;
00039 import javax.xml.rpc.handler.MessageContext;
00040 
00041 import org.objectweb.jonas.naming.NamingManager;
00042 import org.objectweb.jonas_lib.naming.ContainerNaming;
00043 import org.objectweb.util.monolog.api.BasicLevel;
00044 
00049 public class JStatefulContext extends JSessionContext implements Synchronization {
00050 
00055     boolean synchro = false;
00056 
00057     boolean timedout = false;
00058 
00059     // ------------------------------------------------------------------
00060     // constructors
00061     // ------------------------------------------------------------------
00062 
00069     public JStatefulContext(JSessionFactory bf, SessionBean sb, boolean sync) {
00070         super(bf, sb);
00071         TraceEjb.interp.log(BasicLevel.DEBUG, "");
00072         synchro = sync;
00073         // Register this Context in java:comp (used by JTimerHandle)
00074         try {
00075             ContainerNaming naming = NamingManager.getInstance();
00076             Context c = naming.getComponentContext();
00077             c.rebind("MY_SF_CONTEXT", this);
00078         } catch (NamingException ne) {
00079             TraceEjb.logger.log(BasicLevel.ERROR, "Cannot bind EJBContext");
00080         }
00081     }
00082 
00083     // ------------------------------------------------------------------
00084     // EJBContext implementation
00085     // ------------------------------------------------------------------
00086 
00093     public TimerService getTimerService() throws IllegalStateException {
00094         throw new IllegalStateException("getTimerService Not Allowed on Stateful Session Beans");
00095     }
00096 
00103     public MessageContext getMessageContext() throws java.lang.IllegalStateException {
00104         throw new IllegalStateException("getMessageContext Not Allowed on Stateful Session Beans");
00105     }
00106 
00107     // -------------------------------------------------------------------
00108     // Synchronization implementation
00109     // -------------------------------------------------------------------
00110 
00116     public void beforeCompletion() {
00117         TraceEjb.tx.log(BasicLevel.DEBUG, "");
00118 
00119         if (synchro) {
00120             // Set classloader for getting the right component context
00121             ClassLoader old = Thread.currentThread().getContextClassLoader();
00122             Thread.currentThread().setContextClassLoader(bf.myClassLoader());
00123             Context bnctx = bf.setComponentContext();
00124             try {
00125                 SessionSynchronization ss = (SessionSynchronization) getInstance();
00126                 ss.beforeCompletion();
00127             } catch (RemoteException e) {
00128                 TraceEjb.logger.log(BasicLevel.ERROR, "exception:", e);
00129             } finally {
00130                 bf.resetComponentContext(bnctx);
00131                 Thread.currentThread().setContextClassLoader(old);
00132             }
00133         }
00134     }
00135 
00142     public void afterCompletion(int status) {
00143         TraceEjb.tx.log(BasicLevel.DEBUG, "");
00144 
00145         if (synchro) {
00146             // Set classloader for getting the right component context
00147             ClassLoader old = Thread.currentThread().getContextClassLoader();
00148             Thread.currentThread().setContextClassLoader(bf.myClassLoader());
00149             Context bnctx = bf.setComponentContext();
00150             try {
00151                 SessionSynchronization ss = (SessionSynchronization) getInstance();
00152                 setState(3);
00153                 ss.afterCompletion(status == Status.STATUS_COMMITTED);
00154                 setState(2);
00155             } catch (RemoteException e) {
00156                 TraceEjb.logger.log(BasicLevel.ERROR, "exception:", e);
00157             } finally {
00158                 bf.resetComponentContext(bnctx);
00159                 Thread.currentThread().setContextClassLoader(old);
00160             }
00161         }
00162 
00163         // Let the StatefulSwitch now that transaction is over and that it can
00164         // now dispose of this Context for another transaction.
00165         // no need to be in the correct component context ?
00166         JStatefulSwitch jss = (JStatefulSwitch) bs;
00167         jss.txCompleted();
00168 
00169         // The timeout has expired during the transaction. We can now remove
00170         // this session.
00171         if (timedout) {
00172             TraceEjb.logger.log(BasicLevel.WARN, "timeout expired during the transaction");
00173             timedout = false;
00174             // TODO
00175             // ((JStatefulSession)ejbObject).forceSessionRemove();
00176         }
00177     }
00178 
00179     // ------------------------------------------------------------------
00180     // Other methods
00181     // ------------------------------------------------------------------
00182 
00186     public void setRemoved() throws RemoteException, RemoveException {
00187         TraceEjb.interp.log(BasicLevel.DEBUG, "");
00188 
00189         // Cannot remove a stateful session inside a transaction
00190         JStatefulSwitch jss = (JStatefulSwitch) bs;
00191         if (jss.isInTransaction()) {
00192             throw new RemoveException("Cannot remove a statefull session inside a transaction");
00193         }
00194 
00195         // Call ejbRemove on instance
00196         // Assume that if this method is transacted (container transaction
00197         // only), this
00198         // transaction will be committed by the container (always true normally)
00199         SessionBean sb = (SessionBean) instance;
00200         sb.ejbRemove();
00201 
00202         // Set a flag to finish remove at postInvoke.
00203         // getEJBObject should work in ejbRemove.
00204         // => we do this only after ejbRemove call.
00205         ismarkedremoved = true;
00206     }
00207 
00211     public void setConnectionList(List conlist) {
00212         JStatefulSwitch jss = (JStatefulSwitch) bs;
00213         jss.setConnectionList(conlist);
00214     }
00215         
00216 }

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