content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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.alibaba.metrics.jvm;
  
  import com.alibaba.metrics.JmxAttributeGauge;
  import com.alibaba.metrics.Metric;
  import com.alibaba.metrics.MetricName;
  import com.alibaba.metrics.MetricRegistry;
  import com.alibaba.metrics.MetricSet;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import javax.management.JMException;
  import javax.management.MBeanServerConnection;
  import javax.management.ObjectName;
  import java.io.IOException;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * A set of gauges for the count, usage, and capacity of the JVM's direct and mapped buffer pools.
   * <p>
   * These JMX objects are only available on Java 7 and above.
   */
  public class BufferPoolMetricSet implements MetricSet {
      private static final Logger LOGGER = LoggerFactory.getLogger(BufferPoolMetricSet.class);
      private static final String[] ATTRIBUTES = { "Count", "MemoryUsed", "TotalCapacity" };
      private static final String[] NAMES = { "count", "used", "capacity" };
      private static final String[] POOLS = { "direct", "mapped" };
  
      private final MBeanServerConnection mBeanServerConnection;
  
      public BufferPoolMetricSet(MBeanServerConnection mBeanServerConnection) {
          this.mBeanServerConnection = mBeanServerConnection;
      }
  
      @Override
      public Map<MetricName, Metric> getMetrics() {
          final Map<MetricName, Metric> gauges = new HashMap<MetricName, Metric>();
          for (String pool : POOLS) {
              for (int i = 0; i < ATTRIBUTES.length; i++) {
                  final String attribute = ATTRIBUTES[i];
                  final String name = NAMES[i];
                  try {
                      final ObjectName on = new ObjectName("java.nio:type=BufferPool,name=" + pool);
                      mBeanServerConnection.getMBeanInfo(on);
                      gauges.put(MetricRegistry.name(pool, name), new JmxAttributeGauge(mBeanServerConnection, on, attribute));
                  } catch (JMException ignored) {
                      LOGGER.debug("Unable to load buffer pool MBeans, possibly running on Java 6");
                  } catch (IOException ioException) {
                      LOGGER.debug("Unable to load buffer pool MBeans, an exception occur, cause of", ioException);
                  }
              }
          }
          return Collections.unmodifiableMap(gauges);
      }
  
      @Override
      public long lastUpdateTime() {
          return System.currentTimeMillis();
      }
  }
  