View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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  //		jdbcUrl += HSQLDB_JDBCURL_INPROCESS_SUFFIX + ";shutdown=true";		
78  		log_.info("JDBC URL: " + jdbcUrl);								
79  		return jdbcUrl;
80  	}	
81  	
82  }