KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > entity > bmp > BasicBmp2DataSourcesBean


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: BasicBmp2DataSourcesBean.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.test.entity.bmp;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.sql.Connection JavaDoc;
49 import java.sql.PreparedStatement JavaDoc;
50 import java.sql.ResultSet JavaDoc;
51 import java.util.Hashtable JavaDoc;
52 import java.util.Properties JavaDoc;
53 import java.util.StringTokenizer JavaDoc;
54
55 import javax.ejb.EJBException JavaDoc;
56 import javax.ejb.EntityContext JavaDoc;
57 import javax.ejb.FinderException JavaDoc;
58 import javax.ejb.RemoveException JavaDoc;
59 import javax.naming.InitialContext JavaDoc;
60 import javax.sql.DataSource JavaDoc;
61
62 import org.openejb.test.object.OperationsPolicy;
63
64 /**
65  *
66  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
67  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
68  */

69 public class BasicBmp2DataSourcesBean implements javax.ejb.EntityBean JavaDoc{
70     
71     public static int primaryKey = 1;
72     public String JavaDoc firstName;
73     public String JavaDoc lastName;
74     public EntityContext JavaDoc ejbContext;
75     public Hashtable JavaDoc allowedOperationsTable = new Hashtable JavaDoc();
76     
77     
78     //=============================
79
// Home interface methods
80
//
81

82     /**
83      * Maps to BasicBmp2DataSourcesHome.sum
84      *
85      * Adds x and y and returns the result.
86      *
87      * @param one
88      * @param two
89      * @return x + y
90      * @see BasicBmp2DataSourcesHome.sum
91      */

92     public int ejbHomeSum(int x, int y) {
93         testAllowedOperations("ejbHome");
94         return x+y;
95     }
96     
97     
98     /**
99      * Maps to BasicBmp2DataSourcesHome.findEmptyCollection
100      *
101      * @return Collection
102      * @exception javax.ejb.FinderException
103      */

104     public java.util.Collection JavaDoc ejbFindEmptyCollection()
105     throws javax.ejb.FinderException JavaDoc, java.rmi.RemoteException JavaDoc {
106         return new java.util.Vector JavaDoc();
107     }
108
109     /**
110      * Maps to BasicBmp2DataSourcesHome.findByPrimaryKey
111      *
112      * @param primaryKey
113      * @return Integer
114      * @exception javax.ejb.FinderException
115      */

116     public Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc primaryKey)
117     throws javax.ejb.FinderException JavaDoc{
118         boolean found = false;
119         try{
120             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
121             DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
122             Connection JavaDoc con = ds.getConnection();
123             
124             PreparedStatement JavaDoc stmt = con.prepareStatement("select * from entity where id = ?");
125             stmt.setInt(1, primaryKey.intValue());
126             found = stmt.executeQuery().next();
127             con.close();
128         }catch(Exception JavaDoc e){
129             throw new FinderException JavaDoc("FindByPrimaryKey failed");
130         }
131         
132         if(found) return primaryKey;
133         else throw new javax.ejb.ObjectNotFoundException JavaDoc();
134     }
135
136     /**
137      * Maps to BasicBmp2DataSourcesHome.create
138      *
139      * @param name
140      * @return Integer
141      * @exception javax.ejb.CreateException
142      */

143     public Integer JavaDoc ejbCreate(String JavaDoc name)
144     throws javax.ejb.CreateException JavaDoc{
145         try{
146         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(name, " ");
147         firstName = st.nextToken();
148         lastName = st.nextToken();
149         
150         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
151  
152         DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
153         
154         Connection JavaDoc con = ds.getConnection();
155         
156         // Support for Oracle because Oracle doesn't do auto increment
157
PreparedStatement JavaDoc stmt = con.prepareStatement("insert into entity (id, first_name, last_name) values (?,?,?)");
158         stmt.setInt(1, primaryKey++);
159         stmt.setString(2, firstName);
160         stmt.setString(3, lastName);
161         stmt.executeUpdate();
162         
163         stmt = con.prepareStatement("select id from entity where first_name = ? AND last_name = ?");
164         stmt.setString(1, firstName);
165         stmt.setString(2, lastName);
166         ResultSet JavaDoc set = stmt.executeQuery();
167         while(set.next()) primaryKey = set.getInt("id");
168         con.close();
169         
170         // Do backup
171
ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabaseBackup");
172         
173         con = ds.getConnection();
174         
175         // Support for Oracle because Oracle doesn't do auto increment
176
stmt = con.prepareStatement("insert into entityBackup (id, first_name, last_name) values (?,?,?)");
177         stmt.setInt(1, primaryKey);
178         stmt.setString(2, firstName);
179         stmt.setString(3, lastName);
180         stmt.executeUpdate();
181         
182         con.close();
183         
184         return new Integer JavaDoc(primaryKey);
185         
186         }catch(Exception JavaDoc e){
187             e.printStackTrace();
188             throw new javax.ejb.CreateException JavaDoc("can't create");
189         }
190     }
191     
192     public void ejbPostCreate(String JavaDoc name)
193     throws javax.ejb.CreateException JavaDoc{
194     }
195     
196     //
197
// Home interface methods
198
//=============================
199

