2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.md.sal.common.util.jmx;
11 import com.google.common.annotations.Beta;
12 import java.lang.management.ManagementFactory;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanRegistrationException;
17 import javax.management.MBeanServer;
18 import javax.management.MalformedObjectNameException;
19 import javax.management.ObjectName;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Abstract base for an MXBean implementation class.
26 * This class is not intended for use outside of MD-SAL and its part of private
27 * implementation (still exported as public to be reused across MD-SAL implementation
28 * components) and may be removed in subsequent
31 * @author Thomas Pantelis
34 public abstract class AbstractMXBean {
36 private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
38 public static String BASE_JMX_PREFIX = "org.opendaylight.controller:";
40 private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
42 private final String mBeanName;
43 private final String mBeanType;
44 private final String mBeanCategory;
49 * @param mBeanName Used as the <code>name</code> property in the bean's ObjectName.
50 * @param mBeanType Used as the <code>type</code> property in the bean's ObjectName.
51 * @param mBeanCategory Used as the <code>Category</code> property in the bean's ObjectName.
53 protected AbstractMXBean(@Nonnull String mBeanName, @Nonnull String mBeanType,
54 @Nullable String mBeanCategory) {
55 this.mBeanName = mBeanName;
56 this.mBeanType = mBeanType;
57 this.mBeanCategory = mBeanCategory;
60 private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
61 StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
62 .append("type=").append(getMBeanType());
64 if(getMBeanCategory() != null) {
65 builder.append(",Category=").append(getMBeanCategory());
68 builder.append(",name=").append(getMBeanName());
69 return new ObjectName(builder.toString());
73 * Registers this bean with the platform MBean server with the domain defined by
74 * {@link #BASE_JMX_PREFIX}.
76 * @return true is successfully registered, false otherwise.
78 public boolean registerMBean() {
79 boolean registered = false;
81 // Object to identify MBean
82 final ObjectName mbeanName = this.getMBeanObjectName();
84 LOG.debug("Register MBean {}", mbeanName);
86 // unregistered if already registered
87 if(server.isRegistered(mbeanName)) {
89 LOG.debug("MBean {} found to be already registered", mbeanName);
92 unregisterMBean(mbeanName);
93 } catch(Exception e) {
95 LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName, e);
98 server.registerMBean(this, mbeanName);
101 LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
102 } catch(Exception e) {
104 LOG.error("registration failed {}", e);
111 * Unregisters this bean with the platform MBean server.
113 * @return true is successfully unregistered, false otherwise.
115 public boolean unregisterMBean() {
116 boolean unregister = false;
118 ObjectName mbeanName = this.getMBeanObjectName();
119 unregisterMBean(mbeanName);
121 } catch(Exception e) {
122 LOG.debug("Failed when unregistering MBean {}", e);
128 private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
129 InstanceNotFoundException {
130 server.unregisterMBean(mbeanName);
134 * Returns the <code>name</code> property of the bean's ObjectName.
136 public String getMBeanName() {
141 * Returns the <code>type</code> property of the bean's ObjectName.
143 public String getMBeanType() {
148 * Returns the <code>Category</code> property of the bean's ObjectName.
150 public String getMBeanCategory() {
151 return mBeanCategory;