AbstractDriverBasedDataSource.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /**
  14. * Return the JDBC URL to use for connecting through the Driver.
  15. */
  16. public String getUrl() {
  17. return this.url;
  18. }
  19. /**
  20. * Set the JDBC username to use for connecting through the Driver.
  21. *
  22. * @see java.sql.Driver#connect(String, java.util.Properties)
  23. */
  24. public void setUsername(String username) {
  25. this.username = username;
  26. }
  27. /**
  28. * Return the JDBC username to use for connecting through the Driver.
  29. */
  30. public String getUsername() {
  31. return this.username;
  32. }
  33. /**
  34. * Set the JDBC password to use for connecting through the Driver.
  35. *
  36. * @see java.sql.Driver#connect(String, java.util.Properties)
  37. */
  38. public void setPassword(String password) {
  39. this.password = password;
  40. }
  41. /**
  42. * Return the JDBC password to use for connecting through the Driver.
  43. */
  44. public String getPassword() {
  45. return this.password;
  46. }
  47. /**
  48. * Specify arbitrary connection properties as key/value pairs,
  49. * to be passed to the Driver.
  50. * <p>Can also contain "user" and "password" properties. However,
  51. * any "username" and "password" bean properties specified on this
  52. * DataSource will override the corresponding connection properties.
  53. *
  54. * @see java.sql.Driver#connect(String, java.util.Properties)
  55. */
  56. public void setConnectionProperties(Properties connectionProperties) {
  57. this.connectionProperties = connectionProperties;
  58. }
  59. /**
  60. * Return the connection properties to be passed to the Driver, if any.
  61. */
  62. public Properties getConnectionProperties() {
  63. return this.connectionProperties;
  64. }
  65. /**
  66. * This implementation delegates to {@code getConnectionFromDriver},
  67. * using the default username and password of this DataSource.
  68. *
  69. * @see #getConnectionFromDriver(String, String)
  70. * @see #setUsername
  71. * @see #setPassword
  72. */
  73. public Connection getConnection() throws SQLException {
  74. return getConnectionFromDriver(getUsername(), getPassword());
  75. }
  76. /**
  77. * This implementation delegates to {@code getConnectionFromDriver},
  78. * using the given username and password.
  79. *
  80. * @see #getConnectionFromDriver(String, String)
  81. */
  82. public Connection getConnection(String username, String password) throws SQLException {
  83. return getConnectionFromDriver(username, password);
  84. }
  85. /**
  86. * Build properties for the Driver, including the given username and password (if any),
  87. * and obtain a corresponding Connection.
  88. *
  89. * @param username the name of the user
  90. * @param password the password to use
  91. * @return the obtained Connection
  92. * @throws SQLException in case of failure
  93. * @see java.sql.Driver#connect(String, java.util.Properties)
  94. */
  95. protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
  96. Properties mergedProps = new Properties();
  97. Properties connProps = getConnectionProperties();
  98. if (connProps != null) {
  99. mergedProps.putAll(connProps);
  100. }
  101. if (username != null) {
  102. mergedProps.setProperty("user", username);
  103. }
  104. if (password != null) {
  105. mergedProps.setProperty("password", password);
  106. }
  107. return getConnectionFromDriver(mergedProps);
  108. }
  109. /**
  110. * Obtain a Connection using the given properties.
  111. * <p>Template method to be implemented by subclasses.
  112. *
  113. * @param props the merged connection properties
  114. * @return the obtained Connection
  115. * @throws SQLException in case of failure
  116. */
  117. protected abstract Connection getConnectionFromDriver(Properties props) throws SQLException;
  118. }