1 24 25 package org.objectweb.cjdbc.scenario.tools.components.backend.hsqldb; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.sql.Connection ; 32 import java.sql.DriverManager ; 33 import java.sql.Statement ; 34 import java.util.Properties ; 35 36 import org.objectweb.cjdbc.scenario.tools.ScenarioConstants; 37 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface; 38 39 45 public class HypersonicProcess implements ComponentInterface 46 { 47 String templateDir = "/database"; 48 String jarFile = "/hsqldb.jar"; 49 String scriptFile = "database-raidb1.template"; 50 String port; 51 String database; 52 File dir; 53 Process process; 54 55 63 public HypersonicProcess(String port, String database) throws IOException 64 { 65 this.port = port; 66 this.database = database; 67 start(); 68 } 69 70 73 public void start() throws IOException 74 { 75 File file = new File (ScenarioConstants.PROCESS_DIRECTORY); 76 dir = new File (file + File.separator + port); 77 dir.mkdirs(); 78 dir.deleteOnExit(); 79 File jar = new File (getClass().getResource(jarFile).getFile()); 80 Runtime run = Runtime.getRuntime(); 81 String command = "java -Xmx128m -classpath " + jar.getAbsolutePath() 82 + " org.hsqldb.Server -port " + port + " -database " + database; 83 process = run.exec(command, null, dir); 84 } 85 86 91 public String getDatabase() 92 { 93 return database; 94 } 95 96 101 public String getPort() 102 { 103 return port; 104 } 105 106 109 public void loadDatabase() throws Exception 110 { 111 loadDatabase(scriptFile); 112 } 113 114 117 public void loadDatabase(String templateName) throws Exception 118 { 119 Class.forName("org.hsqldb.jdbcDriver"); 120 Properties props = new Properties (); 121 props.put("user", "sa"); 122 props.put("password", ""); 123 Connection con = DriverManager.getConnection( 124 "jdbc:hsqldb:hsql://localhost:" + port, props); 125 Statement statement = con.createStatement(); 126 File file = new File (getClass().getResource( 127 templateDir + "/" + templateName).getFile()); 128 BufferedReader reader = new BufferedReader (new FileReader (file)); 129 String line = ""; 130 while ((line = reader.readLine()) != null) 131 { 132 try 133 { 134 statement.executeQuery(line); 135 } 136 catch (Exception e) 137 { 138 139 } 142 } 143 } 144 145 150 public Object getProcess() 151 { 152 return process; 153 } 154 155 158 public void release() 159 { 160 process.destroy(); 161 162 try 163 { 164 Connection c; 165 while ((c = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/" 166 + database + ":" + port, "sa", "")) != null) 167 { 168 try 169 { 170 System.out.println("Killing process"); 171 Statement s = c.createStatement(); 172 s.executeQuery("SHUTDOWN SQL"); 173 c.close(); 174 } 175 catch (Exception e) 176 { 177 } 179 } 180 181 } 182 catch (Exception e) 183 { 184 } 186 187 if (dir == null || !dir.exists()) 188 return; 189 File [] f = dir.listFiles(); 190 if (f != null) 191 { 192 for (int j = 0; j < f.length; j++) 193 f[j].delete(); 194 } 195 dir.delete(); 196 197 try 198 { 199 process.waitFor(); 200 } 201 catch (InterruptedException e1) 202 { 203 e1.printStackTrace(); 204 } 205 206 System.out.println("Hypersonic on port:" + port + " released.(Exit value:" 207 + process.exitValue() + ")"); 208 } 209 210 214 public void loadDatabase(String xml, String targetDB) throws Exception 215 { 216 this.loadDatabase(xml); 217 } 218 } | Popular Tags |