servicesContainer.addSingleton(GwtI18n.class);
servicesContainer.addSingleton(ResourceTypes.class);
servicesContainer.addSingleton(NewUserNotifier.class);
+ servicesContainer.addSingleton(SettingsChangeNotifier.class);
// Notifications
servicesContainer.addSingleton(EmailSettings.class);
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;
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());
}
@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
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
*/
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;
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);
}
public void setGlobalProperty(String key, @Nullable String value) {
get(ServerSettings.class).setProperty(key, value);
+ get(SettingsChangeNotifier.class).onGlobalPropertyChange(key, value);
}
public Settings getSettings() {
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;
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
@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");
}
@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");
}
@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);
@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");
--- /dev/null
+/*
+ * 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) {
+ }
+ }));
+ }
+}