diff options
author | David Gageot <david@gageot.net> | 2012-10-18 17:47:19 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-10-23 11:48:21 +0200 |
commit | a505a98b4d1ad33c80b939ae6fed12043e635ded (patch) | |
tree | 939b5de8fce6a3f5e3ad150c3ca814c429d462b6 /sonar-batch/src/main | |
parent | 9f509a9600f0dafb62d8c9787d94a910762de385 (diff) | |
download | sonarqube-a505a98b4d1ad33c80b939ae6fed12043e635ded.tar.gz sonarqube-a505a98b4d1ad33c80b939ae6fed12043e635ded.zip |
SONAR-3895 Local mode
Diffstat (limited to 'sonar-batch/src/main')
3 files changed, 182 insertions, 1 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java index 528186df9f4..f5bfb3eadd4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java @@ -30,6 +30,7 @@ import org.sonar.batch.RemoteServerMetadata; import org.sonar.batch.ServerMetadata; import org.sonar.batch.config.BatchDatabaseSettingsLoader; import org.sonar.batch.config.BatchSettings; +import org.sonar.batch.local.LocalDatabase; import org.sonar.core.config.Logback; import org.sonar.core.i18n.I18nManager; import org.sonar.core.i18n.RuleI18nManager; @@ -77,11 +78,15 @@ public class BootstrapModule extends Module { Thread.currentThread().setContextClassLoader(bootstrapClassLoader); addCoreSingleton(RemoteServerMetadata.class); + + // local mode + addCoreSingleton(LocalMode.class); + addCoreSingleton(LocalDatabase.class); + // mybatis addCoreSingleton(BatchDatabase.class); addCoreSingleton(MyBatis.class); addCoreSingleton(DatabaseVersion.class); - addCoreSingleton(DatabaseBatchCompatibility.class); for (Class daoClass : DaoUtils.getDaoClasses()) { addCoreSingleton(daoClass); } @@ -91,6 +96,7 @@ public class BootstrapModule extends Module { addCoreSingleton(ThreadLocalDatabaseSessionFactory.class); addAdapter(new DatabaseSessionProvider()); + addCoreSingleton(DatabaseBatchCompatibility.class); for (Object component : boostrapperComponents) { addCoreSingleton(component); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/LocalMode.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/LocalMode.java new file mode 100644 index 00000000000..618a1e764c7 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/LocalMode.java @@ -0,0 +1,50 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.bootstrap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.BatchComponent; +import org.sonar.api.Property; +import org.sonar.api.config.Settings; + +/** + * @since 3.4 + */ +@Property(key = "sonar.local", defaultValue = "false", name = "Local Mode") +public class LocalMode implements BatchComponent { + private static final Logger LOG = LoggerFactory.getLogger(LocalMode.class); + + private final boolean enabled; + + public LocalMode(Settings settings) { + enabled = settings.getBoolean("sonar.local"); + } + + public boolean isEnabled() { + return enabled; + } + + public void start() { + if (enabled) { + LOG.info("Local Mode"); + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/local/LocalDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/local/LocalDatabase.java new file mode 100644 index 00000000000..a8e1877e6f6 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/local/LocalDatabase.java @@ -0,0 +1,125 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.local; + +import com.google.common.io.Closeables; +import com.google.gson.Gson; +import org.apache.commons.dbcp.BasicDataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.BatchComponent; +import org.sonar.api.config.Settings; +import org.sonar.api.database.DatabaseProperties; +import org.sonar.api.platform.Server; +import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.bootstrap.LocalMode; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.sql.SQLException; + +/** + * @since 3.4 + */ +public class LocalDatabase implements BatchComponent { + private static final Logger LOG = LoggerFactory.getLogger(LocalDatabase.class); + + public static final String API_SYNCHRO = "/api/synchro"; + private static final String DIALECT = "h2"; + private static final String DRIVER = "org.h2.Driver"; + private static final String URL = "jdbc:h2:"; + private static final String USER = "sonar"; + private static final String PASSWORD = "sonar"; + + private final LocalMode localMode; + private final Settings settings; + private final Server server; + private final HttpDownloader httpDownloader; + private BasicDataSource dataSource; + + public LocalDatabase(LocalMode localMode, Settings settings, Server server, HttpDownloader httpDownloader) { + this.localMode = localMode; + this.settings = settings; + this.server = server; + this.httpDownloader = httpDownloader; + } + + public void start() { + if (!localMode.isEnabled()) { + return; + } + + LOG.info("Download database"); + Path path = downloadDatabase(); + + LOG.info("Starting local database"); + replaceSettings(path); + configureDataSource(path); + } + + private Path downloadDatabase() { + InputStream stream = null; + try { + stream = httpDownloader.openStream(URI.create(server.getURL() + API_SYNCHRO)); + return new Gson().fromJson(new InputStreamReader(stream), Path.class); + } finally { + Closeables.closeQuietly(stream); + } + } + + static class Path { + String path; + + String getName() { + return path.replaceAll(".h2.db", ""); + } + } + + public void stop() { + try { + dataSource.close(); + } catch (SQLException e) { + // Ignore error + } + } + + private void replaceSettings(Path path) { + settings + .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT) + .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER) + .setProperty(DatabaseProperties.PROP_USER, USER) + .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD) + .setProperty(DatabaseProperties.PROP_URL, URL + path.getName()); + } + + private void configureDataSource(Path path) { + try { + dataSource = new BasicDataSource(); + dataSource.setDriverClassName(DRIVER); + dataSource.setUsername(USER); + dataSource.setPassword(PASSWORD); + dataSource.setUrl(URL + path.getName()); + } catch (Exception e) { + throw new SonarException("Fail to start local database", e); + } + } +} |