Provide easy way to access plugin configuration
Some plugins need some configuration. The idea is to store this
configuration in gerrit.config inside a "plugin" section having a
subsection per plugin. E.g.:
[plugin "reviewers-by-blame"]
maxReviewers = 3
A plugin can now easily access this configuration by:
int maxReviewers = pluginConfigProvider
.get("reviewers-by-blame")
.getInt("maxReviewers", 0);
Change-Id: I4ef7386b7fa3cf4a986722acb8e16dee744697d1
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/Documentation/dev-plugins.txt b/Documentation/dev-plugins.txt
index 0ba51c4..c109294 100644
--- a/Documentation/dev-plugins.txt
+++ b/Documentation/dev-plugins.txt
@@ -313,6 +313,39 @@
$ ssh -p 29418 review.example.com helloworld print
----
+[[configuration]]
+Configuration
+-------------
+
+In Gerrit, global configuration is stored in the `gerrit.config` file.
+If a plugin needs global configuration, this configuration should be
+stored in a `plugin` subsection in the `gerrit.config` file.
+
+To avoid conflicts with other plugins, it is recommended that plugins
+only use the `plugin` subsection with their own name. For example the
+`helloworld` plugin should store its configuration in the
+`plugin.helloworld` subsection:
+
+----
+[plugin "helloworld"]
+ language = Latin
+----
+
+Via the `com.google.gerrit.server.config.PluginConfigProvider` class a
+plugin can easily access its configuration and there is no need for a
+plugin to parse the `gerrit.config` file on its own:
+
+[source,java]
+----
+ @Inject
+ private com.google.gerrit.server.config.PluginConfigProvider cfg;
+
+ ...
+
+ String language = cfg.get("helloworld")
+ .getString("language", "English");
+----
+
[[capabilities]]
Plugin Owned Capabilities
-------------------------
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfig.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfig.java
new file mode 100644
index 0000000..a026500
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfig.java
@@ -0,0 +1,63 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.server.config;
+
+import com.google.common.base.Objects;
+
+import org.eclipse.jgit.lib.Config;
+
+public class PluginConfig {
+ private static final String PLUGIN = "plugin";
+
+ private final String pluginName;
+ private final Config cfg;
+
+ public PluginConfig(String pluginName, Config cfg) {
+ this.pluginName = pluginName;
+ this.cfg = cfg;
+ }
+
+ public String getString(String name) {
+ return cfg.getString(PLUGIN, pluginName, name);
+ }
+
+ public String getString(String name, String defaultValue) {
+ return Objects.firstNonNull(cfg.getString(PLUGIN, pluginName, name), defaultValue);
+ }
+
+ public String[] getStringList(String name) {
+ return cfg.getStringList(PLUGIN, pluginName, name);
+ }
+
+ public int getInt(String name, int defaultValue) {
+ return cfg.getInt(PLUGIN, pluginName, name, defaultValue);
+ }
+
+ public long getLong(String name, long defaultValue) {
+ return cfg.getLong(PLUGIN, pluginName, name, defaultValue);
+ }
+
+ public boolean getBoolean(String name, boolean defaultValue) {
+ return cfg.getBoolean(PLUGIN, pluginName, name, defaultValue);
+ }
+
+ public <T extends Enum<?>> T getEnum(String name, T defaultValue) {
+ return cfg.getEnum(PLUGIN, pluginName, name, defaultValue);
+ }
+
+ public <T extends Enum<?>> T getEnum(T[] all, String name, T defaultValue) {
+ return cfg.getEnum(all, PLUGIN, pluginName, name, defaultValue);
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigProvider.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigProvider.java
new file mode 100644
index 0000000..a6d3c51
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/PluginConfigProvider.java
@@ -0,0 +1,34 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.server.config;
+
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+import org.eclipse.jgit.lib.Config;
+
+@Singleton
+public class PluginConfigProvider {
+ private final Config cfg;
+
+ @Inject
+ PluginConfigProvider(@GerritServerConfig Config cfg) {
+ this.cfg = cfg;
+ }
+
+ public PluginConfig get(String pluginName) {
+ return new PluginConfig(pluginName, cfg);
+ }
+}