1 package org.kuali.db;
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 protected void showConnectionInfo(Properties properties) {
30 log.info("------------------------------------------------------------------------");
31 log.info("JDBC Connection Information");
32 log.info("------------------------------------------------------------------------");
33 log.info("URL: " + getUrl());
34 String username = properties.getProperty(DRIVER_INFO_PROPERTIES_USER);
35 String password = properties.getProperty(DRIVER_INFO_PROPERTIES_PASSWORD);
36 if (StringUtils.isEmpty(username)) {
37 log.info("Username: <no username was supplied>");
38 } else {
39 log.info("Username: " + username);
40 }
41 if (isShowPassword()) {
42 log.info("Password: " + password);
43 } else {
44 if (StringUtils.isEmpty(password)) {
45 log.info("Password: <no password was supplied>");
46 } else {
47 log.info("Password: " + StringUtils.repeat("*", password.length()));
48 }
49 }
50 log.info("Driver: " + getDriver());
51 log.info("------------------------------------------------------------------------");
52 }
53
54 protected Driver getDriverInstance() throws SQLException {
55 try {
56 Class<?> dc = Class.forName(getDriver());
57 return (Driver) dc.newInstance();
58 } catch (ClassNotFoundException e) {
59 throw new SQLException("Driver class not found: " + getDriver(), e);
60 } catch (Exception e) {
61 throw new SQLException("Failure loading driver: " + getDriver(), e);
62 }
63 }
64
65 protected Properties getInfo() throws SQLException {
66 Properties info = new Properties();
67 if (!enableAnonymousUsername) {
68 info.put(DRIVER_INFO_PROPERTIES_USER, credentials.getUsername());
69 }
70 if (!enableAnonymousPassword) {
71 info.put(DRIVER_INFO_PROPERTIES_PASSWORD, credentials.getPassword());
72 }
73 if (driverProperties != null) {
74 info.putAll(getDriverProperties());
75 }
76 return info;
77 }
78
79 protected void validateConfiguration() throws SQLException {
80 String username = credentials.getUsername();
81 String password = credentials.getPassword();
82 if (!enableAnonymousUsername && StringUtils.isBlank(username)) {
83 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 if (!enableAnonymousPassword && StringUtils.isBlank(password)) {
86 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 if (StringUtils.isBlank(username)) {
90 credentials.setUsername("");
91 }
92 if (StringUtils.isBlank(password)) {
93 credentials.setPassword("");
94 }
95 }
96
97 public Connection getConnection() throws SQLException {
98 validateConfiguration();
99 Properties info = getInfo();
100 Connection conn = null;
101 try {
102 Driver driverInstance = getDriverInstance();
103 showConnectionInfo(info);
104 conn = driverInstance.connect(getUrl(), info);
105
106 if (conn == null) {
107
108 throw new SQLException("No suitable Driver for " + getUrl());
109 }
110
111 conn.setAutoCommit(autocommit);
112 } catch (SQLException e) {
113 if (skipOnConnectionError) {
114
115
116 connectionError = true;
117 return null;
118 } else {
119
120 throw new SQLException("Connection error: " + e.getMessage().toString(), e);
121 }
122 }
123 return conn;
124 }
125
126 public String getUrl() {
127 return url;
128 }
129
130 public void setUrl(String url) {
131 this.url = url;
132 }
133
134 public boolean isAutocommit() {
135 return autocommit;
136 }
137
138 public void setAutocommit(boolean autocommit) {
139 this.autocommit = autocommit;
140 }
141
142 public boolean isSkipOnConnectionError() {
143 return skipOnConnectionError;
144 }
145
146 public void setSkipOnConnectionError(boolean skipOnConnectionError) {
147 this.skipOnConnectionError = skipOnConnectionError;
148 }
149
150 public boolean isConnectionError() {
151 return connectionError;
152 }
153
154 public void setConnectionError(boolean connectionError) {
155 this.connectionError = connectionError;
156 }
157
158 public boolean isEnableAnonymousPassword() {
159 return enableAnonymousPassword;
160 }
161
162 public void setEnableAnonymousPassword(boolean enableAnonymousPassword) {
163 this.enableAnonymousPassword = enableAnonymousPassword;
164 }
165
166 public Credentials getCredentials() {
167 return credentials;
168 }
169
170 public void setCredentials(Credentials credentials) {
171 this.credentials = credentials;
172 }
173
174 public Properties getDriverProperties() {
175 return driverProperties;
176 }
177
178 public void setDriverProperties(Properties driverProperties) {
179 this.driverProperties = driverProperties;
180 }
181
182 public String getDriver() {
183 return driver;
184 }
185
186 public void setDriver(String driver) {
187 this.driver = driver;
188 }
189
190 public boolean isShowPassword() {
191 return showPassword;
192 }
193
194 public void setShowPassword(boolean showPassword) {
195 this.showPassword = showPassword;
196 }
197
198 public boolean isEnableAnonymousUsername() {
199 return enableAnonymousUsername;
200 }
201
202 public void setEnableAnonymousUsername(boolean enableAnonymousUsername) {
203 this.enableAnonymousUsername = enableAnonymousUsername;
204 }
205
206 }