| 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 | 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.\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 | 0 |          } | 
  | 96 |  |   | 
  | 97 |  |          public Connection getConnection() throws SQLException { | 
  | 98 | 0 |                  validateConfiguration(); | 
  | 99 | 0 |                  Properties info = getInfo(); | 
  | 100 | 0 |                  Connection conn = null; | 
  | 101 |  |                  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 | 0 |                  } 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 | 0 |                  } | 
  | 123 | 0 |                  return conn; | 
  | 124 |  |          } | 
  | 125 |  |   | 
  | 126 |  |          public String getUrl() { | 
  | 127 | 0 |                  return url; | 
  | 128 |  |          } | 
  | 129 |  |   | 
  | 130 |  |          public void setUrl(String url) { | 
  | 131 | 0 |                  this.url = url; | 
  | 132 | 0 |          } | 
  | 133 |  |   | 
  | 134 |  |          public boolean isAutocommit() { | 
  | 135 | 0 |                  return autocommit; | 
  | 136 |  |          } | 
  | 137 |  |   | 
  | 138 |  |          public void setAutocommit(boolean autocommit) { | 
  | 139 | 0 |                  this.autocommit = autocommit; | 
  | 140 | 0 |          } | 
  | 141 |  |   | 
  | 142 |  |          public boolean isSkipOnConnectionError() { | 
  | 143 | 0 |                  return skipOnConnectionError; | 
  | 144 |  |          } | 
  | 145 |  |   | 
  | 146 |  |          public void setSkipOnConnectionError(boolean skipOnConnectionError) { | 
  | 147 | 0 |                  this.skipOnConnectionError = skipOnConnectionError; | 
  | 148 | 0 |          } | 
  | 149 |  |   | 
  | 150 |  |          public boolean isConnectionError() { | 
  | 151 | 0 |                  return connectionError; | 
  | 152 |  |          } | 
  | 153 |  |   | 
  | 154 |  |          public void setConnectionError(boolean connectionError) { | 
  | 155 | 0 |                  this.connectionError = connectionError; | 
  | 156 | 0 |          } | 
  | 157 |  |   | 
  | 158 |  |          public boolean isEnableAnonymousPassword() { | 
  | 159 | 0 |                  return enableAnonymousPassword; | 
  | 160 |  |          } | 
  | 161 |  |   | 
  | 162 |  |          public void setEnableAnonymousPassword(boolean enableAnonymousPassword) { | 
  | 163 | 0 |                  this.enableAnonymousPassword = enableAnonymousPassword; | 
  | 164 | 0 |          } | 
  | 165 |  |   | 
  | 166 |  |          public Credentials getCredentials() { | 
  | 167 | 0 |                  return credentials; | 
  | 168 |  |          } | 
  | 169 |  |   | 
  | 170 |  |          public void setCredentials(Credentials credentials) { | 
  | 171 | 0 |                  this.credentials = credentials; | 
  | 172 | 0 |          } | 
  | 173 |  |   | 
  | 174 |  |          public Properties getDriverProperties() { | 
  | 175 | 0 |                  return driverProperties; | 
  | 176 |  |          } | 
  | 177 |  |   | 
  | 178 |  |          public void setDriverProperties(Properties driverProperties) { | 
  | 179 | 0 |                  this.driverProperties = driverProperties; | 
  | 180 | 0 |          } | 
  | 181 |  |   | 
  | 182 |  |          public String getDriver() { | 
  | 183 | 0 |                  return driver; | 
  | 184 |  |          } | 
  | 185 |  |   | 
  | 186 |  |          public void setDriver(String driver) { | 
  | 187 | 0 |                  this.driver = driver; | 
  | 188 | 0 |          } | 
  | 189 |  |   | 
  | 190 |  |          public boolean isShowPassword() { | 
  | 191 | 0 |                  return showPassword; | 
  | 192 |  |          } | 
  | 193 |  |   | 
  | 194 |  |          public void setShowPassword(boolean showPassword) { | 
  | 195 | 0 |                  this.showPassword = showPassword; | 
  | 196 | 0 |          } | 
  | 197 |  |   | 
  | 198 |  |          public boolean isEnableAnonymousUsername() { | 
  | 199 | 0 |                  return enableAnonymousUsername; | 
  | 200 |  |          } | 
  | 201 |  |   | 
  | 202 |  |          public void setEnableAnonymousUsername(boolean enableAnonymousUsername) { | 
  | 203 | 0 |                  this.enableAnonymousUsername = enableAnonymousUsername; | 
  | 204 | 0 |          } | 
  | 205 |  |   | 
  | 206 |  |  } |