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     */
018    package org.apache.hadoop.metrics2.util;
019    
020    import java.lang.management.ManagementFactory;
021    
022    import javax.management.InstanceAlreadyExistsException;
023    import javax.management.MBeanServer;
024    import javax.management.ObjectName;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.hadoop.classification.InterfaceAudience;
029    import org.apache.hadoop.classification.InterfaceStability;
030    import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
031    
032    /**
033     * This util class provides a method to register an MBean using
034     * our standard naming convention as described in the doc
035     *  for {link {@link #register(String, String, Object)}
036     */
037    @InterfaceAudience.Public
038    @InterfaceStability.Stable
039    public class MBeans {
040      private static final Log LOG = LogFactory.getLog(MBeans.class);
041    
042      /**
043       * Register the MBean using our standard MBeanName format
044       * "hadoop:service=<serviceName>,name=<nameName>"
045       * Where the <serviceName> and <nameName> are the supplied parameters
046       *
047       * @param serviceName
048       * @param nameName
049       * @param theMbean - the MBean to register
050       * @return the named used to register the MBean
051       */
052      static public ObjectName register(String serviceName, String nameName,
053                                        Object theMbean) {
054        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
055        ObjectName name = getMBeanName(serviceName, nameName);
056        try {
057          mbs.registerMBean(theMbean, name);
058          LOG.debug("Registered "+ name);
059          return name;
060        } catch (InstanceAlreadyExistsException iaee) {
061          if (LOG.isTraceEnabled()) {
062            LOG.trace("Failed to register MBean \""+ name + "\"", iaee);
063          } else {
064            LOG.warn("Failed to register MBean \""+ name
065                + "\": Instance already exists.");
066          }
067        } catch (Exception e) {
068          LOG.warn("Failed to register MBean \""+ name + "\"", e);
069        }
070        return null;
071      }
072    
073      static public void unregister(ObjectName mbeanName) {
074        LOG.debug("Unregistering "+ mbeanName);
075        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
076        if (mbeanName == null) {
077          LOG.debug("Stacktrace: ", new Throwable());
078          return;
079        }
080        try {
081          mbs.unregisterMBean(mbeanName);
082        } catch (Exception e) {
083          LOG.warn("Error unregistering "+ mbeanName, e);
084        }
085      }
086    
087      static private ObjectName getMBeanName(String serviceName, String nameName) {
088        ObjectName name = null;
089        String nameStr = "Hadoop:service="+ serviceName +",name="+ nameName;
090        try {
091          name = DefaultMetricsSystem.newMBeanName(nameStr);
092        } catch (Exception e) {
093          LOG.warn("Error creating MBean object name: "+ nameStr, e);
094        }
095        return name;
096      }
097    }