Allow configuring config and data dirs This change allows the plugin config and data directories to be configured. It lays the foundation for further extending the configuration in a subsequent change, where the plugin config and content can be read from a repository. Change-Id: I3844dc9183eb84566b3b9bece3a9549b035984db
diff --git a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/ConfigFile.java b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/ConfigFile.java new file mode 100644 index 0000000..ef443b2 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/ConfigFile.java
@@ -0,0 +1,24 @@ +// Copyright (C) 2024 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.googlesource.gerrit.plugins.messageoftheday; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import com.google.inject.BindingAnnotation; +import java.lang.annotation.Retention; + +@Retention(RUNTIME) +@BindingAnnotation +public @interface ConfigFile {}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/DataDir.java b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/DataDir.java new file mode 100644 index 0000000..a7da494 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/DataDir.java
@@ -0,0 +1,24 @@ +// Copyright (C) 2024 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.googlesource.gerrit.plugins.messageoftheday; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import com.google.inject.BindingAnnotation; +import java.lang.annotation.Retention; + +@Retention(RUNTIME) +@BindingAnnotation +public @interface DataDir {}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/GetMessage.java b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/GetMessage.java index d1ec85a..2012f31 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/GetMessage.java +++ b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/GetMessage.java
@@ -17,12 +17,10 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.base.Strings; -import com.google.gerrit.extensions.annotations.PluginData; import com.google.gerrit.extensions.annotations.PluginName; import com.google.gerrit.extensions.restapi.Response; import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.server.config.ConfigResource; -import com.google.gerrit.server.config.SitePaths; import com.google.inject.Inject; import java.io.File; import java.io.IOException; @@ -54,9 +52,9 @@ @Inject public GetMessage( - @PluginName String pluginName, @PluginData Path dataDirPath, SitePaths sitePaths) { + @PluginName String pluginName, @ConfigFile File cfgFile, @DataDir Path dataDirPath) { this.dataDirPath = dataDirPath; - this.cfgFile = sitePaths.etc_dir.resolve(pluginName + ".config").toFile(); + this.cfgFile = cfgFile; } @Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java index 1164004..0b7050e 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java +++ b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java
@@ -16,10 +16,30 @@ import static com.google.gerrit.server.config.ConfigResource.CONFIG_KIND; +import com.google.gerrit.extensions.annotations.PluginData; +import com.google.gerrit.extensions.annotations.PluginName; import com.google.gerrit.extensions.restapi.RestApiModule; +import com.google.gerrit.server.config.PluginConfigFactory; +import com.google.gerrit.server.config.SitePaths; import com.google.inject.AbstractModule; +import com.google.inject.Inject; +import com.google.inject.Provides; +import com.google.inject.Singleton; +import java.io.File; +import java.nio.file.Path; class Module extends AbstractModule { + private static final String CONFIG_DIR = "configDir"; + private static final String DATA_DIR = "dataDir"; + private final SitePaths sitePaths; + private final PluginConfigFactory cfg; + + @Inject + public Module(SitePaths sitePaths, PluginConfigFactory cfg) { + this.sitePaths = sitePaths; + this.cfg = cfg; + } + @Override protected void configure() { install( @@ -30,4 +50,22 @@ } }); } + + @Provides + @Singleton + @ConfigFile + File provideConfigFile(@PluginName String pluginName) { + String configDir = cfg.getFromGerritConfig(pluginName).getString(CONFIG_DIR); + return (configDir != null ? Path.of(configDir) : sitePaths.etc_dir) + .resolve(pluginName + ".config") + .toFile(); + } + + @Provides + @Singleton + @DataDir + Path provideDataDir(@PluginName String pluginName, @PluginData Path dataDirPath) { + String dataDir = cfg.getFromGerritConfig(pluginName).getString(DATA_DIR); + return dataDir != null ? Path.of(dataDir) : dataDirPath; + } }
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index fdf4bc5..6c13155 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -1,7 +1,8 @@ # Configuration Configuration of the @PLUGIN@ plugin is done in the `messageoftheday.config` -file in the site's `etc` directory. +file in the site's `etc` directory by default. Its location can be configured +in `gerrit.config`. NOTE: when the `messageoftheday.config` file changes, the plugin needs to be reloaded for the configuration change to take effect. @@ -31,14 +32,30 @@ will not be displayed. The format of this field is `yyyyMMdd:HHmm`. +## `gerrit.config` +``` + [plugin "@PLUGIN@"] + configDir = /full/path/to/configDir + dataDir = /full/path/to/htmlContentDir +``` +plugin.@PLUGIN@.configDir +: The path where `@PLUGIN@.config` is located. Defaults to site's `etc/` directory. + +plugin.@PLUGIN@.dataDir +: The path where `<message.id>.html` is located. Defaults to site's `data/@PLUGIN@/` directory. + ## Message content and location -A message is an HTML file, named like `<message.id>.html` and stored in the -site's `data/@PLUGIN@/` directory. For the above example, the message.id is -`hello` which means that the file containing the message content is stored under: +A message is an HTML file, named like `<message.id>.html` and stored by default in +the site's `data/@PLUGIN@/` directory. For a config like below, the message.id is +`hello` which means that the file containing the message content is stored under +`$GERRIT_SITE/data/@PLUGIN@/hello.html` ``` - $GERRIT_SITE/data/@PLUGIN@/hello.html + [message] + id = hello + startsAt = 20170803:1420 + expiresAt = 20170810:1730 ``` The message content is an HTML snippet. For example: