KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > database > EmbeddedConnectionProvider


1 /**
2  * $RCSfile: EmbeddedConnectionProvider.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/04/11 21:02:05 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.database;
13
14 import org.jivesoftware.util.JiveGlobals;
15 import org.jivesoftware.util.Log;
16
17 import java.io.BufferedReader JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.sql.Connection JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Statement JavaDoc;
24
25 /**
26  * A connection provider for the embedded hsqlDB database. The database file is stored at
27  * <tt>home/database</tt>. The log file for this connection provider is stored at
28  * <tt>[home]/logs/EmbeddedConnectionProvider.log</tt>, so you should ensure
29  * that the <tt>[home]/logs</tt> directory exists.
30  *
31  * @author Matt Tucker
32  */

33 public class EmbeddedConnectionProvider implements ConnectionProvider {
34
35     private ConnectionPool connectionPool = null;
36     private Object JavaDoc initLock = new Object JavaDoc();
37
38     public boolean isPooled() {
39         return true;
40     }
41
42     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
43         if (connectionPool == null) {
44             // Block until the init has been done
45
synchronized (initLock) {
46                 // If still null, something has gone wrong
47
if (connectionPool == null) {
48                     Log.error("Error: EmbeddedConnectionProvider.getConnection() was" +
49                             "called before the internal pool has been initialized.");
50                     return null;
51                 }
52             }
53         }
54         return connectionPool.getConnection();
55     }
56
57     public void start() {
58         // Acquire lock so that no connections can be returned while creating the pool.
59
synchronized (initLock) {
60             try {
61                 String JavaDoc driver = "org.hsqldb.jdbcDriver";
62                 File JavaDoc databaseDir = new File JavaDoc(JiveGlobals.getHomeDirectory(), File.separator +
63                         "embedded-db");
64                 boolean initData = false;
65                 // If the database doesn't exist, create it.
66
if (!databaseDir.exists()) {
67                     databaseDir.mkdirs();
68                     initData = true;
69                 }
70
71                 String JavaDoc serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() +
72                         File.separator + "messenger";
73                 String JavaDoc username = "sa";
74                 String JavaDoc password = "";
75                 int minConnections = 3;
76                 int maxConnections = 10;
77                 double connectionTimeout = 0.5;
78
79                 connectionPool = new ConnectionPool(driver, serverURL, username, password,
80                         minConnections, maxConnections, connectionTimeout, false);
81                 // Create initial tables if they don't already exist.
82
if (initData) {
83                     initializeDatabase();
84                 }
85             }
86             catch (IOException JavaDoc ioe) {
87                 Log.error("Error starting connection pool.", ioe);
88             }
89         }
90     }
91
92     public void restart() {
93         // Kill off pool.
94
destroy();
95         // Start a new pool.
96
start();
97     }
98
99     public void destroy() {
100         if (connectionPool == null) {
101             return;
102         }
103         // Shutdown the database.
104
Connection JavaDoc con = null;
105         try {
106             con = getConnection();
107             Statement JavaDoc stmt = con.createStatement();
108             stmt.execute("SHUTDOWN");
109             stmt.close();
110         }
111         catch (SQLException JavaDoc sqle) {
112             Log.error(sqle);
113         }
114         finally {
115             try { if (con != null) { con.close(); } }
116             catch (Exception JavaDoc e) { Log.error(e); }
117         }
118         // Close the connection pool.
119
try {
120             connectionPool.destroy();
121         }
122         catch (Exception JavaDoc e) {
123             Log.error(e);
124         }
125         // Release reference to connectionPool
126
connectionPool = null;
127     }
128
129     public void finalize() {
130         destroy();
131     }
132
133     private void initializeDatabase() {
134         BufferedReader JavaDoc in = null;
135         Connection JavaDoc con = null;
136         try {
137             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
138                     getClass().getResourceAsStream("/database/messenger_hsqldb.sql")));
139             con = connectionPool.getConnection();
140             boolean done = false;
141             while (!done) {
142                 StringBuilder JavaDoc command = new StringBuilder JavaDoc();
143                 while (true) {
144                     String JavaDoc line = in.readLine();
145                     if (line == null) {
146                         done = true;
147                         break;
148                     }
149                     // Ignore comments and blank lines.
150
if (DbConnectionManager.isSQLCommandPart(line)) {
151                         command.append(line);
152                     }
153                     if (line.endsWith(";")) {
154                         break;
155                     }
156                 }
157                 // Send command to database.
158
Statement JavaDoc stmt = con.createStatement();
159                 stmt.execute(command.toString());
160                 stmt.close();
161             }
162         }
163         catch (Exception JavaDoc e) {
164             Log.error(e);
165             e.printStackTrace();
166         }
167         finally {
168             if (in != null) {
169                 try { in.close(); }
170                 catch (Exception JavaDoc e) { }
171             }
172             try { if (con != null) { con.close(); } }
173             catch (Exception JavaDoc e) { Log.error(e); }
174         }
175     }
176 }
Popular Tags