diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-04-04 11:07:52 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-04-04 11:09:06 +0200 |
commit | 2e24fb395108f50e96db34398e1f9458b3f1c4c1 (patch) | |
tree | ff08a760fdf6e58998448880054f5a714e552763 /sonar-server | |
parent | 0d284e6c1a5105c396ab85273de2af30b6d63847 (diff) | |
download | sonarqube-2e24fb395108f50e96db34398e1f9458b3f1c4c1.tar.gz sonarqube-2e24fb395108f50e96db34398e1f9458b3f1c4c1.zip |
SONAR-3320 Add an extension point to observe property changes
Diffstat (limited to 'sonar-server')
8 files changed, 91 insertions, 23 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/GlobalSettingsUpdater.java b/sonar-server/src/main/java/org/sonar/server/platform/GlobalSettingsUpdater.java new file mode 100644 index 00000000000..83d6e146f4e --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/platform/GlobalSettingsUpdater.java @@ -0,0 +1,58 @@ +/* + * 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.apache.commons.configuration.Configuration; +import org.sonar.api.config.GlobalPropertyChangeHandler; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.List; + +/** + * Update cache of global settings (see org.sonar.api.config.Settings) and notify org.sonar.api.config.GlobalPropertyChangeHandler extensions + * + * @since 2.15 + */ +public class GlobalSettingsUpdater { + private ServerSettings settings; + private Configuration deprecatedConf; + private List<GlobalPropertyChangeHandler> changeHandlers; + + public GlobalSettingsUpdater(ServerSettings settings, Configuration config, List<GlobalPropertyChangeHandler> changeHandlers) { + this.settings = settings; + this.deprecatedConf = config; + this.changeHandlers = changeHandlers; + } + + public GlobalSettingsUpdater(ServerSettings settings, Configuration config) { + this(settings, config, Collections.<GlobalPropertyChangeHandler>emptyList()); + } + + public void setProperty(String key, @Nullable String value) { + settings.setProperty(key, value); + deprecatedConf.setProperty(key, 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/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index e698c426cd7..52ec833c377 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 @@ -159,7 +159,6 @@ public final class Platform { coreContainer = rootContainer.createChild(); coreContainer.addSingleton(ServerDatabaseSettingsLoader.class); coreContainer.addSingleton(DefaultDatabaseConnector.class); - coreContainer.addSingleton(ServerExtensionInstaller.class); coreContainer.addSingleton(ThreadLocalDatabaseSessionFactory.class); coreContainer.addPicoAdapter(new DatabaseSessionProvider()); @@ -174,6 +173,7 @@ public final class Platform { ServerExtensionInstaller extensionRegistrar = servicesContainer.getComponentByType(ServerExtensionInstaller.class); extensionRegistrar.registerExtensions(servicesContainer); + servicesContainer.addSingleton(GlobalSettingsUpdater.class); servicesContainer.addSingleton(HttpDownloader.class); servicesContainer.addSingleton(UpdateCenterClient.class); servicesContainer.addSingleton(UpdateCenterMatrixFactory.class); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java index 2f1d651d72b..b0eb1eef2af 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java @@ -36,6 +36,5 @@ public final class ServerDatabaseSettingsLoader { public void start() { settings.activateDatabaseSettings(propertiesDao); - settings.load(); } } 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 edca83e0953..fbcaaaa576e 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 @@ -19,6 +19,7 @@ */ 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.PropertyDefinitions; @@ -66,15 +67,21 @@ public class ServerSettings extends Settings { } public ServerSettings activateDatabaseSettings(PropertiesDao dao) { + return activateDatabaseSettings(dao, SonarHome.getHome()); + } + + @VisibleForTesting + ServerSettings activateDatabaseSettings(PropertiesDao dao, File sonarHome) { this.propertiesDao = dao; + load(sonarHome); return this; } - public ServerSettings load() { + private ServerSettings load() { return load(SonarHome.getHome()); } - ServerSettings load(File sonarHome) { + private ServerSettings load(File sonarHome) { clear(); setProperty(CoreProperties.SONAR_HOME, sonarHome.getAbsolutePath()); setProperty(DEPLOY_DIR, deployDir.getAbsolutePath()); 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 f7e153ac0fa..8c34f40739d 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 @@ -50,6 +50,7 @@ import org.sonar.server.filters.Filter; import org.sonar.server.filters.FilterExecutor; import org.sonar.server.filters.FilterResult; import org.sonar.server.notifications.reviews.ReviewsNotificationManager; +import org.sonar.server.platform.GlobalSettingsUpdater; import org.sonar.server.platform.Platform; import org.sonar.server.platform.ServerIdGenerator; import org.sonar.server.platform.ServerSettings; @@ -58,6 +59,7 @@ import org.sonar.server.rules.ProfilesConsole; import org.sonar.server.rules.RulesConsole; import org.sonar.updatecenter.common.Version; +import javax.annotation.Nullable; import java.net.InetAddress; import java.sql.Connection; import java.util.Collection; @@ -324,8 +326,8 @@ public final class JRubyFacade { return getContainer().getComponentByType(ProfilesManager.class); } - public void reloadConfiguration() { - getContainer().getComponentByType(ServerSettings.class).load(); + public void setGlobalProperty(String key, @Nullable String value) { + getContainer().getComponentByType(GlobalSettingsUpdater.class).setProperty(key, value); } public Settings getSettings() { @@ -463,6 +465,4 @@ public final class JRubyFacade { public ComponentContainer getContainer() { return Platform.getInstance().getContainer(); } - - } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb index 18c7e8f0797..d80ef45e1df 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb @@ -39,6 +39,7 @@ class SettingsController < ApplicationController else access_denied unless is_admin? end + is_global=(@project.nil?) load_properties(@project) @@ -46,22 +47,30 @@ class SettingsController < ApplicationController if @category && @definitions_per_category[@category] @definitions_per_category[@category].each do |property| value=params[property.getKey()] + persisted_property = Property.find(:first, :conditions => {:prop_key=> property.key(), :resource_id => params[:resource_id], :user_id => nil}) - persisted_property = Property.find(:first, :conditions => {:prop_key=> property.key(), :resource_id => (@project ? @project.id : nil), :user_id => nil}) + # update the property if persisted_property if value.empty? - Property.delete_all('prop_key' => property.key(), 'resource_id' => (@project ? @project.id : nil), 'user_id' => nil) + Property.delete_all('prop_key' => property.key(), 'resource_id' => params[:resource_id], 'user_id' => nil) + java_facade.setGlobalProperty(property.getKey(), nil) if is_global elsif persisted_property.text_value != value.to_s persisted_property.text_value = value.to_s - persisted_property.save + if persisted_property.save && is_global + java_facade.setGlobalProperty(property.getKey(), value.to_s) + end @persisted_properties_per_key[persisted_property.key]=persisted_property end - elsif !value.blank? - persisted_property=Property.create(:prop_key => property.key(), :text_value => value.to_s, :resource_id => (@project ? @project.id : nil)) + + # create the property + elsif value.present? + persisted_property=Property.new(:prop_key => property.key(), :text_value => value.to_s, :resource_id => params[:resource_id]) + if persisted_property.save && is_global + java_facade.setGlobalProperty(property.getKey(), value.to_s) + end @persisted_properties_per_key[persisted_property.key]=persisted_property end end - java_facade.reloadConfiguration() params[:layout]='false' render :partial => 'settings/properties' diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb index 8e6c6805d49..4e9af8d7cd4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb @@ -56,14 +56,14 @@ class Property < ActiveRecord::Base Property.delete_all('prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil) prop.save end - reload_java_configuration + Java::OrgSonarServerUi::JRubyFacade.getInstance().setGlobalProperty(key, value) unless resource_id end prop end def self.clear(key, resource_id=nil) Property.delete_all('prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil) - reload_java_configuration + Java::OrgSonarServerUi::JRubyFacade.getInstance().setGlobalProperty(key, nil) unless resource_id end def self.by_key(key, resource_id=nil) @@ -78,7 +78,7 @@ class Property < ActiveRecord::Base property = Property.find(:first, :conditions => {:prop_key => key, :resource_id => nil, :user_id => nil}); property.text_value = value property.save - reload_java_configuration + Java::OrgSonarServerUi::JRubyFacade.getInstance().setGlobalProperty(key, value) property end @@ -117,8 +117,4 @@ class Property < ActiveRecord::Base errors.add_to_base(validation_result.getErrorKey()) unless validation_result.isValid() end end - - def self.reload_java_configuration - Java::OrgSonarServerUi::JRubyFacade.getInstance().reloadConfiguration() - end end 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 c356cfaab36..65727c2a8c7 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 @@ -62,8 +62,7 @@ public class ServerSettingsTest extends AbstractDbUnitTestCase { setupData("db/shared"); ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home); - settings.activateDatabaseSettings(new PropertiesDao(getMyBatis())); - settings.load(home); + settings.activateDatabaseSettings(new PropertiesDao(getMyBatis()), home); assertThat(settings.getString("global_only"), is("is_global")); assertThat(settings.getString("global_and_project"), is("is_global")); |