Clover Coverage Report - JDBC Support Utilities 1.0.21-SNAPSHOT
Coverage timestamp: Wed Dec 31 1969 19:00:00 EST
../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
74   206   42   2.96
24   168   0.57   25
25     1.68  
1    
 
  ConnectionHandler       Line # 12 74 0% 42 123 0% 0.0
 
No Tests
 
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    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   
 
29  0 toggle 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   
 
54  0 toggle 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   
 
65  0 toggle 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  0 toggle 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    // 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    }
96   
 
97  0 toggle 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    // Driver doesn't understand the URL
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    // Error getting the connection but they have asked us not to throw an exception
115    // Set our flag and return
116  0 connectionError = true;
117  0 return null;
118    } else {
119    // Otherwise, throw an exception
120  0 throw new SQLException("Connection error: " + e.getMessage().toString(), e);
121    }
122    }
123  0 return conn;
124    }
125   
 
126  0 toggle public String getUrl() {
127  0 return url;
128    }
129   
 
130  0 toggle public void setUrl(String url) {
131  0 this.url = url;
132    }
133   
 
134  0 toggle public boolean isAutocommit() {
135  0 return autocommit;
136    }
137   
 
138  0 toggle public void setAutocommit(boolean autocommit) {
139  0 this.autocommit = autocommit;
140    }
141   
 
142  0 toggle public boolean isSkipOnConnectionError() {
143  0 return skipOnConnectionError;
144    }
145   
 
146  0 toggle public void setSkipOnConnectionError(boolean skipOnConnectionError) {
147  0 this.skipOnConnectionError = skipOnConnectionError;
148    }
149   
 
150  0 toggle public boolean isConnectionError() {
151  0 return connectionError;
152    }
153   
 
154  0 toggle public void setConnectionError(boolean connectionError) {
155  0 this.connectionError = connectionError;
156    }
157   
 
158  0 toggle public boolean isEnableAnonymousPassword() {
159  0 return enableAnonymousPassword;
160    }
161   
 
162  0 toggle public void setEnableAnonymousPassword(boolean enableAnonymousPassword) {
163  0 this.enableAnonymousPassword = enableAnonymousPassword;
164    }
165   
 
166  0 toggle public Credentials getCredentials() {
167  0 return credentials;
168    }
169   
 
170  0 toggle public void setCredentials(Credentials credentials) {
171  0 this.credentials = credentials;
172    }
173   
 
174  0 toggle public Properties getDriverProperties() {
175  0 return driverProperties;
176    }
177   
 
178  0 toggle public void setDriverProperties(Properties driverProperties) {
179  0 this.driverProperties = driverProperties;
180    }
181   
 
182  0 toggle public String getDriver() {
183  0 return driver;
184    }
185   
 
186  0 toggle public void setDriver(String driver) {
187  0 this.driver = driver;
188    }
189   
 
190  0 toggle public boolean isShowPassword() {
191  0 return showPassword;
192    }
193   
 
194  0 toggle public void setShowPassword(boolean showPassword) {
195  0 this.showPassword = showPassword;
196    }
197   
 
198  0 toggle public boolean isEnableAnonymousUsername() {
199  0 return enableAnonymousUsername;
200    }
201   
 
202  0 toggle public void setEnableAnonymousUsername(boolean enableAnonymousUsername) {
203  0 this.enableAnonymousUsername = enableAnonymousUsername;
204    }
205   
206    }