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: JSessionContext.java 7525 2005-10-19 10:43:24Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.io.Serializable;
027: import java.rmi.RemoteException;
028: import java.util.List;
029:
030: import javax.ejb.EJBLocalObject;
031: import javax.ejb.EJBObject;
032: import javax.ejb.RemoveException;
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import javax.xml.rpc.handler.MessageContext;
036:
037: import org.objectweb.util.monolog.api.BasicLevel;
038:
039: /**
040: * This class implements javax.ejb.SessionContext interface. it should be
041: * implemented by JStatefulContext and JStatelessContext depending if the beans
042: * is stateful or stateless.
043: * @author Philippe Coq, Philippe Durieux
044: */
045:
046: public abstract class JSessionContext extends JContext implements
047: SessionContext, Serializable {
048:
049: protected JSessionSwitch bs = null;
050:
051: protected boolean ismarkedremoved;
052:
053: /**
054: * Constructs a SessionContext
055: * @param bf The Session Factory
056: * @param eb The Session bean instance
057: */
058: public JSessionContext(JSessionFactory bf, SessionBean eb) {
059: super (bf, eb);
060: if (TraceEjb.isDebugIc()) {
061: TraceEjb.interp.log(BasicLevel.DEBUG, "");
062: }
063: }
064:
065: // ------------------------------------------------------------------
066: // SessionContext implementation
067: // ------------------------------------------------------------------
068:
069: /**
070: * Obtains a reference to the EJB object that is currently associated with
071: * the instance.
072: * @return The EJB object currently associated with the instance.
073: * @exception IllegalStateException: Thrown if the instance invokes this
074: * method while the instance is in a state that does not allow
075: * the instance to invoke this method.
076: */
077: public EJBObject getEJBObject() throws IllegalStateException {
078: if (TraceEjb.isDebugIc()) {
079: TraceEjb.interp.log(BasicLevel.DEBUG, "");
080: }
081: if (ismarkedremoved || bs == null) {
082: if (ismarkedremoved)
083: TraceEjb.logger.log(BasicLevel.ERROR, "marked removed");
084: else
085: TraceEjb.logger.log(BasicLevel.ERROR,
086: "no SessionSwitch");
087: throw new IllegalStateException(
088: "getEJBObject: EJB is removed");
089: }
090: EJBObject ejbobject = bs.getRemote();
091: if (ejbobject == null) {
092: throw new IllegalStateException(
093: "No Remote Interface for this bean");
094: }
095: return ejbobject;
096: }
097:
098: /**
099: * Obtain a reference to the EJB local object that is currently associated
100: * with the instance.
101: * @return The EJB local object currently associated with the instance.
102: * @throws java.lang.IllegalStateException - if the instance invokes this
103: * method while the instance is in a state that does not allow the
104: * instance to invoke this method, or if the instance does not have
105: * a local interface.
106: */
107: public EJBLocalObject getEJBLocalObject()
108: throws IllegalStateException {
109: if (TraceEjb.isDebugIc()) {
110: TraceEjb.interp.log(BasicLevel.DEBUG, "");
111: }
112: if (ismarkedremoved || bs == null) {
113: if (ismarkedremoved)
114: TraceEjb.logger.log(BasicLevel.ERROR, "marked removed");
115: else
116: TraceEjb.logger.log(BasicLevel.ERROR,
117: "no SessionSwitch");
118: throw new IllegalStateException(
119: "getEJBLocalObject: EJB is removed");
120: }
121: EJBLocalObject ejblocalobject = bs.getLocal();
122: if (ejblocalobject == null)
123: throw new IllegalStateException(
124: "No Local Object for this bean");
125: return ejblocalobject;
126: }
127:
128: /**
129: * Obtain a reference to the JAX-RPC MessageContext.
130: * @return The MessageContext for this web service invocation.
131: * @throws java.lang.IllegalStateException - the instance is in a state that
132: * does not allow access to this method.
133: */
134: public abstract MessageContext getMessageContext()
135: throws java.lang.IllegalStateException;
136:
137: /**
138: * Tests if the transaction has been marked for rollback only.
139: * @return true if transaction will rollback
140: */
141: public boolean getRollbackOnly() throws IllegalStateException {
142: if (TraceEjb.isDebugIc()) {
143: TraceEjb.interp.log(BasicLevel.DEBUG, "");
144: }
145:
146: if (getState() < 2) {
147: throw new IllegalStateException(
148: "the instance is not allowed to call this method");
149: }
150: return super .getRollbackOnly();
151: }
152:
153: // -------------------------------------------------------------------
154: // Other public methods
155: // -------------------------------------------------------------------
156:
157: /**
158: * Reinit Context for reuse
159: * @param bs The SessionSwitch to reuse.
160: */
161: public void initSessionContext(JSessionSwitch bs) {
162: if (TraceEjb.isDebugIc()) {
163: TraceEjb.interp.log(BasicLevel.DEBUG, "");
164: }
165: this .bs = bs;
166: ismarkedremoved = false;
167: }
168:
169: /**
170: * Returns the bean instance of this context Used in the generated classes
171: * to retrieve the instance
172: * @return the bean instance
173: * @throws RemoteException if no instance
174: */
175: // RemoteException is throwned to allow simpler genic templates
176: public SessionBean getInstance() throws RemoteException {
177: if (myinstance == null) {
178: TraceEjb.logger.log(BasicLevel.ERROR, "null!");
179: throw new RemoteException("No instance available");
180: }
181: return (SessionBean) myinstance;
182: }
183:
184: /**
185: * @return True if bean instance is marked removed.
186: */
187: public boolean isMarkedRemoved() {
188: return ismarkedremoved;
189: }
190:
191: // ------------------------------------------------------------------
192: // Other methods
193: // ------------------------------------------------------------------
194:
195: abstract public void setRemoved() throws RemoteException,
196: RemoveException;
197:
198: abstract public void setConnectionList(List conlist);
199: }
|