View Javadoc

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