atomic-storage: remove type dependency at segment level I/O
[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 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;
22
23 /**
24  * Abstract base for an MXBean implementation class.
25  *
26  * <p>
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
30  * releases.
31  *
32  * @author Thomas Pantelis
33  */
34 public abstract class AbstractMXBean {
35     private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
36
37     public static final String BASE_JMX_PREFIX = "org.opendaylight.controller:";
38
39     private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
40
41     private final String beanName;
42     private final String beanType;
43     private final String beanCategory;
44
45     /**
46      * Constructor.
47      *
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.
51      */
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;
57     }
58
59     private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
60         StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
61                 .append("type=").append(getMBeanType());
62
63         if (getMBeanCategory() != null) {
64             builder.append(",Category=").append(getMBeanCategory());
65         }
66
67         builder.append(",name=").append(getMBeanName());
68         return new ObjectName(builder.toString());
69     }
70
71     /**
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.
74      */
75     public void register() {
76         registerMBean();
77     }
78
79     /**
80      * Registers this bean with the platform MBean server with the domain defined by
81      * {@link #BASE_JMX_PREFIX}.
82      *
83      * @return true is successfully registered, false otherwise.
84      */
85     public boolean registerMBean() {
86         boolean registered = false;
87         try {
88             // Object to identify MBean
89             final ObjectName mbeanName = getMBeanObjectName();
90
91             LOG.debug("Register MBean {}", mbeanName);
92
93             // unregistered if already registered
94             if (server.isRegistered(mbeanName)) {
95
96                 LOG.debug("MBean {} found to be already registered", mbeanName);
97
98                 try {
99                     unregisterMBean(mbeanName);
100                 } catch (MBeanRegistrationException | InstanceNotFoundException e) {
101                     LOG.warn("unregister mbean {} resulted in exception", mbeanName, e);
102                 }
103             }
104             server.registerMBean(this, mbeanName);
105             registered = true;
106
107             LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName());
108         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException
109                 | MalformedObjectNameException e) {
110             LOG.error("registration failed", e);
111         }
112         return registered;
113     }
114
115     /**
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.
118      */
119     public void unregister() {
120         unregisterMBean();
121     }
122
123     /**
124      * Unregisters this bean with the platform MBean server.
125      *
126      * @return true is successfully unregistered, false otherwise.
127      */
128     public boolean unregisterMBean() {
129         try {
130             unregisterMBean(getMBeanObjectName());
131             return true;
132         } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
133             LOG.debug("Failed when unregistering MBean", e);
134             return false;
135         }
136     }
137
138     private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException,
139             InstanceNotFoundException {
140         server.unregisterMBean(mbeanName);
141     }
142
143     /**
144      * Returns the <code>name</code> property of the bean's ObjectName.
145      */
146     public String getMBeanName() {
147         return beanName;
148     }
149
150     /**
151      * Returns the <code>type</code> property of the bean's ObjectName.
152      */
153     public String getMBeanType() {
154         return beanType;
155     }
156
157     /**
158      * Returns the <code>Category</code> property of the bean's ObjectName.
159      */
160     public String getMBeanCategory() {
161         return beanCategory;
162     }
163 }