]> source.dussan.org Git - sonarqube.git/commitdiff
Fix loading of GlobalPropertyChangeHandler extensions
authorSimon Brandhof <simon.brandhof@gmail.com>
Sun, 15 Jul 2012 20:53:47 +0000 (22:53 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Sun, 15 Jul 2012 20:53:47 +0000 (22:53 +0200)
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
sonar-server/src/main/java/org/sonar/server/platform/SettingsChangeNotifier.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java
sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java [new file with mode: 0644]

index b3295221436f1bc0a5c527dfd128222de6295d3e..a995d5b86858dc1a80741bdf843ed155c5601eab 100644 (file)
@@ -231,6 +231,7 @@ public final class Platform {
     servicesContainer.addSingleton(GwtI18n.class);
     servicesContainer.addSingleton(ResourceTypes.class);
     servicesContainer.addSingleton(NewUserNotifier.class);
+    servicesContainer.addSingleton(SettingsChangeNotifier.class);
 
     // Notifications
     servicesContainer.addSingleton(EmailSettings.class);
index ba2d3dfdf332b6704e2aee7cff8abc9d206399c7..1958f421cfa4ed940b5dc4707d5140c1eae729a1 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.server.platform;
 import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.configuration.Configuration;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.config.GlobalPropertyChangeHandler;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
 import org.sonar.core.config.ConfigurationUtils;
@@ -52,23 +51,17 @@ public class ServerSettings extends Settings {
   private Configuration deprecatedConfiguration;
   private File deployDir;
   private File sonarHome;
-  private GlobalPropertyChangeHandler[] changeHandlers;
-
-  public ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, ServletContext servletContext, GlobalPropertyChangeHandler[] changeHandlers) {
-    this(definitions, deprecatedConfiguration, getDeployDir(servletContext), SonarHome.getHome(), changeHandlers);
-  }
 
   public ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, ServletContext servletContext) {
-    this(definitions, deprecatedConfiguration, servletContext, new GlobalPropertyChangeHandler[0]);
+    this(definitions, deprecatedConfiguration, getDeployDir(servletContext), SonarHome.getHome());
   }
 
   @VisibleForTesting
-  ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, File deployDir, File sonarHome, GlobalPropertyChangeHandler[] changeHandlers) {
+  ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, File deployDir, File sonarHome) {
     super(definitions);
     this.deprecatedConfiguration = deprecatedConfiguration;
     this.deployDir = deployDir;
     this.sonarHome = sonarHome;
-    this.changeHandlers = changeHandlers;
     load(Collections.<String, String>emptyMap());
   }
 
