Fix findbugs violations in md-sal - part 1
[controller.git] / opendaylight / md-sal / sal-common-util / src / main / java / org / opendaylight / controller / md / sal / common / util / jmx / AbstractMXBean.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.md.sal.common.util.jmx;
10
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;
24
25 /**
26  * Abstract base for an MXBean implementation class.
27  *
28  * <p>
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
32  * releases.
33  *
34  * @author Thomas Pantelis
35  */
36 @Beta
37 public abstract class AbstractMXBean {
38
39     private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
40
41     public static final String BASE_JMX_PREFIX = "org.opendaylight.controller:";
42
43     private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
44
45     private final String beanName;
46     private final String beanType;
47     private final String beanCategory;
48
49     /**
50      * Constructor.
51      *
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.
55      */
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;
61     }
62
63     private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
64         StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
65                 .append("type=").append(getMBeanType());
66
67         if (getMBeanCategory() != null) {
68             builder.append(",Category=").append(getMBeanCategory());
69         }
70
71         builder.append(",name=").append(getMBeanName());
72         return new ObjectName(builder.toString());
73     }
74
75     /**
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.
78      */
79     public void register() {
80         registerMBean();
81     }
82
83     /**
84      * Registers this bean with the platform MBean server with the domain defined by
85      * {@link #BASE_JMX_PREFIX}.
86      *
87      * @return true is successfully registered, false otherwise.
88      */
89     public boolean registerMBean() {
90         boolean registered = false;
91         try {
92             // Object to identify MBean
93             final ObjectName mbeanName = this.getMBeanObjectName();
94
95             LOG.debug("Register MBean {}", mbeanName);
96
97             // unregistered if already registered
98             if (server.isRegistered(mbeanName)) {
99
100                 LOG.debug("MBean {} found to be already registered", mbeanName);
101
102                 try {
103                     unregisterMBean(mbeanName);
104                 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
105                     LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName, e);
106                 }
107             }
108             server.registerMBean(this, mbeanName);
109             registered = true;
110
111             LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
112         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException
113                 | MalformedObjectNameException e) {
114             LOG.error("registration failed {}", e);
115         }
116         return registered;
117     }
118
119     /**
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.
122      */
123     public void unregister() {
124         unregisterMBean();
125     }
126
127     /**
128      * Unregisters this bean with the platform MBean server.
129      *
130      * @return true is successfully unregistered, false otherwise.
131      */
132     public boolean unregisterMBean() {
133         boolean unregister = false;
134         try {
135             ObjectName mbeanName = this.getMBeanObjectName();
136             unregisterMBean(mbeanName);
137             unregister = true;
138         } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
139             LOG.debug("Failed when unregistering MBean {}", e);
140         }
141
142         return unregister;
143     }
144
145     private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
146             InstanceNotFoundException {
147         server.unregisterMBean(mbeanName);
148     }
149
150     /**
151      * Returns the <code>name</code> property of the bean's ObjectName.
152      */
153     public String getMBeanName() {
154         return beanName;
155     }
156
157     /**
158      * Returns the <code>type</code> property of the bean's ObjectName.
159      */
160     public String getMBeanType() {
161         return beanType;
162     }
163
164     /**
165      * Returns the <code>Category</code> property of the bean's ObjectName.
166      */
167     public String getMBeanCategory() {
168         return beanCategory;
169     }
170 }