001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JStatefulContext.java 7525 2005-10-19 10:43:24Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.rmi.RemoteException;
027: import java.util.List;
028:
029: import javax.ejb.RemoveException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionSynchronization;
032: import javax.ejb.TimerService;
033: import javax.naming.Context;
034: import javax.naming.NamingException;
035: import javax.transaction.Status;
036: import javax.transaction.Synchronization;
037: import javax.xml.rpc.handler.MessageContext;
038:
039: import org.objectweb.jonas.naming.NamingManager;
040: import org.objectweb.jonas_lib.naming.ContainerNaming;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * This class extends JSessionContext in case of Stateful Session Bean.
045: * @author Philippe Coq, Philippe Durieux
046: */
047: public class JStatefulContext extends JSessionContext implements
048: Synchronization {
049:
050: /**
051: * the bean instance implements SessionSynchronization interface
052: * @serial
053: */
054: boolean synchro = false;
055:
056: boolean timedout = false;
057:
058: // ------------------------------------------------------------------
059: // constructors
060: // ------------------------------------------------------------------
061:
062: /**
063: * Constructs a JStatefulContext
064: * @param bf - the Session Factory
065: * @param sb - the Enterprise Bean instance
066: * @param sync - True if implements SessionSymchronization.
067: */
068: public JStatefulContext(JSessionFactory bf, SessionBean sb,
069: boolean sync) {
070: super (bf, sb);
071: if (TraceEjb.isDebugIc()) {
072: TraceEjb.interp.log(BasicLevel.DEBUG, "");
073: }
074: synchro = sync;
075: // Register this Context in java:comp (used by JTimerHandle)
076: try {
077: ContainerNaming naming = NamingManager.getInstance();
078: Context c = naming.getComponentContext();
079: c.rebind("MY_SF_CONTEXT", this );
080: } catch (NamingException ne) {
081: TraceEjb.logger.log(BasicLevel.ERROR,
082: "Cannot bind EJBContext", ne);
083: }
084: }
085:
086: /**
087: * set instance. Used for passivation/activation.
088: * @param sb
089: */
090: public void setInstance(SessionBean sb) {
091: TraceEjb.logger.log(BasicLevel.DEBUG, "Set to " + sb);
092: myinstance = sb;
093: }
094:
095: // ------------------------------------------------------------------
096: // EJBContext implementation
097: // ------------------------------------------------------------------
098:
099: /**
100: * Get access to the EJB Timer Service.
101: * @return the EJB Timer Service
102: * @throws IllegalStateException Thrown if the instance is not allowed to
103: * use this method
104: */
105: public TimerService getTimerService() throws IllegalStateException {
106: throw new IllegalStateException(
107: "getTimerService Not Allowed on Stateful Session Beans");
108: }
109:
110: /**
111: * Obtain a reference to the JAX-RPC MessageContext.
112: * @return The MessageContext for this web service invocation.
113: * @throws java.lang.IllegalStateException - the instance is in a state that
114: * does not allow access to this method.
115: */
116: public MessageContext getMessageContext()
117: throws java.lang.IllegalStateException {
118: throw new IllegalStateException(
119: "getMessageContext Not Allowed on Stateful Session Beans");
120: }
121:
122: // -------------------------------------------------------------------
123: // Synchronization implementation
124: // -------------------------------------------------------------------
125:
126: /**
127: * This beforeCompletion method is called by the transaction manager prior
128: * to the start of the transaction completion process. This method executes
129: * in the transaction context of the calling thread.
130: */
131: public void beforeCompletion() {
132: if (TraceEjb.isDebugTx()) {
133: TraceEjb.tx.log(BasicLevel.DEBUG, "");
134: }
135:
136: if (synchro) {
137: // Set classloader for getting the right component context
138: ClassLoader old = Thread.currentThread()
139: .getContextClassLoader();
140: Thread.currentThread().setContextClassLoader(
141: bf.myClassLoader());
142: Context bnctx = bf.setComponentContext();
143: try {
144: SessionSynchronization ss = (SessionSynchronization) getInstance();
145: ss.beforeCompletion();
146: } catch (RemoteException e) {
147: TraceEjb.logger.log(BasicLevel.ERROR, "exception:", e);
148: } finally {
149: bf.resetComponentContext(bnctx);
150: Thread.currentThread().setContextClassLoader(old);
151: }
152: }
153: }
154:
155: /**
156: * The afterCompletion method is called by the transaction manager after the
157: * transaction is committed or rolled back. This method executes without a
158: * transaction context.
159: * @param status The status of the transaction completion.
160: */
161: public void afterCompletion(int status) {
162: if (TraceEjb.isDebugTx()) {
163: TraceEjb.tx.log(BasicLevel.DEBUG, "");
164: }
165:
166: if (synchro) {
167: // Set classloader for getting the right component context
168: ClassLoader old = Thread.currentThread()
169: .getContextClassLoader();
170: Thread.currentThread().setContextClassLoader(
171: bf.myClassLoader());
172: Context bnctx = bf.setComponentContext();
173: try {
174: SessionSynchronization ss = (SessionSynchronization) getInstance();
175: setState(3);
176: ss.afterCompletion(status == Status.STATUS_COMMITTED);
177: setState(2);
178: } catch (RemoteException e) {
179: TraceEjb.logger.log(BasicLevel.ERROR, "exception:", e);
180: } finally {
181: bf.resetComponentContext(bnctx);
182: Thread.currentThread().setContextClassLoader(old);
183: }
184: }
185:
186: // Let the StatefulSwitch now that transaction is over and that it can
187: // now dispose of this Context for another transaction.
188: // no need to be in the correct component context ?
189: JStatefulSwitch jss = (JStatefulSwitch) bs;
190: jss.txCompleted();
191:
192: // The timeout has expired during the transaction. We can now remove
193: // this session.
194: if (timedout) {
195: TraceEjb.logger.log(BasicLevel.WARN,
196: "timeout expired during the transaction");
197: timedout = false;
198: // TODO
199: // ((JStatefulSession)ejbObject).forceSessionRemove();
200: }
201: }
202:
203: // ------------------------------------------------------------------
204: // Other methods
205: // ------------------------------------------------------------------
206:
207: /**
208: * set this instance as removed
209: */
210: public void setRemoved() throws RemoteException, RemoveException {
211: if (TraceEjb.isDebugIc()) {
212: TraceEjb.interp.log(BasicLevel.DEBUG, "");
213: }
214:
215: // Cannot remove a stateful session inside a transaction
216: JStatefulSwitch jss = (JStatefulSwitch) bs;
217: if (jss.isInTransaction()) {
218: throw new RemoveException(
219: "Cannot remove a statefull session inside a transaction");
220: }
221:
222: // Call ejbRemove on instance
223: // Assume that if this method is transacted (container transaction
224: // only), this
225: // transaction will be committed by the container (always true normally)
226: SessionBean sb = (SessionBean) myinstance;
227: if (sb != null) {
228: sb.ejbRemove();
229: } else {
230: TraceEjb.interp.log(BasicLevel.WARN,
231: "remove a passivated instance ?");
232: }
233:
234: // Set a flag to finish remove at postInvoke.
235: // getEJBObject should work in ejbRemove.
236: // => we do this only after ejbRemove call.
237: ismarkedremoved = true;
238: }
239:
240: /**
241: * Set the connection list for this instance.
242: */
243: public void setConnectionList(List conlist) {
244: JStatefulSwitch jss = (JStatefulSwitch) bs;
245: jss.setConnectionList(conlist);
246: }
247:
248: }
|