1 22 package org.jboss.test.bmp.beans; 23 24 import java.rmi.RemoteException ; 25 import java.sql.Connection ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 30 import javax.ejb.CreateException ; 31 import javax.ejb.EJBException ; 32 import javax.ejb.SessionBean ; 33 import javax.ejb.SessionContext ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 import javax.sql.DataSource ; 37 38 import org.jboss.logging.Logger; 39 import org.jboss.test.bmp.interfaces.SimpleBMP; 40 import org.jboss.test.bmp.interfaces.SimpleBMPHome; 41 42 public class BMPHelperSessionBean implements SessionBean 43 { 44 45 private static final long serialVersionUID = 1L; 46 47 Logger log = Logger.getLogger(getClass()); 48 49 SessionContext ctx = null; 50 private DataSource ds = null; 51 52 public void ejbCreate () throws CreateException , RemoteException 53 { 54 try 55 { 56 ds = (DataSource )new InitialContext ().lookup ("java:comp/env/datasource"); 57 } 58 catch (NamingException _ne) 59 { 60 throw new CreateException ("Datasource not found: "+_ne.getMessage ()); 61 } 62 } 63 64 public boolean existsSimpleBeanTable () 65 { 66 return tableExists ("SIMPLEBEAN"); 67 } 68 69 public void createSimpleBeanTable () 70 { 71 createTable ("CREATE TABLE SIMPLEBEAN (id INTEGER, name VARCHAR(200))"); 72 } 73 74 public void dropSimpleBeanTable () 75 { 76 dropTable ("SIMPLEBEAN"); 77 } 78 79 public String doTest () throws RemoteException 80 { 81 StringBuffer sb = new StringBuffer (); 82 SimpleBMP b; 83 try 84 { 85 SimpleBMPHome home = (SimpleBMPHome) new InitialContext ().lookup ("java:comp/env/bean"); 86 b = home.findByPrimaryKey(new Integer (1)); 87 } 88 catch (Exception _ne) 89 { 90 throw new EJBException ("couldnt find entity: "+_ne.getMessage ()); 91 } 92 sb.append ("found: "+b.getName ()+"\n"); 93 sb.append ("set name to \"Name for rollback\"\n"); 94 b.setName ("Name for rollback"); 95 sb.append ("current name is: "+b.getName ()+"\n"); 96 try 97 { 98 sb.append ("now rolling back...\n"); 99 100 ctx.setRollbackOnly(); 101 } 102 catch (Exception _e) 103 { 104 sb.append ("Error on rolling back: "+_e.getMessage ()+"\n"); 105 } 106 sb.append ("done."); 107 108 return sb.toString (); 109 } 110 111 public String doTestAfterRollback () throws RemoteException 112 { 113 StringBuffer sb = new StringBuffer (); 114 SimpleBMP b; 115 try 116 { 117 SimpleBMPHome home = (SimpleBMPHome) new InitialContext ().lookup ("java:comp/env/bean"); 118 b = home.findByPrimaryKey(new Integer (1)); 119 } 120 catch (Exception _ne) 121 { 122 throw new EJBException ("couldnt find entity: "+_ne.getMessage ()); 123 } 124 sb.append ("found: "+b.getName ()+"\n"); 125 sb.append ("done."); 126 127 return sb.toString (); 128 } 129 130 private boolean tableExists (String _tableName) 131 { 132 boolean result = false; 133 Connection con = null; 134 try 135 { 136 con = ds.getConnection (); 137 DatabaseMetaData dmd = con.getMetaData (); 138 ResultSet rs = dmd.getTables (con.getCatalog (), null, _tableName, null); 139 if (rs.next ()) 140 result = true; 141 142 rs.close (); 143 } 144 catch (Exception _e) 145 { 146 throw new EJBException ("Error while looking up table: "+_e.getMessage ()); 147 } 148 finally 149 { 150 try 151 { 152 if (con != null) 153 con.close (); 154 } 155 catch (Exception _sqle) 156 { 157 } 158 } 159 return result; 160 } 161 162 163 private void createTable (String _sql) 164 { 165 Connection con = null; 166 try 167 { 168 con = ds.getConnection (); 169 Statement s = con.createStatement (); 170 s.executeUpdate (_sql); 171 s.close (); 172 } 173 catch (Exception _e) 174 { 175 throw new EJBException ("Error while creating table: "+_e.getMessage ()); 176 } 177 finally 178 { 179 try 180 { 181 if (con != null) 182 con.close (); 183 } 184 catch (Exception _sqle) 185 { 186 } 187 } 188 } 189 190 private void dropTable (String _tableName) 191 { 192 Connection con = null; 193 try 194 { 195 con = ds.getConnection (); 196 Statement s = con.createStatement (); 197 s.executeUpdate ("DROP TABLE "+_tableName); 198 s.close (); 199 } 200 catch (Exception _e) 201 { 202 throw new EJBException ("Error while dropping table: "+_e.getMessage ()); 203 } 204 finally 205 { 206 try 207 { 208 if (con != null) 209 con.close (); 210 } 211 catch (Exception _sqle) 212 { 213 } 214 } 215 } 216 217 218 public void ejbActivate () {} 219 public void ejbPassivate () {} 220 public void ejbRemove () {} 221 public void setSessionContext (SessionContext _ctx) {ctx = _ctx;} 222 } 223 | Popular Tags |