diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-23 15:11:58 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-23 15:15:55 +0200 |
commit | d0f9dca7b2d3dd27ab0b43ecdd9c0ea93d4ae734 (patch) | |
tree | 1a001474155c522e7a237ac5abdb7a61566d21e7 | |
parent | 10308195e71712147314b49dfc449d6c927916e7 (diff) | |
download | sonarqube-d0f9dca7b2d3dd27ab0b43ecdd9c0ea93d4ae734.tar.gz sonarqube-d0f9dca7b2d3dd27ab0b43ecdd9c0ea93d4ae734.zip |
SONAR-5477 Plug global referential loader on remote WS
11 files changed, 48 insertions, 78 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java index baeb2706ad1..96922f5b93f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java @@ -20,41 +20,36 @@ package org.sonar.batch.bootstrap; import org.apache.commons.configuration.Configuration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; -import org.sonar.batch.settings.SettingsReferential; +import org.sonar.batch.protocol.input.GlobalReferentials; import javax.annotation.Nullable; public class GlobalSettings extends Settings { - private static final Logger LOG = LoggerFactory.getLogger(GlobalSettings.class); - private Configuration deprecatedConfiguration; private final BootstrapProperties bootstrapProps; - private final SettingsReferential settingsReferential; + private final GlobalReferentials globalReferentials; private final AnalysisMode mode; public GlobalSettings(BootstrapProperties bootstrapProps, PropertyDefinitions propertyDefinitions, - SettingsReferential settingsReferential, Configuration deprecatedConfiguration, AnalysisMode mode) { + GlobalReferentials globalReferentials, Configuration deprecatedConfiguration, AnalysisMode mode) { super(propertyDefinitions); this.mode = mode; getEncryption().setPathToSecretKey(bootstrapProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); this.bootstrapProps = bootstrapProps; - this.settingsReferential = settingsReferential; + this.globalReferentials = globalReferentials; this.deprecatedConfiguration = deprecatedConfiguration; init(); } private void init() { - LOG.info("Load global settings"); - addProperties(settingsReferential.globalSettings()); + addProperties(globalReferentials.globalSettings()); addProperties(bootstrapProps.properties()); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index d43ef8e0e71..1d0cefb14b4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -92,7 +92,7 @@ public class ServerClient implements BatchComponent { } } - private InputSupplier<InputStream> doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) { + public InputSupplier<InputStream> doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) { Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /"); String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash); diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java index cde2d635f2e..c3aa2646c6c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -108,7 +108,7 @@ public class BatchMediumTester { public BatchMediumTesterBuilder addDefaultQProfile(String language, String name) { addQProfile(language, name); - settingsReferential.globalSettings().put("sonar.profile." + language, name); + globalRefProvider.globalSettings().put("sonar.profile." + language, name); return this; } @@ -253,6 +253,10 @@ public class BatchMediumTester { return ref; } + public Map<String, String> globalSettings() { + return ref.globalSettings(); + } + public FakeGlobalReferentialsLoader add(Metric metric) { ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metricId, metric.key(), @@ -292,15 +296,9 @@ public class BatchMediumTester { private static class FakeSettingsReferential implements SettingsReferential { - private Map<String, String> globalSettings = new HashMap<String, String>(); private Map<String, Map<String, String>> projectSettings = new HashMap<String, Map<String, String>>(); @Override - public Map<String, String> globalSettings() { - return globalSettings; - } - - @Override public Map<String, String> projectSettings(String projectKey) { return projectSettings.containsKey(projectKey) ? projectSettings.get(projectKey) : Collections.<String, String>emptyMap(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java index ec7e93382eb..239835275c4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java @@ -19,49 +19,33 @@ */ package org.sonar.batch.referential; -import org.sonar.api.measures.Metric; +import com.google.common.base.Charsets; +import com.google.common.io.InputSupplier; +import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.protocol.input.GlobalReferentials; -import org.sonar.jpa.session.DatabaseSessionFactory; -import java.util.Collection; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; -/** - * TODO This is currently implemented by accessing DB but should be replaced by WS call - */ public class DefaultGlobalReferentialsLoader implements GlobalReferentialsLoader { - private static final String ENABLED = "enabled"; + private static final String BATCH_GLOBAL_URL = "/batch/global"; - private final DatabaseSessionFactory sessionFactory; + private final ServerClient serverClient; - public DefaultGlobalReferentialsLoader(DatabaseSessionFactory sessionFactory) { - this.sessionFactory = sessionFactory; + public DefaultGlobalReferentialsLoader(ServerClient serverClient) { + this.serverClient = serverClient; } @Override public GlobalReferentials load() { - GlobalReferentials ref = new GlobalReferentials(); - for (Metric m : sessionFactory.getSession().getResults(Metric.class, ENABLED, true)) { - Boolean optimizedBestValue = m.isOptimizedBestValue(); - Boolean qualitative = m.getQualitative(); - Boolean userManaged = m.getUserManaged(); - ref.metrics().add( - new org.sonar.batch.protocol.input.Metric(m.getId(), m.getKey(), - m.getType().name(), - m.getDescription(), - m.getDirection(), - m.getName(), - qualitative != null ? m.getQualitative() : false, - userManaged != null ? m.getUserManaged() : false, - m.getWorstValue(), - m.getBestValue(), - optimizedBestValue != null ? optimizedBestValue : false)); + InputSupplier<InputStream> jsonStream = serverClient.doRequest(BATCH_GLOBAL_URL, null); + try { + return GlobalReferentials.fromJson(new InputStreamReader(jsonStream.getInput(), Charsets.UTF_8)); + } catch (IOException e) { + throw new IllegalStateException("Unable to load global referentials", e); } - - return ref; } - private Collection<Metric> doFindAll() { - return sessionFactory.getSession().getResults(Metric.class, ENABLED, true); - } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/GlobalReferentialsProvider.java b/sonar-batch/src/main/java/org/sonar/batch/referential/GlobalReferentialsProvider.java index 87da7f92d65..8348549e201 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/GlobalReferentialsProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/GlobalReferentialsProvider.java @@ -20,11 +20,21 @@ package org.sonar.batch.referential; import org.picocontainer.injectors.ProviderAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.utils.TimeProfiler; import org.sonar.batch.protocol.input.GlobalReferentials; public class GlobalReferentialsProvider extends ProviderAdapter { + private static final Logger LOG = LoggerFactory.getLogger(GlobalReferentialsProvider.class); + public GlobalReferentials provide(GlobalReferentialsLoader loader) { - return loader.load(); + TimeProfiler profiler = new TimeProfiler(LOG).start("Load global referentials"); + try { + return loader.load(); + } finally { + profiler.stop(); + } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/settings/DefaultSettingsReferential.java b/sonar-batch/src/main/java/org/sonar/batch/settings/DefaultSettingsReferential.java index 6241b0804dc..f9d7d499018 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/settings/DefaultSettingsReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/settings/DefaultSettingsReferential.java @@ -47,11 +47,6 @@ public class DefaultSettingsReferential implements SettingsReferential { } @Override - public Map<String, String> globalSettings() { - return downloadSettings(null); - } - - @Override public Map<String, String> projectSettings(String moduleKey) { return downloadSettings(moduleKey); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/settings/SettingsReferential.java b/sonar-batch/src/main/java/org/sonar/batch/settings/SettingsReferential.java index d4d7367e3ea..d8e4603826d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/settings/SettingsReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/settings/SettingsReferential.java @@ -30,11 +30,6 @@ import java.util.Map; public interface SettingsReferential extends BatchComponent { /** - * Provide global settings - */ - Map<String, String> globalSettings(); - - /** * Provide settings for a given project or sub-project (includes global settings) * @param projectKey */ diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java index b7b367dea46..34bd571d7ce 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java @@ -19,7 +19,6 @@ */ package org.sonar.batch.bootstrap; -import com.google.common.collect.ImmutableMap; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import org.junit.Before; @@ -28,20 +27,19 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.PropertyDefinitions; -import org.sonar.batch.settings.SettingsReferential; +import org.sonar.batch.protocol.input.GlobalReferentials; import java.util.Collections; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class GlobalSettingsTest { @Rule public ExpectedException thrown = ExpectedException.none(); - SettingsReferential settingsRef = mock(SettingsReferential.class); + GlobalReferentials globalRef; ProjectDefinition project = ProjectDefinition.create().setKey("struts"); Configuration deprecatedConf = new BaseConfiguration(); BootstrapProperties bootstrapProps; @@ -50,15 +48,16 @@ public class GlobalSettingsTest { @Before public void prepare() { + globalRef = new GlobalReferentials(); bootstrapProps = new BootstrapProperties(Collections.<String, String>emptyMap()); mode = mock(AnalysisMode.class); } @Test public void should_load_global_settings() { - when(settingsRef.globalSettings()).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true")); + globalRef.globalSettings().put("sonar.cpd.cross", "true"); - GlobalSettings batchSettings = new GlobalSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode); + GlobalSettings batchSettings = new GlobalSettings(bootstrapProps, new PropertyDefinitions(), globalRef, deprecatedConf, mode); assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue(); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java index e3824aa70f1..ce7466ecc23 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java @@ -40,6 +40,7 @@ import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.GlobalSettings; import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; +import org.sonar.batch.protocol.input.GlobalReferentials; import org.sonar.batch.protocol.input.ProjectReferentials; import org.sonar.batch.referential.ProjectReferentialsLoader; import org.sonar.batch.scan.maven.MavenPluginExecutor; @@ -70,10 +71,10 @@ public class ProjectScanContainerTest { parentContainer.add(bootstrapProperties); parentContainer.add(analysisMode); parentContainer.add(new PropertiesConfiguration()); - SettingsReferential settingsReferential = mock(SettingsReferential.class); - settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), settingsReferential, new PropertiesConfiguration(), analysisMode); + GlobalReferentials globalRef = new GlobalReferentials(); + settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), globalRef, new PropertiesConfiguration(), analysisMode); parentContainer.add(settings); - parentContainer.add(settingsReferential); + parentContainer.add(mock(SettingsReferential.class)); ProjectReferentialsLoader projectReferentialsLoader = new ProjectReferentialsLoader() { @Override public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java index 2d318ee628e..1c6dd28f3b1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java @@ -34,6 +34,7 @@ import org.sonar.api.utils.MessageException; import org.sonar.batch.bootstrap.AnalysisMode; import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.GlobalSettings; +import org.sonar.batch.protocol.input.GlobalReferentials; import org.sonar.batch.settings.SettingsReferential; import java.util.Collections; @@ -57,7 +58,7 @@ public class ProjectSettingsTest { @Before public void prepare() { mode = mock(AnalysisMode.class); - bootstrapProps = new GlobalSettings(new BootstrapProperties(Collections.<String, String>emptyMap()), new PropertyDefinitions(), settingsRef, deprecatedConf, mode); + bootstrapProps = new GlobalSettings(new BootstrapProperties(Collections.<String, String>emptyMap()), new PropertyDefinitions(), new GlobalReferentials(), deprecatedConf, mode); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/settings/DefaultSettingsReferentialTest.java b/sonar-batch/src/test/java/org/sonar/batch/settings/DefaultSettingsReferentialTest.java index 7fda9daaa6c..8687bf8a0e8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/settings/DefaultSettingsReferentialTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/settings/DefaultSettingsReferentialTest.java @@ -36,8 +36,6 @@ public class DefaultSettingsReferentialTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private static final String JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}]"; - private static final String REACTOR_JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}," + "{\"k\":\"sonar.java.coveragePlugin\",\"v\":\"jacoco\"}]"; @@ -60,10 +58,4 @@ public class DefaultSettingsReferentialTest { assertThat(ref.projectSettings("struts")).hasSize(2).includes(MapAssert.entry("sonar.cpd.cross", "true")); } - @Test - public void should_load_global_settings() { - when(client.request("/batch_bootstrap/properties?dryRun=false")).thenReturn(JSON_RESPONSE); - - assertThat(ref.globalSettings()).hasSize(1).includes(MapAssert.entry("sonar.cpd.cross", "true")); - } } |