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
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
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
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
00109
00110
00116 public void beforeCompletion() {
00117 TraceEjb.tx.log(BasicLevel.DEBUG, "");
00118
00119 if (synchro) {
00120
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
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
00164
00165
00166 JStatefulSwitch jss = (JStatefulSwitch) bs;
00167 jss.txCompleted();
00168
00169
00170
00171 if (timedout) {
00172 TraceEjb.logger.log(BasicLevel.WARN, "timeout expired during the transaction");
00173 timedout = false;
00174
00175
00176 }
00177 }
00178
00179
00180
00181
00182
00186 public void setRemoved() throws RemoteException, RemoveException {
00187 TraceEjb.interp.log(BasicLevel.DEBUG, "");
00188
00189
00190 JStatefulSwitch jss = (JStatefulSwitch) bs;
00191 if (jss.isInTransaction()) {
00192 throw new RemoveException("Cannot remove a statefull session inside a transaction");
00193 }
00194
00195
00196
00197
00198
00199 SessionBean sb = (SessionBean) instance;
00200 sb.ejbRemove();
00201
00202
00203
00204
00205 ismarkedremoved = true;
00206 }
00207
00211 public void setConnectionList(List conlist) {
00212 JStatefulSwitch jss = (JStatefulSwitch) bs;
00213 jss.setConnectionList(conlist);
00214 }
00215
00216 }