You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseChecker.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.db;
  21. import com.google.common.base.Throwables;
  22. import org.apache.commons.dbutils.DbUtils;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.picocontainer.Startable;
  25. import org.sonar.api.server.ServerSide;
  26. import org.sonar.api.utils.MessageException;
  27. import org.sonar.api.utils.log.Loggers;
  28. import org.sonar.db.Database;
  29. import org.sonar.db.dialect.H2;
  30. import org.sonar.db.dialect.Oracle;
  31. import java.sql.Connection;
  32. import java.sql.SQLException;
  33. @ServerSide
  34. public class DatabaseChecker implements Startable {
  35. public static final int ORACLE_MIN_MAJOR_VERSION = 11;
  36. private final Database db;
  37. public DatabaseChecker(Database db) {
  38. this.db = db;
  39. }
  40. @Override
  41. public void start() {
  42. try {
  43. if (H2.ID.equals(db.getDialect().getId())) {
  44. Loggers.get(DatabaseChecker.class).warn("H2 database should be used for evaluation purpose only");
  45. } else if (Oracle.ID.equals(db.getDialect().getId())) {
  46. checkOracleVersion();
  47. }
  48. } catch (Exception e) {
  49. Throwables.propagate(e);
  50. }
  51. }
  52. @Override
  53. public void stop() {
  54. // nothing to do
  55. }
  56. private void checkOracleVersion() throws SQLException {
  57. Connection connection = db.getDataSource().getConnection();
  58. try {
  59. // check version of db
  60. // See http://jira.sonarsource.com/browse/SONAR-6434
  61. int majorVersion = connection.getMetaData().getDatabaseMajorVersion();
  62. if (majorVersion < ORACLE_MIN_MAJOR_VERSION) {
  63. throw MessageException.of(String.format(
  64. "Unsupported Oracle version: %s. Minimal required version is %d.", connection.getMetaData().getDatabaseProductVersion(), ORACLE_MIN_MAJOR_VERSION));
  65. }
  66. // check version of driver
  67. String driverVersion = connection.getMetaData().getDriverVersion();
  68. String[] parts = StringUtils.split(driverVersion, ".");
  69. int intVersion = Integer.parseInt(parts[0]) * 100 + Integer.parseInt(parts[1]);
  70. if (intVersion < 1102) {
  71. throw MessageException.of(String.format(
  72. "Unsupported Oracle JDBC driver version: %s. Minimal required version is 11.2.", driverVersion));
  73. }
  74. } finally {
  75. DbUtils.closeQuietly(connection);
  76. }
  77. }
  78. }