KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > components > backend > hsqldb > HypersonicProcess


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.components.backend.hsqldb;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.sql.Connection JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import org.objectweb.cjdbc.scenario.tools.ScenarioConstants;
37 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface;
38
39 /**
40  * This class defines a HyperSonicProcess
41  *
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
43  * @version 1.0
44  */

45 public class HypersonicProcess implements ComponentInterface
46 {
47   String JavaDoc templateDir = "/database";
48   String JavaDoc jarFile = "/hsqldb.jar";
49   String JavaDoc scriptFile = "database-raidb1.template";
50   String JavaDoc port;
51   String JavaDoc database;
52   File JavaDoc dir;
53   Process JavaDoc process;
54
55   /**
56    * Creates a new <code>HyperSonicProcess.java</code> object Start a new
57    * independant process
58    *
59    * @param port to start hypersonic on
60    * @param database to load by default
61    * @throws IOException if fails to start
62    */

63   public HypersonicProcess(String JavaDoc port, String JavaDoc database) throws IOException JavaDoc
64   {
65     this.port = port;
66     this.database = database;
67     start();
68   }
69
70   /**
71    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#start()
72    */

73   public void start() throws IOException JavaDoc
74   {
75     File JavaDoc file = new File JavaDoc(ScenarioConstants.PROCESS_DIRECTORY);
76     dir = new File JavaDoc(file + File.separator + port);
77     dir.mkdirs();
78     dir.deleteOnExit();
79     File JavaDoc jar = new File JavaDoc(getClass().getResource(jarFile).getFile());
80     Runtime JavaDoc run = Runtime.getRuntime();
81     String JavaDoc command = "java -Xmx128m -classpath " + jar.getAbsolutePath()
82         + " org.hsqldb.Server -port " + port + " -database " + database;
83     process = run.exec(command, null, dir);
84   }
85
86   /**
87    * Returns the database value.
88    *
89    * @return Returns the database.
90    */

91   public String JavaDoc getDatabase()
92   {
93     return database;
94   }
95
96   /**
97    * Returns the port value.
98    *
99    * @return Returns the port.
100    */

101   public String JavaDoc getPort()
102   {
103     return port;
104   }
105
106   /**
107    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#loadDatabase()
108    */

109   public void loadDatabase() throws Exception JavaDoc
110   {
111     loadDatabase(scriptFile);
112   }
113
114   /**
115    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#loadDatabase(java.lang.String)
116    */

117   public void loadDatabase(String JavaDoc templateName) throws Exception JavaDoc
118   {
119     Class.forName("org.hsqldb.jdbcDriver");
120     Properties JavaDoc props = new Properties JavaDoc();
121     props.put("user", "sa");
122     props.put("password", "");
123     Connection JavaDoc con = DriverManager.getConnection(
124         "jdbc:hsqldb:hsql://localhost:" + port, props);
125     Statement JavaDoc statement = con.createStatement();
126     File JavaDoc file = new File JavaDoc(getClass().getResource(
127         templateDir + "/" + templateName).getFile());
128     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
129     String JavaDoc line = "";
130     while ((line = reader.readLine()) != null)
131     {
132       try
133       {
134         statement.executeQuery(line);
135       }
136       catch (Exception JavaDoc e)
137       {
138         /* Execute all the queries */
139         //System.out.println(e.getMessage());
140
//break;
141
}
142     }
143   }
144
145   /**
146    * Returns the process associated to this database.
147    *
148    * @return Returns the process.
149    */

150   public Object JavaDoc getProcess()
151   {
152     return process;
153   }
154
155   /**
156    * Remove files generated by this process
157    */

158   public void release()
159   {
160     process.destroy();
161
162     try
163     {
164       Connection JavaDoc 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 JavaDoc s = c.createStatement();
172           s.executeQuery("SHUTDOWN SQL");
173           c.close();
174         }
175         catch (Exception JavaDoc e)
176         {
177           // ignore and then try again
178
}
179       }
180
181     }
182     catch (Exception JavaDoc e)
183     {
184       // cannot get connections anymore
185
}
186
187     if (dir == null || !dir.exists())
188       return;
189     File JavaDoc[] 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 JavaDoc e1)
202     {
203       e1.printStackTrace();
204     }
205
206     System.out.println("Hypersonic on port:" + port + " released.(Exit value:"
207         + process.exitValue() + ")");
208   }
209
210   /**
211    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#loadDatabase(java.lang.String,
212    * java.lang.String)
213    */

214   public void loadDatabase(String JavaDoc xml, String JavaDoc targetDB) throws Exception JavaDoc
215   {
216     this.loadDatabase(xml);
217   }
218 }
Popular Tags