Fix logging arguments
[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 package org.opendaylight.controller.md.sal.common.util.jmx;
9
10 import com.google.common.annotations.Beta;
11 import java.lang.management.ManagementFactory;
12 import javax.management.InstanceAlreadyExistsException;
13 import javax.management.InstanceNotFoundException;
14 import javax.management.MBeanRegistrationException;
15 import javax.management.MBeanServer;
16 import javax.management.MalformedObjectNameException;
17 import javax.management.NotCompliantMBeanException;
18 import javax.management.ObjectName;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Abstract base for an MXBean implementation class.
26  *
27  * <p>
28  * This class is not intended for use outside of MD-SAL and its part of private
29  * implementation (still exported as public to be reused across MD-SAL implementation
30  * components) and may be removed in subsequent
31  * releases.
32  *
33  * @author Thomas Pantelis
34  */
35 @Beta
36 public abstract class AbstractMXBean {
37
38     private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
39
40     public static final String BASE_JMX_PREFIX = "org.opendaylight.controller:";
41
42     private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
43
44     private final String beanName;
45     private final String beanType;
46     private final String beanCategory;
47
48     /**
49      * Constructor.
50      *
51      * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
52      * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
53      * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
54      */
55     protected AbstractMXBean(@NonNull String beanName, @NonNull String beanType,
56             @Nullable String beanCategory) {
57         this.beanName = beanName;
58         this.beanType = beanType;
59         this.beanCategory = beanCategory;
60     }
61
62     private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
63         StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
64                 .append("type=").append(getMBeanType());
65
66         if (getMBeanCategory() != null) {
67             builder.append(",Category=").append(getMBeanCategory());
68         }
69
70         builder.append(",name=").append(getMBeanName());
71         return new ObjectName(builder.toString());
72     }
73
74     /**
75      * This method is a wrapper for registerMBean with void return type so it can be invoked by dependency
76      * injection frameworks such as Spring and Blueprint.
77      */
78     public void register() {
79         registerMBean();
80     }
81
82     /**
83      * Registers this bean with the platform MBean server with the domain defined by
84      * {@link #BASE_JMX_PREFIX}.
85      *
86      * @return true is successfully registered, false otherwise.
87      */
88     public boolean registerMBean() {
89         boolean registered = false;
90         try {
91             // Object to identify MBean
92             final ObjectName mbeanName = this.getMBeanObjectName();
93
94             LOG.debug("Register MBean {}", mbeanName);
95
96             // unregistered if already registered
97             if (server.isRegistered(mbeanName)) {
98
99                 LOG.debug("MBean {} found to be already registered", mbeanName);
100
101                 try {
102                     unregisterMBean(mbeanName);
103                 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
104                     LOG.warn("unregister mbean {} resulted in exception", mbeanName, e);
105                 }
106             }
107             server.registerMBean(this, mbeanName);
108             registered = true;
109
110             LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
111         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException
112                 | MalformedObjectNameException e) {
113             LOG.error("registration failed", e);
114         }
115         return registered;
116     }
117
118     /**
119      * This method is a wrapper for unregisterMBean with void return type so it can be invoked by dependency
120      * injection frameworks such as Spring and Blueprint.
121      */
122     public void unregister() {
123         unregisterMBean();
124     }
125
126     /**
127      * Unregisters this bean with the platform MBean server.
128      *
129      * @return true is successfully unregistered, false otherwise.
130      */
131     public boolean unregisterMBean() {
132         boolean unregister = false;
133         try {
134             ObjectName mbeanName = this.getMBeanObjectName();
135             unregisterMBean(mbeanName);
136             unregister = true;
137         } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
138             LOG.debug("Failed when unregistering MBean", e);
139         }
140
141         return unregister;
142     }
143
144     private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
145             InstanceNotFoundException {
146         server.unregisterMBean(mbeanName);
147     }
148
149     /**
150      * Returns the <code>name</code> property of the bean's ObjectName.
151      */
152     public String getMBeanName() {
153         return beanName;
154     }
155
156     /**
157      * Returns the <code>type</code> property of the bean's ObjectName.
158      */
159     public String getMBeanType() {
160         return beanType;
161     }
162
163     /**
164      * Returns the <code>Category</code> property of the bean's ObjectName.
165      */
166     public String getMBeanCategory() {
167         return beanCategory;
168     }
169 }