001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.bmp.beans;
023:
024: import java.rmi.RemoteException;
025: import java.sql.Connection;
026: import java.sql.DatabaseMetaData;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029:
030: import javax.ejb.CreateException;
031: import javax.ejb.EJBException;
032: import javax.ejb.SessionBean;
033: import javax.ejb.SessionContext;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036: import javax.sql.DataSource;
037:
038: import org.jboss.logging.Logger;
039: import org.jboss.test.bmp.interfaces.SimpleBMP;
040: import org.jboss.test.bmp.interfaces.SimpleBMPHome;
041:
042: public class BMPHelperSessionBean implements SessionBean {
043: /** The serialVersionUID */
044: private static final long serialVersionUID = 1L;
045:
046: Logger log = Logger.getLogger(getClass());
047:
048: SessionContext ctx = null;
049: private DataSource ds = null;
050:
051: public void ejbCreate() throws CreateException, RemoteException {
052: try {
053: ds = (DataSource) new InitialContext()
054: .lookup("java:comp/env/datasource");
055: } catch (NamingException _ne) {
056: throw new CreateException("Datasource not found: "
057: + _ne.getMessage());
058: }
059: }
060:
061: public boolean existsSimpleBeanTable() {
062: return tableExists("SIMPLEBEAN");
063: }
064:
065: public void createSimpleBeanTable() {
066: createTable("CREATE TABLE SIMPLEBEAN (id INTEGER, name VARCHAR(200))");
067: }
068:
069: public void dropSimpleBeanTable() {
070: dropTable("SIMPLEBEAN");
071: }
072:
073: public String doTest() throws RemoteException {
074: StringBuffer sb = new StringBuffer();
075: SimpleBMP b;
076: try {
077: SimpleBMPHome home = (SimpleBMPHome) new InitialContext()
078: .lookup("java:comp/env/bean");
079: b = home.findByPrimaryKey(new Integer(1));
080: } catch (Exception _ne) {
081: throw new EJBException("couldnt find entity: "
082: + _ne.getMessage());
083: }
084: sb.append("found: " + b.getName() + "\n");
085: sb.append("set name to \"Name for rollback\"\n");
086: b.setName("Name for rollback");
087: sb.append("current name is: " + b.getName() + "\n");
088: try {
089: sb.append("now rolling back...\n");
090:
091: ctx.setRollbackOnly();
092: } catch (Exception _e) {
093: sb.append("Error on rolling back: " + _e.getMessage()
094: + "\n");
095: }
096: sb.append("done.");
097:
098: return sb.toString();
099: }
100:
101: public String doTestAfterRollback() throws RemoteException {
102: StringBuffer sb = new StringBuffer();
103: SimpleBMP b;
104: try {
105: SimpleBMPHome home = (SimpleBMPHome) new InitialContext()
106: .lookup("java:comp/env/bean");
107: b = home.findByPrimaryKey(new Integer(1));
108: } catch (Exception _ne) {
109: throw new EJBException("couldnt find entity: "
110: + _ne.getMessage());
111: }
112: sb.append("found: " + b.getName() + "\n");
113: sb.append("done.");
114:
115: return sb.toString();
116: }
117:
118: private boolean tableExists(String _tableName) {
119: boolean result = false;
120: Connection con = null;
121: try {
122: con = ds.getConnection();
123: DatabaseMetaData dmd = con.getMetaData();
124: ResultSet rs = dmd.getTables(con.getCatalog(), null,
125: _tableName, null);
126: if (rs.next())
127: result = true;
128:
129: rs.close();
130: } catch (Exception _e) {
131: throw new EJBException("Error while looking up table: "
132: + _e.getMessage());
133: } finally {
134: try {
135: if (con != null)
136: con.close();
137: } catch (Exception _sqle) {
138: }
139: }
140: return result;
141: }
142:
143: private void createTable(String _sql) {
144: Connection con = null;
145: try {
146: con = ds.getConnection();
147: Statement s = con.createStatement();
148: s.executeUpdate(_sql);
149: s.close();
150: } catch (Exception _e) {
151: throw new EJBException("Error while creating table: "
152: + _e.getMessage());
153: } finally {
154: try {
155: if (con != null)
156: con.close();
157: } catch (Exception _sqle) {
158: }
159: }
160: }
161:
162: private void dropTable(String _tableName) {
163: Connection con = null;
164: try {
165: con = ds.getConnection();
166: Statement s = con.createStatement();
167: s.executeUpdate("DROP TABLE " + _tableName);
168: s.close();
169: } catch (Exception _e) {
170: throw new EJBException("Error while dropping table: "
171: + _e.getMessage());
172: } finally {
173: try {
174: if (con != null)
175: con.close();
176: } catch (Exception _sqle) {
177: }
178: }
179: }
180:
181: public void ejbActivate() {
182: }
183:
184: public void ejbPassivate() {
185: }
186:
187: public void ejbRemove() {
188: }
189:
190: public void setSessionContext(SessionContext _ctx) {
191: ctx = _ctx;
192: }
193: }
|