KickJava   Java API By Example, From Geeks To Geeks.

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


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: BasicCmpBean.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.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 BasicCmpBean implements javax.ejb.EntityBean JavaDoc{
65     
66     public static int key = 1000;
67     
68     public int primaryKey;
69     public String JavaDoc firstName;
70     public String JavaDoc lastName;
71     public EntityContext JavaDoc ejbContext;
72     public Hashtable JavaDoc allowedOperationsTable = new Hashtable JavaDoc();
73     
74     
75     //=============================
76
// Home interface methods
77
//
78

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

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

102     public Integer JavaDoc ejbCreate(String JavaDoc name)
103     throws javax.ejb.CreateException JavaDoc{
104         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(name, " ");
105         firstName = st.nextToken();
106         lastName = st.nextToken();
107         this.primaryKey = key++;
108         return null;
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     /**
138      * Throws an ApplicationException when invoked
139      *
140      */

141     public void throwApplicationException() throws ApplicationException{
142         throw new ApplicationException("Testing ability to throw Application Exceptions");
143     }
144     
145     /**
146      * Throws a java.lang.NullPointerException when invoked
147      * This is a system exception and should result in the
148      * destruction of the instance and invalidation of the
149      * remote reference.
150      *
151      */

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

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

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

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

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

199     public void ejbLoad() throws EJBException JavaDoc,RemoteException JavaDoc {
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     }
226     
227     /**
228      * A container invokes this method before it removes the EJB object
229      * that is currently associated with the instance. This method
230      * is invoked when a client invokes a remove operation on the
231      * enterprise Bean's home interface or the EJB object's remote interface.
232      * This method transitions the instance from the ready state to the pool
233      * of available instances.
234      */

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

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

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

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

317         allowedOperationsTable.put(methodName, policy);
318     }
319
320 }
321
Popular Tags