001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.batch.bootstrap;
021
022 import org.sonar.api.BatchComponent;
023 import org.sonar.api.config.Settings;
024 import org.sonar.api.database.DatabaseProperties;
025 import org.sonar.api.platform.Server;
026 import org.sonar.api.utils.SonarException;
027 import org.sonar.batch.RemoteServerMetadata;
028 import org.sonar.core.persistence.BadDatabaseVersion;
029 import org.sonar.core.persistence.DatabaseVersion;
030
031 import java.io.IOException;
032
033 /**
034 * Detects if database is not up-to-date with the version required by the batch.
035 */
036 public class DatabaseBatchCompatibility implements BatchComponent {
037
038 private DatabaseVersion version;
039 private Server server;
040 private Settings settings;
041 private RemoteServerMetadata remoteServer;
042
043 public DatabaseBatchCompatibility(DatabaseVersion version, Server server, RemoteServerMetadata remoteServer, Settings settings) {
044 this.version = version;
045 this.server = server;
046 this.settings = settings;
047 this.remoteServer = remoteServer;
048 }
049
050 public void start() {
051 checkCorrectServerId();
052 checkDatabaseStatus();
053 }
054
055 private void checkCorrectServerId() {
056 String remoteServerId;
057 try {
058 remoteServerId = remoteServer.getServerId();
059 } catch (IOException e) {
060 throw new SonarException("Impossible to get the ID of the remote server: " + server.getURL(), e);
061 }
062
063 if (!version.getSonarCoreId().equals(remoteServerId)) {
064 StringBuilder message = new StringBuilder("The current batch process and the configured remote server do not share the same DB configuration.\n");
065 message.append("\t- Batch side: ");
066 message.append(settings.getString(DatabaseProperties.PROP_URL));
067 message.append(" (");
068 String userName = settings.getString(DatabaseProperties.PROP_USER);
069 message.append(userName == null ? "sonar" : userName);
070 message.append(" / *****)\n\t- Server side: check the configuration at ");
071 message.append(server.getURL());
072 message.append("/system\n");
073 throw new BadDatabaseVersion(message.toString());
074 }
075 }
076
077 private void checkDatabaseStatus() {
078 DatabaseVersion.Status status = version.getStatus();
079 if (status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) {
080 throw new BadDatabaseVersion("Database relates to a more recent version of Sonar. Please check your settings (JDBC settings, version of Maven plugin)");
081 }
082 if (status == DatabaseVersion.Status.REQUIRES_UPGRADE) {
083 throw new BadDatabaseVersion("Database must be upgraded. Please browse " + server.getURL() + "/setup");
084 }
085 if (status != DatabaseVersion.Status.UP_TO_DATE) {
086 // Support other future values
087 throw new BadDatabaseVersion("Unknown database status: " + status);
088 }
089 }
090
091 }