KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > entity > cmp > AllowedOperationsCmpBean


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

45 package org.openejb.test.entity.cmp;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.util.Properties JavaDoc;
50 import java.util.StringTokenizer JavaDoc;
51
52 import javax.ejb.EJBException JavaDoc;
53 import javax.ejb.EntityContext JavaDoc;
54 import javax.ejb.RemoveException JavaDoc;
55
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 AllowedOperationsCmpBean implements javax.ejb.EntityBean JavaDoc{
64     
65     public static int key = 20;
66     
67     public int primaryKey;
68     public String JavaDoc firstName;
69     public String JavaDoc lastName;
70     public EntityContext JavaDoc ejbContext;
71     public Hashtable JavaDoc allowedOperationsTable = new Hashtable JavaDoc();
72     
73     
74     //=============================
75
// Home interface methods
76
//
77

78     /**
79      * Maps to BasicCmpHome.sum
80      *
81      * Adds x and y and returns the result.
82      *
83      * @param one
84      * @param two
85      * @return x + y
86      * @see BasicCmpHome.sum
87      */

88     public int ejbHomeSum(int x, int y) {
89         testAllowedOperations("ejbHome");
90         return x+y;
91     }
92     
93     /**
94      * Maps to BasicCmpHome.create
95      *
96      * @param name
97      * @return
98      * @exception javax.ejb.CreateException
99      * @see BasicCmpHome.create
100      */

101     public Integer JavaDoc ejbCreate(String JavaDoc name)
102     throws javax.ejb.CreateException JavaDoc{
103         testAllowedOperations("ejbCreate");
104         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(name, " ");
105         firstName = st.nextToken();
106         lastName = st.nextToken();
107         this.primaryKey = key++;
108         return new Integer JavaDoc(primaryKey);
109     }
110     
111     public void ejbPostCreate(String JavaDoc name)
112     throws javax.ejb.CreateException JavaDoc{
113     }
114
115     //
116
// Home interface methods
117
//=============================
118

119
120     //=============================
121
// Remote interface methods
122
//
123

124     /**
125      * Maps to BasicCmpObject.businessMethod
126      *
127      * @return
128      * @see BasicCmpObject.businessMethod
129      */

130     public String JavaDoc businessMethod(String JavaDoc text){
131         testAllowedOperations("businessMethod");
132         StringBuffer JavaDoc b = new StringBuffer JavaDoc(text);
133         return b.reverse().toString();
134     }
135
136     /**
137      * Throws an ApplicationException when invoked
138      *
139      */

140     public void throwApplicationException() throws org.openejb.test.ApplicationException{
141         throw new org.openejb.test.ApplicationException("Don't Panic");
142     }
143     
144     /**
145      * Throws a java.lang.NullPointerException when invoked
146      * This is a system exception and should result in the
147      * destruction of the instance and invalidation of the
148      * remote reference.
149      *
150      */

151     public void throwSystemException_NullPointer() {
152         throw new NullPointerException JavaDoc("Panic");
153     }
154     
155     
156     /**
157      * Maps to BasicCmpObject.getPermissionsReport
158      *
159      * Returns a report of the bean's
160      * runtime permissions
161      *
162      * @return
163      * @see BasicCmpObject.getPermissionsReport
164      */

165     public Properties JavaDoc getPermissionsReport(){
166         /* TO DO: */
167         return null;
168     }
169     
170     /**
171      * Maps to BasicCmpObject.getAllowedOperationsReport
172      *
173      * Returns a report of the allowed opperations
174      * for one of the bean's methods.
175      *
176      * @param methodName The method for which to get the allowed opperations report
177      * @return
178      * @see BasicCmpObject.getAllowedOperationsReport
179      */

180     public OperationsPolicy getAllowedOperationsReport(String JavaDoc methodName){
181         return (OperationsPolicy) allowedOperationsTable.get(methodName);
182     }
183     
184     //
185
// Remote interface methods
186
//=============================
187

188
189     //================================
190
// EntityBean interface methods
191
//
192

193     /**
194      * A container invokes this method to instruct the
195      * instance to synchronize its state by loading it state from the
196      * underlying database.
197      */

198     public void ejbLoad() throws EJBException JavaDoc,RemoteException JavaDoc {
199         testAllowedOperations("ejbLoad");
200     }
201     
202     /**
203      * Set the associated entity context. The container invokes this method
204      * on an instance after the instance has been created.
205      */

206     public void setEntityContext(EntityContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
207         ejbContext = ctx;
208         testAllowedOperations("setEntityContext");
209     }
210     
211     /**
212      * Unset the associated entity context. The container calls this method
213      * before removing the instance.
214      */

215     public void unsetEntityContext() throws EJBException JavaDoc,RemoteException JavaDoc {
216         testAllowedOperations("unsetEntityContext");
217     }
218     
219     /**
220      * A container invokes this method to instruct the
221      * instance to synchronize its state by storing it to the underlying
222      * database.
223      */

224     public void ejbStore() throws EJBException JavaDoc,RemoteException JavaDoc {
225         testAllowedOperations("ejbStore");
226     }
227     
228     /**
229      * A container invokes this method before it removes the EJB object
230      * that is currently associated with the instance. This method
231      * is invoked when a client invokes a remove operation on the
232      * enterprise Bean's home interface or the EJB object's remote interface.
233      * This method transitions the instance from the ready state to the pool
234      * of available instances.
235      */

236     public void ejbRemove() throws RemoveException JavaDoc,EJBException JavaDoc,RemoteException JavaDoc {
237         testAllowedOperations("ejbRemove");
238     }
239     
240     /**
241      * A container invokes this method when the instance
242      * is taken out of the pool of available instances to become associated
243      * with a specific EJB object. This method transitions the instance to
244      * the ready state.
245      */

246     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
247         testAllowedOperations("ejbActivate");
248     }
249     
250     /**
251      * A container invokes this method on an instance before the instance
252      * becomes disassociated with a specific EJB object. After this method
253      * completes, the container will place the instance into the pool of
254      * available instances.
255      */

