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: JStatelessContext.java 6673 2005-04-28 16:53:00Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.io.Serializable;
027: import java.rmi.RemoteException;
028: import java.security.Principal;
029: import java.util.Collection;
030: import java.util.Date;
031: import java.util.List;
032:
033: import javax.ejb.EJBException;
034: import javax.ejb.EJBHome;
035: import javax.ejb.EJBLocalHome;
036: import javax.ejb.RemoveException;
037: import javax.ejb.SessionBean;
038: import javax.ejb.Timer;
039: import javax.ejb.TimerService;
040: import javax.xml.rpc.handler.MessageContext;
041:
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * This class extends JSessionContext in case of Stateless Session Bean.
046: * @author Philippe Durieux
047: */
048: public class JStatelessContext extends JSessionContext implements
049: TimerService {
050:
051: /**
052: * constructor
053: * @param bf The Bean Factory
054: * @param sb The Session Bean instance.
055: */
056: public JStatelessContext(JSessionFactory bf, SessionBean sb) {
057: super (bf, sb);
058: if (TraceEjb.isDebugIc()) {
059: TraceEjb.interp.log(BasicLevel.DEBUG, "");
060: }
061: }
062:
063: /**
064: * Get access to the EJB Timer Service.
065: * @return the EJB Timer Service
066: * @throws IllegalStateException Thrown if the instance is not allowed to
067: * use this method
068: */
069: public TimerService getTimerService() throws IllegalStateException {
070: if (TraceEjb.isDebugIc()) {
071: TraceEjb.interp.log(BasicLevel.DEBUG, "");
072: }
073: if (getState() == 0) {
074: throw new IllegalStateException(
075: "the instance is not allowed to call this method");
076: }
077: if (getState() == 1) {
078: // Called from ejbCreate : getTimerService should work, but
079: // not the timer operations! (See spec EJB 2.1 page 100)
080: return this ;
081: }
082: return bf.getTimerService();
083: }
084:
085: /**
086: * set this instance as removed
087: */
088: public void setRemoved() throws RemoteException, RemoveException {
089: if (TraceEjb.isDebugIc()) {
090: TraceEjb.interp.log(BasicLevel.DEBUG, "");
091: }
092: // Set a flag to finish remove at postInvoke.
093: ismarkedremoved = true;
094: }
095:
096: /**
097: * Obtain a reference to the JAX-RPC MessageContext.
098: * @return The MessageContext for this web service invocation.
099: * @throws java.lang.IllegalStateException - the instance is in a state that
100: * does not allow access to this method.
101: */
102: public MessageContext getMessageContext()
103: throws IllegalStateException {
104: if (bs == null) {
105: throw new IllegalStateException(
106: "No SessionSwitch for that bean");
107: }
108: MessageContext mc = ((JStatelessSwitch) bs).getMsgContext();
109: if (mc == null) {
110: throw new IllegalStateException(
111: "No ServiceEndpoint for that bean");
112: }
113: return mc;
114: }
115:
116: /**
117: * Set the connection list for this instance.
118: */
119: public void setConnectionList(List conlist) {
120: throw new IllegalStateException(
121: "Stateless beans should not reuse connections");
122: }
123:
124: /**
125: * Obtain the java.security.Principal that identifies the caller.
126: * throws a java.lang.IllegalStateException if there is no security context available
127: * @return The Principal object that identifies the caller.
128: * @throws IllegalStateException no security context exists
129: */
130: public Principal getCallerPrincipal() throws IllegalStateException {
131: if (getState() < 2) {
132: throw new IllegalStateException(
133: "the instance is not allowed to call this method");
134: }
135: return super .getCallerPrincipal();
136: }
137:
138: /**
139: * Test if the caller has a given role.
140: * @param roleName The name of the security role. The role must be one of
141: * the security-role-ref that is defined in the deployment
142: * descriptor.
143: * @return True if the caller has the specified role.
144: * @throws IllegalStateException Security service not started
145: */
146: public boolean isCallerInRole(String roleName)
147: throws IllegalStateException {
148: if (getState() < 2) {
149: throw new IllegalStateException(
150: "the instance is not allowed to call this method");
151: }
152: return super .isCallerInRole(roleName);
153: }
154:
155: /**
156: * @see javax.ejb.TimerService#createTimer(long, java.io.Serializable)
157: */
158: public Timer createTimer(long arg0, Serializable arg1)
159: throws IllegalArgumentException, IllegalStateException,
160: EJBException {
161: // Not allowed when called from ejbCreate
162: if (getState() < 2) {
163: throw new IllegalStateException(
164: "the instance is not allowed to call this method");
165: }
166: return getTimerService().createTimer(arg0, arg1);
167: }
168:
169: /**
170: * @see javax.ejb.TimerService#createTimer(long, long, java.io.Serializable)
171: */
172: public Timer createTimer(long arg0, long arg1, Serializable arg2)
173: throws IllegalArgumentException, IllegalStateException,
174: EJBException {
175: // Not allowed when called from ejbCreate
176: if (getState() < 2) {
177: throw new IllegalStateException(
178: "the instance is not allowed to call this method");
179: }
180: return getTimerService().createTimer(arg0, arg1, arg2);
181: }
182:
183: /**
184: * @see javax.ejb.TimerService#createTimer(java.util.Date, java.io.Serializable)
185: */
186: public Timer createTimer(Date arg0, Serializable arg1)
187: throws IllegalArgumentException, IllegalStateException,
188: EJBException {
189: // Not allowed when called from ejbCreate
190: if (getState() < 2) {
191: throw new IllegalStateException(
192: "the instance is not allowed to call this method");
193: }
194: return getTimerService().createTimer(arg0, arg1);
195: }
196:
197: /**
198: * @see javax.ejb.TimerService#createTimer(java.util.Date, long, java.io.Serializable)
199: */
200: public Timer createTimer(Date arg0, long arg1, Serializable arg2)
201: throws IllegalArgumentException, IllegalStateException,
202: EJBException {
203: // Not allowed when called from ejbCreate
204: if (getState() < 2) {
205: throw new IllegalStateException(
206: "the instance is not allowed to call this method");
207: }
208: return getTimerService().createTimer(arg0, arg1, arg2);
209: }
210:
211: /**
212: * @see javax.ejb.TimerService#getTimers()
213: */
214: public Collection getTimers() throws IllegalStateException,
215: EJBException {
216: // Not allowed when called from ejbCreate
217: if (getState() < 2) {
218: throw new IllegalStateException(
219: "the instance is not allowed to call this method");
220: }
221: return getTimerService().getTimers();
222: }
223:
224: }
|