| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 123 (123) |
Complexity: 42 |
Complexity Density: 0.57 |
|
| 12 |
|
public class ConnectionHandler { |
| 13 |
|
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 |
|
boolean autocommit = true; |
| 20 |
|
boolean skipOnConnectionError = false; |
| 21 |
|
boolean connectionError = false; |
| 22 |
|
boolean enableAnonymousPassword = false;; |
| 23 |
|
boolean enableAnonymousUsername = false;; |
| 24 |
|
Credentials credentials; |
| 25 |
|
Properties driverProperties; |
| 26 |
|
String driver; |
| 27 |
|
boolean showPassword = false; |
| 28 |
|
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
| 29 |
0
|
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 |
|
} |
| 53 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 54 |
0
|
protected Driver getDriverInstance() throws SQLException {... |
| 55 |
0
|
try { |
| 56 |
0
|
Class<?> dc = Class.forName(getDriver()); |
| 57 |
0
|
return (Driver) dc.newInstance(); |
| 58 |
|
} catch (ClassNotFoundException e) { |
| 59 |
0
|
throw new SQLException("Driver class not found: " + getDriver(), e); |
| 60 |
|
} catch (Exception e) { |
| 61 |
0
|
throw new SQLException("Failure loading driver: " + getDriver(), e); |
| 62 |
|
} |
| 63 |
|
} |
| 64 |
|
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 65 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 7 |
Complexity Density: 0.7 |
|
| 79 |
0
|
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.\nYou can supply a username in the plugin configuration or provide it as a system property.\n\nFor example:\n-Dusername=myuser\n\n."); |
| 84 |
|
} |
| 85 |
0
|
if (!enableAnonymousPassword && StringUtils.isBlank(password)) { |
| 86 |
0
|
throw new SQLException("\n\nNo password was supplied.\nYou can supply a password in the plugin configuration or provide it as a system property.\n\nFor example:\n-Dpassword=mypassword\n\n."); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
0
|
if (StringUtils.isBlank(username)) { |
| 90 |
0
|
credentials.setUsername(""); |
| 91 |
|
} |
| 92 |
0
|
if (StringUtils.isBlank(password)) { |
| 93 |
0
|
credentials.setPassword(""); |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
| 97 |
0
|
public Connection getConnection() throws SQLException {... |
| 98 |
0
|
validateConfiguration(); |
| 99 |
0
|
Properties info = getInfo(); |
| 100 |
0
|
Connection conn = null; |
| 101 |
0
|
try { |
| 102 |
0
|
Driver driverInstance = getDriverInstance(); |
| 103 |
0
|
showConnectionInfo(info); |
| 104 |
0
|
conn = driverInstance.connect(getUrl(), info); |
| 105 |
|
|
| 106 |
0
|
if (conn == null) { |
| 107 |
|
|
| 108 |
0
|
throw new SQLException("No suitable Driver for " + getUrl()); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
0
|
conn.setAutoCommit(autocommit); |
| 112 |
|
} catch (SQLException e) { |
| 113 |
0
|
if (skipOnConnectionError) { |
| 114 |
|
|
| 115 |
|
|
| 116 |
0
|
connectionError = true; |
| 117 |
0
|
return null; |
| 118 |
|
} else { |
| 119 |
|
|
| 120 |
0
|
throw new SQLException("Connection error: " + e.getMessage().toString(), e); |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
0
|
return conn; |
| 124 |
|
} |
| 125 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 126 |
0
|
public String getUrl() {... |
| 127 |
0
|
return url; |
| 128 |
|
} |
| 129 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 130 |
0
|
public void setUrl(String url) {... |
| 131 |
0
|
this.url = url; |
| 132 |
|
} |
| 133 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 134 |
0
|
public boolean isAutocommit() {... |
| 135 |
0
|
return autocommit; |
| 136 |
|
} |
| 137 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
0
|
public void setAutocommit(boolean autocommit) {... |
| 139 |
0
|
this.autocommit = autocommit; |
| 140 |
|
} |
| 141 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 142 |
0
|
public boolean isSkipOnConnectionError() {... |
| 143 |
0
|
return skipOnConnectionError; |
| 144 |
|
} |
| 145 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 146 |
0
|
public void setSkipOnConnectionError(boolean skipOnConnectionError) {... |
| 147 |
0
|
this.skipOnConnectionError = skipOnConnectionError; |
| 148 |
|
} |
| 149 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 150 |
0
|
public boolean isConnectionError() {... |
| 151 |
0
|
return connectionError; |
| 152 |
|
} |
| 153 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 154 |
0
|
public void setConnectionError(boolean connectionError) {... |
| 155 |
0
|
this.connectionError = connectionError; |
| 156 |
|
} |
| 157 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 158 |
0
|
public boolean isEnableAnonymousPassword() {... |
| 159 |
0
|
return enableAnonymousPassword; |
| 160 |
|
} |
| 161 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 162 |
0
|
public void setEnableAnonymousPassword(boolean enableAnonymousPassword) {... |
| 163 |
0
|
this.enableAnonymousPassword = enableAnonymousPassword; |
| 164 |
|
} |
| 165 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 166 |
0
|
public Credentials getCredentials() {... |
| 167 |
0
|
return credentials; |
| 168 |
|
} |
| 169 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 170 |
0
|
public void setCredentials(Credentials credentials) {... |
| 171 |
0
|
this.credentials = credentials; |
| 172 |
|
} |
| 173 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 174 |
0
|
public Properties getDriverProperties() {... |
| 175 |
0
|
return driverProperties; |
| 176 |
|
} |
| 177 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 178 |
0
|
public void setDriverProperties(Properties driverProperties) {... |
| 179 |
0
|
this.driverProperties = driverProperties; |
| 180 |
|
} |
| 181 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 182 |
0
|
public String getDriver() {... |
| 183 |
0
|
return driver; |
| 184 |
|
} |
| 185 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 186 |
0
|
public void setDriver(String driver) {... |
| 187 |
0
|
this.driver = driver; |
| 188 |
|
} |
| 189 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 190 |
0
|
public boolean isShowPassword() {... |
| 191 |
0
|
return showPassword; |
| 192 |
|
} |
| 193 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 194 |
0
|
public void setShowPassword(boolean showPassword) {... |
| 195 |
0
|
this.showPassword = showPassword; |
| 196 |
|
} |
| 197 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 198 |
0
|
public boolean isEnableAnonymousUsername() {... |
| 199 |
0
|
return enableAnonymousUsername; |
| 200 |
|
} |
| 201 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 202 |
0
|
public void setEnableAnonymousUsername(boolean enableAnonymousUsername) {... |
| 203 |
0
|
this.enableAnonymousUsername = enableAnonymousUsername; |
| 204 |
|
} |
| 205 |
|
|
| 206 |
|
} |