1 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 ; 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.sql.Connection ; 22 import java.sql.SQLException ; 23 import java.sql.Statement ; 24 25 33 public class EmbeddedConnectionProvider implements ConnectionProvider { 34 35 private ConnectionPool connectionPool = null; 36 private Object initLock = new Object (); 37 38 public boolean isPooled() { 39 return true; 40 } 41 42 public Connection getConnection() throws SQLException { 43 if (connectionPool == null) { 44 synchronized (initLock) { 46 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 synchronized (initLock) { 60 try { 61 String driver = "org.hsqldb.jdbcDriver"; 62 File databaseDir = new File (JiveGlobals.getHomeDirectory(), File.separator + 63 "embedded-db"); 64 boolean initData = false; 65 if (!databaseDir.exists()) { 67 databaseDir.mkdirs(); 68 initData = true; 69 } 70 71 String serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + 72 File.separator + "messenger"; 73 String username = "sa"; 74 String 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 if (initData) { 83 initializeDatabase(); 84 } 85 } 86 catch (IOException ioe) { 87 Log.error("Error starting connection pool.", ioe); 88 } 89 } 90 } 91 92 public void restart() { 93 destroy(); 95 start(); 97 } 98 99 public void destroy() { 100 if (connectionPool == null) { 101 return; 102 } 103 Connection con = null; 105 try { 106 con = getConnection(); 107 Statement stmt = con.createStatement(); 108 stmt.execute("SHUTDOWN"); 109 stmt.close(); 110 } 111 catch (SQLException sqle) { 112 Log.error(sqle); 113 } 114 finally { 115 try { if (con != null) { con.close(); } } 116 catch (Exception e) { Log.error(e); } 117 } 118 try { 120 connectionPool.destroy(); 121 } 122 catch (Exception e) { 123 Log.error(e); 124 } 125 connectionPool = null; 127 } 128 129 public void finalize() { 130 destroy(); 131 } 132 133 private void initializeDatabase() { 134 BufferedReader in = null; 135 Connection con = null; 136 try { 137 in = new BufferedReader (new InputStreamReader ( 138 getClass().getResourceAsStream("/database/messenger_hsqldb.sql"))); 139 con = connectionPool.getConnection(); 140 boolean done = false; 141 while (!done) { 142 StringBuilder command = new StringBuilder (); 143 while (true) { 144 String line = in.readLine(); 145 if (line == null) { 146 done = true; 147 break; 148 } 149 if (DbConnectionManager.isSQLCommandPart(line)) { 151 command.append(line); 152 } 153 if (line.endsWith(";")) { 154 break; 155 } 156 } 157 Statement stmt = con.createStatement(); 159 stmt.execute(command.toString()); 160 stmt.close(); 161 } 162 } 163 catch (Exception e) { 164 Log.error(e); 165 e.printStackTrace(); 166 } 167 finally { 168 if (in != null) { 169 try { in.close(); } 170 catch (Exception e) { } 171 } 172 try { if (con != null) { con.close(); } } 173 catch (Exception e) { Log.error(e); } 174 } 175 } 176 } | Popular Tags |