View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import java.lang.management.ManagementFactory;
21  
22  import javax.management.InstanceNotFoundException;
23  import javax.management.MBeanServer;
24  import javax.management.MalformedObjectNameException;
25  import javax.management.ObjectName;
26  import javax.management.InstanceAlreadyExistsException;
27  
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  
30  @InterfaceAudience.Private
31  public class MBeanUtil {
32  	
33    /**
34     * Register the MBean using our standard MBeanName format
35     * "hadoop:service=<serviceName>,name=<nameName>"
36     * Where the <serviceName> and <nameName> are the supplied parameters
37     *    
38     * @param serviceName
39     * @param nameName
40     * @param theMbean - the MBean to register
41     * @return the named used to register the MBean
42     */	
43    static public ObjectName registerMBean(final String serviceName, 
44  		  							final String nameName,
45  		  							final Object theMbean) {
46      final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
47      ObjectName name = getMBeanName(serviceName, nameName);
48      try {
49        mbs.registerMBean(theMbean, name);
50        return name;
51      } catch (InstanceAlreadyExistsException ie) {
52        // Ignore if instance already exists 
53      } catch (Exception e) {
54        e.printStackTrace();
55      }
56      return null;
57    }
58    
59    static public void unregisterMBean(ObjectName mbeanName) {
60      final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
61      if (mbeanName == null) 
62          return;
63      try {
64        mbs.unregisterMBean(mbeanName);
65      } catch (InstanceNotFoundException e ) {
66        // ignore
67      } catch (Exception e) {
68        e.printStackTrace();
69      } 
70    }
71    
72    static private ObjectName getMBeanName(final String serviceName,
73  		  								 final String nameName) {
74      ObjectName name = null;
75      try {
76        name = new ObjectName("hadoop:" +
77                    "service=" + serviceName + ",name=" + nameName);
78      } catch (MalformedObjectNameException e) {
79        e.printStackTrace();
80      }
81      return name;
82    }
83  }