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
8 package org.opendaylight.controller.md.sal.common.util.jmx;
10 import java.lang.management.ManagementFactory;
11 import javax.management.InstanceAlreadyExistsException;
12 import javax.management.InstanceNotFoundException;
13 import javax.management.MBeanRegistrationException;
14 import javax.management.MBeanServer;
15 import javax.management.MalformedObjectNameException;
16 import javax.management.NotCompliantMBeanException;
17 import javax.management.ObjectName;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Abstract base for an MXBean implementation class.
27 * This class is not intended for use outside of MD-SAL and its part of private
28 * implementation (still exported as public to be reused across MD-SAL implementation
29 * components) and may be removed in subsequent
32 * @author Thomas Pantelis
34 public abstract class AbstractMXBean {
35 private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
37 public static final String BASE_JMX_PREFIX = "org.opendaylight.controller:";
39 private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
41 private final String beanName;
42 private final String beanType;
43 private final String beanCategory;
48 * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
49 * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
50 * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
52 protected AbstractMXBean(@NonNull String beanName, @NonNull String beanType,
53 @Nullable String beanCategory) {
54 this.beanName = beanName;
55 this.beanType = beanType;
56 this.beanCategory = beanCategory;
59 private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
60 StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
61 .append("type=").append(getMBeanType());
63 if (getMBeanCategory() != null) {
64 builder.append(",Category=").append(getMBeanCategory());
67 builder.append(",name=").append(getMBeanName());
68 return new ObjectName(builder.toString());
72 * This method is a wrapper for registerMBean with void return type so it can be invoked by dependency
73 * injection frameworks such as Spring and Blueprint.
75 public void register() {
80 * Registers this bean with the platform MBean server with the domain defined by
81 * {@link #BASE_JMX_PREFIX}.
83 * @return true is successfully registered, false otherwise.
85 public boolean registerMBean() {
86 boolean registered = false;
88 // Object to identify MBean
89 final ObjectName mbeanName = getMBeanObjectName();
91 LOG.debug("Register MBean {}", mbeanName);
93 // unregistered if already registered
94 if (server.isRegistered(mbeanName)) {
96 LOG.debug("MBean {} found to be already registered", mbeanName);
99 unregisterMBean(mbeanName);
100 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
101 LOG.warn("unregister mbean {} resulted in exception", mbeanName, e);
104 server.registerMBean(this, mbeanName);
107 LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
108 } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException
109 | MalformedObjectNameException e) {
110 LOG.error("registration failed", e);
116 * This method is a wrapper for unregisterMBean with void return type so it can be invoked by dependency
117 * injection frameworks such as Spring and Blueprint.
119 public void unregister() {
124 * Unregisters this bean with the platform MBean server.
126 * @return true is successfully unregistered, false otherwise.
128 public boolean unregisterMBean() {
130 unregisterMBean(getMBeanObjectName());
132 } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
133 LOG.debug("Failed when unregistering MBean", e);
138 private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
139 InstanceNotFoundException {
140 server.unregisterMBean(mbeanName);
144 * Returns the <code>name</code> property of the bean's ObjectName.
146 public String getMBeanName() {
151 * Returns the <code>type</code> property of the bean's ObjectName.
153 public String getMBeanType() {
158 * Returns the <code>Category</code> property of the bean's ObjectName.
160 public String getMBeanCategory() {