KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > stateful > BasicStatefulBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: BasicStatefulBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.stateful;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.util.Properties JavaDoc;
50
51 import javax.ejb.EJBException JavaDoc;
52 import javax.ejb.SessionContext JavaDoc;
53 import javax.ejb.SessionSynchronization JavaDoc;
54
55 import org.openejb.test.ApplicationException;
56 import org.openejb.test.object.OperationsPolicy;
57
58 /**
59  *
60  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
61  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
62  */

63 public class BasicStatefulBean implements javax.ejb.SessionBean JavaDoc, SessionSynchronization JavaDoc {
64
65     
66     private String JavaDoc name;
67     private SessionContext JavaDoc ejbContext;
68     private Hashtable JavaDoc allowedOperationsTable = new Hashtable JavaDoc();
69     
70     
71     //=============================
72
// Home interface methods
73
//
74
/**
75      * Maps to BasicStatefulHome.create
76      *
77      * @param name
78      * @exception javax.ejb.CreateException
79      * @see BasicStatefulHome.create
80      */

81     public void ejbCreate(String JavaDoc name)
82     throws javax.ejb.CreateException JavaDoc{
83         testAllowedOperations("ejbCreate");
84         this.name = name;
85     }
86     //
87
// Home interface methods
88
//=============================
89

90
91     //=============================
92
// Remote interface methods
93
//
94

95     /**
96      * Maps to BasicStatefulObject.businessMethod
97      *
98      * @return
99      * @see BasicStatefulObject.businessMethod
100      */

101     public String JavaDoc businessMethod(String JavaDoc text){
102         testAllowedOperations("businessMethod");
103         StringBuffer JavaDoc b = new StringBuffer JavaDoc(text);
104         return b.reverse().toString();
105     }
106
107     /**
108      * Throws an ApplicationException when invoked
109      *
110      */

111     public void throwApplicationException() throws ApplicationException{
112         throw new ApplicationException("Testing ability to throw Application Exceptions");
113     }
114     
115     /**
116      * Throws a java.lang.NullPointerException when invoked
117      * This is a system exception and should result in the
118      * destruction of the instance and invalidation of the
119      * remote reference.
120      *
121      */

122     public void throwSystemException_NullPointer() {
123         throw new NullPointerException JavaDoc("Testing ability to throw System Exceptions");
124     }
125     
126     /**
127      * Maps to BasicStatefulObject.getPermissionsReport
128      *
129      * Returns a report of the bean's
130      * runtime permissions
131      *
132      * @return
133      * @see BasicStatefulObject.getPermissionsReport
134      */

135     public Properties JavaDoc getPermissionsReport(){
136         /* TO DO: */
137         return null;
138     }
139     
140     /**
141      * Maps to BasicStatefulObject.getAllowedOperationsReport
142      *
143      * Returns a report of the allowed opperations
144      * for one of the bean's methods.
145      *
146      * @param methodName The method for which to get the allowed opperations report
147      * @return
148      * @see BasicStatefulObject.getAllowedOperationsReport
149      */

150     public OperationsPolicy getAllowedOperationsReport(String JavaDoc methodName){
151         return (OperationsPolicy) allowedOperationsTable.get(methodName);
152     }
153     
154     //
155
// Remote interface methods
156
//=============================
157

158
159     //=================================
160
// SessionBean interface methods
161
//
162
/**
163      * Set the associated session context. The container calls this method
164      * after the instance creation.
165      */

166     public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
167         ejbContext = ctx;
168         testAllowedOperations("setSessionContext");
169     }
170     /**
171      * A container invokes this method before it ends the life of the session
172      * object. This happens as a result of a client's invoking a remove
173      * operation, or when a container decides to terminate the session object
174      * after a timeout.
175      */

