diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-05 22:54:34 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-05 22:54:34 +0100 |
commit | 2bdd7679edce5c08f5d9ee8f232bd2f96d7973e7 (patch) | |
tree | ed0549d65757b21bd81fe2780156b83ba1faf802 /sonar-batch/src/main | |
parent | 86543fe81dd9cf3ab6aeed9557dada4e125ff7bb (diff) | |
download | sonarqube-2bdd7679edce5c08f5d9ee8f232bd2f96d7973e7.tar.gz sonarqube-2bdd7679edce5c08f5d9ee8f232bd2f96d7973e7.zip |
SONAR-3895 optimize loading of project settings
Diffstat (limited to 'sonar-batch/src/main')
11 files changed, 151 insertions, 193 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java index 0414d6ee424..85901d68b74 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java @@ -70,6 +70,7 @@ public class BatchModule extends Module { } private void registerCoreComponents() { + container.addSingleton(BatchSettings.class); container.addSingleton(EmailSettings.class); container.addSingleton(I18nManager.class); container.addSingleton(RuleI18nManager.class); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java index d0674039a48..2adca6754bb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java @@ -30,7 +30,6 @@ import org.sonar.api.Plugin; import org.sonar.api.config.Settings; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; -import org.sonar.batch.config.BootstrapSettings; import org.sonar.core.plugins.PluginClassloaders; import org.sonar.core.plugins.PluginInstaller; import org.sonar.core.plugins.RemotePlugin; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java new file mode 100644 index 00000000000..f620b6d0e7b --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java @@ -0,0 +1,105 @@ +/* + * 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 com.google.common.collect.Maps; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.StringUtils; +import org.json.simple.JSONValue; +import org.slf4j.LoggerFactory; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; + +import javax.annotation.Nullable; + +import java.util.List; +import java.util.Map; + +public class BatchSettings extends Settings { + private Configuration deprecatedConfiguration; + + // Keep module settings for initialization of ProjectSettings + // module key -> <key,val> + private Map<String, Map<String, String>> moduleProperties = Maps.newHashMap(); + + public BatchSettings(BootstrapSettings bootstrapSettings, ProjectReactor reactor, ServerClient client, + Configuration deprecatedConfiguration) { + super(bootstrapSettings.getDefinitions()); + this.deprecatedConfiguration = deprecatedConfiguration; + init(bootstrapSettings, reactor, client); + } + + private void init(BootstrapSettings bootstrapSettings, ProjectReactor reactor, ServerClient client) { + LoggerFactory.getLogger(BatchSettings.class).info("Load project settings"); + + String branch = bootstrapSettings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY); + String projectKey = reactor.getRoot().getKey(); + if (StringUtils.isNotBlank(branch)) { + projectKey = String.format("%s:%s", projectKey, branch); + } + downloadSettings(client, projectKey); + + + // order is important -> bottom-up. The last one overrides all the others. + addProperties(reactor.getRoot().getProperties()); + addEnvironmentVariables(); + addSystemProperties(); + } + + private void downloadSettings(ServerClient client, String projectKey) { + String jsonText = client.request("/batch_bootstrap/properties?project=" + projectKey); + List<Map<String, String>> json = (List<Map<String, String>>) JSONValue.parse(jsonText); + for (Map<String, String> jsonProperty : json) { + String key = jsonProperty.get("k"); + String value = jsonProperty.get("v"); + String moduleKey = jsonProperty.get("p"); + if (moduleKey == null || projectKey.equals(moduleKey)) { + setProperty(key, value); + } else { + Map<String, String> map = moduleProperties.get(moduleKey); + if (map == null) { + map = Maps.newHashMap(); + moduleProperties.put(moduleKey, map); + } + map.put(key, value); + } + } + } + + public Map<String, String> getModuleProperties(String projectKey) { + return moduleProperties.get(projectKey); + } + + @Override + protected void doOnSetProperty(String key, @Nullable String value) { + deprecatedConfiguration.setProperty(key, value); + } + + @Override + protected void doOnRemoveProperty(String key) { + deprecatedConfiguration.clearProperty(key); + } + + @Override + protected void doOnClearProperties() { + deprecatedConfiguration.clear(); + } +} 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 6951993e578..31d7fcb5846 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 @@ -26,10 +26,7 @@ import org.sonar.api.utils.UriReader; import org.sonar.batch.FakeMavenPluginExecutor; import org.sonar.batch.MavenPluginExecutor; import org.sonar.batch.ServerMetadata; -import org.sonar.batch.config.BootstrapSettings; -import org.sonar.batch.config.BootstrapSettingsLoader; import org.sonar.core.config.Logback; -import org.sonar.wsclient.Sonar; /** * Level 1 components @@ -55,13 +52,10 @@ public class BootstrapModule extends Module { container.addSingleton(Logback.class); container.addSingleton(ServerClient.class); container.addSingleton(ServerMetadata.class); - container.addSingleton(WsConnector.class); - container.addSingleton(Sonar.class); container.addSingleton(TempDirectories.class); container.addSingleton(HttpDownloader.class); container.addSingleton(UriReader.class); container.addSingleton(PluginDownloader.class); - container.addSingleton(BootstrapSettingsLoader.class); for (Object component : boostrapperComponents) { if (component != null) { container.addSingleton(component); diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapSettings.java index 1d7caab9509..ccd87b92176 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapSettings.java @@ -17,13 +17,14 @@ * 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.config; +package org.sonar.batch.bootstrap; import org.apache.commons.configuration.Configuration; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; -import org.sonar.core.config.ConfigurationUtils; + +import javax.annotation.Nullable; /** * @since 2.12 @@ -36,23 +37,28 @@ public class BootstrapSettings extends Settings { super(propertyDefinitions); this.reactor = reactor; this.deprecatedConfiguration = deprecatedConfiguration; - load(); + init(); } - private BootstrapSettings load() { - clear(); - + private void init() { // order is important -> bottom-up. The last one overrides all the others. addProperties(reactor.getRoot().getProperties()); addEnvironmentVariables(); addSystemProperties(); + } - updateDeprecatedCommonsConfiguration(); + @Override + protected void doOnSetProperty(String key, @Nullable String value) { + deprecatedConfiguration.setProperty(key, value); + } - return this; + @Override + protected void doOnRemoveProperty(String key) { + deprecatedConfiguration.clearProperty(key); } - public void updateDeprecatedCommonsConfiguration() { - ConfigurationUtils.copyToCommonsConfiguration(properties, deprecatedConfiguration); + @Override + protected void doOnClearProperties() { + deprecatedConfiguration.clear(); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java index 0acaa7c9ba0..8e7a22c33ef 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java @@ -37,8 +37,6 @@ import org.sonar.batch.ProjectTree; import org.sonar.batch.ResourceFilters; import org.sonar.batch.ViolationFilters; import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.config.ProjectSettings; -import org.sonar.batch.config.UnsupportedProperties; import org.sonar.batch.events.EventBus; import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.index.ResourcePersister; diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectSettings.java index 330100f328b..9a35a3ef117 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectSettings.java @@ -17,7 +17,7 @@ * 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.config; +package org.sonar.batch.bootstrap; import com.google.common.collect.Lists; import org.apache.commons.configuration.Configuration; @@ -26,14 +26,11 @@ import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; -import org.sonar.core.config.ConfigurationUtils; -import org.sonar.wsclient.Sonar; -import org.sonar.wsclient.services.Property; -import org.sonar.wsclient.services.PropertyQuery; import javax.annotation.Nullable; import java.util.List; +import java.util.Map; /** * @since 2.12 @@ -42,41 +39,44 @@ public class ProjectSettings extends Settings { private Configuration deprecatedCommonsConf; - public ProjectSettings(BootstrapSettings bootstrapSettings, ProjectDefinition project, - Sonar wsClient, Configuration deprecatedCommonsConf) { - super(bootstrapSettings.getDefinitions()); + public ProjectSettings(BatchSettings batchSettings, ProjectDefinition project, Configuration deprecatedCommonsConf) { + super(batchSettings.getDefinitions()); LoggerFactory.getLogger(ProjectSettings.class).info("Load module settings"); this.deprecatedCommonsConf = deprecatedCommonsConf; if (project.getParent() == null) { // root project -> no need to reload settings - copy(bootstrapSettings); + copy(batchSettings); } else { - init(project, bootstrapSettings, wsClient); + init(project, batchSettings); } } - private void copy(BootstrapSettings bootstrapSettings) { - setProperties(bootstrapSettings); + private void copy(BatchSettings batchSettings) { + setProperties(batchSettings); } - private ProjectSettings init(ProjectDefinition project, BootstrapSettings bootstrapSettings, Sonar wsClient) { - addPersistedProperties(project, bootstrapSettings, wsClient); + private ProjectSettings init(ProjectDefinition project, BatchSettings batchSettings) { + addProjectProperties(project, batchSettings); addBuildProperties(project); addEnvironmentVariables(); addSystemProperties(); + //addProgrammaticProperties(); return this; } - private void addPersistedProperties(ProjectDefinition project, BootstrapSettings bootstrapSettings, Sonar wsClient) { - String branch = bootstrapSettings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY); + private void addProjectProperties(ProjectDefinition project, BatchSettings batchSettings) { + String branch = batchSettings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY); String projectKey = project.getKey(); if (StringUtils.isNotBlank(branch)) { projectKey = String.format("%s:%s", projectKey, branch); } - List<Property> wsProperties = wsClient.findAll(PropertyQuery.createForAll().setResourceKeyOrId(projectKey)); - for (Property wsProperty : wsProperties) { - setProperty(wsProperty.getKey(), wsProperty.getValue()); + addProperties(batchSettings.getProperties()); + Map<String, String> moduleProps = batchSettings.getModuleProperties(projectKey); + if (moduleProps != null) { + for (Map.Entry<String, String> entry : moduleProps.entrySet()) { + setProperty(entry.getKey(), entry.getValue()); + } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/UnsupportedProperties.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/UnsupportedProperties.java index 554bac7240e..6d6661255eb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/config/UnsupportedProperties.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/UnsupportedProperties.java @@ -17,7 +17,7 @@ * 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.config; +package org.sonar.batch.bootstrap; import org.sonar.api.BatchComponent; import org.sonar.api.config.Settings; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java deleted file mode 100644 index 9e7dc579532..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.sonar.wsclient.connectors.Connector; -import org.sonar.wsclient.services.CreateQuery; -import org.sonar.wsclient.services.DeleteQuery; -import org.sonar.wsclient.services.Query; -import org.sonar.wsclient.services.UpdateQuery; - -/** - * @since 3.4 - */ -public class WsConnector extends Connector { - - private ServerClient server; - - public WsConnector(ServerClient server) { - this.server = server; - } - - /** - * @return JSON response or null if 404 NOT FOUND error - * @throws org.sonar.wsclient.connectors.ConnectionException - * if connection error or HTTP status not in (200, 404) - */ - @Override - public String execute(Query<?> query) { - return server.request(query.getUrl()); - } - - /** - * @return JSON response or null if 404 NOT FOUND error - * @since 2.2 - */ - @Override - public String execute(CreateQuery<?> query) { - throw new UnsupportedOperationException(); - } - - /** - * @return JSON response or null if 404 NOT FOUND error - * @since 2.2 - */ - @Override - public String execute(DeleteQuery query) { - throw new UnsupportedOperationException(); - } - - /** - * @return JSON response or null if 404 NOT FOUND error - * @since 2.6 - */ - @Override - public String execute(UpdateQuery<?> query) { - throw new UnsupportedOperationException(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettingsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettingsLoader.java deleted file mode 100644 index 02563c7a240..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettingsLoader.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.config; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.wsclient.Sonar; -import org.sonar.wsclient.services.Property; -import org.sonar.wsclient.services.PropertyQuery; - -import java.util.List; - -/** - * Load global settings and project settings. Note that the definition of modules is - * incomplete before the execution of ProjectBuilder extensions, so module settings - * are not loaded yet. - * @since 3.4 - */ -public final class BootstrapSettingsLoader { - - private BootstrapSettings settings; - private ProjectReactor reactor; - private Sonar wsClient; - - public BootstrapSettingsLoader(BootstrapSettings settings, ProjectReactor reactor, Sonar wsClient) { - this.settings = settings; - this.reactor = reactor; - this.wsClient = wsClient; - } - - public void start() { - LoggerFactory.getLogger(BootstrapSettingsLoader.class).info("Load project settings"); - String branch = settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY); - String projectKey = reactor.getRoot().getKey(); - if (StringUtils.isNotBlank(branch)) { - projectKey = String.format("%s:%s", projectKey, branch); - } - List<Property> wsProperties = wsClient.findAll(PropertyQuery.createForAll().setResourceKeyOrId(projectKey)); - for (Property wsProperty : wsProperties) { - setIfNotDefined(wsProperty); - } - settings.updateDeprecatedCommonsConfiguration(); - } - - private void setIfNotDefined(Property wsProperty) { - if (!settings.hasKey(wsProperty.getKey())) { - settings.setProperty(wsProperty.getKey(), wsProperty.getValue()); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java index 4149940feb8..9e5115671a3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java @@ -43,7 +43,6 @@ import java.io.IOException; public class DryRunDatabase implements BatchComponent { private static final Logger LOG = LoggerFactory.getLogger(DryRunDatabase.class); - private 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:"; @@ -57,8 +56,8 @@ public class DryRunDatabase implements BatchComponent { private final ProjectReactor reactor; public DryRunDatabase(DryRun dryRun, Settings settings, ServerClient server, TempDirectories tempDirectories, ProjectReactor reactor, - // project reactor must be completely built - ProjectReactorReady reactorReady) { + // project reactor must be completely built + ProjectReactorReady reactorReady) { this.dryRun = dryRun; this.settings = settings; this.server = server; @@ -81,7 +80,7 @@ public class DryRunDatabase implements BatchComponent { private void downloadDatabase(String projectKey, File toFile) { try { - server.download(API_SYNCHRO + "?resource=" + projectKey, toFile); + server.download("/batch_bootstrap/db?project=" + projectKey, toFile); } catch (SonarException e) { Throwable rootCause = Throwables.getRootCause(e); if (rootCause instanceof FileNotFoundException) { @@ -95,11 +94,11 @@ public class DryRunDatabase implements BatchComponent { private void replaceSettings(String databasePath) { settings - .setProperty("sonar.jdbc.schema", "") - .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 + databasePath); + .setProperty("sonar.jdbc.schema", "") + .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 + databasePath); } } |