200
201     //=============================
202
// Remote interface methods
203
//
204

205     /**
206      * Maps to BasicBmp2DataSourcesObject.businessMethod
207      *
208      * @return String
209      */

210     public String JavaDoc businessMethod(String JavaDoc text){
211         testAllowedOperations("businessMethod");
212         StringBuffer JavaDoc b = new StringBuffer JavaDoc(text);
213         return b.reverse().toString();
214     }
215
216     
217     /**
218      * Maps to BasicBmp2DataSourcesObject.getPermissionsReport
219      *
220      * Returns a report of the bean's
221      * runtime permissions
222      *
223      * @return null
224      */

225     public Properties JavaDoc getPermissionsReport(){
226         /* TO DO: */
227         return null;
228     }
229     
230     /**
231      * Maps to BasicBmp2DataSourcesObject.getAllowedOperationsReport
232      *
233      * Returns a report of the allowed opperations
234      * for one of the bean's methods.
235      *
236      * @param methodName The method for which to get the allowed opperations report
237      * @return OperationPolicy
238      */

239     public OperationsPolicy getAllowedOperationsReport(String JavaDoc methodName){
240         return (OperationsPolicy) allowedOperationsTable.get(methodName);
241     }
242     
243     //
244
// Remote interface methods
245
//=============================
246

247
248     //================================
249
// EntityBean interface methods
250
//
251

252     /**
253      * A container invokes this method to instruct the
254      * instance to synchronize its state by loading it state from the
255      * underlying database.
256      */

257     public void ejbLoad() throws EJBException JavaDoc,RemoteException JavaDoc {
258         try{
259         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
260         DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
261         Connection JavaDoc con = ds.getConnection();
262         
263         PreparedStatement JavaDoc stmt = con.prepareStatement("select * from entity where id = ?");
264         Integer JavaDoc primaryKey = (Integer JavaDoc)ejbContext.getPrimaryKey();
265         stmt.setInt(1, primaryKey.intValue());
266         ResultSet JavaDoc rs = stmt.executeQuery();
267         while(rs.next()){
268             lastName = rs.getString("last_name");
269             firstName = rs.getString("first_name");
270         }
271         con.close();
272         
273         }catch(Exception JavaDoc e){
274             e.printStackTrace();
275         }
276     }
277     
278     /**
279      * Set the associated entity context. The container invokes this method
280      * on an instance after the instance has been created.
281      */

282     public void setEntityContext(EntityContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
283         ejbContext = ctx;
284         testAllowedOperations("setEntityContext");
285     }
286     
287     /**
288      * Unset the associated entity context. The container calls this method
289      * before removing the instance.
290      */

291     public void unsetEntityContext() throws EJBException JavaDoc,RemoteException JavaDoc {
292         testAllowedOperations("unsetEntityContext");
293     }
294     
295     /**
296      * A container invokes this method to instruct the
297      * instance to synchronize its state by storing it to the underlying
298      * database.
299      */

