Merge "Be sure to shutdown instance when destroyed"
[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 java.lang.management.ManagementFactory;
12
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;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.annotations.Beta;
25
26 /**
27  * Abstract base for an MXBean implementation class.
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 String BASE_JMX_PREFIX = "org.opendaylight.controller:";
42
43     private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
44
45     private final String mBeanName;
46     private final String mBeanType;
47     private final String mBeanCategory;
48
49     /**
50      * Constructor.
51      *
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.
55      */
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;
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      * Registers this bean with the platform MBean server with the domain defined by
77      * {@link #BASE_JMX_PREFIX}.
78      *
79      * @return true is successfully registered, false otherwise.
80      */
81     public boolean registerMBean() {
82         boolean registered = false;
83         try {
84             // Object to identify MBean
85             final ObjectName mbeanName = this.getMBeanObjectName();
86
87             LOG.debug("Register MBean {}", mbeanName);
88
89             // unregistered if already registered
90             if(server.isRegistered(mbeanName)) {
91
92                 LOG.debug("MBean {} found to be already registered", mbeanName);
93
94                 try {
95                     unregisterMBean(mbeanName);
96                 } catch(Exception e) {
97
98                     LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName, e);
99                 }
100             }
101             server.registerMBean(this, mbeanName);
102             registered = true;
103
104             LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
105         } catch(Exception e) {
106
107             LOG.error("registration failed {}", e);
108
109         }
110         return registered;
111     }
112
113     /**
114      * Unregisters this bean with the platform MBean server.
115      *
116      * @return true is successfully unregistered, false otherwise.
117      */
118     public boolean unregisterMBean() {
119         boolean unregister = false;
120         try {
121             ObjectName mbeanName = this.getMBeanObjectName();
122             unregisterMBean(mbeanName);
123             unregister = true;
124         } catch(Exception e) {
125
126             LOG.error("Failed when unregistering MBean {}", e);
127         }
128
129         return unregister;
130     }
131
132     private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
133             InstanceNotFoundException {
134         server.unregisterMBean(mbeanName);
135     }
136
137     /**
138      * Returns the <code>name</code> property of the bean's ObjectName.
139      */
140     public String getMBeanName() {
141         return mBeanName;
142     }
143
144     /**
145      * Returns the <code>type</code> property of the bean's ObjectName.
146      */
147     public String getMBeanType() {
148         return mBeanType;
149     }
150
151     /**
152      * Returns the <code>Category</code> property of the bean's ObjectName.
153      */
154     public String getMBeanCategory() {
155         return mBeanCategory;
156     }
157 }