KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > hsqldb > Driver


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.hsqldb;
17
18 import scriptella.jdbc.GenericDriver;
19 import scriptella.jdbc.JdbcConnection;
20 import scriptella.spi.ConnectionParameters;
21
22 import java.sql.SQLException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 /**
30  * Scriptella Adapter for HSLQDB database.
31  * <p>The primary feature of this driver is {@link HsqlConnection#SHUTDOWN_ON_EXIT}.
32  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
33  *
34  * @author Fyodor Kupolov
35  * @version 1.0
36  */

37 public class Driver extends GenericDriver {
38     private static final Logger JavaDoc LOG = Logger.getLogger(Driver.class.getName());
39     public static final String JavaDoc HSQLDB_DRIVER_NAME = "org.hsqldb.jdbcDriver";
40
41     private static Map JavaDoc<String JavaDoc, HsqlConnection> lastConnections = null; //Send SHUTDOWN on JVM exit to fix
42
private static boolean hookAdded = false;
43
44
45     /**
46      * Shutdown hook closing all the databases being used.
47      */

48     static final Thread JavaDoc HOOK = new Thread JavaDoc("Scriptella HSLQDB Shutdown Fix") {
49         public void run() {
50             if (lastConnections != null) {
51                 for (Map.Entry JavaDoc<String JavaDoc, HsqlConnection> entry : lastConnections.entrySet()) {
52                     try {
53                         entry.getValue().shutdown();
54                     } catch (Exception JavaDoc e) {
55                         LOG.log(Level.WARNING, "Problem occured while trying to shutdown an in-process HSQLDB database " + entry.getKey(), e);
56                     }
57                 }
58                 lastConnections = null;
59             }
60         }
61     };
62
63
64     public Driver() {
65         loadDrivers(HSQLDB_DRIVER_NAME);
66     }
67
68     @Override JavaDoc
69     protected JdbcConnection connect(ConnectionParameters parameters, Properties JavaDoc props) throws SQLException JavaDoc {
70         return new HsqlConnection(getConnection(parameters.getUrl(), props), parameters);
71     }
72
73
74     /**
75      * Sets last connection and returns previous value of lastConnection for connection url.
76      * <p>Driver stores map of connections using URLs as keys.
77      *
78      * @param connection last connection
79      * @return previous value of lastConnection field.Null if no connections have been registered.
80      * @see #HOOK
81      */

82     static synchronized HsqlConnection setLastConnection(HsqlConnection connection) {
83         if (lastConnections == null) {
84             lastConnections = new HashMap JavaDoc<String JavaDoc, HsqlConnection>();
85         }
86         final HsqlConnection old = lastConnections.put(getConnectionURL(connection), connection);
87         if (!hookAdded) {
88             Runtime.getRuntime().addShutdownHook(HOOK);
89             hookAdded = true;
90         }
91         return old;
92     }
93
94     private static String JavaDoc getConnectionURL(HsqlConnection connection) {
95         try {
96             return connection.getNativeConnection().getMetaData().getURL();
97         } catch (Exception JavaDoc e) {
98             LOG.log(Level.WARNING, "Unable to read connection meta data", e);
99             return "";
100         }
101     }
102
103
104 }
105
Popular Tags