@@ -125,11 +118,6 @@ public class ServerSettings extends Settings {
   @Override
   protected void doOnSetProperty(String key, @Nullable String value) {
     deprecatedConfiguration.setProperty(key, value);
-
-    GlobalPropertyChangeHandler.PropertyChange change = GlobalPropertyChangeHandler.PropertyChange.create(key, value);
-    for (GlobalPropertyChangeHandler changeHandler : changeHandlers) {
-      changeHandler.onChange(change);
-    }
   }
 
   @Override
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/SettingsChangeNotifier.java b/sonar-server/src/main/java/org/sonar/server/platform/SettingsChangeNotifier.java
new file mode 100644 (file)
index 0000000..3cbeaa2
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * 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.server.platform;
+
+import org.sonar.api.ServerComponent;
+import org.sonar.api.config.GlobalPropertyChangeHandler;
+
+import javax.annotation.Nullable;
+
+public class SettingsChangeNotifier implements ServerComponent {
+
+  private GlobalPropertyChangeHandler[] changeHandlers;
+
+  public SettingsChangeNotifier(GlobalPropertyChangeHandler[] changeHandlers) {
+    this.changeHandlers = changeHandlers;
+  }
+
+  public SettingsChangeNotifier() {
+    this(new GlobalPropertyChangeHandler[0]);
+  }
+
+  public void onGlobalPropertyChange(String key, @Nullable String value) {
+    GlobalPropertyChangeHandler.PropertyChange change = GlobalPropertyChangeHandler.PropertyChange.create(key, value);
+    for (GlobalPropertyChangeHandler changeHandler : changeHandlers) {
+      changeHandler.onChange(change);
+    }
+  }
+}
index 7d461004756dc7778d60554149b4d23a10c38124..e7407901e765c0aa74f19ce13086ddd46ccd5163 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.plugins;
 
-import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.Properties;
@@ -69,17 +68,12 @@ public class UpdateCenterClient implements ServerComponent {
   private long lastRefreshDate = 0;
   private UriReader uriReader;
 
-  @VisibleForTesting
-  UpdateCenterClient(UriReader uriReader, URI uri) {
+  public UpdateCenterClient(UriReader uriReader, Settings settings) throws URISyntaxException {
     this.uriReader = uriReader;
-    this.uri = uri;
+    this.uri = new URI(settings.getString(URL_PROPERTY));
     LoggerFactory.getLogger(getClass()).info("Update center: " + uriReader.description(uri));
   }
 
-  public UpdateCenterClient(UriReader uriReader, Settings settings) throws URISyntaxException {
-    this(uriReader, new URI(settings.getString(URL_PROPERTY)));
-  }
-
   public UpdateCenter getCenter() {
     return getCenter(false);
   }
index cad1ff6d9a7bd1f560d3c3b999f32339d88b439d..263bf5631123814c0e504dabcfeb5aebe68cf8c4 100644 (file)
@@ -333,6 +333,7 @@ public final class JRubyFacade {
 
   public void setGlobalProperty(String key, @Nullable String value) {
     get(ServerSettings.class).setProperty(key, value);
+    get(SettingsChangeNotifier.class).onGlobalPropertyChange(key, value);
   }
 
   public Settings getSettings() {
index d858e48093f59b2bd111b983e979f8509ec4988c..a4346e5866a1871e2e34c3ebe1fc909e2755d2ef 100644 (file)
@@ -25,7 +25,6 @@ import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
 import org.junit.Before;
 import org.junit.Test;
-import org.sonar.api.config.GlobalPropertyChangeHandler;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.core.properties.PropertiesDao;
 import org.sonar.core.properties.PropertyDto;
@@ -50,8 +49,7 @@ public class PersistentSettingsTest {
       new PropertyDefinitions(),
       new PropertiesConfiguration(),
       new File("."),
-      new File(PersistentSettingsTest.class.getResource("/org/sonar/server/platform/PersistentSettingsTest/").toURI()),
-      new GlobalPropertyChangeHandler[0]);
+      new File(PersistentSettingsTest.class.getResource("/org/sonar/server/platform/PersistentSettingsTest/").toURI()));
   }
 
   @Test
index a1ff85a216f57a4ea165b630e1584a298da0e934..262d3ef146c05602d6fd700278aab3897574f663 100644 (file)
@@ -37,7 +37,7 @@ public class ServerSettingsTest {
 
   @Test
   public void shouldLoadPropertiesFile() {
-    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home, new GlobalPropertyChangeHandler[0]);
+    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home);
 
     assertThat(settings.getString("hello")).isEqualTo("world");
   }
@@ -45,7 +45,7 @@ public class ServerSettingsTest {
   @Test
   public void systemPropertiesShouldOverridePropertiesFile() {
     System.setProperty("ServerSettingsTestEnv", "in_env");
-    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home, new GlobalPropertyChangeHandler[0]);
+    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home);
 
     assertThat(settings.getString("ServerSettingsTestEnv")).isEqualTo("in_env");
   }
@@ -53,12 +53,12 @@ public class ServerSettingsTest {
   @Test(expected = IllegalStateException.class)
   public void shouldFailIfPropertiesFileNotFound() {
     File sonarHome = new File("unknown/path");
-    new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), sonarHome, new GlobalPropertyChangeHandler[0]);
+    new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), sonarHome);
   }
 
   @Test
   public void activateDatabaseSettings() {
-    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home, new GlobalPropertyChangeHandler[0]);
+    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home);
 
     Map<String, String> databaseProperties = ImmutableMap.of("in_db", "true");
     settings.activateDatabaseSettings(databaseProperties);
@@ -68,7 +68,7 @@ public class ServerSettingsTest {
 
   @Test
   public void file_settings_override_db_settings() {
-    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home, new GlobalPropertyChangeHandler[0]);
+    ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home);
     assertThat(settings.getString("in_file")).isEqualTo("true");
 
     Map<String, String> databaseProperties = ImmutableMap.of("in_file", "false");
diff --git a/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java b/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java
new file mode 100644 (file)
index 0000000..d064745
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.server.platform;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Test;
+import org.sonar.api.config.GlobalPropertyChangeHandler;
+
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class SettingsChangeNotifierTest {
+  @Test
+  public void onGlobalPropertyChange() {
+    GlobalPropertyChangeHandler handler = mock(GlobalPropertyChangeHandler.class);
+    SettingsChangeNotifier notifier = new SettingsChangeNotifier(new GlobalPropertyChangeHandler[]{handler});
+
+    notifier.onGlobalPropertyChange("foo", "bar");
+
+    verify(handler).onChange(argThat(new BaseMatcher<GlobalPropertyChangeHandler.PropertyChange>() {
+      public boolean matches(Object o) {
+        GlobalPropertyChangeHandler.PropertyChange change = (GlobalPropertyChangeHandler.PropertyChange) o;
+        return change.getKey().equals("foo") && change.getNewValue().equals("bar");
+      }
+
+      public void describeTo(Description description) {
+      }
+    }));
+  }
+}