KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > ebasic > SimpleEB


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SimpleEB.java,v 1.10 2005/02/07 17:42:26 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.ebasic;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.ejb.CreateException JavaDoc;
37 import javax.ejb.EntityBean JavaDoc;
38 import javax.ejb.FinderException JavaDoc;
39 import javax.ejb.ObjectNotFoundException JavaDoc;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.sql.DataSource JavaDoc;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * This is an entity bean with "bean managed persistence". The state of an
48  * instance is stored into a relational database.
49  * The table must exist.
50  * @author Philippe Durieux, Philippe Coq
51  */

52 public class SimpleEB extends SimpleEC implements EntityBean JavaDoc {
53
54     static final String JavaDoc tableName = "ebasicSimpleEB";
55
56     // Database related information
57
private DataSource JavaDoc dataSource = null;
58     private static String JavaDoc dataSourceName;
59
60
61     public void ejbLoad() {
62         logger.log(BasicLevel.DEBUG, "");
63         String JavaDoc testname = (String JavaDoc)entityContext.getPrimaryKey();
64         PreparedStatement JavaDoc loadPstmt = null;
65         Connection JavaDoc conn = null;
66         try {
67             conn = getConnection();
68             loadPstmt = conn.prepareStatement("select c_info, c_numtest, c_testname from "+tableName+" where c_testname=?");
69
70             // PK fields
71
loadPstmt.setString(1, testname);
72             ResultSet JavaDoc rs = loadPstmt.executeQuery();
73             if (rs.next() == false) {
74                 throw new javax.ejb.EJBException JavaDoc("Failed to load bean from database "+testname);
75             };
76             info = rs.getInt(1);
77             testname = rs.getString(3);
78             numtest = rs.getInt(2);
79         
80         } catch (SQLException JavaDoc e) {
81             throw new javax.ejb.EJBException JavaDoc("Failed to load bean from database" +e);
82         } finally {
83             try{
84                 loadPstmt.close();
85                 conn.close();
86             } catch(Exception JavaDoc e) {
87                 System.err.println("ejbLoad: Ignore exception:"+e);
88             }
89         }
90
91     }
92
93     public void ejbStore() {
94         logger.log(BasicLevel.DEBUG, "");
95         String JavaDoc testname = (String JavaDoc)entityContext.getPrimaryKey();
96         PreparedStatement JavaDoc storePstmt = null;
97         Connection JavaDoc conn = null;
98     
99         try {
100             conn = getConnection();
101             storePstmt = conn.prepareStatement("update "+tableName+" set c_info=?,c_numtest=? where c_testname=?");
102
103             storePstmt.setString(3, testname);
104             storePstmt.setInt(1, info);
105             storePstmt.setInt(2, numtest);
106             storePstmt.executeUpdate();
107         } catch (SQLException JavaDoc e) {
108             throw new javax.ejb.EJBException JavaDoc("Failed to store bean to database "+testname);
109         
110         } finally {
111             try{
112                 storePstmt.close();
113                 conn.close();
114             } catch(Exception JavaDoc e) {
115                 System.err.println("ejbStore: Ignore exception:"+e);
116             }
117         }
118     }
119
120     public void ejbRemove() {
121         logger.log(BasicLevel.DEBUG, "");
122         String JavaDoc testname = (String JavaDoc)entityContext.getPrimaryKey();
123         PreparedStatement JavaDoc removePstmt = null;
124         Connection JavaDoc conn = null;
125         try {
126             conn = getConnection();
127             removePstmt = conn.prepareStatement("delete from "+tableName+" where c_testname=?");
128             // PK fields
129
removePstmt.setString(1, testname );
130             removePstmt.executeUpdate();
131         } catch (SQLException JavaDoc e) {
132             throw new javax.ejb.EJBException JavaDoc("Failed to delete bean from database "+testname);
133         } finally {
134             try{
135                 removePstmt.close();
136                 conn.close();
137             } catch(Exception JavaDoc e) {
138                 System.err.println("ejbRemove: Ignore exception:"+e);
139             }
140         }
141     }
142
143
144     public String JavaDoc ejbCreate(String JavaDoc name, int info, int num) throws CreateException JavaDoc {
145
146         super.ejbCreate(name, info, num);
147
148         PreparedStatement JavaDoc createPstmt = null;
149         Connection JavaDoc conn = null;
150
151         // persistence management
152
try {
153             conn = getConnection();
154             createPstmt = conn.prepareStatement("insert into "+tableName+" values (?, ?, ?)");
155             // all fields
156
createPstmt.setString(1, testname);
157             createPstmt.setInt(2, info);
158             createPstmt.setInt(3, numtest);
159             createPstmt.executeUpdate();
160         } catch (SQLException JavaDoc e) {
161             logger.log(BasicLevel.ERROR, "ejbCreate failed: " + e);
162             throw new javax.ejb.EJBException JavaDoc("Failed to create bean in database "+testname);
163         } finally {
164             try{
165                 createPstmt.close();
166                 conn.close();
167             } catch(Exception JavaDoc e) {
168                 // ignore exception
169
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
170             }
171         }
172         return name;
173     }
174
175     public void ejbPostCreate(String JavaDoc name, int info, int num) {
176         super.ejbPostCreate(name, info, num);
177     }
178
179  
180
181     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc name ) throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
182         logger.log(BasicLevel.DEBUG, "name="+ name);
183         PreparedStatement JavaDoc loadPstmt = null;
184         Connection JavaDoc conn = null;
185         try {
186             conn = getConnection();
187             loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?");
188             loadPstmt.setString(1,name );
189             ResultSet JavaDoc rs = loadPstmt.executeQuery();
190             if (rs.next() == false) {
191                 throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key : "+name);
192             }
193         } catch (SQLException JavaDoc e) {
194             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
195         } finally {
196             try{
197                 loadPstmt.close();
198                 conn.close();
199             } catch(Exception JavaDoc e) {
200                 // ignore exception
201
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
202             }
203         }
204         return name;
205     }
206
207
208     public String JavaDoc ejbFindByTestName(String JavaDoc name)
209         throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
210         logger.log(BasicLevel.DEBUG, "name="+ name);
211         PreparedStatement JavaDoc loadPstmt = null;
212         Connection JavaDoc conn = null;
213
214         try {
215             conn = getConnection();
216             loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?");
217             loadPstmt.setString(1, name);
218             ResultSet JavaDoc rs = loadPstmt.executeQuery();
219             if (rs.next() == false) {
220                 throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key : "+name);
221             }
222         } catch (SQLException JavaDoc e) {
223             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
224         } finally {
225             try{
226                 loadPstmt.close();
227                 conn.close();
228             } catch(Exception JavaDoc e) {
229                 // ignore exception
230
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
231             }
232         }
233  
234         return name;
235     }
236
237     /** is similar to ejbFindByTestName but try to access to TimerService and PK that should get
238         IllegalStateException.
239     **/