176     public void ejbRemove() throws EJBException JavaDoc,RemoteException JavaDoc {
177         testAllowedOperations("ejbRemove");
178     }
179     /**
180      * The activate method is called when the instance is activated
181      * from its "passive" state. The instance should acquire any resource
182      * that it has released earlier in the ejbPassivate() method.
183      */

184     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
185         testAllowedOperations("ejbActivate");
186     }
187     /**
188      * The passivate method is called before the instance enters
189      * the "passive" state. The instance should release any resources that
190      * it can re-acquire later in the ejbActivate() method.
191      */

192     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
193         testAllowedOperations("ejbPassivate");
194     }
195     //
196
// SessionBean interface methods
197
//==================================
198

199     
200     //============================================
201
// SessionSynchronization interface methods
202
//
203
/**
204      * The afterBegin method notifies a session Bean instance that a new
205      * transaction has started, and that the subsequent business methods on the
206      * instance will be invoked in the context of the transaction.
207      */

208     public void afterBegin() throws EJBException JavaDoc,RemoteException JavaDoc {
209         testAllowedOperations("afterBegin");
210     }
211     /**
212      * The beforeCompletion method notifies a session Bean instance that
213      * a transaction is about to be committed. The instance can use this
214      * method, for example, to write any cached data to a database.
215      */

216     public void beforeCompletion() throws EJBException JavaDoc,RemoteException JavaDoc {
217         testAllowedOperations("beforeCompletion");
218     }
219     /**
220      * The afterCompletion method notifies a session Bean instance that a
221      * transaction commit protocol has completed, and tells the instance
222      * whether the transaction has been committed or rolled back.
223      */

224     public void afterCompletion(boolean committed) throws EJBException JavaDoc,RemoteException JavaDoc {
225         testAllowedOperations("afterCompletion");
226     }
227     //
228
// SessionSynchronization interface methods
229
//============================================
230

231     protected void testAllowedOperations(String JavaDoc methodName){
232         OperationsPolicy policy = new OperationsPolicy();
233         
234         /*[1] Test getEJBHome /////////////////*/
235         try{
236             ejbContext.getEJBHome();
237             policy.allow(policy.Context_getEJBHome);
238         }catch(IllegalStateException JavaDoc ise){}
239         
240         /*[2] Test getCallerPrincipal /////////*/
241         try{
242             ejbContext.getCallerPrincipal();
243             policy.allow( policy.Context_getCallerPrincipal );
244         }catch(IllegalStateException JavaDoc ise){}
245         
246         /*[3] Test isCallerInRole /////////////*/
247         try{
248             ejbContext.isCallerInRole("ROLE");
249             policy.allow( policy.Context_isCallerInRole );
250         }catch(IllegalStateException JavaDoc ise){}
251         
252         /*[4] Test getRollbackOnly ////////////*/
253         try{
254             ejbContext.getRollbackOnly();
255             policy.allow( policy.Context_getRollbackOnly );
256         }catch(IllegalStateException JavaDoc ise){}
257         
258         /*[5] Test setRollbackOnly ////////////*/
259         try{
260             ejbContext.setRollbackOnly();
261             policy.allow( policy.Context_setRollbackOnly );
262         }catch(IllegalStateException JavaDoc ise){}
263         
264         /*[6] Test getUserTransaction /////////*/
265         try{
266             ejbContext.getUserTransaction();
267             policy.allow( policy.Context_getUserTransaction );
268         }catch(IllegalStateException JavaDoc ise){}
269         
270         /*[7] Test getEJBObject ///////////////*/
271         try{
272             ejbContext.getEJBObject();
273             policy.allow( policy.Context_getEJBObject );
274         }catch(IllegalStateException JavaDoc ise){}
275          
276         /* TO DO:
277          * Check for policy.Enterprise_bean_access
278          * Check for policy.JNDI_access_to_java_comp_env
279          * Check for policy.Resource_manager_access
280          */

281         allowedOperationsTable.put(methodName, policy);
282     }
283
284 }
285
Popular Tags