1 package org.openejb.test; 2 3 import java.rmi.RemoteException ; 4 import java.util.Properties ; 5 6 import javax.naming.InitialContext ; 7 8 import org.openejb.test.beans.Database; 9 import org.openejb.test.beans.DatabaseHome; 10 11 15 public class InstantDbTestDatabase implements TestDatabase{ 16 17 protected Database database; 18 protected InitialContext initialContext; 19 20 private static String _createAccount = "CREATE TABLE account ( ssn CHAR(11) PRIMARY KEY, first_name CHAR(20), last_name CHAR(20), balance INT)"; 21 private static String _dropAccount = "DROP TABLE account"; 23 24 private static String _createEntity = "CREATE TABLE entity ( id INT PRIMARY KEY AUTO INCREMENT, first_name CHAR(20), last_name CHAR(20) )"; 26 private static String _dropEntity = "DROP TABLE entity"; 27 28 static{ 29 System.setProperty("noBanner", "true"); 30 } 31 32 33 public void createEntityTable() throws java.sql.SQLException { 34 try{ 35 try{ 36 database.execute(_dropEntity); 37 } catch (Exception e){ 38 } 40 database.execute(_createEntity); 41 } catch (RemoteException re){ 42 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 43 throw (java.sql.SQLException )re.detail; 44 } else { 45 throw new java.sql.SQLException ("Cannot create entity table: "+re.getMessage(), _createEntity); 46 } 47 } 48 } 49 public void dropEntityTable() throws java.sql.SQLException { 50 try { 51 database.execute(_dropEntity); 52 } catch (RemoteException re){ 53 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 54 throw (java.sql.SQLException )re.detail; 55 } else { 56 throw new java.sql.SQLException ("Unable to drop entity table: "+re.getMessage(), _dropEntity); 57 } 58 } 59 } 60 61 62 public void createAccountTable() throws java.sql.SQLException { 63 try{ 64 try{ 65 database.execute(_dropAccount); 66 } catch (Exception e){ 67 } 69 database.execute(_createAccount); 70 } catch (RemoteException re){ 71 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 72 throw (java.sql.SQLException )re.detail; 73 } else { 74 throw new java.sql.SQLException ("Cannot create account table: "+re.getMessage(), _createAccount); 75 } 76 } 77 } 78 79 public void dropAccountTable() throws java.sql.SQLException { 80 try { 81 database.execute(_dropAccount); 82 } catch (RemoteException re){ 83 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 84 throw (java.sql.SQLException )re.detail; 85 } else { 86 throw new java.sql.SQLException ("Cannot drop account table: "+re.getMessage(), _dropAccount); 87 } 88 } 89 } 90 91 public void start() throws IllegalStateException { 92 try { 93 Properties properties = TestManager.getServer().getContextEnvironment(); 94 initialContext = new InitialContext (properties); 95 } catch (Exception e){ 96 throw new IllegalStateException ("Cannot create initial context: "+e.getClass().getName()+" "+e.getMessage()); 97 } 98 99 Object obj =null; 100 DatabaseHome databaseHome =null; 101 try { 102 103 obj = initialContext.lookup("client/tools/DatabaseHome"); 104 databaseHome = (DatabaseHome)javax.rmi.PortableRemoteObject.narrow( obj, DatabaseHome.class); 105 } catch (Exception e){ 106 throw new IllegalStateException ("Cannot find 'client/tools/DatabaseHome': "+e.getClass().getName()+" "+e.getMessage()); 107 } 108 try { 109 database = databaseHome.create(); 110 } catch (Exception e){ 111 throw new IllegalStateException ("Cannot start database: "+e.getClass().getName()+" "+e.getMessage()); 112 } 113 } 114 115 116 public void stop() throws IllegalStateException { 117 } 118 119 public void init(Properties props) throws IllegalStateException { 120 } 121 } 122 123 124 125 | Popular Tags |