Expose ApiModule with ReplicationConfigOverrides

To ease working with `ReplicationConfigOverrides` expose them as
_Cross-Plugin Communicaiton_ module (aka _ApiModule_). Consumers will
now be able to put `replication.jar` together with their own custom
configuration provider in `plugins/` directory and customize plugin
configuration.

It is also possible to mix _Cross-Plugin Communication_ with
_libModule_. For this use case the `replication.jar` must be moved to
`lib/` directory and `gerrit.installMoudle` configuration option added
to `gerrit.config`.

Bug: Issue 310510978
Change-Id: Ib7a04eea503b221eae02cdeb393e3f727dda540f
diff --git a/BUILD b/BUILD
index ee97660..04b470b 100644
--- a/BUILD
+++ b/BUILD
@@ -9,6 +9,7 @@
         "Implementation-Title: Replication plugin",
         "Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/replication",
         "Gerrit-PluginName: replication",
+        "Gerrit-ApiModule: com.googlesource.gerrit.plugins.replication.ApiModule",
         "Gerrit-InitStep: com.googlesource.gerrit.plugins.replication.Init",
         "Gerrit-Module: com.googlesource.gerrit.plugins.replication.ReplicationModule",
         "Gerrit-SshModule: com.googlesource.gerrit.plugins.replication.SshModule",
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ApiModule.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ApiModule.java
new file mode 100644
index 0000000..38a249f
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ApiModule.java
@@ -0,0 +1,25 @@
+// 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.replication;
+
+import com.google.gerrit.extensions.registration.DynamicItem;
+import com.google.inject.AbstractModule;
+
+public class ApiModule extends AbstractModule {
+  @Override
+  protected void configure() {
+    DynamicItem.itemOf(binder(), ReplicationConfigOverrides.class);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfigModule.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfigModule.java
index bcea543..93a2b85 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfigModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfigModule.java
@@ -18,7 +18,6 @@
 import static com.googlesource.gerrit.plugins.replication.FileConfigResource.CONFIG_NAME;
 
 import com.google.gerrit.extensions.events.LifecycleListener;
-import com.google.gerrit.extensions.registration.DynamicItem;
 import com.google.gerrit.server.config.SitePaths;
 import com.google.inject.AbstractModule;
 import com.google.inject.Inject;
@@ -47,7 +46,6 @@
   @Override
   protected void configure() {
     bind(ConfigResource.class).to(getConfigResourceClass());
-    DynamicItem.itemOf(binder(), ReplicationConfigOverrides.class);
 
     if (getReplicationConfig().getBoolean("gerrit", "autoReload", false)) {
       bind(ReplicationConfig.class).to(AutoReloadConfigDecorator.class).in(Scopes.SINGLETON);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationExtensionPointModule.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationExtensionPointModule.java
index b92a54a..6b8706f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationExtensionPointModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationExtensionPointModule.java
@@ -27,6 +27,7 @@
 
   @Override
   protected void configure() {
+    install(new ApiModule());
     DynamicItem.itemOf(binder(), ReplicationPushFilter.class);
   }
 }
diff --git a/src/main/resources/Documentation/extension-point.md b/src/main/resources/Documentation/extension-point.md
index f6579fa..106b4bf 100644
--- a/src/main/resources/Documentation/extension-point.md
+++ b/src/main/resources/Documentation/extension-point.md
@@ -52,3 +52,41 @@
   ```java
   DynamicItem.bind(binder(), AdminApiFactory.class).to(AdminApiFactoryImpl.class);
   ```
+
+@PLUGIN@ Cross Plugin Communication
+===================================
+
+The @PLUGIN@ plugin exposes _ApiModule_ that allows to provide _Cross Plugin
+Communication_.
+
+Setup
+-----
+
+Check the [official documentation](https://gerrit-review.googlesource.com/Documentation/dev-plugins.html#_cross_plugin_communication)
+on how to setup your project.
+
+Working with [Extension Points](./extension-point.md)
+-----------------------------------------------------
+
+In order to use both, the _Cross Plugin Communication_ and replication
+_Extension Points_, follow the [Install extension libModule](./extension-point.md#install-extension-libmodule)
+steps and make sure that `replication.jar` is only present in `lib/` directory.
+
+Exposed API
+-----------
+
+* `com.googlesource.gerrit.plugins.replication.ReplicationConfigOverrides`
+
+  Override current replication configuration from external source (eg. git
+  repository, ZooKeeper).
+
+  Replication plugin will still use configuration from `$gerrit_site/etc/`, but
+  with overrides it can be modified dynamically from external source, similarly to
+  how `git config` uses _user_ and _repository_ configuration files.
+
+  Only one override at a time is supported. The implementation needs to bind a
+  `DynamicItem`.
+
+  ```java
+  DynamicItem.bind(binder(), ReplicationConfigOverrides.class).to(ReplicationConfigOverridesImpl.class);
+  ```
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/MergedConfigResourceTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/MergedConfigResourceTest.java
index 80ad449..86da1c0 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/MergedConfigResourceTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/MergedConfigResourceTest.java
@@ -54,9 +54,10 @@
             new AbstractModule() {
               @Override
               protected void configure() {
+                install(new ApiModule());
+
                 bind(ConfigResource.class).to(TestBaseConfigResource.class);
 
-                DynamicItem.itemOf(binder(), ReplicationConfigOverrides.class);
                 if (overrides != null) {
                   DynamicItem.bind(binder(), ReplicationConfigOverrides.class).to(overrides);
                 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java
index 3bc86c7..ba4a958 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java
@@ -49,7 +49,7 @@
 @UseLocalDisk
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationDaemon extends LightweightPluginDaemonTest {
   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
   protected static final Optional<String> ALL_PROJECTS = Optional.empty();
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDistributorIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDistributorIT.java
index dfcf250..dc73036 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDistributorIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDistributorIT.java
@@ -39,7 +39,7 @@
 @UseLocalDisk
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationDistributorIT extends ReplicationStorageDaemon {
   private static final int TEST_DISTRIBUTION_INTERVAL_SECONDS = 3;
   private static final int TEST_DISTRIBUTION_DURATION_SECONDS = 1;
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationEventsIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationEventsIT.java
index b32829c..f31d013 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationEventsIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationEventsIT.java
@@ -58,7 +58,7 @@
 @Sandboxed
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationEventsIT extends ReplicationDaemon {
   private static final Duration TEST_POST_EVENT_TIMEOUT = Duration.ofSeconds(1);
 
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationFanoutIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationFanoutIT.java
index 5f80e8c..0d222b7 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationFanoutIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationFanoutIT.java
@@ -43,7 +43,7 @@
 @UseLocalDisk
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationFanoutIT extends ReplicationDaemon {
   private ReplicationTasksStorage tasksStorage;
 
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
index 9285c58..bdd9d6c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
@@ -52,7 +52,7 @@
 @UseLocalDisk
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationIT extends ReplicationDaemon {
   private static final int TEST_REPLICATION_DELAY = 1;
   private static final int TEST_REPLICATION_RETRY = 1;
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationPushInBatchesIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationPushInBatchesIT.java
index cf8dbe3..67caae9 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationPushInBatchesIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationPushInBatchesIT.java
@@ -29,7 +29,7 @@
 
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationPushInBatchesIT extends ReplicationDaemon {
 
   @Override
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java
index 9390798..60fd708 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java
@@ -49,7 +49,7 @@
 @UseLocalDisk
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationStorageIT extends ReplicationStorageDaemon {
 
   @Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageMPIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageMPIT.java
index 1001e6c..45cf310 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageMPIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageMPIT.java
@@ -34,7 +34,7 @@
 @UseLocalDisk
 @TestPlugin(
     name = "replication",
-    sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule")
+    sysModule = "com.googlesource.gerrit.plugins.replication.TestReplicationModule")
 public class ReplicationStorageMPIT extends ReplicationStorageDaemon {
 
   @Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/TestReplicationModule.java b/src/test/java/com/googlesource/gerrit/plugins/replication/TestReplicationModule.java
new file mode 100644
index 0000000..ae2940e
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/TestReplicationModule.java
@@ -0,0 +1,33 @@
+// 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.replication;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+
+public class TestReplicationModule extends AbstractModule {
+  private final ReplicationModule replicationModule;
+
+  @Inject
+  TestReplicationModule(ReplicationModule replicationModule) {
+    this.replicationModule = replicationModule;
+  }
+
+  @Override
+  protected void configure() {
+    install(new ApiModule());
+    install(replicationModule);
+  }
+}