X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-common-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fcommon%2Futil%2Fjmx%2FAbstractMXBean.java;fp=opendaylight%2Fmd-sal%2Fsal-common-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fcommon%2Futil%2Fjmx%2FAbstractMXBean.java;h=a2db29d1e81c3d3e505a117bb93cefdd27faf9dc;hb=8e5ae982ee8db5775a1b59356aac84221cb6ba7b;hp=0000000000000000000000000000000000000000;hpb=7a6d33e8c7c4aac8a9667b0f998401dc7b4f108e;p=mdsal.git diff --git a/opendaylight/md-sal/sal-common-util/src/main/java/org/opendaylight/controller/md/sal/common/util/jmx/AbstractMXBean.java b/opendaylight/md-sal/sal-common-util/src/main/java/org/opendaylight/controller/md/sal/common/util/jmx/AbstractMXBean.java new file mode 100644 index 0000000000..a2db29d1e8 --- /dev/null +++ b/opendaylight/md-sal/sal-common-util/src/main/java/org/opendaylight/controller/md/sal/common/util/jmx/AbstractMXBean.java @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.md.sal.common.util.jmx; + +import java.lang.management.ManagementFactory; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.management.InstanceNotFoundException; +import javax.management.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.Beta; + +/** + * Abstract base for an MXBean implementation class. + *

+ * This class is not intended for use outside of MD-SAL and its part of private + * implementation (still exported as public to be reused across MD-SAL implementation + * components) and may be removed in subsequent + * releases. + * + * @author Thomas Pantelis + */ +@Beta +public abstract class AbstractMXBean { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class); + + public static String BASE_JMX_PREFIX = "org.opendaylight.controller:"; + + private final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + + private final String mBeanName; + private final String mBeanType; + private final String mBeanCategory; + + /** + * Constructor. + * + * @param mBeanName Used as the name property in the bean's ObjectName. + * @param mBeanType Used as the type property in the bean's ObjectName. + * @param mBeanCategory Used as the Category property in the bean's ObjectName. + */ + protected AbstractMXBean(@Nonnull String mBeanName, @Nonnull String mBeanType, + @Nullable String mBeanCategory) { + this.mBeanName = mBeanName; + this.mBeanType = mBeanType; + this.mBeanCategory = mBeanCategory; + } + + private ObjectName getMBeanObjectName() throws MalformedObjectNameException { + StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX) + .append("type=").append(getMBeanType()); + + if(getMBeanCategory() != null) { + builder.append(",Category=").append(getMBeanCategory()); + } + + builder.append(",name=").append(getMBeanName()); + return new ObjectName(builder.toString()); + } + + /** + * Registers this bean with the platform MBean server with the domain defined by + * {@link #BASE_JMX_PREFIX}. + * + * @return true is successfully registered, false otherwise. + */ + public boolean registerMBean() { + boolean registered = false; + try { + // Object to identify MBean + final ObjectName mbeanName = this.getMBeanObjectName(); + + LOG.debug("Register MBean {}", mbeanName); + + // unregistered if already registered + if(server.isRegistered(mbeanName)) { + + LOG.debug("MBean {} found to be already registered", mbeanName); + + try { + unregisterMBean(mbeanName); + } catch(Exception e) { + + LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName, e); + } + } + server.registerMBean(this, mbeanName); + registered = true; + + LOG.debug("MBean {} registered successfully", mbeanName.getCanonicalName()); + } catch(Exception e) { + + LOG.error("registration failed {}", e); + + } + return registered; + } + + /** + * Unregisters this bean with the platform MBean server. + * + * @return true is successfully unregistered, false otherwise. + */ + public boolean unregisterMBean() { + boolean unregister = false; + try { + ObjectName mbeanName = this.getMBeanObjectName(); + unregisterMBean(mbeanName); + unregister = true; + } catch(Exception e) { + + LOG.error("Failed when unregistering MBean {}", e); + } + + return unregister; + } + + private void unregisterMBean(ObjectName mbeanName) throws MBeanRegistrationException, + InstanceNotFoundException { + server.unregisterMBean(mbeanName); + } + + /** + * Returns the name property of the bean's ObjectName. + */ + public String getMBeanName() { + return mBeanName; + } + + /** + * Returns the type property of the bean's ObjectName. + */ + public String getMBeanType() { + return mBeanType; + } + + /** + * Returns the Category property of the bean's ObjectName. + */ + public String getMBeanCategory() { + return mBeanCategory; + } +}