Refactor limit exceeded messaging

* encapsulate the messaging functionality
* make the configurability more transparent
* dispose of static imports

Change-Id: Ib0bd927b66e40abf15e8b3aa381e565fff765a81
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java b/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java
index f2e7ebc..59c36ed 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java
@@ -22,6 +22,7 @@
 import com.google.common.base.Optional;
 import com.google.common.cache.CacheLoader;
 import com.google.common.util.concurrent.RateLimiter;
+import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.extensions.events.GarbageCollectorListener;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.extensions.events.ProjectDeletedListener;
@@ -32,6 +33,8 @@
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.IdentifiedUser.GenericFactory;
 import com.google.gerrit.server.cache.CacheModule;
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.gerrit.server.git.ReceivePackInitializer;
 import com.google.gerrit.server.git.validators.UploadValidationListener;
 import com.google.gerrit.server.group.SystemGroupBackend;
@@ -39,6 +42,7 @@
 import com.google.inject.Inject;
 import com.google.inject.Scopes;
 import com.google.inject.internal.UniqueAnnotations;
+import com.google.inject.name.Names;
 import com.googlesource.gerrit.plugins.quota.AccountLimitsConfig.RateLimit;
 import org.eclipse.jgit.transport.PostReceiveHook;
 
@@ -46,6 +50,16 @@
   static final String CACHE_NAME_ACCOUNTID = "rate_limits_by_account";
   static final String CACHE_NAME_REMOTEHOST = "rate_limits_by_ip";
 
+  private final String uploadpackLimitExceededMsg;
+
+  @Inject
+  Module(PluginConfigFactory plugincf, @PluginName String pluginName) {
+    PluginConfig pc = plugincf.getFromGerritConfig(pluginName);
+    uploadpackLimitExceededMsg =
+        new RateMsgHelper(pc.getString(RateMsgHelper.UPLOADPACK_CONFIGURABLE_MSG_ANNOTATION))
+            .getMessageFormatMsg();
+  }
+
   @Override
   protected void configure() {
     DynamicSet.bind(binder(), ProjectCreationValidationListener.class)
@@ -75,6 +89,9 @@
     DynamicSet.bind(binder(), UploadValidationListener.class).to(RateLimitUploadListener.class);
     cache(CACHE_NAME_ACCOUNTID, Account.Id.class, Holder.class).loader(LoaderAccountId.class);
     cache(CACHE_NAME_REMOTEHOST, String.class, Holder.class).loader(LoaderRemoteHost.class);
+    bindConstant()
+        .annotatedWith(Names.named(RateMsgHelper.UPLOADPACK_CONFIGURABLE_MSG_ANNOTATION))
+        .to(uploadpackLimitExceededMsg);
   }
 
   static class Holder {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/RateLimitUploadListener.java b/src/main/java/com/googlesource/gerrit/plugins/quota/RateLimitUploadListener.java
index 3e0ad11..44cfd21 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/RateLimitUploadListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/RateLimitUploadListener.java
@@ -14,17 +14,12 @@
 
 package com.googlesource.gerrit.plugins.quota;
 
-import static com.googlesource.gerrit.plugins.quota.Module.CACHE_NAME_ACCOUNTID;
-import static com.googlesource.gerrit.plugins.quota.Module.CACHE_NAME_REMOTEHOST;
-
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.LoadingCache;
 import com.google.common.util.concurrent.RateLimiter;
-import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.reviewdb.client.Account;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.CurrentUser;
-import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.gerrit.server.git.validators.UploadValidationListener;
 import com.google.gerrit.server.validators.ValidationException;
 import com.google.inject.Inject;
@@ -48,9 +43,6 @@
   private static final Logger log = LoggerFactory.getLogger(RateLimitUploadListener.class);
   private static final Method createStopwatchMethod;
   private static final Constructor<?> constructor;
-  private static final String RATE_LIMIT_TOKEN = "${rateLimit}";
-  private static final String DEFAULT_RATE_LIMIT_EXCEEDED_MSG =
-      "Exceeded rate limit of " + RATE_LIMIT_TOKEN + " fetch requests/hour";
 
   static {
     try {
@@ -115,17 +107,13 @@
   @Inject
   RateLimitUploadListener(
       Provider<CurrentUser> user,
-      @Named(CACHE_NAME_ACCOUNTID) LoadingCache<Account.Id, Holder> limitsPerAccount,
-      @Named(CACHE_NAME_REMOTEHOST) LoadingCache<String, Holder> limitsPerRemoteHost,
-      PluginConfigFactory cfg,
-      @PluginName String pluginName) {
+      @Named(Module.CACHE_NAME_ACCOUNTID) LoadingCache<Account.Id, Holder> limitsPerAccount,
+      @Named(Module.CACHE_NAME_REMOTEHOST) LoadingCache<String, Holder> limitsPerRemoteHost,
+      @Named(RateMsgHelper.UPLOADPACK_CONFIGURABLE_MSG_ANNOTATION) String limitExceededMsg) {
     this.user = user;
     this.limitsPerAccount = limitsPerAccount;
     this.limitsPerRemoteHost = limitsPerRemoteHost;
-    String msg =
-        cfg.getFromGerritConfig(pluginName)
-            .getString("uploadpackLimitExceededMsg", DEFAULT_RATE_LIMIT_EXCEEDED_MSG);
-    limitExceededMsg = msg.replace(RATE_LIMIT_TOKEN, "{0,number,##.##}");
+    this.limitExceededMsg = limitExceededMsg;
   }
 
   @Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/RateMsgHelper.java b/src/main/java/com/googlesource/gerrit/plugins/quota/RateMsgHelper.java
new file mode 100644
index 0000000..550ca33
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/RateMsgHelper.java
@@ -0,0 +1,34 @@
+// Copyright (C) 2017 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.quota;
+
+public class RateMsgHelper {
+  static final String UPLOADPACK_CONFIGURABLE_MSG_ANNOTATION = "uploadpackLimitExceededMsg";
+  private static final String RATE_LIMIT_TOKEN = "${rateLimit}";
+  private static final String RATE_LIMIT_FORMAT_DOUBLE = "{0,number,##.##}";
+  private static final String UPLOADPACK_DEFAULT_TEMPLATE_MSG =
+      "Exceeded rate limit of " + RATE_LIMIT_TOKEN + " fetch requests/hour";
+
+  private String messageFormatMsg;
+
+  public RateMsgHelper(String templateMsg) {
+    messageFormatMsg = templateMsg == null ? UPLOADPACK_DEFAULT_TEMPLATE_MSG : templateMsg;
+    messageFormatMsg = messageFormatMsg.replace(RATE_LIMIT_TOKEN, RATE_LIMIT_FORMAT_DOUBLE);
+  }
+
+  public String getMessageFormatMsg() {
+    return messageFormatMsg;
+  }
+}