[120] | 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.metrics.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 | |
---|
| 29 | /** |
---|
| 30 | * This util class provides a method to register an MBean using |
---|
| 31 | * our standard naming convention as described in the doc |
---|
| 32 | * for {link {@link #registerMBean(String, String, Object)} |
---|
| 33 | * |
---|
| 34 | */ |
---|
| 35 | public class MBeanUtil { |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Register the MBean using our standard MBeanName format |
---|
| 39 | * "hadoop:service=<serviceName>,name=<nameName>" |
---|
| 40 | * Where the <serviceName> and <nameName> are the supplied parameters |
---|
| 41 | * |
---|
| 42 | * @param serviceName |
---|
| 43 | * @param nameName |
---|
| 44 | * @param theMbean - the MBean to register |
---|
| 45 | * @return the named used to register the MBean |
---|
| 46 | */ |
---|
| 47 | static public ObjectName registerMBean(final String serviceName, |
---|
| 48 | final String nameName, |
---|
| 49 | final Object theMbean) { |
---|
| 50 | final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
---|
| 51 | ObjectName name = getMBeanName(serviceName, nameName); |
---|
| 52 | try { |
---|
| 53 | mbs.registerMBean(theMbean, name); |
---|
| 54 | return name; |
---|
| 55 | } catch (InstanceAlreadyExistsException ie) { |
---|
| 56 | // Ignore if instance already exists |
---|
| 57 | } catch (Exception e) { |
---|
| 58 | e.printStackTrace(); |
---|
| 59 | } |
---|
| 60 | return null; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | static public void unregisterMBean(ObjectName mbeanName) { |
---|
| 64 | final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
---|
| 65 | if (mbeanName == null) |
---|
| 66 | return; |
---|
| 67 | try { |
---|
| 68 | mbs.unregisterMBean(mbeanName); |
---|
| 69 | } catch (InstanceNotFoundException e ) { |
---|
| 70 | // ignore |
---|
| 71 | } catch (Exception e) { |
---|
| 72 | e.printStackTrace(); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static private ObjectName getMBeanName(final String serviceName, |
---|
| 77 | final String nameName) { |
---|
| 78 | ObjectName name = null; |
---|
| 79 | try { |
---|
| 80 | name = new ObjectName("hadoop:" + |
---|
| 81 | "service=" + serviceName + ",name=" + nameName); |
---|
| 82 | } catch (MalformedObjectNameException e) { |
---|
| 83 | e.printStackTrace(); |
---|
| 84 | } |
---|
| 85 | return name; |
---|
| 86 | } |
---|
| 87 | } |
---|