/container/src/main/java/com/atlassian/plugin/remotable/container/service/sal/JdbcPluginSettings.java

https://bitbucket.org/rodogu/remotable-plugins · Java · 154 lines · 141 code · 10 blank · 3 comment · 7 complexity · b198ef3e35041135bf24873269962896 MD5 · raw file

  1. package com.atlassian.plugin.remotable.container.service.sal;
  2. import com.atlassian.sal.core.pluginsettings.AbstractStringPluginSettings;
  3. import javax.sql.DataSource;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import static com.atlassian.plugin.remotable.container.service.sal.JdbcPluginSettingsFactory
  9. .TABLE_NAME;
  10. /**
  11. */
  12. public class JdbcPluginSettings extends AbstractStringPluginSettings
  13. {
  14. private final DataSource dataSource;
  15. private final String namespace;
  16. public JdbcPluginSettings(DataSource dataSource, String namespace)
  17. {
  18. this.dataSource = dataSource;
  19. this.namespace = namespace;
  20. }
  21. @Override
  22. protected void putActual(final String key, final String val)
  23. {
  24. runQuery(new JdbcCallback<Void>()
  25. {
  26. @Override
  27. public Void execute(Connection conn) throws SQLException
  28. {
  29. if (getActual(key) != null)
  30. {
  31. PreparedStatement updateStatement = conn.prepareStatement(
  32. "UPDATE " + TABLE_NAME + " SET value = ? " +
  33. "WHERE key = ? AND namespace = ?");
  34. updateStatement.setString(1, val);
  35. updateStatement.setString(2, key);
  36. updateStatement.setString(3, namespace);
  37. updateStatement.executeUpdate();
  38. }
  39. else
  40. {
  41. PreparedStatement insertStatement = conn.prepareStatement(
  42. "INSERT INTO " + TABLE_NAME + " (namespace, key, value) " +
  43. "VALUES (?, ?, ?)");
  44. insertStatement.setString(1, namespace);
  45. insertStatement.setString(2, key);
  46. insertStatement.setString(3, val);
  47. insertStatement.executeUpdate();
  48. }
  49. return null;
  50. }
  51. });
  52. }
  53. private <T> T runQuery(JdbcCallback<T> jdbcCallback)
  54. {
  55. Connection conn = null;
  56. try
  57. {
  58. conn = dataSource.getConnection();
  59. conn.setAutoCommit(false);
  60. T result = jdbcCallback.execute(conn);
  61. conn.commit();
  62. return result;
  63. }
  64. catch (SQLException e)
  65. {
  66. if (conn != null)
  67. {
  68. try
  69. {
  70. conn.rollback();
  71. }
  72. catch (SQLException e1)
  73. {
  74. throw new RuntimeException(e1);
  75. }
  76. }
  77. throw new RuntimeException(e);
  78. }
  79. finally
  80. {
  81. try
  82. {
  83. if (conn != null)
  84. {
  85. conn.close();
  86. }
  87. }
  88. catch (SQLException e)
  89. {
  90. e.printStackTrace();
  91. // cannot close connection
  92. }
  93. }
  94. }
  95. @Override
  96. protected String getActual(final String key)
  97. {
  98. return runQuery(new JdbcCallback<String>()
  99. {
  100. @Override
  101. public String execute(Connection conn) throws SQLException
  102. {
  103. PreparedStatement getStatement = conn.prepareStatement(
  104. "SELECT value FROM " + TABLE_NAME + " WHERE " +
  105. "namespace = ? AND " +
  106. "key = ?");
  107. getStatement.setString(1, namespace);
  108. getStatement.setString(2, key);
  109. ResultSet rs = getStatement.executeQuery();
  110. if (rs.next())
  111. {
  112. return rs.getString("value");
  113. }
  114. else
  115. {
  116. return null;
  117. }
  118. }
  119. });
  120. }
  121. @Override
  122. protected void removeActual(final String key)
  123. {
  124. runQuery(new JdbcCallback<Void>()
  125. {
  126. @Override
  127. public Void execute(Connection conn) throws SQLException
  128. {
  129. PreparedStatement removeStatement = conn.prepareStatement(
  130. "DELETE FROM " + TABLE_NAME + " WHERE " +
  131. "namespace = ? AND " +
  132. "key = ?");
  133. removeStatement.setString(1, namespace);
  134. removeStatement.setString(2, key);
  135. removeStatement.executeUpdate();
  136. return null;
  137. }
  138. });
  139. }
  140. private interface JdbcCallback<T>
  141. {
  142. public T execute(Connection conn) throws SQLException;
  143. }
  144. }