// 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.webhooks;

import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class Configuration {
  private static final Logger log = LoggerFactory.getLogger(Configuration.class);

  private static final int DEFAULT_TIMEOUT_MS = 5000;
  private static final int DEFAULT_MAX_TRIES = 5;
  private static final int DEFAULT_RETRY_INTERVAL = 1000;
  private static final int DEFAULT_THREAD_POOL_SIZE = 2;
  private static final boolean DEFAULT_SSL_VERIFY = false;

  private final int connectionTimeout;
  private final int socketTimeout;
  private final int maxTries;
  private final int retryInterval;
  private final int threadPoolSize;
  private final boolean sslVerify;

  @Inject
  protected Configuration(PluginConfigFactory config, @PluginName String pluginName) {
    PluginConfig cfg = config.getFromGerritConfig(pluginName, true);
    connectionTimeout = getInt(cfg, RemoteConfig.CONNECTION_TIMEOUT, DEFAULT_TIMEOUT_MS);
    socketTimeout = getInt(cfg, RemoteConfig.SOCKET_TIMEOUT, DEFAULT_TIMEOUT_MS);
    maxTries = getInt(cfg, RemoteConfig.MAX_TRIES, DEFAULT_MAX_TRIES);
    retryInterval = getInt(cfg, RemoteConfig.RETRY_INTERVAL, DEFAULT_RETRY_INTERVAL);
    threadPoolSize = getInt(cfg, "threadPoolSize", DEFAULT_THREAD_POOL_SIZE);
    sslVerify = getBoolean(cfg, RemoteConfig.SSL_VERIFY, DEFAULT_SSL_VERIFY);
  }

  protected boolean getBoolean(PluginConfig cfg, String name, boolean defaultValue) {
    try {
      return cfg.getBoolean(name, defaultValue);
    } catch (IllegalArgumentException e) {
      logError(name, "boolean", defaultValue, e);
      return defaultValue;
    }
  }

  protected int getInt(PluginConfig cfg, String name, int defaultValue) {
    try {
      return cfg.getInt(name, defaultValue);
    } catch (IllegalArgumentException e) {
      logError(name, "integer", defaultValue, e);
      return defaultValue;
    }
  }

  protected void logError(String name, String type, Object defaultValue, Exception e) {
    log.error("invalid value for{}; using default value {}", name, defaultValue);
    log.debug("Failed retrieve {} value: {}", type, e.getMessage(), e);
  }

  public int getConnectionTimeout() {
    return connectionTimeout;
  }

  public int getMaxTries() {
    return maxTries;
  }

  public int getRetryInterval() {
    return retryInterval;
  }

  public int getSocketTimeout() {
    return socketTimeout;
  }

  public int getThreadPoolSize() {
    return threadPoolSize;
  }

  public boolean getSslVerify() {
    return sslVerify;
  }
}

@@ -899,188 +899,48 @@
 on;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A@Singleton%0Apublic class Configuration %7B%0A  private static final Logger log = LoggerFactory.getLogger(Configuration.class);%0A
+%0A@Singleton%0Apublic class Configuration %7B
 %0A  p
@@ -1612,36 +1612,35 @@
 onTimeout = 
+cfg.
 getInt(
-cfg, 
 RemoteConfig
@@ -1701,28 +1701,27 @@
 meout = 
+cfg.
 getInt(
-cfg, 
 RemoteCo
@@ -1777,28 +1777,27 @@
 Tries = 
+cfg.
 getInt(
-cfg, 
 RemoteCo
@@ -1852,28 +1852,27 @@
 erval = 
+cfg.
 getInt(
-cfg, 
 RemoteCo
@@ -1942,20 +1942,19 @@
 e = 
+cfg.
 getInt(
-cfg, 
 %22thr
@@ -2010,16 +2010,20 @@
 erify = 
+cfg.
 getBoole
@@ -2025,21 +2025,16 @@
 Boolean(
-cfg, 
 RemoteCo
@@ -2080,791 +2080,8 @@
  %7D%0A%0A
-  protected boolean getBoolean(PluginConfig cfg, String name, boolean defaultValue) %7B%0A    try %7B%0A      return cfg.getBoolean(name, defaultValue);%0A    %7D catch (IllegalArgumentException e) %7B%0A      logError(name, %22boolean%22, defaultValue, e);%0A      return defaultValue;%0A    %7D%0A  %7D%0A%0A  protected int getInt(PluginConfig cfg, String name, int defaultValue) %7B%0A    try %7B%0A      return cfg.getInt(name, defaultValue);%0A    %7D catch (IllegalArgumentException e) %7B%0A      logError(name, %22integer%22, defaultValue, e);%0A      return defaultValue;%0A    %7D%0A  %7D%0A%0A  protected void logError(String name, String type, Object defaultValue, Exception e) %7B%0A    log.error(%22invalid value for%7B%7D; using default value %7B%7D%22, name, defaultValue);%0A    log.debug(%22Failed retrieve %7B%7D value: %7B%7D%22, type, e.getMessage(), e);%0A  %7D%0A%0A
   pu

Align handling of configuration error with other plugins