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.InstanceAlreadyExistsException;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.MBeanRegistrationException;
18 import javax.management.MBeanServer;
19 import javax.management.MalformedObjectNameException;
20 import javax.management.NotCompliantMBeanException;
21 import javax.management.ObjectName;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * 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 final String BASE_JMX_PREFIX = "org.opendaylight.controller:";
43 private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
45 private final String beanName;
46 private final String beanType;
47 private final String beanCategory;
52 * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
53 * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
54 * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
56 protected AbstractMXBean(@Nonnull String beanName, @Nonnull String beanType,
57 @Nullable String beanCategory) {
58 this.beanName = beanName;
59 this.beanType = beanType;
60 this.beanCategory = beanCategory;
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 * This method is a wrapper for registerMBean with void return type so it can be invoked by dependency
77 * injection frameworks such as Spring and Blueprint.
79 public void register() {
84 * Registers this bean with the platform MBean server with the domain defined by
85 * {@link #BASE_JMX_PREFIX}.
87 * @return true is successfully registered, false otherwise.
89 public boolean registerMBean() {
90 boolean registered = false;
92 // Object to identify MBean
93 final ObjectName mbeanName = this.getMBeanObjectName();
95 LOG.debug("Register MBean {}", mbeanName);
97 // unregistered if already registered
98 if (server.isRegistered(mbeanName)) {
100 LOG.debug("MBean {} found to be already registered", mbeanName);
103 unregisterMBean(mbeanName);
104 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
105 LOG.warn("unregister mbean {} resulted in exception", mbeanName, e);
108 server.registerMBean(this, mbeanName);
111 LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
112 } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException
113 | MalformedObjectNameException e) {
114 LOG.error("registration failed {}", e);
120 * This method is a wrapper for unregisterMBean with void return type so it can be invoked by dependency
121 * injection frameworks such as Spring and Blueprint.
123 public void unregister() {
128 * Unregisters this bean with the platform MBean server.
130 * @return true is successfully unregistered, false otherwise.
132 public boolean unregisterMBean() {
133 boolean unregister = false;
135 ObjectName mbeanName = this.getMBeanObjectName();
136 unregisterMBean(mbeanName);
138 } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
139 LOG.debug("Failed when unregistering MBean {}", e);
145 private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
146 InstanceNotFoundException {
147 server.unregisterMBean(mbeanName);
151 * Returns the <code>name</code> property of the bean's ObjectName.
153 public String getMBeanName() {
158 * Returns the <code>type</code> property of the bean's ObjectName.
160 public String getMBeanType() {
165 * Returns the <code>Category</code> property of the bean's ObjectName.
167 public String getMBeanCategory() {