From: Simon Brandhof Date: Sun, 15 Jul 2012 20:53:47 +0000 (+0200) Subject: Fix loading of GlobalPropertyChangeHandler extensions X-Git-Tag: 3.2~80 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=aa7ffed3ca2a18e4b8b9a292cea960699e7dbd92;p=sonarqube.git Fix loading of GlobalPropertyChangeHandler extensions --- diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index b3295221436..a995d5b8685 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -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); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java index ba2d3dfdf33..1958f421cfa 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java @@ -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.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 index 00000000000..3cbeaa2168b --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/platform/SettingsChangeNotifier.java @@ -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); + } + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java index 7d461004756..e7407901e76 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java @@ -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); } diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index cad1ff6d9a7..263bf563112 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -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() { diff --git a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java index d858e48093f..a4346e5866a 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java @@ -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 diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java index a1ff85a216f..262d3ef146c 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java @@ -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 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 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 index 00000000000..d064745c04a --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java @@ -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() { + 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) { + } + })); + } +}