1 package com.sslexplorer.jdbc; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.sql.Connection ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.util.Calendar ; 9 import java.util.Properties ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import com.sslexplorer.boot.Util; 15 import com.sslexplorer.vfs.webdav.DAVUtilities; 16 17 36 public abstract class JDBCDatabaseEngine { 37 38 Log log = LogFactory.getLog(JDBCDatabaseEngine.class); 39 Connection conn; 40 Properties SQL; 41 String vendor; 42 String db; 43 String driver; 44 String alias; 45 String username; 46 String password; 47 48 private JDBCConnectionImpl.JDBCPool connectionPool; 51 52 public JDBCDatabaseEngine(String vendor, String driver) { 53 this.vendor = vendor; 54 this.driver = driver; 55 } 56 57 public abstract String getURL(); 58 59 public String getDatabase() { 60 return db; 61 } 62 63 public String getAlias() { 64 return alias; 65 } 66 67 public void init(String alias, String db, String username, String password, ClassLoader classLoader) throws SQLException , ClassNotFoundException { 68 init(alias, db, username, password, null, classLoader); 69 } 70 71 85 public void init(String alias, String db, String username, String password, String defaultResourcePath, ClassLoader classLoader) throws SQLException , 86 ClassNotFoundException { 87 try { 88 89 this.db = db; 90 this.alias = alias; 91 this.username = username; 92 this.password = password; 93 94 104 105 String defaultResources; 106 if (defaultResourcePath == null) 107 defaultResources = "/prepared/" + alias + ".properties"; 108 else 109 defaultResources = defaultResourcePath + "/" + alias + ".properties"; 110 111 114 String dbResources; 115 116 if (defaultResourcePath == null) 117 dbResources = "/prepared/" + vendor.toLowerCase() + "/" + alias + ".properties"; 118 else 119 dbResources = defaultResourcePath + "/" + vendor.toLowerCase() + "/" + alias + ".properties"; 120 121 124 SQL = new Properties (); 125 InputStream in = classLoader == null ? getClass().getResourceAsStream(defaultResources) 126 : classLoader.getResourceAsStream(DAVUtilities.stripLeadingSlash(defaultResources)); 127 if(in != null) { 128 try { 129 SQL.load(in); 130 } 131 finally { 132 in.close(); 133 } 134 } 135 136 139 in = classLoader == null ? getClass().getResourceAsStream(dbResources) 140 : classLoader.getResourceAsStream(DAVUtilities.stripLeadingSlash(dbResources)); 141 if (in != null) { 142 try { 143 SQL.load(in); 144 } 145 finally { 146 in.close(); 147 } 148 } 149 connectionPool = JDBCConnectionImpl.JDBCPool.getInstance(); 150 String url = getURL(); 151 if (log.isInfoEnabled()) 152 log.info("Aliasing database " + alias + " to " + db + " using driver " + driver + " and URL " + url); 153 connectionPool.createImpl(alias + db, driver, getURL(), username, password); 154 } catch (IOException ex) { 155 if (log.isInfoEnabled()) 156 log.info("Failed to load database resources for " + db, ex); 157 throw new SQLException ("Failed to load database resources for " + db); 158 } 159 } 160 161 public JDBCPreparedStatement getStatement(String key) throws SQLException , ClassNotFoundException { 162 return getStatement(null, key); 163 } 164 165 public JDBCPreparedStatement getStatement(JDBCPreparedStatement ps, String key) throws SQLException , ClassNotFoundException { 166 167 if (SQL.containsKey(key)) { 168 String sql = SQL.getProperty(key); 169 if (log.isDebugEnabled()) 170 log.debug("Aquiring statement for " + key + " = '" + sql + "'"); 171 return aquirePreparedStatement(key, sql, ps); 172 } 173 174 throw new SQLException ("Unable to locate database resource " + key + " in " + getAlias()); 175 } 176 177 public JDBCConnectionImpl aquireConnection() throws SQLException , ClassNotFoundException { 178 return connectionPool.acquireImpl(alias + db); 179 } 180 181 public void releaseConnection(JDBCConnectionImpl con) throws SQLException { 182 connectionPool.releaseImpl(con); 183 } 184 185 JDBCPreparedStatement aquirePreparedStatement(String key, String sql, JDBCPreparedStatement ps) throws SQLException , ClassNotFoundException { 186 return new JDBCPreparedStatement(key, sql, this, ps == null ? aquireConnection() : ps.getConnection(), ps != null); 187 } 188 189 public void releasePreparedStatement(JDBCPreparedStatement ps) throws SQLException { 190 ps.releasePreparedStatement(); 191 } 192 193 public ResultSet executeQuery(String sqlString) throws SQLException , ClassNotFoundException { 195 JDBCConnectionImpl impl = connectionPool.acquireImpl(alias + db); 196 try { 197 ResultSet rs = impl.executeQuery(sqlString); 198 return rs; 199 } finally { 200 connectionPool.releaseImpl(impl); 201 } 202 } 203 204 public void execute(String sqlString) throws SQLException , ClassNotFoundException { 205 JDBCConnectionImpl impl = connectionPool.acquireImpl(alias + db); 206 try { 207 impl.execute(sqlString); 208 } finally { 209 connectionPool.releaseImpl(impl); 210 } 211 } 212 213 public boolean isDatabaseExists() { 214 return true; 215 } 216 217 public long getLastInsertIdLong(JDBCPreparedStatement ps, String key) throws Exception { 218 String sql = SQL.getProperty(key); 219 if (log.isDebugEnabled()) 220 log.debug("Aquiring statement for " + key + " = '" + sql + "'"); 221 ps.reprepare(key, sql); 222 ResultSet rs = ps.executeQuery(); 223 try { 224 if (!rs.next()) { 225 throw new Exception ("Failed to select last inserted ID from table"); 226 } 227 return rs.getLong(1); 228 } 229 finally { 230 rs.close(); 231 } 232 } 233 234 public int getLastInsertId(JDBCPreparedStatement ps, String key) throws Exception { 235 String sql = SQL.getProperty(key); 236 if (log.isDebugEnabled()) 237 log.debug("Aquiring statement for " + key + " = '" + sql + "'"); 238 ps.reprepare(key, sql); 239 ResultSet rs = ps.executeQuery(); 240 try { 241 if (!rs.next()) { 242 throw new Exception ("Failed to select last inserted ID from table"); 243 } 244 return rs.getInt(1); 245 } 246 finally { 247 rs.close(); 248 } 249 } 250 251 public abstract String formatTimestamp(Calendar c); 252 253 public void stop() { 254 if(connectionPool != null) { 255 connectionPool.closeAll(); 256 } 257 } 258 } | Popular Tags |