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;h=0ce05ebd1e315b05ed91c521b2a7569809fa12eb;hb=refs%2Fchanges%2F11%2F80211%2F6;hp=c24c8dc068d19015fbed686bb321831bd056ccfb;hpb=769ef0f950f2ed6cfc14d274e6a8edc583a36a96;p=controller.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 index c24c8dc068..0ce05ebd1e 100644 --- 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 @@ -5,23 +5,25 @@ * 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 com.google.common.annotations.Beta; import java.lang.management.ManagementFactory; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 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
@@ -35,33 +37,33 @@ public abstract class AbstractMXBean {
private static final Logger LOG = LoggerFactory.getLogger(AbstractMXBean.class);
- public static String BASE_JMX_PREFIX = "org.opendaylight.controller:";
+ public static final 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;
+ private final String beanName;
+ private final String beanType;
+ private final String beanCategory;
/**
* 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.
+ * @param beanName Used as the name
property in the bean's ObjectName.
+ * @param beanType Used as the type
property in the bean's ObjectName.
+ * @param beanCategory 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;
+ protected AbstractMXBean(@NonNull String beanName, @NonNull String beanType,
+ @Nullable String beanCategory) {
+ this.beanName = beanName;
+ this.beanType = beanType;
+ this.beanCategory = beanCategory;
}
private ObjectName getMBeanObjectName() throws MalformedObjectNameException {
StringBuilder builder = new StringBuilder(BASE_JMX_PREFIX)
.append("type=").append(getMBeanType());
- if(getMBeanCategory() != null) {
+ if (getMBeanCategory() != null) {
builder.append(",Category=").append(getMBeanCategory());
}
@@ -69,6 +71,14 @@ public abstract class AbstractMXBean {
return new ObjectName(builder.toString());
}
+ /**
+ * This method is a wrapper for registerMBean with void return type so it can be invoked by dependency
+ * injection frameworks such as Spring and Blueprint.
+ */
+ public void register() {
+ registerMBean();
+ }
+
/**
* Registers this bean with the platform MBean server with the domain defined by
* {@link #BASE_JMX_PREFIX}.
@@ -84,29 +94,35 @@ public abstract class AbstractMXBean {
LOG.debug("Register MBean {}", mbeanName);
// unregistered if already registered
- if(server.isRegistered(mbeanName)) {
+ 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);
+ } catch (MBeanRegistrationException | InstanceNotFoundException 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) {
-
+ } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException
+ | MalformedObjectNameException e) {
LOG.error("registration failed {}", e);
-
}
return registered;
}
+ /**
+ * This method is a wrapper for unregisterMBean with void return type so it can be invoked by dependency
+ * injection frameworks such as Spring and Blueprint.
+ */
+ public void unregister() {
+ unregisterMBean();
+ }
+
/**
* Unregisters this bean with the platform MBean server.
*
@@ -118,7 +134,7 @@ public abstract class AbstractMXBean {
ObjectName mbeanName = this.getMBeanObjectName();
unregisterMBean(mbeanName);
unregister = true;
- } catch(Exception e) {
+ } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
LOG.debug("Failed when unregistering MBean {}", e);
}
@@ -134,20 +150,20 @@ public abstract class AbstractMXBean {
* Returns the name
property of the bean's ObjectName.
*/
public String getMBeanName() {
- return mBeanName;
+ return beanName;
}
/**
* Returns the type
property of the bean's ObjectName.
*/
public String getMBeanType() {
- return mBeanType;
+ return beanType;
}
/**
* Returns the Category
property of the bean's ObjectName.
*/
public String getMBeanCategory() {
- return mBeanCategory;
+ return beanCategory;
}
}