1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util.extlib;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.URL;
20 import java.net.URLDecoder;
21 import java.sql.Connection;
22 import java.sql.DatabaseMetaData;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25
26 import javax.sql.DataSource;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /***
32 * @author someda
33 */
34 public class HSQLDBUtil {
35
36 private static Log log_ = LogFactory.getLog(HSQLDBUtil.class);
37
38 private static final String SYSTEM_ENC = System.getProperty("file.encoding");
39
40 private static final String HSQLDB_SHUTDOWN_SQL = "SHUTDOWN COMPACT";
41 private static final String HSQLDB_JDBCURL_INPROCESS_PREFIX = "jdbc:hsqldb:";
42 private static final String HSQLDB_JDBCURL_INPROCESS_SUFFIX = "tuigwaa";
43
44 private static final String HSQLDB_JDBC_DATABASE_PRODUCTNAME="HSQL Database Engine";
45
46 public static synchronized void shutdown(DataSource datasource) throws SQLException{
47
48 if(datasource == null){
49 log_.info("datasource is null, cannot shutdown or shutdown might be already completed.");
50 return;
51 }
52
53 try{
54 Connection con = datasource.getConnection();
55 DatabaseMetaData metadata = con.getMetaData();
56 String name = metadata.getDatabaseProductName();
57 if(name.equals(HSQLDB_JDBC_DATABASE_PRODUCTNAME)){
58 Statement s = con.createStatement();
59 s.execute(HSQLDB_SHUTDOWN_SQL);
60 log_.info("Shutting down HSQLDB in-process instance completed.");
61 }
62
63 }catch(SQLException se){
64 log_.error("shutdown HSQLDB might be failed.");
65 throw se;
66 }
67 }
68
69 public static String getJDBCURL() throws UnsupportedEncodingException{
70 URL url = Thread.currentThread().getContextClassLoader().getResource("hsqldb");
71 String file = URLDecoder.decode(url.toString(),SYSTEM_ENC);
72 String jdbcUrl = HSQLDB_JDBCURL_INPROCESS_PREFIX + file;
73 if(!file.endsWith("/")){
74 jdbcUrl += "/";
75 }
76 jdbcUrl += HSQLDB_JDBCURL_INPROCESS_SUFFIX;
77
78 log_.info("JDBC URL: " + jdbcUrl);
79 return jdbcUrl;
80 }
81
82 }