001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SimpleEB.java 6238 2005-02-07 17:42:26Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.ebasic;
027:
028: import java.sql.Connection;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.util.Collection;
033: import java.util.Enumeration;
034: import java.util.Vector;
035:
036: import javax.ejb.CreateException;
037: import javax.ejb.EntityBean;
038: import javax.ejb.FinderException;
039: import javax.ejb.ObjectNotFoundException;
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.sql.DataSource;
043:
044: import org.objectweb.util.monolog.api.BasicLevel;
045:
046: /**
047: * This is an entity bean with "bean managed persistence". The state of an
048: * instance is stored into a relational database.
049: * The table must exist.
050: * @author Philippe Durieux, Philippe Coq
051: */
052: public class SimpleEB extends SimpleEC implements EntityBean {
053:
054: static final String tableName = "ebasicSimpleEB";
055:
056: // Database related information
057: private DataSource dataSource = null;
058: private static String dataSourceName;
059:
060: public void ejbLoad() {
061: logger.log(BasicLevel.DEBUG, "");
062: String testname = (String) entityContext.getPrimaryKey();
063: PreparedStatement loadPstmt = null;
064: Connection conn = null;
065: try {
066: conn = getConnection();
067: loadPstmt = conn
068: .prepareStatement("select c_info, c_numtest, c_testname from "
069: + tableName + " where c_testname=?");
070:
071: // PK fields
072: loadPstmt.setString(1, testname);
073: ResultSet rs = loadPstmt.executeQuery();
074: if (rs.next() == false) {
075: throw new javax.ejb.EJBException(
076: "Failed to load bean from database " + testname);
077: }
078: ;
079: info = rs.getInt(1);
080: testname = rs.getString(3);
081: numtest = rs.getInt(2);
082:
083: } catch (SQLException e) {
084: throw new javax.ejb.EJBException(
085: "Failed to load bean from database" + e);
086: } finally {
087: try {
088: loadPstmt.close();
089: conn.close();
090: } catch (Exception e) {
091: System.err.println("ejbLoad: Ignore exception:" + e);
092: }
093: }
094:
095: }
096:
097: public void ejbStore() {
098: logger.log(BasicLevel.DEBUG, "");
099: String testname = (String) entityContext.getPrimaryKey();
100: PreparedStatement storePstmt = null;
101: Connection conn = null;
102:
103: try {
104: conn = getConnection();
105: storePstmt = conn.prepareStatement("update " + tableName
106: + " set c_info=?,c_numtest=? where c_testname=?");
107:
108: storePstmt.setString(3, testname);
109: storePstmt.setInt(1, info);
110: storePstmt.setInt(2, numtest);
111: storePstmt.executeUpdate();
112: } catch (SQLException e) {
113: throw new javax.ejb.EJBException(
114: "Failed to store bean to database " + testname);
115:
116: } finally {
117: try {
118: storePstmt.close();
119: conn.close();
120: } catch (Exception e) {
121: System.err.println("ejbStore: Ignore exception:" + e);
122: }
123: }
124: }
125:
126: public void ejbRemove() {
127: logger.log(BasicLevel.DEBUG, "");
128: String testname = (String) entityContext.getPrimaryKey();
129: PreparedStatement removePstmt = null;
130: Connection conn = null;
131: try {
132: conn = getConnection();
133: removePstmt = conn.prepareStatement("delete from "
134: + tableName + " where c_testname=?");
135: // PK fields
136: removePstmt.setString(1, testname);
137: removePstmt.executeUpdate();
138: } catch (SQLException e) {
139: throw new javax.ejb.EJBException(
140: "Failed to delete bean from database " + testname);
141: } finally {
142: try {
143: removePstmt.close();
144: conn.close();
145: } catch (Exception e) {
146: System.err.println("ejbRemove: Ignore exception:" + e);
147: }
148: }
149: }
150:
151: public String ejbCreate(String name, int info, int num)
152: throws CreateException {
153:
154: super .ejbCreate(name, info, num);
155:
156: PreparedStatement createPstmt = null;
157: Connection conn = null;
158:
159: // persistence management
160: try {
161: conn = getConnection();
162: createPstmt = conn.prepareStatement("insert into "
163: + tableName + " values (?, ?, ?)");
164: // all fields
165: createPstmt.setString(1, testname);
166: createPstmt.setInt(2, info);
167: createPstmt.setInt(3, numtest);
168: createPstmt.executeUpdate();
169: } catch (SQLException e) {
170: logger.log(BasicLevel.ERROR, "ejbCreate failed: " + e);
171: throw new javax.ejb.EJBException(
172: "Failed to create bean in database " + testname);
173: } finally {
174: try {
175: createPstmt.close();
176: conn.close();
177: } catch (Exception e) {
178: // ignore exception
179: logger.log(BasicLevel.ERROR,
180: "ejbFindByPrimaryKey: Ignore exception:" + e);
181: }
182: }
183: return name;
184: }
185:
186: public void ejbPostCreate(String name, int info, int num) {
187: super .ejbPostCreate(name, info, num);
188: }
189:
190: public String ejbFindByPrimaryKey(String name)
191: throws ObjectNotFoundException, FinderException {
192: logger.log(BasicLevel.DEBUG, "name=" + name);
193: PreparedStatement loadPstmt = null;
194: Connection conn = null;
195: try {
196: conn = getConnection();
197: loadPstmt = conn
198: .prepareStatement("select c_info,c_numtest from "
199: + tableName + " where c_testname=?");
200: loadPstmt.setString(1, name);
201: ResultSet rs = loadPstmt.executeQuery();
202: if (rs.next() == false) {
203: throw new javax.ejb.ObjectNotFoundException(
204: "primary key : " + name);
205: }
206: } catch (SQLException e) {
207: throw new javax.ejb.FinderException(
208: "Failed to executeQuery " + e);
209: } finally {
210: try {
211: loadPstmt.close();
212: conn.close();
213: } catch (Exception e) {
214: // ignore exception
215: logger.log(BasicLevel.ERROR,
216: "ejbFindByPrimaryKey: Ignore exception:" + e);
217: }
218: }
219: return name;
220: }
221:
222: public String ejbFindByTestName(String name)
223: throws ObjectNotFoundException, FinderException {
224: logger.log(BasicLevel.DEBUG, "name=" + name);
225: PreparedStatement loadPstmt = null;
226: Connection conn = null;
227:
228: try {
229: conn = getConnection();
230: loadPstmt = conn
231: .prepareStatement("select c_info,c_numtest from "
232: + tableName + " where c_testname=?");
233: loadPstmt.setString(1, name);
234: ResultSet rs = loadPstmt.executeQuery();
235: if (rs.next() == false) {
236: throw new javax.ejb.ObjectNotFoundException(
237: "primary key : " + name);
238: }
239: } catch (SQLException e) {
240: throw new javax.ejb.FinderException(
241: "Failed to executeQuery " + e);
242: } finally {
243: try {
244: loadPstmt.close();
245: conn.close();
246: } catch (Exception e) {
247: // ignore exception
248: logger.log(BasicLevel.ERROR,
249: "ejbFindByPrimaryKey: Ignore exception:" + e);
250: }
251: }
252:
253: return name;
254: }
255:
256: /** is similar to ejbFindByTestName but try to access to TimerService and PK that should get
257: IllegalStateException.
258: **/
259: public String ejbFindByName(String name)
260: throws ObjectNotFoundException, FinderException {
261: logger.log(BasicLevel.DEBUG, "name=" + name);
262: PreparedStatement loadPstmt = null;
263: Connection conn = null;
264:
265: try {
266: conn = getConnection();
267: loadPstmt = conn
268: .prepareStatement("select c_info,c_numtest from "
269: + tableName + " where c_testname=?");
270: loadPstmt.setString(1, name);
271: ResultSet rs = loadPstmt.executeQuery();
272: if (rs.next() == false) {
273: throw new javax.ejb.ObjectNotFoundException(
274: "primary key : " + name);
275: }
276: } catch (SQLException e) {
277: throw new javax.ejb.FinderException(
278: "Failed to executeQuery " + e);
279: } finally {
280: try {
281: loadPstmt.close();
282: conn.close();
283: } catch (Exception e) {
284: // ignore exception
285: logger.log(BasicLevel.ERROR,
286: "ejbFindByPrimaryKey: Ignore exception:" + e);
287: }
288: }
289: // Try to access TimerService and PK: Should get IllegalStateException.
290: if (entityContext != null) {
291: try {
292: entityContext.getTimerService();
293: throw new FinderException(
294: "Should not get TimerService from a finder method");
295: } catch (IllegalStateException e) {
296: logger.log(BasicLevel.DEBUG,
297: "IllegalStateException has been thrown");
298: }
299: try {
300: entityContext.getPrimaryKey();
301: throw new FinderException(
302: "Should not get PK from a finder method");
303: } catch (IllegalStateException e) {
304: logger.log(BasicLevel.DEBUG,
305: "IllegalStateException has been thrown");
306: }
307: }
308: return name;
309: }
310:
311: public Enumeration ejbFindInfoForNum(int num)
312: throws FinderException {
313: logger.log(BasicLevel.DEBUG, "num=" + num);
314: PreparedStatement loadPstmt = null;
315: Connection conn = null;
316: Vector pkV = new Vector();
317: try {
318: conn = getConnection();
319: loadPstmt = conn
320: .prepareStatement("select c_testname from "
321: + tableName + " where c_numtest=?");
322: loadPstmt.setInt(1, num);
323: ResultSet rs = loadPstmt.executeQuery();
324:
325: while (rs.next()) {
326: String pk = rs.getString(1);
327: pkV.addElement(pk);
328:
329: }
330:
331: } catch (SQLException e) {
332: throw new javax.ejb.FinderException(
333: "Failed to executeQuery " + e);
334: } finally {
335: try {
336: loadPstmt.close();
337: conn.close();
338: } catch (Exception e) {
339: // ignore exception
340: logger.log(BasicLevel.ERROR,
341: "ejbFindByPrimaryKey: Ignore exception:" + e);
342: }
343: }
344: return (pkV.elements());
345: }
346:
347: public String ejbFindOneByNum(int numTest)
348: throws ObjectNotFoundException, FinderException {
349: logger.log(BasicLevel.DEBUG, "numTest=" + numTest);
350: throw new FinderException();
351: }
352:
353: public Collection ejbFindInCollection() throws FinderException {
354: logger.log(BasicLevel.DEBUG, "");
355: PreparedStatement loadPstmt = null;
356: Connection conn = null;
357: Vector pkV = new Vector();
358: try {
359: conn = getConnection();
360: loadPstmt = conn
361: .prepareStatement("select c_testname from "
362: + tableName + " where c_numtest =?");
363: loadPstmt.setInt(1, 8);
364: ResultSet rs = loadPstmt.executeQuery();
365: while (rs.next()) {
366: String pk = rs.getString(1);
367: pkV.addElement(pk);
368: }
369: } catch (SQLException e) {
370: throw new javax.ejb.FinderException(
371: "Failed to executeQuery " + e);
372: } finally {
373: try {
374: loadPstmt.close();
375: conn.close();
376: } catch (Exception e) {
377: // ignore exception
378: logger.log(BasicLevel.ERROR,
379: "ejbFindByPrimaryKey: Ignore exception:" + e);
380: }
381: }
382: return (pkV);
383: }
384:
385: public Enumeration ejbFindInfo_beetwen(int p1, int p2)
386: throws FinderException {
387: logger.log(BasicLevel.DEBUG, "");
388: Vector pkC = new Vector();
389: Connection conn = null;
390: PreparedStatement loadPstmt = null;
391: try {
392: conn = getConnection();
393: loadPstmt = conn
394: .prepareStatement("select c_testname from "
395: + tableName
396: + " where ?<=c_info and c_info<=? and ?<=c_numtest and c_numtest<=?");
397: loadPstmt.setInt(1, p1);
398: loadPstmt.setInt(2, p2);
399: loadPstmt.setInt(3, p1);
400: loadPstmt.setInt(4, p2);
401:
402: ResultSet rs = loadPstmt.executeQuery();
403: while (rs.next()) {
404: String pk = rs.getString(1);
405: pkC.addElement(pk);
406: }
407: } catch (Exception e) {
408: throw new javax.ejb.FinderException(
409: "Failed to find bean from database (findInfo_beetwen)");
410: } finally {
411: try {
412: loadPstmt.close();
413: conn.close();
414: } catch (Exception e) {
415: // ignore exception
416: logger.log(BasicLevel.ERROR,
417: "ejbFindByPrimaryKey: Ignore exception:" + e);
418: }
419: }
420: return (pkC.elements());
421: }
422:
423: public Enumeration ejbFindEqualOne(int p1) throws FinderException {
424: logger.log(BasicLevel.DEBUG, "");
425: Vector pkC = new Vector();
426: Connection conn = null;
427: PreparedStatement loadPstmt = null;
428: try {
429: conn = getConnection();
430: loadPstmt = conn
431: .prepareStatement("select c_testname from "
432: + tableName
433: + " where c_info = ? and c_numtest = ?");
434: loadPstmt.setInt(1, p1);
435: loadPstmt.setInt(2, p1);
436:
437: ResultSet rs = loadPstmt.executeQuery();
438: while (rs.next()) {
439: String pk = rs.getString(1);
440: pkC.addElement(pk);
441: }
442: } catch (Exception e) {
443: throw new javax.ejb.FinderException(
444: "Failed to find bean from database (findInfo_beetwen)");
445: } finally {
446: try {
447: loadPstmt.close();
448: conn.close();
449: } catch (Exception e) {
450: // ignore exception
451: logger.log(BasicLevel.ERROR,
452: "ejbFindByPrimaryKey: Ignore exception:" + e);
453: }
454: }
455: return (pkC.elements());
456: }
457:
458: public Enumeration ejbFindEqualTwo(int p1, int p2)
459: throws FinderException {
460: logger.log(BasicLevel.DEBUG, "");
461: Vector pkC = new Vector();
462: Connection conn = null;
463: PreparedStatement loadPstmt = null;
464: try {
465: conn = getConnection();
466: loadPstmt = conn
467: .prepareStatement("select c_testname from "
468: + tableName
469: + " where c_info = ? and c_numtest = ?");
470: loadPstmt.setInt(1, p1);
471: loadPstmt.setInt(2, p2);
472:
473: ResultSet rs = loadPstmt.executeQuery();
474: while (rs.next()) {
475: String pk = rs.getString(1);
476: pkC.addElement(pk);
477: }
478: } catch (Exception e) {
479: throw new javax.ejb.FinderException(
480: "Failed to find bean from database (findInfo_beetwen)");
481: } finally {
482: try {
483: loadPstmt.close();
484: conn.close();
485: } catch (Exception e) {
486: // ignore exception
487: logger.log(BasicLevel.ERROR,
488: "ejbFindByPrimaryKey: Ignore exception:" + e);
489: }
490: }
491: // Try to access TimerService and PK: Should get IllegalStateException.
492: if (entityContext != null) {
493: try {
494: entityContext.getTimerService();
495: throw new FinderException(
496: "Should not get TimerService from a finder method");
497: } catch (IllegalStateException e) {
498: logger.log(BasicLevel.DEBUG,
499: "IllegalStateException has been thrown");
500: }
501: try {
502: entityContext.getPrimaryKey();
503: throw new FinderException(
504: "Should not get PK from a finder method");
505: } catch (IllegalStateException e) {
506: logger.log(BasicLevel.DEBUG,
507: "IllegalStateException has been thrown");
508: }
509: }
510: return (pkC.elements());
511: }
512:
513: private Connection getConnection() throws java.sql.SQLException {
514: if (dataSource == null) {
515: // Finds DataSource from JNDI
516: Context initialContext = null;
517: try {
518: initialContext = new InitialContext();
519: dataSource = (DataSource) initialContext
520: .lookup("java:comp/env/jdbc/BMP");
521: } catch (Exception e) {
522: throw new javax.ejb.EJBException(
523: "Pb with naming context ");
524: }
525: }
526: Connection ret = dataSource.getConnection();
527: if (ret == null) {
528: throw new javax.ejb.EJBException(
529: "dataSource.getConnection() returned null");
530: }
531: return ret;
532: }
533:
534: // Just to test we can define a method named 'ejbSelect' (see bug #522)
535: public void ejbSelect() {
536: }
537:
538: }
|