001: package org.dbbrowser.drivermanager;
002:
003: import infrastructure.logging.Log;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.net.URLClassLoader;
007: import java.sql.Connection;
008: import java.sql.Driver;
009: import java.sql.SQLException;
010: import java.util.Hashtable;
011: import java.util.Properties;
012:
013: import org.dbbrowser.security.AsymmetricEncryptionEngine;
014: import org.dbbrowser.security.EncryptionEngineException;
015: import org.dbbrowser.ui.UIControllerForQueries;
016:
017: /**
018: * Used to get the connection to the database
019: */
020: public class DBBrowserDriverManager {
021: private static DBBrowserDriverManager driverManager = new DBBrowserDriverManager();
022: private Hashtable pooledConnections = new Hashtable();
023:
024: /**
025: * Private constructer as it is a singleton
026: *
027: */
028: private DBBrowserDriverManager() {
029:
030: }
031:
032: /**
033: * Returns the singleton object
034: * @return
035: */
036: public static DBBrowserDriverManager getInstance() {
037: return driverManager;
038: }
039:
040: /**
041: * Get the connection. the 'name' attribute in the connectionInfo uniquely identifies a connection.
042: * This class pools the connection and returns the same connection for subsequent calls with the 'same'
043: * connectionInfo object. Do not close the connection as it is cached. <br /> <br />
044: * <ul>
045: * <li>For MySQL, the url is of the form jdbc:mysql://localhost:3306/mysql</li>
046: * <li>For Oracle, the url is of the form jdbc:oracle:thin:@localhost:1521:live</li>
047: * </ul>
048: * @param connectionInfo
049: * @param masterPassword
050: * @return
051: * @throws DriverManagerException
052: */
053: public Connection getConnection(ConnectionInfo connectionInfo,
054: String masterPassword) throws DriverManagerException {
055: Connection connection = null;
056: Object connectionFromPool = this .pooledConnections
057: .get(connectionInfo);
058: if (connectionFromPool == null) {
059: try {
060: URL jdbcDriverURL = connectionInfo
061: .getJdbcDriverJarFileLocation().toURL();
062: URL[] urls = new URL[1];
063: urls[0] = jdbcDriverURL;
064: URLClassLoader urlClassLoader = new URLClassLoader(urls);
065: Class driverClass = Class.forName(connectionInfo
066: .getDriverClassName(), true, urlClassLoader);
067: Driver driver = (Driver) driverClass.newInstance();
068:
069: //Decrypt the password
070: String passwordInClearText = AsymmetricEncryptionEngine
071: .decrypt(connectionInfo.getPassword(),
072: masterPassword);
073:
074: //Get the connection
075: Properties props = new Properties();
076: props.put("user", connectionInfo.getUsername());
077: props.put("password", passwordInClearText);
078:
079: //If it is a oracle connection, then check if encryption and checksum is required
080: if (UIControllerForQueries.ORACLE_DBMS
081: .equals(connectionInfo.getDBMSType())) {
082: if (connectionInfo.getEncryptionAlgorithm() != null) {
083: props
084: .put(
085: "oracle.net.encryption_types_client",
086: connectionInfo
087: .getEncryptionAlgorithm());
088: }
089:
090: if (connectionInfo.getCheckSumAlgorithm() != null) {
091: props
092: .put(
093: "oracle.net.crypto_checksum_types_client",
094: connectionInfo
095: .getCheckSumAlgorithm());
096: }
097: }
098:
099: //Connect
100: connection = driver.connect(connectionInfo
101: .getDatabaseURL(), props);
102:
103: this .pooledConnections.put(connectionInfo, connection);
104: Log.getInstance().infoMessage(
105: "Connection setup for "
106: + connectionInfo.toString(),
107: DBBrowserDriverManager.class.getName());
108: } catch (MalformedURLException exc) {
109: throw new DriverManagerException(exc);
110: } catch (ClassNotFoundException exc) {
111: throw new DriverManagerException(exc);
112: } catch (InstantiationException exc) {
113: throw new DriverManagerException(exc);
114: } catch (IllegalAccessException exc) {
115: throw new DriverManagerException(exc);
116: } catch (SQLException exc) {
117: throw new DriverManagerException(exc);
118: } catch (EncryptionEngineException exc) {
119: throw new DriverManagerException(exc);
120: }
121: } else {
122: connection = (Connection) connectionFromPool;
123: }
124: return connection;
125: }
126: }
|