300     public void ejbStore() throws EJBException JavaDoc,RemoteException JavaDoc {
301         try{
302         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
303         DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
304         Connection JavaDoc con = ds.getConnection();
305         
306         PreparedStatement JavaDoc stmt = con.prepareStatement("update entity set first_name = ?, last_name = ? where EmployeeID = ?");
307         stmt.setString(1, firstName);
308         stmt.setString(2, lastName);
309         stmt.setInt(3, primaryKey);
310         stmt.execute();
311         con.close();
312         }catch(Exception JavaDoc e){
313             e.printStackTrace();
314         }
315     }
316     
317     /**
318      * A container invokes this method before it removes the EJB object
319      * that is currently associated with the instance. This method
320      * is invoked when a client invokes a remove operation on the
321      * enterprise Bean's home interface or the EJB object's remote interface.
322      * This method transitions the instance from the ready state to the pool
323      * of available instances.
324      */

325     public void ejbRemove() throws RemoveException JavaDoc,EJBException JavaDoc,RemoteException JavaDoc {
326         try{
327             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
328             DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
329             Connection JavaDoc con = ds.getConnection();
330             
331             PreparedStatement JavaDoc stmt = con.prepareStatement("delete from entity where id = ?");
332             Integer JavaDoc primaryKey = (Integer JavaDoc)ejbContext.getPrimaryKey();
333             stmt.setInt(1, primaryKey.intValue());
334             stmt.executeUpdate();
335             con.close();
336         
337         }catch(Exception JavaDoc e){
338             e.printStackTrace();
339             throw new javax.ejb.EJBException JavaDoc(e);
340         }
341     }
342     
343     /**
344      * A container invokes this method when the instance
345      * is taken out of the pool of available instances to become associated
346      * with a specific EJB object. This method transitions the instance to
347      * the ready state.
348      */

349     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
350         testAllowedOperations("ejbActivate");
351     }
352     
353     /**
354      * A container invokes this method on an instance before the instance
355      * becomes disassociated with a specific EJB object. After this method
356      * completes, the container will place the instance into the pool of
357      * available instances.
358      */

359     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
360         testAllowedOperations("ejbPassivate");
361     }
362     //
363
// EntityBean interface methods
364
//================================
365

366     protected void testAllowedOperations(String JavaDoc methodName){
367         OperationsPolicy policy = new OperationsPolicy();
368         
369         /*[1] Test getEJBHome /////////////////*/
370         try{
371             ejbContext.getEJBHome();
372             policy.allow(policy.Context_getEJBHome);
373         }catch(IllegalStateException JavaDoc ise){}
374         
375         /*[2] Test getCallerPrincipal /////////*/
376         try{
377             ejbContext.getCallerPrincipal();
378             policy.allow( policy.Context_getCallerPrincipal );
379         }catch(IllegalStateException JavaDoc ise){}
380         
381         /*[3] Test isCallerInRole /////////////*/
382         try{
383             ejbContext.isCallerInRole("ROLE");
384             policy.allow( policy.Context_isCallerInRole );
385         }catch(IllegalStateException JavaDoc ise){}
386         
387         /*[4] Test getRollbackOnly ////////////*/
388         try{
389             ejbContext.getRollbackOnly();
390             policy.allow( policy.Context_getRollbackOnly );
391         }catch(IllegalStateException JavaDoc ise){}
392         
393         /*[5] Test setRollbackOnly ////////////*/
394         try{
395             ejbContext.setRollbackOnly();
396             policy.allow( policy.Context_setRollbackOnly );
397         }catch(IllegalStateException JavaDoc ise){}
398         
399         /*[6] Test getUserTransaction /////////*/
400         try{
401             ejbContext.getUserTransaction();
402             policy.allow( policy.Context_getUserTransaction );
403         }catch(Exception JavaDoc e){}
404         
405         /*[7] Test getEJBObject ///////////////*/
406         try{
407             ejbContext.getEJBObject();
408             policy.allow( policy.Context_getEJBObject );
409         }catch(IllegalStateException JavaDoc ise){}
410
411         /*[8] Test getPrimaryKey //////////////*/
412         try{
413             ejbContext.getPrimaryKey();
414             policy.allow( policy.Context_getPrimaryKey );
415         }catch(IllegalStateException JavaDoc ise){}
416          
417         /* TO DO:
418          * Check for policy.Enterprise_bean_access
419          * Check for policy.JNDI_access_to_java_comp_env
420          * Check for policy.Resource_manager_access
421          */

422         allowedOperationsTable.put(methodName, policy);
423     }
424
425 }
426
Popular Tags