KickJava   Java API By Example, From Geeks To Geeks.

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


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: JStatelessContext.java,v 1.16 2005/04/28 16:52:59 benoitf 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.security.Principal JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.ejb.EJBException JavaDoc;
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.ejb.EJBLocalHome JavaDoc;
38 import javax.ejb.RemoveException JavaDoc;
39 import javax.ejb.SessionBean JavaDoc;
40 import javax.ejb.Timer JavaDoc;
41 import javax.ejb.TimerService JavaDoc;
42 import javax.xml.rpc.handler.MessageContext JavaDoc;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * This class extends JSessionContext in case of Stateless Session Bean.
48  * @author Philippe Durieux
49  */

50 public class JStatelessContext extends JSessionContext implements TimerService JavaDoc {
51
52     /**
53      * constructor
54      * @param bf The Bean Factory
55      * @param sb The Session Bean instance.
56      */

57     public JStatelessContext(JSessionFactory bf, SessionBean JavaDoc sb) {
58         super(bf, sb);
59         if (TraceEjb.isDebugIc()) {
60             TraceEjb.interp.log(BasicLevel.DEBUG, "");
61         }
62     }
63
64     /**
65      * Get access to the EJB Timer Service.
66      * @return the EJB Timer Service
67      * @throws IllegalStateException Thrown if the instance is not allowed to
68      * use this method
69      */

70     public TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc {
71         if (TraceEjb.isDebugIc()) {
72             TraceEjb.interp.log(BasicLevel.DEBUG, "");
73         }
74         if (getState() == 0) {
75             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
76         }
77         if (getState() == 1) {
78             // Called from ejbCreate : getTimerService should work, but
79
// not the timer operations! (See spec EJB 2.1 page 100)
80
return this;
81         }
82         return bf.getTimerService();
83     }
84
85     /**
86      * set this instance as removed
87      */

88     public void setRemoved() throws RemoteException JavaDoc, RemoveException JavaDoc {
89         if (TraceEjb.isDebugIc()) {
90             TraceEjb.interp.log(BasicLevel.DEBUG, "");
91         }
92         // Set a flag to finish remove at postInvoke.
93
ismarkedremoved = true;
94     }
95
96     /**
97      * Obtain a reference to the JAX-RPC MessageContext.
98      * @return The MessageContext for this web service invocation.
99      * @throws java.lang.IllegalStateException - the instance is in a state that
100      * does not allow access to this method.
101      */

102     public MessageContext JavaDoc getMessageContext() throws IllegalStateException JavaDoc {
103         if (bs == null) {
104             throw new IllegalStateException JavaDoc("No SessionSwitch for that bean");
105         }
106         MessageContext JavaDoc mc = ((JStatelessSwitch) bs).getMsgContext();
107         if (mc == null) {
108             throw new IllegalStateException JavaDoc("No ServiceEndpoint for that bean");
109         }
110         return mc;
111     }
112
113     /**
114      * Set the connection list for this instance.
115      */

116     public void setConnectionList(List JavaDoc conlist) {
117         throw new IllegalStateException JavaDoc("Stateless beans should not reuse connections");
118     }
119
120     /**
121      * Obtain the java.security.Principal that identifies the caller.
122      * throws a java.lang.IllegalStateException if there is no security context available
123      * @return The Principal object that identifies the caller.
124      * @throws IllegalStateException no security context exists
125      */

126     public Principal JavaDoc getCallerPrincipal() throws IllegalStateException JavaDoc {
127         if (getState() < 2) {
128             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
129         }
130         return super.getCallerPrincipal();
131     }
132
133     /**
134      * Test if the caller has a given role.
135      * @param roleName The name of the security role. The role must be one of
136      * the security-role-ref that is defined in the deployment
137      * descriptor.
138      * @return True if the caller has the specified role.
139      * @throws IllegalStateException Security service not started
140      */

141     public boolean isCallerInRole(String JavaDoc roleName) throws IllegalStateException JavaDoc {
142         if (getState() < 2) {
143             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
144         }
145         return super.isCallerInRole(roleName);
146     }
147
148     /**
149      * @see javax.ejb.TimerService#createTimer(long, java.io.Serializable)
150      */

151     public Timer JavaDoc createTimer(long arg0, Serializable JavaDoc arg1) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
152         // Not allowed when called from ejbCreate
153
if (getState() < 2) {
154             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
155         }
156         return getTimerService().createTimer(arg0, arg1);
157     }
158
159     /**
160      * @see javax.ejb.TimerService#createTimer(long, long, java.io.Serializable)
161      */

162     public Timer JavaDoc createTimer(long arg0, long arg1, Serializable JavaDoc arg2) throws IllegalArgumentException JavaDoc,
163             IllegalStateException JavaDoc, EJBException JavaDoc {
164         // Not allowed when called from ejbCreate
165
if (getState() < 2) {
166             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
167         }
168         return getTimerService().createTimer(arg0, arg1, arg2);
169     }
170
171     /**
172      * @see javax.ejb.TimerService#createTimer(java.util.Date, java.io.Serializable)
173      */

174     public Timer JavaDoc createTimer(Date JavaDoc arg0, Serializable JavaDoc arg1) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
175         // Not allowed when called from ejbCreate
176
if (getState() < 2) {
177             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
178         }
179         return getTimerService().createTimer(arg0, arg1);
180     }
181
182     /**
183      * @see javax.ejb.TimerService#createTimer(java.util.Date, long, java.io.Serializable)
184      */

185     public Timer JavaDoc createTimer(Date JavaDoc arg0, long arg1, Serializable JavaDoc arg2) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
186         // Not allowed when called from ejbCreate
187
if (getState() < 2) {
188             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
189         }
190         return getTimerService().createTimer(arg0, arg1, arg2);
191   }
192
193     /**
194      * @see javax.ejb.TimerService#getTimers()
195      */

196     public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc {
197         // Not allowed when called from ejbCreate
198
if (getState() < 2) {
199             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
200         }
201         return getTimerService().getTimers();
202    }
203
204
205 }
206
Popular Tags