001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.metrics2.util;
019
020import java.lang.management.ManagementFactory;
021import javax.management.MBeanServer;
022import javax.management.ObjectName;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.apache.hadoop.classification.InterfaceAudience;
027import org.apache.hadoop.classification.InterfaceStability;
028import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
029
030/**
031 * This util class provides a method to register an MBean using
032 * our standard naming convention as described in the doc
033 *  for {link {@link #register(String, String, Object)}
034 */
035@InterfaceAudience.Public
036@InterfaceStability.Stable
037public class MBeans {
038  private static final Log LOG = LogFactory.getLog(MBeans.class);
039
040  /**
041   * Register the MBean using our standard MBeanName format
042   * "hadoop:service=<serviceName>,name=<nameName>"
043   * Where the <serviceName> and <nameName> are the supplied parameters
044   *
045   * @param serviceName
046   * @param nameName
047   * @param theMbean - the MBean to register
048   * @return the named used to register the MBean
049   */
050  static public ObjectName register(String serviceName, String nameName,
051                                    Object theMbean) {
052    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
053    ObjectName name = getMBeanName(serviceName, nameName);
054    try {
055      mbs.registerMBean(theMbean, name);
056      LOG.debug("Registered "+ name);
057      return name;
058    } catch (Exception e) {
059      LOG.warn("Error registering "+ name, e);
060    }
061    return null;
062  }
063
064  static public void unregister(ObjectName mbeanName) {
065    LOG.debug("Unregistering "+ mbeanName);
066    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
067    if (mbeanName == null) {
068      LOG.debug("Stacktrace: ", new Throwable());
069      return;
070    }
071    try {
072      mbs.unregisterMBean(mbeanName);
073    } catch (Exception e) {
074      LOG.warn("Error unregistering "+ mbeanName, e);
075    }
076  }
077
078  static private ObjectName getMBeanName(String serviceName, String nameName) {
079    ObjectName name = null;
080    String nameStr = "Hadoop:service="+ serviceName +",name="+ nameName;
081    try {
082      name = DefaultMetricsSystem.newMBeanName(nameStr);
083    } catch (Exception e) {
084      LOG.warn("Error creating MBean object name: "+ nameStr, e);
085    }
086    return name;
087  }
088}