001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.bootstrap;
021    
022    import org.sonar.api.BatchComponent;
023    import org.sonar.api.CoreProperties;
024    import org.sonar.api.config.Settings;
025    import org.sonar.api.database.DatabaseProperties;
026    import org.sonar.api.utils.MessageException;
027    import org.sonar.core.persistence.DatabaseVersion;
028    import org.sonar.core.properties.PropertiesDao;
029    
030    /**
031     * Detects if database is not up-to-date with the version required by the batch.
032     */
033    public class DatabaseCompatibility implements BatchComponent {
034    
035      private DatabaseVersion version;
036      private Settings settings;
037      private PropertiesDao propertiesDao;
038      private ServerMetadata server;
039      private AnalysisMode analysisMode;
040    
041      public DatabaseCompatibility(DatabaseVersion version, ServerMetadata server, Settings settings, PropertiesDao propertiesDao, AnalysisMode mode) {
042        this.version = version;
043        this.server = server;
044        this.settings = settings;
045        this.propertiesDao = propertiesDao;
046        this.analysisMode = mode;
047      }
048    
049      public void start() {
050        if (!analysisMode.isPreview()) {
051          checkCorrectServerId();
052          checkDatabaseStatus();
053        }
054      }
055    
056      private void checkCorrectServerId() {
057        if (!propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID).getValue().equals(server.getServerId())) {
058          StringBuilder message = new StringBuilder("The current batch process and the configured remote server do not share the same DB configuration.\n");
059          message.append("\t- Batch side: ");
060          message.append(settings.getString(DatabaseProperties.PROP_URL));
061          message.append(" (");
062          String userName = settings.getString(DatabaseProperties.PROP_USER);
063          message.append(userName == null ? "sonar" : userName);
064          message.append(" / *****)\n\t- Server side: check the configuration at ");
065          message.append(server.getURL());
066          message.append("/system\n");
067          throw MessageException.of(message.toString());
068        }
069      }
070    
071      private void checkDatabaseStatus() {
072        DatabaseVersion.Status status = version.getStatus();
073        if (status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) {
074          throw MessageException.of("Database relates to a more recent version of SonarQube. Please check your settings (JDBC settings, version of Maven plugin)");
075        }
076        if (status == DatabaseVersion.Status.REQUIRES_UPGRADE) {
077          throw MessageException.of("Database must be upgraded. Please browse " + server.getURL() + "/setup");
078        }
079        if (status != DatabaseVersion.Status.UP_TO_DATE) {
080          // Support other future values
081          throw MessageException.of("Unknown database status: " + status);
082        }
083      }
084    
085    }