From 9ed9540325b9bcd2f6566d1e0de5a7fec781335a Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Mon, 27 Jan 2014 16:21:19 +0100 Subject: [PATCH] Allow plugins to check for config updates when reading plugin config Plugins can have their global configuration in a plugin subsection of the gerrit.config file. PluginConfigFactory provides easy access to this global plugin configuration and ensures that the gerrit.config file is not on any access loaded from the filesystem. The GerritServerConfig is injected into the PluginConfigFactory so that updates of the gerrit.config file only get visible in PluginConfigFactory after the server has been restarted. Plugins may want to provide ways to live update their global configuration, e.g. they could provide a REST endpoint to update their plugin section in gerrit.config. To make the changes visible in PluginConfigFactory the cached GerritServerConfig in it needs to be reloaded. For this purpose this change adds a refresh flag to the method in PluginConfigFactory that reads the global plugin config. The GerritServerConfig is then reloaded when it is was changed in the filesystem. Change-Id: Ide6b1457bfe5a63459e816ca9e00604a62da8365 Signed-off-by: Edwin Kempin --- .../server/config/PluginConfigFactory.java | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigFactory.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigFactory.java index 8e1e568f0f..11a7c90c7d 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigFactory.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigFactory.java @@ -26,6 +26,7 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import org.eclipse.jgit.errors.ConfigInvalidException; +import org.eclipse.jgit.internal.storage.file.FileSnapshot; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.util.FS; @@ -42,19 +43,25 @@ public class PluginConfigFactory implements ReloadPluginListener { LoggerFactory.getLogger(PluginConfigFactory.class); private final SitePaths site; - private final Config cfg; + private final GerritServerConfigProvider cfgProvider; private final ProjectCache projectCache; private final ProjectState.Factory projectStateFactory; private final Map pluginConfigs; + private volatile FileSnapshot cfgSnapshot; + private volatile Config cfg; + @Inject - PluginConfigFactory(SitePaths site, @GerritServerConfig Config cfg, + PluginConfigFactory(SitePaths site, GerritServerConfigProvider cfgProvider, ProjectCache projectCache, ProjectState.Factory projectStateFactory) { this.site = site; - this.cfg = cfg; + this.cfgProvider = cfgProvider; this.projectCache = projectCache; this.projectStateFactory = projectStateFactory; this.pluginConfigs = Maps.newHashMap(); + + this.cfgSnapshot = FileSnapshot.save(site.gerrit_config); + this.cfg = cfgProvider.get(); } /** @@ -74,6 +81,31 @@ public class PluginConfigFactory implements ReloadPluginListener { * @return the plugin configuration from the 'gerrit.config' file */ public PluginConfig getFromGerritConfig(String pluginName) { + return getFromGerritConfig(pluginName, false); + } + + /** + * Returns the configuration for the specified plugin that is stored in the + * 'gerrit.config' file. + * + * The returned plugin configuration provides access to all parameters of the + * 'gerrit.config' file that are set in the 'plugin' subsection of the + * specified plugin. + * + * E.g.: [plugin "my-plugin"] myKey = myValue + * + * @param pluginName the name of the plugin for which the configuration should + * be returned + * @param refresh if true it is checked if the 'gerrit.config' + * file was modified and if yes the Gerrit configuration is reloaded, + * if false the cached Gerrit configuration is used + * @return the plugin configuration from the 'gerrit.config' file + */ + public PluginConfig getFromGerritConfig(String pluginName, boolean refresh) { + if (refresh && cfgSnapshot.isModified(site.gerrit_config)) { + cfgSnapshot = FileSnapshot.save(site.gerrit_config); + cfg = cfgProvider.get(); + } return new PluginConfig(pluginName, cfg); }