Extract configuration handling out to a separate class
So it can be injected into other classes.
Change-Id: Ib3b89319e2f710199f8e2767a3aa51e8282bf84c
diff --git a/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnly.java b/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnly.java
index d9482f1..8abe38f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnly.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnly.java
@@ -14,12 +14,9 @@
package com.googlesource.gerrit.plugins.readonly;
-import static com.google.common.base.MoreObjects.firstNonNull;
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
-import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.httpd.AllRequestFilter;
-import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.gerrit.server.events.CommitReceivedEvent;
import com.google.gerrit.server.git.validators.CommitValidationException;
import com.google.gerrit.server.git.validators.CommitValidationListener;
@@ -34,26 +31,20 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.eclipse.jgit.lib.Config;
@Singleton
class ReadOnly extends AllRequestFilter implements CommitValidationListener {
- private static final String MESSAGE_KEY = "message";
- private static final String DEFAULT_MESSAGE =
- "Gerrit is under maintenance - all data is READ ONLY";
-
- private final String message;
+ private final ReadOnlyConfig config;
@Inject
- ReadOnly(PluginConfigFactory pluginConfigFactory, @PluginName String pluginName) {
- Config cfg = pluginConfigFactory.getGlobalPluginConfig(pluginName);
- this.message = firstNonNull(cfg.getString(pluginName, null, MESSAGE_KEY), DEFAULT_MESSAGE);
+ ReadOnly(ReadOnlyConfig config) {
+ this.config = config;
}
@Override
public List<CommitValidationMessage> onCommitReceived(CommitReceivedEvent receiveEvent)
throws CommitValidationException {
- throw new CommitValidationException(message);
+ throw new CommitValidationException(config.message());
}
@Override
@@ -62,7 +53,7 @@
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
String method = ((HttpServletRequest) request).getMethod();
if (method == "POST" || method == "PUT" || method == "DELETE") {
- ((HttpServletResponse) response).sendError(SC_SERVICE_UNAVAILABLE, message);
+ ((HttpServletResponse) response).sendError(SC_SERVICE_UNAVAILABLE, config.message());
return;
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnlyConfig.java b/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnlyConfig.java
new file mode 100644
index 0000000..718a894
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/readonly/ReadOnlyConfig.java
@@ -0,0 +1,42 @@
+// Copyright (C) 2018 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.readonly;
+
+import static com.google.common.base.MoreObjects.firstNonNull;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import org.eclipse.jgit.lib.Config;
+
+@Singleton
+class ReadOnlyConfig {
+ private static final String MESSAGE_KEY = "message";
+ private static final String DEFAULT_MESSAGE =
+ "Gerrit is under maintenance - all data is READ ONLY";
+
+ private final String message;
+
+ @Inject
+ ReadOnlyConfig(PluginConfigFactory pluginConfigFactory, @PluginName String pluginName) {
+ Config cfg = pluginConfigFactory.getGlobalPluginConfig(pluginName);
+ this.message = firstNonNull(cfg.getString(pluginName, null, MESSAGE_KEY), DEFAULT_MESSAGE);
+ }
+
+ String message() {
+ return message;
+ }
+}