KickJava   Java API By Example, From Geeks To Geeks.

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


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: BMTStatefulBean.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
54 import org.openejb.test.ApplicationException;
55 import org.openejb.test.object.OperationsPolicy;
56
57 /**
58  * A Stateful SessionBean with bean-managed transaction demarcation
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 BMTStatefulBean implements javax.ejb.SessionBean JavaDoc {
64     
65     private String JavaDoc name;
66     private SessionContext JavaDoc ejbContext;
67     private Hashtable JavaDoc allowedOperationsTable = new Hashtable JavaDoc();
68     
69     
70     //=============================
71
// Home interface methods
72
//
73
/**
74      * Maps to BasicStatefulHome.create
75      *
76      * @param name
77      * @exception javax.ejb.CreateException
78      * @see BasicStatefulHome.create
79      */

80     public void ejbCreate(String JavaDoc name)
81     throws javax.ejb.CreateException JavaDoc{
82         testAllowedOperations("ejbCreate");
83         this.name = name;
84     }
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("Don't Panic");
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("Panic");
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
// StatefulBean interface methods
197
//==================================
198

199     protected void testAllowedOperations(String JavaDoc methodName){
200         OperationsPolicy policy = new OperationsPolicy();
201         
202         /*[1] Test getEJBHome /////////////////*/
203         try{
204             ejbContext.getEJBHome();
205             policy.allow(policy.Context_getEJBHome);
206         }catch(IllegalStateException JavaDoc ise){}
207         
208         /*[2] Test getCallerPrincipal /////////*/
209         try{
210             ejbContext.getCallerPrincipal();
211             policy.allow( policy.Context_getCallerPrincipal );
212         }catch(IllegalStateException JavaDoc ise){}
213         
214         /*[3] Test isCallerInRole /////////////*/
215         try{
216             ejbContext.isCallerInRole("ROLE");
217             policy.allow( policy.Context_isCallerInRole );
218         }catch(IllegalStateException JavaDoc ise){}
219         
220         /*[4] Test getRollbackOnly ////////////*/
221         try{
222             ejbContext.getRollbackOnly();
223             policy.allow( policy.Context_getRollbackOnly );
224         }catch(IllegalStateException JavaDoc ise){}
225         
226         /*[5] Test setRollbackOnly ////////////*/
227         try{
228             ejbContext.setRollbackOnly();
229             policy.allow( policy.Context_setRollbackOnly );
230         }catch(IllegalStateException JavaDoc ise){}
231         
232         /*[6] Test getUserTransaction /////////*/
233         try{
234             ejbContext.getUserTransaction();
235             policy.allow( policy.Context_getUserTransaction );
236         }catch(IllegalStateException JavaDoc ise){}
237         
238         /*[7] Test getEJBObject ///////////////*/
239         try{
240             ejbContext.getEJBObject();
241             policy.allow( policy.Context_getEJBObject );
242         }catch(IllegalStateException JavaDoc ise){}
243          
244         /* TO DO:
245          * Check for policy.Enterprise_bean_access
246          * Check for policy.JNDI_access_to_java_comp_env
247          * Check for policy.Resource_manager_access
248          */

249         allowedOperationsTable.put(methodName, policy);
250     }
251 }
252
Popular Tags