]> source.dussan.org Git - sonarqube.git/commitdiff
SONARCLOUD-61 sonarcloud edition sets organization flags automatically
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 19 Jun 2018 12:21:33 +0000 (14:21 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 21 Jun 2018 18:21:31 +0000 (20:21 +0200)
server/sonar-server/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java
server/sonar-server/src/test/java/org/sonar/server/setting/ThreadLocalSettingsTest.java

index 979267e8c02bfc62dae33ac7a48e7302deaeba54..c36471f6934fb35a4122d6885c16b563d7f9b0bf 100644 (file)
@@ -33,7 +33,10 @@ import org.sonar.api.config.Encryption;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
 
+import static java.lang.String.format;
 import static java.util.Collections.unmodifiableMap;
 import static java.util.Objects.requireNonNull;
 
@@ -54,8 +57,10 @@ import static java.util.Objects.requireNonNull;
 @ComputeEngineSide
 @ServerSide
 public class ThreadLocalSettings extends Settings {
+  private static final Logger LOG = Loggers.get(ThreadLocalSettings.class);
 
-  private final Properties systemProps;
+  private final Properties overwrittenSystemProps = new Properties();
+  private final Properties systemProps = new Properties();
   private static final ThreadLocal<Map<String, String>> CACHE = new ThreadLocal<>();
   private Map<String, String> getPropertyDbFailureCache = Collections.emptyMap();
   private Map<String, String> getPropertiesDbFailureCache = Collections.emptyMap();
@@ -69,7 +74,6 @@ public class ThreadLocalSettings extends Settings {
   ThreadLocalSettings(PropertyDefinitions definitions, Properties props, SettingLoader settingLoader) {
     super(definitions, new Encryption(null));
     this.settingLoader = settingLoader;
-    this.systemProps = new Properties();
     props.forEach((k, v) -> systemProps.put(k, v == null ? null : v.toString().trim()));
 
     // TODO something wrong about lifecycle here. It could be improved
@@ -88,11 +92,17 @@ public class ThreadLocalSettings extends Settings {
   @Override
   protected Optional<String> get(String key) {
     // search for the first value available in
-    // 1. system properties
-    // 2. thread local cache (if enabled)
-    // 3. db
+    // 1. overwritten system properties
+    // 2. system properties
+    // 3. thread local cache (if enabled)
+    // 4. db
 
-    String value = systemProps.getProperty(key);
+    String value =  overwrittenSystemProps.getProperty(key);
+    if (value != null) {
+      return Optional.of(value);
+    }
+
+    value = systemProps.getProperty(key);
     if (value != null) {
       return Optional.of(value);
     }
@@ -125,16 +135,29 @@ public class ThreadLocalSettings extends Settings {
     }
   }
 
+  public void setSystemProperty(String key, String value) {
+    checkKeyAndValue(key, value);
+    String systemValue = systemProps.getProperty(key);
+    if (LOG.isDebugEnabled() && systemValue != null && !value.equals(systemValue)) {
+      LOG.debug(format("System property '%s' with value '%s' overwritten with value '%s'", key, systemValue, value));
+    }
+    overwrittenSystemProps.put(key, value.trim());
+  }
+
   @Override
   protected void set(String key, String value) {
-    requireNonNull(key, "key can't be null");
-    requireNonNull(value, "value can't be null");
+    checkKeyAndValue(key, value);
     Map<String, String> dbProps = CACHE.get();
     if (dbProps != null) {
       dbProps.put(key, value.trim());
     }
   }
 
+  private static void checkKeyAndValue(String key, String value) {
+    requireNonNull(key, "key can't be null");
+    requireNonNull(value, "value can't be null");
+  }
+
   @Override
   protected void remove(String key) {
     Map<String, String> dbProps = CACHE.get();
index 31680b31c2af71d022f41c87866e54d88c9fa717..1dcdaec02de332086108838527769d5d40f3d188 100644 (file)
@@ -165,6 +165,25 @@ public class ThreadLocalSettingsTest {
     assertThat(underTest.get("foo")).isNotPresent();
   }
 
+  @Test
+  public void overwritten_system_settings_have_precedence_over_system_and_databse() {
+    underTest = create(ImmutableMap.of("foo", "from system"));
+
+    underTest.setSystemProperty("foo", "donut");
+
+    assertThat(underTest.get("foo")).hasValue("donut");
+  }
+
+  @Test
+  public void overwritten_system_settings_have_precedence_over_databse() {
+    insertPropertyIntoDb("foo", "from db");
+    underTest = create(Collections.emptyMap());
+
+    underTest.setSystemProperty("foo", "donut");
+
+    assertThat(underTest.get("foo")).hasValue("donut");
+  }
+
   @Test
   public void system_settings_have_precedence_over_database() {
     insertPropertyIntoDb("foo", "from db");