240     public String JavaDoc ejbFindByName(String JavaDoc name)
241         throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
242         logger.log(BasicLevel.DEBUG, "name="+ name);
243         PreparedStatement JavaDoc loadPstmt = null;
244         Connection JavaDoc conn = null;
245
246         try {
247             conn = getConnection();
248             loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?");
249             loadPstmt.setString(1, name);
250             ResultSet JavaDoc rs = loadPstmt.executeQuery();
251             if (rs.next() == false) {
252                 throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key : "+name);
253             }
254         } catch (SQLException JavaDoc e) {
255             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
256         } finally {
257             try{
258                 loadPstmt.close();
259                 conn.close();
260             } catch(Exception JavaDoc e) {
261                 // ignore exception
262
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
263             }
264         }
265        // Try to access TimerService and PK: Should get IllegalStateException.
266
if (entityContext != null) {
267             try {
268                 entityContext.getTimerService();
269                 throw new FinderException JavaDoc("Should not get TimerService from a finder method");
270             } catch (IllegalStateException JavaDoc e) {
271                 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown");
272             }
273             try {
274                 entityContext.getPrimaryKey();
275                 throw new FinderException JavaDoc("Should not get PK from a finder method");
276             } catch (IllegalStateException JavaDoc e) {
277                 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown");
278             }
279         }
280         return name;
281     }
282
283     public Enumeration JavaDoc ejbFindInfoForNum(int num) throws FinderException JavaDoc {
284         logger.log(BasicLevel.DEBUG, "num="+ num);
285         PreparedStatement JavaDoc loadPstmt = null;
286         Connection JavaDoc conn = null;
287         Vector JavaDoc pkV = new Vector JavaDoc();
288         try {
289             conn = getConnection();
290             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest=?");
291             loadPstmt.setInt(1, num);
292             ResultSet JavaDoc rs = loadPstmt.executeQuery();
293
294             while(rs.next()) {
295                 String JavaDoc pk = rs.getString(1);
296                 pkV.addElement(pk);
297         
298             }
299
300         } catch (SQLException JavaDoc e) {
301             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
302         } finally {
303             try{
304                 loadPstmt.close();
305                 conn.close();
306             } catch(Exception JavaDoc e) {
307                 // ignore exception
308
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
309             }
310         }
311         return (pkV.elements());
312     }
313
314     public String JavaDoc ejbFindOneByNum(int numTest)
315         throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
316         logger.log(BasicLevel.DEBUG, "numTest=" + numTest);
317         throw new FinderException JavaDoc();
318     }
319     
320     public Collection JavaDoc ejbFindInCollection() throws FinderException JavaDoc {
321         logger.log(BasicLevel.DEBUG, "");
322         PreparedStatement JavaDoc loadPstmt = null;
323         Connection JavaDoc conn = null;
324         Vector JavaDoc pkV = new Vector JavaDoc();
325         try {
326             conn = getConnection();
327             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest =?");
328             loadPstmt.setInt(1, 8);
329             ResultSet JavaDoc rs = loadPstmt.executeQuery();
330             while (rs.next()) {
331                 String JavaDoc pk = rs.getString(1);
332                 pkV.addElement(pk);
333             }
334         } catch (SQLException JavaDoc e) {
335             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
336         } finally {
337             try{
338                 loadPstmt.close();
339                 conn.close();
340             } catch(Exception JavaDoc e) {
341                 // ignore exception
342
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
343             }
344         }
345         return (pkV);
346     }
347
348     public Enumeration JavaDoc ejbFindInfo_beetwen(int p1, int p2) throws FinderException JavaDoc {
349         logger.log(BasicLevel.DEBUG, "");
350         Vector JavaDoc pkC = new Vector JavaDoc();
351         Connection JavaDoc conn = null;
352         PreparedStatement JavaDoc loadPstmt = null;
353         try {
354             conn = getConnection();
355             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where ?<=c_info and c_info<=? and ?<=c_numtest and c_numtest<=?");
356             loadPstmt.setInt(1, p1);
357             loadPstmt.setInt(2, p2);
358             loadPstmt.setInt(3, p1);
359             loadPstmt.setInt(4, p2);
360  
361             ResultSet JavaDoc rs = loadPstmt.executeQuery();
362             while (rs.next()) {
363                 String JavaDoc pk = rs.getString(1);
364                 pkC.addElement(pk);
365             }
366         } catch (Exception JavaDoc e) {
367             throw new javax.ejb.FinderException JavaDoc("Failed to find bean from database (findInfo_beetwen)");
368         } finally {
369             try{
370                 loadPstmt.close();
371                 conn.close();
372             } catch(Exception JavaDoc e) {
373                 // ignore exception
374
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
375             }
376         }
377         return(pkC.elements());
378     }
379
380     public Enumeration JavaDoc ejbFindEqualOne(int p1) throws FinderException JavaDoc {
381         logger.log(BasicLevel.DEBUG, "");
382         Vector JavaDoc pkC = new Vector JavaDoc();
383         Connection JavaDoc conn = null;
384         PreparedStatement JavaDoc loadPstmt = null;
385         try {
386             conn = getConnection();
387             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_info = ? and c_numtest = ?");
388             loadPstmt.setInt(1, p1);
389             loadPstmt.setInt(2, p1);
390  
391             ResultSet JavaDoc rs = loadPstmt.executeQuery();
392             while (rs.next()) {
393                 String JavaDoc pk = rs.getString(1);
394                 pkC.addElement(pk);
395             }
396         } catch (Exception JavaDoc e) {
397             throw new javax.ejb.FinderException JavaDoc("Failed to find bean from database (findInfo_beetwen)");
398         } finally {
399             try{
400                 loadPstmt.close();
401                 conn.close();
402             } catch(Exception JavaDoc e) {
403                 // ignore exception
404
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
405             }
406         }
407         return(pkC.elements());
408     }
409
410     public Enumeration JavaDoc ejbFindEqualTwo(int p1, int p2) throws FinderException JavaDoc {
411         logger.log(BasicLevel.DEBUG, "");
412         Vector JavaDoc pkC = new Vector JavaDoc();
413         Connection JavaDoc conn = null;
414         PreparedStatement JavaDoc loadPstmt = null;
415         try {
416             conn = getConnection();
417             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_info = ? and c_numtest = ?");
418             loadPstmt.setInt(1, p1);
419             loadPstmt.setInt(2, p2);
420  
421             ResultSet JavaDoc rs = loadPstmt.executeQuery();
422             while (rs.next()) {
423                 String JavaDoc pk = rs.getString(1);
424                 pkC.addElement(pk);
425             }
426         } catch (Exception JavaDoc e) {
427             throw new javax.ejb.FinderException JavaDoc("Failed to find bean from database (findInfo_beetwen)");
428         } finally {
429             try{
430                 loadPstmt.close();
431                 conn.close();
432             } catch(Exception JavaDoc e) {
433                 // ignore exception
434
logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
435             }
436         }
437         // Try to access TimerService and PK: Should get IllegalStateException.
438
if (entityContext != null) {
439             try {
440                 entityContext.getTimerService();
441                 throw new FinderException JavaDoc("Should not get TimerService from a finder method");
442             } catch (IllegalStateException JavaDoc e) {
443                 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown");
444             }
445             try {
446                 entityContext.getPrimaryKey();
447                 throw new FinderException JavaDoc("Should not get PK from a finder method");
448             } catch (IllegalStateException JavaDoc e) {
449                 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown");
450             }
451         }
452         return(pkC.elements());
453     }
454
455     private Connection JavaDoc getConnection() throws java.sql.SQLException JavaDoc {
456         if (dataSource == null) {
457             // Finds DataSource from JNDI
458
Context JavaDoc initialContext = null;
459             try {
460                 initialContext = new InitialContext JavaDoc();
461                 dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/BMP");
462             } catch (Exception JavaDoc e) {
463                 throw new javax.ejb.EJBException JavaDoc("Pb with naming context ");
464             }
465         }
466         Connection JavaDoc ret = dataSource.getConnection();
467         if (ret == null) {
468             throw new javax.ejb.EJBException JavaDoc("dataSource.getConnection() returned null");
469         }
470         return ret;
471     }
472
473     // Just to test we can define a method named 'ejbSelect' (see bug #522)
474
public void ejbSelect() {
475     }
476
477 }
478
Popular Tags