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 3845 2003-12-05 14:19:55Z legrasi $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.fbasic;
027:
028: import java.rmi.RemoteException;
029: import java.sql.Connection;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033: import java.util.Collection;
034: import java.util.Enumeration;
035: import java.util.Properties;
036: import java.util.Vector;
037:
038: import javax.ejb.CreateException;
039: import javax.ejb.EJBException;
040: import javax.ejb.EntityBean;
041: import javax.ejb.EntityContext;
042: import javax.ejb.FinderException;
043: import javax.ejb.ObjectNotFoundException;
044:
045: import javax.naming.Context;
046: import javax.naming.InitialContext;
047: import javax.sql.DataSource;
048:
049: /**
050: * This is an entity bean with "bean managed persistence". The state of an
051: * instance is stored into a relational database.
052: * The table must exist.
053: * @author Philippe Durieux, Philippe Coq
054: */
055: public class SimpleEB extends SimpleEC implements EntityBean {
056:
057: static final String tableName = "fbasicSimpleEB";
058:
059: // Database related information
060: private DataSource dataSource = null;
061: private static String dataSourceName;
062:
063: public void ejbLoad() {
064: // logger.log(BasicLevel.DEBUG, "");
065: String testname = (String) entityContext.getPrimaryKey();
066: PreparedStatement loadPstmt = null;
067: Connection conn = null;
068: try {
069: conn = getConnection();
070: loadPstmt = conn
071: .prepareStatement("select c_info, c_numtest, c_testname from "
072: + tableName + " where c_testname=?");
073:
074: // PK fields
075: loadPstmt.setString(1, testname);
076: ResultSet rs = loadPstmt.executeQuery();
077: if (rs.next() == false) {
078: throw new javax.ejb.EJBException(
079: "Failed to load bean from database " + testname);
080: }
081: ;
082: info = rs.getInt(1);
083: testname = rs.getString(3);
084: numtest = rs.getInt(2);
085:
086: } catch (SQLException e) {
087: throw new javax.ejb.EJBException(
088: "Failed to load bean from database" + e);
089: } finally {
090: try {
091: loadPstmt.close();
092: conn.close();
093: } catch (Exception e) {
094: System.err.println("ejbLoad: Ignore exception:" + e);
095: }
096: }
097:
098: }
099:
100: public void ejbStore() {
101: // logger.log(BasicLevel.DEBUG, "");
102: String testname = (String) entityContext.getPrimaryKey();
103: PreparedStatement storePstmt = null;
104: Connection conn = null;
105:
106: try {
107: conn = getConnection();
108: storePstmt = conn.prepareStatement("update " + tableName
109: + " set c_info=?,c_numtest=? where c_testname=?");
110:
111: storePstmt.setString(3, testname);
112: storePstmt.setInt(1, info);
113: storePstmt.setInt(2, numtest);
114: storePstmt.executeUpdate();
115: } catch (SQLException e) {
116: throw new javax.ejb.EJBException(
117: "Failed to store bean to database " + testname);
118:
119: } finally {
120: try {
121: storePstmt.close();
122: conn.close();
123: } catch (Exception e) {
124: System.err.println("ejbStore: Ignore exception:" + e);
125: }
126: }
127: }
128:
129: public void ejbRemove() {
130: //logger.log(BasicLevel.DEBUG, "");
131: String testname = (String) entityContext.getPrimaryKey();
132: PreparedStatement removePstmt = null;
133: Connection conn = null;
134: try {
135: conn = getConnection();
136: removePstmt = conn.prepareStatement("delete from "
137: + tableName + " where c_testname=?");
138: // PK fields
139: removePstmt.setString(1, testname);
140: removePstmt.executeUpdate();
141: } catch (SQLException e) {
142: throw new javax.ejb.EJBException(
143: "Failed to delete bean from database " + testname);
144: } finally {
145: try {
146: removePstmt.close();
147: conn.close();
148: } catch (Exception e) {
149: System.err.println("ejbRemove: Ignore exception:" + e);
150: }
151: }
152: }
153:
154: public String ejbCreate(String name, int info, int num)
155: throws CreateException {
156:
157: super .ejbCreate(name, info, num);
158:
159: PreparedStatement createPstmt = null;
160: Connection conn = null;
161:
162: // persistence management
163: try {
164: conn = getConnection();
165: createPstmt = conn.prepareStatement("insert into "
166: + tableName + " values (?, ?, ?)");
167: // all fields
168: createPstmt.setString(1, testname);
169: createPstmt.setInt(2, info);
170: createPstmt.setInt(3, numtest);
171: createPstmt.executeUpdate();
172: } catch (SQLException e) {
173: throw new javax.ejb.EJBException(
174: "Failed to create bean in database " + testname);
175: } finally {
176: try {
177: createPstmt.close();
178: conn.close();
179: } catch (Exception e) {
180: // ignore exception
181: //logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
182: }
183: }
184: return name;
185: }
186:
187: public void ejbPostCreate(String name, int info, int num) {
188: super .ejbPostCreate(name, info, num);
189: }
190:
191: public String ejbFindByPrimaryKey(String name)
192: throws ObjectNotFoundException, FinderException {
193: //logger.log(BasicLevel.DEBUG, "name="+ name);
194: PreparedStatement loadPstmt = null;
195: Connection conn = null;
196: try {
197: conn = getConnection();
198: loadPstmt = conn
199: .prepareStatement("select c_info,c_numtest from "
200: + tableName + " where c_testname=?");
201: loadPstmt.setString(1, name);
202: ResultSet rs = loadPstmt.executeQuery();
203: if (rs.next() == false) {
204: throw new javax.ejb.ObjectNotFoundException(
205: "primary key : " + name);
206: }
207: } catch (SQLException e) {
208: throw new javax.ejb.FinderException(
209: "Failed to executeQuery " + e);
210: } finally {
211: try {
212: loadPstmt.close();
213: conn.close();
214: } catch (Exception e) {
215: // ignore exception
216: //logger.log(BasicLevel.ERROR, "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, "ejbFindByPrimaryKey: Ignore exception:"+e);
249: }
250: }
251: return name;
252: }
253:
254: public Enumeration ejbFindInfoForNum(int num)
255: throws FinderException {
256: // logger.log(BasicLevel.DEBUG, "num="+ num);
257: PreparedStatement loadPstmt = null;
258: Connection conn = null;
259: Vector pkV = new Vector();
260: try {
261: conn = getConnection();
262: loadPstmt = conn
263: .prepareStatement("select c_testname from "
264: + tableName + " where c_numtest=?");
265: loadPstmt.setInt(1, num);
266: ResultSet rs = loadPstmt.executeQuery();
267:
268: while (rs.next()) {
269: String pk = rs.getString(1);
270: pkV.addElement(pk);
271:
272: }
273:
274: } catch (SQLException e) {
275: throw new javax.ejb.FinderException(
276: "Failed to executeQuery " + e);
277: } finally {
278: try {
279: loadPstmt.close();
280: conn.close();
281: } catch (Exception e) {
282: // ignore exception
283: //logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
284: }
285: }
286: return (pkV.elements());
287: }
288:
289: public Collection ejbFindInCollection() throws FinderException {
290: //logger.log(BasicLevel.DEBUG, "");
291: PreparedStatement loadPstmt = null;
292: Connection conn = null;
293: Vector pkV = new Vector();
294: try {
295: conn = getConnection();
296: loadPstmt = conn
297: .prepareStatement("select c_testname from "
298: + tableName + " where c_numtest =?");
299: loadPstmt.setInt(1, 8);
300: ResultSet rs = loadPstmt.executeQuery();
301: while (rs.next()) {
302: String pk = rs.getString(1);
303: pkV.addElement(pk);
304: }
305: } catch (SQLException e) {
306: throw new javax.ejb.FinderException(
307: "Failed to executeQuery " + e);
308: } finally {
309: try {
310: loadPstmt.close();
311: conn.close();
312: } catch (Exception e) {
313: // ignore exception
314: //logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
315: }
316: }
317: return (pkV);
318: }
319:
320: public Enumeration ejbFindInfo_beetwen(int p1, int p2)
321: throws FinderException {
322: //logger.log(BasicLevel.DEBUG, "");
323: Vector pkC = new Vector();
324: Connection conn = null;
325: PreparedStatement loadPstmt = null;
326: try {
327: conn = getConnection();
328: loadPstmt = conn
329: .prepareStatement("select c_testname from "
330: + tableName
331: + " where ?<=c_info and c_info<=? and ?<=c_numtest and c_numtest<=?");
332: loadPstmt.setInt(1, p1);
333: loadPstmt.setInt(2, p2);
334: loadPstmt.setInt(3, p1);
335: loadPstmt.setInt(4, p2);
336:
337: ResultSet rs = loadPstmt.executeQuery();
338: while (rs.next()) {
339: String pk = rs.getString(1);
340: pkC.addElement(pk);
341: }
342: } catch (Exception e) {
343: throw new javax.ejb.FinderException(
344: "Failed to find bean from database (findInfo_beetwen)");
345: } finally {
346: try {
347: loadPstmt.close();
348: conn.close();
349: } catch (Exception e) {
350: // ignore exception
351: //logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
352: }
353: }
354: return (pkC.elements());
355: }
356:
357: public Enumeration ejbFindEqualOne(int p1) throws FinderException {
358: // logger.log(BasicLevel.DEBUG, "");
359: Vector pkC = new Vector();
360: Connection conn = null;
361: PreparedStatement loadPstmt = null;
362: try {
363: conn = getConnection();
364: loadPstmt = conn
365: .prepareStatement("select c_testname from "
366: + tableName
367: + " where c_info = ? and c_numtest = ?");
368: loadPstmt.setInt(1, p1);
369: loadPstmt.setInt(2, p1);
370:
371: ResultSet rs = loadPstmt.executeQuery();
372: while (rs.next()) {
373: String pk = rs.getString(1);
374: pkC.addElement(pk);
375: }
376: } catch (Exception e) {
377: throw new javax.ejb.FinderException(
378: "Failed to find bean from database (findInfo_beetwen)");
379: } finally {
380: try {
381: loadPstmt.close();
382: conn.close();
383: } catch (Exception e) {
384: // ignore exception
385: //logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
386: }
387: }
388: return (pkC.elements());
389: }
390:
391: public Enumeration ejbFindEqualTwo(int p1, int p2)
392: throws FinderException {
393: // logger.log(BasicLevel.DEBUG, "");
394: Vector pkC = new Vector();
395: Connection conn = null;
396: PreparedStatement loadPstmt = null;
397: try {
398: conn = getConnection();
399: loadPstmt = conn
400: .prepareStatement("select c_testname from "
401: + tableName
402: + " where c_info = ? and c_numtest = ?");
403: loadPstmt.setInt(1, p1);
404: loadPstmt.setInt(2, p2);
405:
406: ResultSet rs = loadPstmt.executeQuery();
407: while (rs.next()) {
408: String pk = rs.getString(1);
409: pkC.addElement(pk);
410: }
411: } catch (Exception e) {
412: throw new javax.ejb.FinderException(
413: "Failed to find bean from database (findInfo_beetwen)");
414: } finally {
415: try {
416: loadPstmt.close();
417: conn.close();
418: } catch (Exception e) {
419: // ignore exception
420: // logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
421: }
422: }
423: return (pkC.elements());
424: }
425:
426: private Connection getConnection() throws java.sql.SQLException {
427: if (dataSource == null) {
428: // Finds DataSource from JNDI
429: Context initialContext = null;
430: try {
431: initialContext = new InitialContext();
432: dataSource = (DataSource) initialContext
433: .lookup("java:comp/env/jdbc/BMP");
434: } catch (Exception e) {
435: throw new javax.ejb.EJBException(
436: "Pb with naming context ");
437: }
438: }
439: Connection ret = dataSource.getConnection();
440: if (ret == null) {
441: throw new javax.ejb.EJBException(
442: "dataSource.getConnection() returned null");
443: }
444: return ret;
445: }
446:
447: // Just to test we can define a method named 'ejbSelect' (see bug #522)
448: public void ejbSelect() {
449: }
450:
451: }
|