// Copyright (C) 2015 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.cookbook;

import com.google.gerrit.extensions.annotations.Listen;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;

import java.util.concurrent.TimeUnit;

/**
 * Demonstration of how to add a new Dropwizard  Metrics Reporter using
 * Gerrit's plug-in API.
 *
 * @see <a href="https://dropwizard.github.io/metrics/3.1.0/getting-started/#reporting-via-jmx">here</a>
 * @see <a href="https://dropwizard.github.io/metrics/3.1.0/getting-started/#reporting-via-http">here</a>
 * @see <a href="https://dropwizard.github.io/metrics/3.1.0/getting-started/#other-reporting">here</a>
 */
@Listen
@Singleton
public class ConsoleMetricReporter implements LifecycleListener {
  private ConsoleReporter consoleReporter;

  @Inject
  public ConsoleMetricReporter(MetricRegistry registry) {
    this.consoleReporter =
        ConsoleReporter.forRegistry(registry).convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS).build();
  }

  @Override
  public void start() {
    consoleReporter.start(1, TimeUnit.MINUTES);
  }

  @Override
  public void stop() {
    consoleReporter.stop();
  }
}

@@ -748,32 +748,152 @@
 ons.
-events.LifecycleListener
+annotations.PluginName;%0Aimport com.google.gerrit.extensions.events.LifecycleListener;%0Aimport com.google.gerrit.server.config.PluginConfigFactory
 ;%0Aim
@@ -1050,16 +1050,53 @@
 istry;%0A%0A
+import org.eclipse.jgit.lib.Config;%0A%0A
 import j
@@ -1544,16 +1544,251 @@
 ere%3C/a%3E%0A
+ *%0A * Also shows how to fetch plug-in specific configuration values.%0A * %3C@see com.google.gerrit.server.config.PluginConfigFactory%3E%0A *%0A * Enable by adding the file etc/cookbook.config:%0A *%0A *  %5Bconsole-metrics%5D%0A *      enabled = true%0A *%0A
  */%0A@Lis
@@ -1910,16 +1910,43 @@
 eporter;
+%0A  private boolean enabled;
 %0A%0A  @Inj
@@ -2003,19 +2003,276 @@
 registry
-) %7B
+,%0A      PluginConfigFactory configFactory,%0A      @PluginName String pluginName) %7B%0A    Config config = configFactory.getGlobalPluginConfig(pluginName);%0A    enabled = config.getBoolean(%22console-metrics%22, %22enabled%22, false);%0A    if (!enabled) %7B%0A      return;%0A    %7D
 %0A    thi
@@ -2471,24 +2471,45 @@
 d start() %7B%0A
+    if (enabled) %7B%0A  
     consoleR
@@ -2544,16 +2544,22 @@
 NUTES);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -2586,24 +2586,45 @@
 id stop() %7B%0A
+    if (enabled) %7B%0A  
     consoleR
@@ -2635,18 +2635,24 @@
 ter.stop();%0A
+    %7D%0A
   %7D%0A%7D%0A

Introduce example of plugin specific configs