Move file store specific Guice bindings to the FileBasedModule Change-Id: If614f7346ad6b39f9ac8c03811a38471fe0fe22e
diff --git a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/FileBasedModule.java b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/FileBasedModule.java new file mode 100644 index 0000000..9137876 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/FileBasedModule.java
@@ -0,0 +1,55 @@ +// Copyright (C) 2025 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 com.google.gerrit.extensions.annotations.PluginData; +import com.google.gerrit.extensions.annotations.PluginName; +import com.google.gerrit.server.config.PluginConfigFactory; +import com.google.gerrit.server.config.SitePaths; +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.Singleton; +import java.io.File; +import java.nio.file.Path; + +public class FileBasedModule extends AbstractModule { + private static final String CONFIG_DIR = "configDir"; + private static final String DATA_DIR = "dataDir"; + + @Override + protected void configure() { + bind(MessageStore.class).to(FileBasedMessageStore.class); + } + + @Provides + @Singleton + @ConfigFile + File provideConfigFile( + PluginConfigFactory cfg, SitePaths sitePaths, @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( + PluginConfigFactory cfg, @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/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java index 085608b..dd7f7c8 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java +++ b/src/main/java/com/googlesource/gerrit/plugins/messageoftheday/Module.java
@@ -17,34 +17,14 @@ import static com.google.gerrit.server.config.ConfigResource.CONFIG_KIND; import com.google.gerrit.extensions.annotations.Exports; -import com.google.gerrit.extensions.annotations.PluginData; -import com.google.gerrit.extensions.annotations.PluginName; import com.google.gerrit.extensions.config.CapabilityDefinition; 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() { - bind(MessageStore.class).to(FileBasedMessageStore.class); bind(CapabilityDefinition.class) .annotatedWith(Exports.named(UpdateBannerCapability.NAME)) .to(UpdateBannerCapability.class); @@ -56,23 +36,6 @@ post(CONFIG_KIND, "message").to(SetMessage.class); } }); - } - - @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; + install(new FileBasedModule()); } }