256     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
257
258         testAllowedOperations("ejbPassivate");
259     }
260     //
261
// EntityBean interface methods
262
//================================
263

264     protected void testAllowedOperations(String JavaDoc methodName){
265         OperationsPolicy policy = new OperationsPolicy();
266         
267         /*[1] Test getEJBHome /////////////////*/
268         try{
269             ejbContext.getEJBHome();
270             policy.allow(policy.Context_getEJBHome);
271         }catch(IllegalStateException JavaDoc ise){}
272         
273         /*[2] Test getCallerPrincipal /////////*/
274         try{
275             ejbContext.getCallerPrincipal();
276             policy.allow( policy.Context_getCallerPrincipal );
277         }catch(IllegalStateException JavaDoc ise){}
278         
279         /*[3] Test isCallerInRole /////////////*/
280         try{
281             ejbContext.isCallerInRole("ROLE");
282             policy.allow( policy.Context_isCallerInRole );
283         }catch(IllegalStateException JavaDoc ise){}
284         
285         /*[4] Test getRollbackOnly ////////////*/
286         try{
287             ejbContext.getRollbackOnly();
288             policy.allow( policy.Context_getRollbackOnly );
289         }catch(IllegalStateException JavaDoc ise){}
290         
291         /*[5] Test setRollbackOnly ////////////*/
292         try{
293             ejbContext.setRollbackOnly();
294             policy.allow( policy.Context_setRollbackOnly );
295         }catch(IllegalStateException JavaDoc ise){}
296         
297         /*[6] Test getUserTransaction /////////*/
298         try{
299             ejbContext.getUserTransaction();
300             policy.allow( policy.Context_getUserTransaction );
301         }catch(Exception JavaDoc e){}
302         
303         /*[7] Test getEJBObject ///////////////*/
304         try{
305             ejbContext.getEJBObject();
306             policy.allow( policy.Context_getEJBObject );
307         }catch(IllegalStateException JavaDoc ise){}
308
309         /*[8] Test getPrimaryKey //////////////*/
310         try{
311             ejbContext.getPrimaryKey();
312             policy.allow( policy.Context_getPrimaryKey );
313         }catch(IllegalStateException JavaDoc ise){}
314          
315         /* TO DO:
316          * Check for policy.Enterprise_bean_access
317          * Check for policy.JNDI_access_to_java_comp_env
318          * Check for policy.Resource_manager_access
319          */

320         allowedOperationsTable.put(methodName, policy);
321     }
322
323 }
324
Popular Tags