2 * Copyright (c) 2014 Cisco 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
10 package org.opendaylight.controller.cluster.datastore.jmx.mbeans;
13 import com.google.common.base.Preconditions;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.MBeanRegistrationException;
19 import javax.management.MBeanServer;
20 import javax.management.MalformedObjectNameException;
21 import javax.management.ObjectName;
22 import java.lang.management.ManagementFactory;
25 * All MBeans should extend this class that help in registering and
26 * unregistering the MBeans.
31 public abstract class AbstractBaseMBean {
34 public static String BASE_JMX_PREFIX = "org.opendaylight.controller:";
35 public static String JMX_TYPE_DISTRIBUTED_DATASTORE = "DistributedDatastore";
36 public static String JMX_CATEGORY_SHARD = "Shard";
37 public static String JMX_CATEGORY_SHARD_MANAGER = "ShardManager";
39 private static final Logger LOG = LoggerFactory
40 .getLogger(AbstractBaseMBean.class);
42 MBeanServer server = ManagementFactory.getPlatformMBeanServer();
44 * gets the MBean ObjectName
46 * @return Object name of the MBean
47 * @throws MalformedObjectNameException - The bean name does not have the right format.
48 * @throws NullPointerException - The bean name is null
50 protected ObjectName getMBeanObjectName()
51 throws MalformedObjectNameException, NullPointerException {
52 String name = BASE_JMX_PREFIX + "type="+getMBeanType()+",Category="+
53 getMBeanCategory() + ",name="+
57 return new ObjectName(name);
60 public boolean registerMBean() {
61 boolean registered = false;
63 // Object to identify MBean
64 final ObjectName mbeanName = this.getMBeanObjectName();
66 Preconditions.checkArgument(mbeanName != null,
67 "Object name of the MBean cannot be null");
69 LOG.debug("Register MBean {}", mbeanName);
71 // unregistered if already registered
72 if (server.isRegistered(mbeanName)) {
74 LOG.debug("MBean {} found to be already registered", mbeanName);
77 unregisterMBean(mbeanName);
78 } catch (Exception e) {
80 LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName,
84 server.registerMBean(this, mbeanName);
86 LOG.debug("MBean {} registered successfully",
87 mbeanName.getCanonicalName());
89 } catch (Exception e) {
91 LOG.error("registration failed {}", e);
98 public boolean unregisterMBean() {
99 boolean unregister = false;
101 ObjectName mbeanName = this.getMBeanObjectName();
103 unregisterMBean(mbeanName);
104 } catch (Exception e) {
106 LOG.error("Failed when unregistering MBean {}", e);
111 private void unregisterMBean(ObjectName mbeanName)
112 throws MBeanRegistrationException, InstanceNotFoundException {
114 server.unregisterMBean(mbeanName);
120 * @return name of bean
122 protected abstract String getMBeanName();
125 * @return type of the MBean
127 protected abstract String getMBeanType();
131 * @return Category name of teh bean
133 protected abstract String getMBeanCategory();
135 //require for test cases
136 public MBeanServer getMBeanServer() {