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());
}
}
}
- 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);
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;
}
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(),
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();
*/
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);
- }
}
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();
+ }
}
}
this.analysisMode = analysisMode;
}
- @Override
- public Map<String, String> globalSettings() {
- return downloadSettings(null);
- }
-
@Override
public Map<String, String> projectSettings(String moduleKey) {
return downloadSettings(moduleKey);
*/
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
*/
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;
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;
@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();
}
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;
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) {
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;
@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
@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\"}]";
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"));
- }
}