KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JSessionContext


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  *----------------------------------------------------------------
22  * $Id: JSessionContext.java,v 1.16 2005/05/02 19:09:18 ashah Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.io.Serializable JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.ejb.EJBLocalObject JavaDoc;
33 import javax.ejb.EJBObject JavaDoc;
34 import javax.ejb.RemoveException JavaDoc;
35 import javax.ejb.SessionBean JavaDoc;
36 import javax.ejb.SessionContext JavaDoc;
37 import javax.xml.rpc.handler.MessageContext JavaDoc;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40
41 /**
42  * This class implements javax.ejb.SessionContext interface. it should be
43  * implemented by JStatefulContext and JStatelessContext depending if the beans
44  * is stateful or stateless.
45  * @author Philippe Coq, Philippe Durieux
46  */

47
48 public abstract class JSessionContext extends JContext implements SessionContext JavaDoc, Serializable JavaDoc {
49
50     protected JSessionSwitch bs = null;
51
52     protected boolean ismarkedremoved;
53
54     /**
55      * Constructs a SessionContext
56      * @param bf The Session Factory
57      * @param eb The Session bean instance
58      */

59     public JSessionContext(JSessionFactory bf, SessionBean JavaDoc eb) {
60         super(bf, eb);
61         if (TraceEjb.isDebugIc()) {
62             TraceEjb.interp.log(BasicLevel.DEBUG, "");
63         }
64     }
65
66     // ------------------------------------------------------------------
67
// SessionContext implementation
68
// ------------------------------------------------------------------
69

70     /**
71      * Obtains a reference to the EJB object that is currently associated with
72      * the instance.
73      * @return The EJB object currently associated with the instance.
74      * @exception IllegalStateException: Thrown if the instance invokes this
75      * method while the instance is in a state that does not allow
76      * the instance to invoke this method.
77      */

78     public EJBObject JavaDoc getEJBObject() throws IllegalStateException JavaDoc {
79         if (TraceEjb.isDebugIc()) {
80             TraceEjb.interp.log(BasicLevel.DEBUG, "");
81         }
82         if (ismarkedremoved || bs == null) {
83             if (ismarkedremoved)
84                 TraceEjb.logger.log(BasicLevel.ERROR, "marked removed");
85             else
86                 TraceEjb.logger.log(BasicLevel.ERROR, "no SessionSwitch");
87             throw new IllegalStateException JavaDoc("getEJBObject: EJB is removed");
88         }
89         EJBObject JavaDoc ejbobject = bs.getRemote();
90         if (ejbobject == null) {
91             throw new IllegalStateException JavaDoc("No Remote Interface for this bean");
92         }
93         return ejbobject;
94     }
95
96     /**
97      * Obtain a reference to the EJB local object that is currently associated
98      * with the instance.
99      * @return The EJB local object currently associated with the instance.
100      * @throws java.lang.IllegalStateException - if the instance invokes this
101      * method while the instance is in a state that does not allow the
102      * instance to invoke this method, or if the instance does not have
103      * a local interface.
104      */

105     public EJBLocalObject JavaDoc getEJBLocalObject() throws IllegalStateException JavaDoc {
106         if (TraceEjb.isDebugIc()) {
107             TraceEjb.interp.log(BasicLevel.DEBUG, "");
108         }
109         if (ismarkedremoved || bs == null) {
110             if (ismarkedremoved)
111                 TraceEjb.logger.log(BasicLevel.ERROR, "marked removed");
112             else
113                 TraceEjb.logger.log(BasicLevel.ERROR, "no SessionSwitch");
114             throw new IllegalStateException JavaDoc("getEJBLocalObject: EJB is removed");
115         }
116         EJBLocalObject JavaDoc ejblocalobject = bs.getLocal();
117         if (ejblocalobject == null) throw new IllegalStateException JavaDoc("No Local Object for this bean");
118         return ejblocalobject;
119     }
120
121     /**
122      * Obtain a reference to the JAX-RPC MessageContext.
123      * @return The MessageContext for this web service invocation.
124      * @throws java.lang.IllegalStateException - the instance is in a state that
125      * does not allow access to this method.
126      */

127     public abstract MessageContext JavaDoc getMessageContext() throws java.lang.IllegalStateException JavaDoc;
128
129     /**
130      * Tests if the transaction has been marked for rollback only.
131      * @return true if transaction will rollback
132      */

133     public boolean getRollbackOnly() throws IllegalStateException JavaDoc {
134         if (TraceEjb.isDebugIc()) {
135             TraceEjb.interp.log(BasicLevel.DEBUG, "");
136         }
137
138         if (getState() < 2) {
139             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
140         }
141         return super.getRollbackOnly();
142     }
143
144     // -------------------------------------------------------------------
145
// Other public methods
146
// -------------------------------------------------------------------
147

148     /**
149      * Reinit Context for reuse
150      * @param bs The SessionSwitch to reuse.
151      */

152     public void initSessionContext(JSessionSwitch bs) {
153         if (TraceEjb.isDebugIc()) {
154             TraceEjb.interp.log(BasicLevel.DEBUG, "");
155         }
156         this.bs = bs;
157         ismarkedremoved = false;
158     }
159
160     /**
161      * Returns the bean instance of this context Used in the generated classes
162      * to retrieve the instance
163      * @return the bean instance
164      * @throws RemoteException if no instance
165      */

166     // RemoteException is throwned to allow simpler genic templates
167
public SessionBean JavaDoc getInstance() throws RemoteException JavaDoc {
168         if (instance == null) {
169             TraceEjb.logger.log(BasicLevel.ERROR, "null!");
170             throw new RemoteException JavaDoc("No instance available");
171         }
172         return (SessionBean JavaDoc) instance;
173     }
174
175     /**
176      * @return True if bean instance is marked removed.
177      */

178     public boolean isMarkedRemoved() {
179         return ismarkedremoved;
180     }
181
182     // ------------------------------------------------------------------
183
// Other methods
184
// ------------------------------------------------------------------
185

186     abstract public void setRemoved() throws RemoteException JavaDoc, RemoveException JavaDoc;
187     abstract public void setConnectionList(List JavaDoc conlist);
188 }
189
Popular Tags