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 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;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.annotations.Beta;
27 * Abstract base for an MXBean implementation class.
29 * This class is not intended for use outside of MD-SAL and its part of private
30 * implementation (still exported as public to be reused across MD-SAL implementation
31 * components) and may be removed in subsequent
34 * @author Thomas Pantelis
37 public abstract class AbstractMXBean {
39 private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
41 public static String BASE_JMX_PREFIX = "org.opendaylight.controller:";
43 private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
45 private final String mBeanName;
46 private final String mBeanType;
47 private final String mBeanCategory;
52 * @param mBeanName Used as the <code>name</code> property in the bean's ObjectName.
53 * @param mBeanType Used as the <code>type</code> property in the bean's ObjectName.
54 * @param mBeanCategory Used as the <code>Category</code> property in the bean's ObjectName.
56 protected AbstractMXBean(@Nonnull String mBeanName, @Nonnull String mBeanType,
57 @Nullable String mBeanCategory) {
58 this.mBeanName = mBeanName;
59 this.mBeanType = mBeanType;
60 this.mBeanCategory = mBeanCategory;
63 private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
64 StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
65 .append("type=").append(getMBeanType());
67 if(getMBeanCategory() != null) {
68 builder.append(",Category=").append(getMBeanCategory());
71 builder.append(",name=").append(getMBeanName());
72 return new ObjectName(builder.toString());
76 * Registers this bean with the platform MBean server with the domain defined by
77 * {@link #BASE_JMX_PREFIX}.
79 * @return true is successfully registered, false otherwise.
81 public boolean registerMBean() {
82 boolean registered = false;
84 // Object to identify MBean
85 final ObjectName mbeanName = this.getMBeanObjectName();
87 LOG.debug("Register MBean {}", mbeanName);
89 // unregistered if already registered
90 if(server.isRegistered(mbeanName)) {
92 LOG.debug("MBean {} found to be already registered", mbeanName);
95 unregisterMBean(mbeanName);
96 } catch(Exception e) {
98 LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName, e);
101 server.registerMBean(this, mbeanName);
104 LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
105 } catch(Exception e) {
107 LOG.error("registration failed {}", e);
114 * Unregisters this bean with the platform MBean server.
116 * @return true is successfully unregistered, false otherwise.
118 public boolean unregisterMBean() {
119 boolean unregister = false;
121 ObjectName mbeanName = this.getMBeanObjectName();
122 unregisterMBean(mbeanName);
124 } catch(Exception e) {
126 LOG.error("Failed when unregistering MBean {}", e);
132 private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
133 InstanceNotFoundException {
134 server.unregisterMBean(mbeanName);
138 * Returns the <code>name</code> property of the bean's ObjectName.
140 public String getMBeanName() {
145 * Returns the <code>type</code> property of the bean's ObjectName.
147 public String getMBeanType() {
152 * Returns the <code>Category</code> property of the bean's ObjectName.
154 public String getMBeanCategory() {
155 return mBeanCategory;