AbstractDriverBasedDataSource.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package org.elasticsearch.jdbc;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.util.Properties;
  5. public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {
  6. private String url;
  7. private String username;
  8. private String password;
  9. private Properties connectionProperties;
  10. public void setUrl(String url) {
  11. this.url = url.trim();
  12. }
  13. public String getUrl() {
  14. return this.url;
  15. }
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19. public String getUsername() {
  20. return this.username;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password;
  24. }
  25. public String getPassword() {
  26. return this.password;
  27. }
  28. public void setConnectionProperties(Properties connectionProperties) {
  29. this.connectionProperties = connectionProperties;
  30. }
  31. public Properties getConnectionProperties() {
  32. return this.connectionProperties;
  33. }
  34. public Connection getConnection() throws SQLException {
  35. return getConnectionFromDriver(getUsername(), getPassword());
  36. }
  37. public Connection getConnection(String username, String password) throws SQLException {
  38. return getConnectionFromDriver(username, password);
  39. }
  40. protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
  41. Properties mergedProps = new Properties();
  42. Properties connProps = getConnectionProperties();
  43. if (connProps != null) {
  44. mergedProps.putAll(connProps);
  45. }
  46. if (username != null) {
  47. mergedProps.setProperty("user", username);
  48. }
  49. if (password != null) {
  50. mergedProps.setProperty("password", password);
  51. }
  52. return getConnectionFromDriver(mergedProps);
  53. }
  54. protected abstract Connection getConnectionFromDriver(Properties props) throws SQLException;
  55. }