KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > stateless > BasicStatelessBean


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: BasicStatelessBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.stateless;
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.naming.InitialContext JavaDoc;
54 import javax.sql.DataSource JavaDoc;
55
56 import org.openejb.test.ApplicationException;
57 import org.openejb.test.object.OperationsPolicy;
58
59 /**
60  *
61  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
62  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
63  */

64 public class BasicStatelessBean implements javax.ejb.SessionBean JavaDoc{
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
// Home interface methods
76
//=============================
77

78
79     //=============================
80
// Remote interface methods
81
//
82
/**
83      * Maps to BasicStatelessObject.businessMethod
84      *
85      * @return
86      * @see BasicStatelessObject.businessMethod
87      */

88     public String JavaDoc businessMethod(String JavaDoc text){
89         testAllowedOperations("businessMethod");
90         StringBuffer JavaDoc b = new StringBuffer JavaDoc(text);
91         return b.reverse().toString();
92     }
93     
94
95     /**
96      * Throws an ApplicationException when invoked
97      *
98      */

99     public void throwApplicationException() throws ApplicationException{
100         throw new ApplicationException("Testing ability to throw Application Exceptions");
101     }
102     
103     /**
104      * Throws a java.lang.NullPointerException when invoked
105      * This is a system exception and should result in the
106      * destruction of the instance and invalidation of the
107      * remote reference.
108      *
109      */

110     public void throwSystemException_NullPointer() {
111         throw new NullPointerException JavaDoc("Testing ability to throw System Exceptions");
112     }
113     
114     /**
115      * Maps to BasicStatelessObject.getPermissionsReport
116      *
117      * Returns a report of the bean's
118      * runtime permissions
119      *
120      * @return
121      * @see BasicStatelessObject.getPermissionsReport
122      */

123     public Properties JavaDoc getPermissionsReport(){
124         /* TO DO: */
125         return null;
126     }
127     
128     /**
129      * Maps to BasicStatelessObject.getAllowedOperationsReport
130      *
131      * Returns a report of the allowed opperations
132      * for one of the bean's methods.
133      *
134      * @param methodName The method for which to get the allowed opperations report
135      * @return
136      * @see BasicStatelessObject.getAllowedOperationsReport
137      */

138     public OperationsPolicy getAllowedOperationsReport(String JavaDoc methodName){
139         return (OperationsPolicy) allowedOperationsTable.get(methodName);
140     }
141
142     //
143
// Remote interface methods
144
//=============================
145

146
147     //================================
148
// SessionBean interface methods
149
//
150
/**
151      * Set the associated session context. The container calls this method
152      * after the instance creation.
153      */

154     public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
155         ejbContext = ctx;
156         testAllowedOperations("setSessionContext");
157     }
158     /**
159      *
160      * @param name
161      * @exception javax.ejb.CreateException
162      */

163     public void ejbCreate() throws javax.ejb.CreateException JavaDoc{
164         testAllowedOperations("ejbCreate");
165         this.name = "nameless automaton";
166     }
167     /**
168      * A container invokes this method before it ends the life of the session
169      * object. This happens as a result of a client's invoking a remove
170      * operation, or when a container decides to terminate the session object
171      * after a timeout.
172      */

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

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

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

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

256     
257         /*[8] Test JNDI_access_to_java_comp_env ///////////////*/
258         try {
259             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
260     
261             String JavaDoc actual = (String JavaDoc)jndiContext.lookup("java:comp/env/stateless/references/JNDI_access_to_java_comp_env");
262     
263             policy.allow( policy.JNDI_access_to_java_comp_env );
264         } catch (IllegalStateException JavaDoc ise) {
265         } catch (javax.naming.NamingException JavaDoc ne) {
266         }
267     
268         /*[9] Test Resource_manager_access ///////////////*/
269         try {
270             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
271     
272             DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/stateless/references/Resource_manager_access");
273     
274             policy.allow( policy.Resource_manager_access );
275         } catch (IllegalStateException JavaDoc ise) {
276         } catch (javax.naming.NamingException JavaDoc ne) {
277         }
278     
279         /*[10] Test Enterprise_bean_access ///////////////*/
280         try {
281             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
282     
283             Object JavaDoc obj = jndiContext.lookup("java:comp/env/stateless/beanReferences/Enterprise_bean_access");
284     
285             policy.allow( policy.Enterprise_bean_access );
286         } catch (IllegalStateException JavaDoc ise) {
287         } catch (javax.naming.NamingException JavaDoc ne) {
288         }
289     
290         allowedOperationsTable.put(methodName, policy);
291     }
292
293 }
294
Popular Tags