Coverage Report - org.kuali.db.jdbc.ConnectionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectionHandler
0%
0/96
0%
0/28
2
 
 1  
 package org.kuali.db.jdbc;
 2  
 
 3  
 import java.sql.Connection;
 4  
 import java.sql.Driver;
 5  
 import java.sql.SQLException;
 6  
 import java.util.Properties;
 7  
 
 8  
 import org.apache.commons.lang.StringUtils;
 9  
 import org.apache.commons.logging.Log;
 10  
 import org.apache.commons.logging.LogFactory;
 11  
 
 12  0
 public class ConnectionHandler {
 13  0
     private static final Log log = LogFactory.getLog(ConnectionHandler.class);
 14  
 
 15  
     public static final String DRIVER_INFO_PROPERTIES_USER = "user";
 16  
     public static final String DRIVER_INFO_PROPERTIES_PASSWORD = "password";
 17  
 
 18  
     String url;
 19  0
     boolean autocommit = true;
 20  0
     boolean skipOnConnectionError = false;
 21  0
     boolean connectionError = false;
 22  0
     boolean enableAnonymousPassword = false;;
 23  0
     boolean enableAnonymousUsername = false;;
 24  
     Credentials credentials;
 25  
     Properties driverProperties;
 26  
     String driver;
 27  0
     boolean showPassword = false;
 28  
 
 29  
     protected void showConnectionInfo(Properties properties) {
 30  0
         log.info("------------------------------------------------------------------------");
 31  0
         log.info("JDBC Connection Information");
 32  0
         log.info("------------------------------------------------------------------------");
 33  0
         log.info("URL: " + getUrl());
 34  0
         String username = properties.getProperty(DRIVER_INFO_PROPERTIES_USER);
 35  0
         String password = properties.getProperty(DRIVER_INFO_PROPERTIES_PASSWORD);
 36  0
         if (StringUtils.isEmpty(username)) {
 37  0
             log.info("Username: <no username was supplied>");
 38  
         } else {
 39  0
             log.info("Username: " + username);
 40  
         }
 41  0
         if (isShowPassword()) {
 42  0
             log.info("Password: " + password);
 43  
         } else {
 44  0
             if (StringUtils.isEmpty(password)) {
 45  0
                 log.info("Password: <no password was supplied>");
 46  
             } else {
 47  0
                 log.info("Password: " + StringUtils.repeat("*", password.length()));
 48  
             }
 49  
         }
 50  0
         log.info("Driver: " + getDriver());
 51  0
         log.info("------------------------------------------------------------------------");
 52  0
     }
 53  
 
 54  
     protected Driver getDriverInstance() throws SQLException {
 55  
         try {
 56  0
             Class<?> dc = Class.forName(getDriver());
 57  0
             return (Driver) dc.newInstance();
 58  0
         } catch (ClassNotFoundException e) {
 59  0
             throw new SQLException("Driver class not found: " + getDriver(), e);
 60  0
         } catch (Exception e) {
 61  0
             throw new SQLException("Failure loading driver: " + getDriver(), e);
 62  
         }
 63  
     }
 64  
 
 65  
     protected Properties getInfo() throws SQLException {
 66  0
         Properties info = new Properties();
 67  0
         if (!enableAnonymousUsername) {
 68  0
             info.put(DRIVER_INFO_PROPERTIES_USER, credentials.getUsername());
 69  
         }
 70  0
         if (!enableAnonymousPassword) {
 71  0
             info.put(DRIVER_INFO_PROPERTIES_PASSWORD, credentials.getPassword());
 72  
         }
 73  0
         if (driverProperties != null) {
 74  0
             info.putAll(getDriverProperties());
 75  
         }
 76  0
         return info;
 77  
     }
 78  
 
 79  
     protected void validateConfiguration() throws SQLException {
 80  0
         String username = credentials.getUsername();
 81  0
         String password = credentials.getPassword();
 82  0
         if (!enableAnonymousUsername && StringUtils.isBlank(username)) {
 83  0
             throw new SQLException("\n\nNo username was supplied and enableAnonymousUsername is false");
 84  
         }
 85  0
         if (!enableAnonymousPassword && StringUtils.isBlank(password)) {
 86  0
             throw new SQLException("\n\nNo password was supplied and enableAnonymousPassword is false\n\n.");
 87  
         }
 88  
         // Convert null to the empty string if needed
 89  0
         if (StringUtils.isBlank(username)) {
 90  0
             credentials.setUsername("");
 91  
         }
 92  0
         if (StringUtils.isBlank(password)) {
 93  0
             credentials.setPassword("");
 94  
         }
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Get a connection
 99  
      */
 100  
     public Connection getConnection() throws SQLException {
 101  0
         validateConfiguration();
 102  0
         Properties info = getInfo();
 103  0
         Connection conn = null;
 104  
         try {
 105  0
             Driver driverInstance = getDriverInstance();
 106  0
             showConnectionInfo(info);
 107  0
             conn = driverInstance.connect(getUrl(), info);
 108  
 
 109  0
             if (conn == null) {
 110  
                 // Driver doesn't understand the URL
 111  0
                 throw new SQLException("No suitable Driver for " + getUrl());
 112  
             }
 113  
 
 114  0
             conn.setAutoCommit(autocommit);
 115  0
         } catch (SQLException e) {
 116  0
             if (skipOnConnectionError) {
 117  
                 // Error getting the connection but they have asked us not to
 118  
                 // throw an exception
 119  
                 // Set our flag and return
 120  0
                 connectionError = true;
 121  0
                 return null;
 122  
             } else {
 123  
                 // Otherwise, throw an exception
 124  0
                 throw new SQLException("Connection error: " + e.getMessage().toString(), e);
 125  
             }
 126  0
         }
 127  0
         return conn;
 128  
     }
 129  
 
 130  
     public String getUrl() {
 131  0
         return url;
 132  
     }
 133  
 
 134  
     public void setUrl(String url) {
 135  0
         this.url = url;
 136  0
     }
 137  
 
 138  
     public boolean isAutocommit() {
 139  0
         return autocommit;
 140  
     }
 141  
 
 142  
     public void setAutocommit(boolean autocommit) {
 143  0
         this.autocommit = autocommit;
 144  0
     }
 145  
 
 146  
     public boolean isSkipOnConnectionError() {
 147  0
         return skipOnConnectionError;
 148  
     }
 149  
 
 150  
     public void setSkipOnConnectionError(boolean skipOnConnectionError) {
 151  0
         this.skipOnConnectionError = skipOnConnectionError;
 152  0
     }
 153  
 
 154  
     public boolean isConnectionError() {
 155  0
         return connectionError;
 156  
     }
 157  
 
 158  
     public void setConnectionError(boolean connectionError) {
 159  0
         this.connectionError = connectionError;
 160  0
     }
 161  
 
 162  
     public boolean isEnableAnonymousPassword() {
 163  0
         return enableAnonymousPassword;
 164  
     }
 165  
 
 166  
     public void setEnableAnonymousPassword(boolean enableAnonymousPassword) {
 167  0
         this.enableAnonymousPassword = enableAnonymousPassword;
 168  0
     }
 169  
 
 170  
     public Credentials getCredentials() {
 171  0
         return credentials;
 172  
     }
 173  
 
 174  
     public void setCredentials(Credentials credentials) {
 175  0
         this.credentials = credentials;
 176  0
     }
 177  
 
 178  
     public Properties getDriverProperties() {
 179  0
         return driverProperties;
 180  
     }
 181  
 
 182  
     public void setDriverProperties(Properties driverProperties) {
 183  0
         this.driverProperties = driverProperties;
 184  0
     }
 185  
 
 186  
     public String getDriver() {
 187  0
         return driver;
 188  
     }
 189  
 
 190  
     public void setDriver(String driver) {
 191  0
         this.driver = driver;
 192  0
     }
 193  
 
 194  
     public boolean isShowPassword() {
 195  0
         return showPassword;
 196  
     }
 197  
 
 198  
     public void setShowPassword(boolean showPassword) {
 199  0
         this.showPassword = showPassword;
 200  0
     }
 201  
 
 202  
     public boolean isEnableAnonymousUsername() {
 203  0
         return enableAnonymousUsername;
 204  
     }
 205  
 
 206  
     public void setEnableAnonymousUsername(boolean enableAnonymousUsername) {
 207  0
         this.enableAnonymousUsername = enableAnonymousUsername;
 208  0
     }
 209  
 
 210  
 }