X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fdynamicmbean%2FAbstractDynamicWrapper.java;h=23a4424a0b028f313147f1e7109a43202464eb04;hp=7e48af1caae9adc31cb0c76b64a86d0fb7bca428;hb=6227570f6482136b72a26675d43c8bb88279503e;hpb=32275b7cbe98c361f08970eff34ecb721f45c9e4 diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dynamicmbean/AbstractDynamicWrapper.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dynamicmbean/AbstractDynamicWrapper.java index 7e48af1caa..23a4424a0b 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dynamicmbean/AbstractDynamicWrapper.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dynamicmbean/AbstractDynamicWrapper.java @@ -7,15 +7,15 @@ */ package org.opendaylight.controller.config.manager.impl.dynamicmbean; -import org.opendaylight.controller.config.api.ModuleIdentifier; -import org.opendaylight.controller.config.api.annotations.Description; -import org.opendaylight.controller.config.api.annotations.RequireInterface; -import org.opendaylight.controller.config.api.jmx.ObjectNameUtil; -import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper; -import org.opendaylight.controller.config.spi.Module; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import java.lang.reflect.Array; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import javax.management.Attribute; import javax.management.AttributeList; import javax.management.AttributeNotFoundException; @@ -38,17 +38,14 @@ import javax.management.Notification; import javax.management.NotificationListener; import javax.management.ObjectName; import javax.management.ReflectionException; -import java.lang.reflect.Array; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static java.lang.String.format; +import org.opendaylight.controller.config.api.ModuleIdentifier; +import org.opendaylight.controller.config.api.annotations.Description; +import org.opendaylight.controller.config.api.annotations.RequireInterface; +import org.opendaylight.controller.config.api.jmx.ObjectNameUtil; +import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper; +import org.opendaylight.controller.config.spi.Module; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Contains common code for readable/rw dynamic mbean wrappers. Routes all @@ -57,10 +54,39 @@ import static java.lang.String.format; * a read only wrapper. */ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { - private static final Logger logger = LoggerFactory - .getLogger(AbstractDynamicWrapper.class); + private static final class ModuleNotificationListener implements NotificationListener { + private final ObjectName objectNameInternal; + private final MBeanServer internalServer; + private final MBeanServer configMBeanServer; + + private ModuleNotificationListener(final ObjectName objectNameInternal, final MBeanServer internalServer, + final MBeanServer configMBeanServer) { + this.objectNameInternal = objectNameInternal; + this.internalServer = internalServer; + this.configMBeanServer = configMBeanServer; + } - protected final boolean writable; + @Override + public void handleNotification(final Notification n, final Object handback) { + if (n instanceof MBeanServerNotification + && n.getType() + .equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION) + && ((MBeanServerNotification) n).getMBeanName().equals( + objectNameInternal)) { + try { + internalServer.unregisterMBean(objectNameInternal); + configMBeanServer.removeNotificationListener( + MBeanServerDelegate.DELEGATE_NAME, this); + } catch (MBeanRegistrationException + | ListenerNotFoundException + | InstanceNotFoundException e) { + throw new IllegalStateException(e); + } + } + } + } + + private static final Logger LOG = LoggerFactory.getLogger(AbstractDynamicWrapper.class); protected final Module module; private final MBeanInfo mbeanInfo; @@ -69,25 +95,22 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { protected final ModuleIdentifier moduleIdentifier; protected final MBeanServer internalServer; - public AbstractDynamicWrapper(Module module, boolean writable, - ModuleIdentifier moduleIdentifier, - ObjectName thisWrapperObjectName, MBeanOperationInfo[] dOperations, - MBeanServer internalServer, MBeanServer configMBeanServer) { + public AbstractDynamicWrapper(final Module module, final boolean writable, + final ModuleIdentifier moduleIdentifier, + final ObjectName thisWrapperObjectName, final MBeanOperationInfo[] dOperations, + final MBeanServer internalServer, final MBeanServer configMBeanServer) { - this.writable = writable; this.module = module; this.moduleIdentifier = moduleIdentifier; this.internalServer = internalServer; this.objectNameInternal = thisWrapperObjectName; // register the actual instance into an mbean server. - registerActualModule(module, thisWrapperObjectName, objectNameInternal, - internalServer, configMBeanServer); + registerActualModule(objectNameInternal, configMBeanServer); Set> jmxInterfaces = InterfacesHelper.getMXInterfaces(module .getClass()); - this.attributeHolderMap = buildMBeanInfo(module, writable, - moduleIdentifier, jmxInterfaces, internalServer, - objectNameInternal); - this.mbeanInfo = generateMBeanInfo(module.getClass().getName(), module, + this.attributeHolderMap = buildMBeanInfo(writable, + moduleIdentifier, jmxInterfaces, objectNameInternal); + this.mbeanInfo = generateMBeanInfo(module, attributeHolderMap, dOperations, jmxInterfaces); } @@ -96,12 +119,8 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { * platform mbean server. Wait until this wrapper gets unregistered, in that * case unregister the module and remove listener. */ - private final NotificationListener registerActualModule(Module module, - final ObjectName thisWrapperObjectName, - final ObjectName objectNameInternal, - final MBeanServer internalServer, + private final NotificationListener registerActualModule(final ObjectName objectNameInternal, final MBeanServer configMBeanServer) { - try { internalServer.registerMBean(module, objectNameInternal); } catch (InstanceAlreadyExistsException | MBeanRegistrationException @@ -110,27 +129,7 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { "Error occured during mbean registration with name " + objectNameInternal, e); } - NotificationListener listener = new NotificationListener() { - @Override - public void handleNotification(Notification n, Object handback) { - if (n instanceof MBeanServerNotification - && n.getType() - .equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) { - if (((MBeanServerNotification) n).getMBeanName().equals( - thisWrapperObjectName)) { - try { - internalServer.unregisterMBean(objectNameInternal); - configMBeanServer.removeNotificationListener( - MBeanServerDelegate.DELEGATE_NAME, this); - } catch (MBeanRegistrationException - | ListenerNotFoundException - | InstanceNotFoundException e) { - throw new IllegalStateException(e); - } - } - } - } - }; + NotificationListener listener = new ModuleNotificationListener(objectNameInternal, internalServer, configMBeanServer); try { configMBeanServer.addNotificationListener( MBeanServerDelegate.DELEGATE_NAME, listener, null, null); @@ -140,9 +139,9 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { return listener; } - private static MBeanInfo generateMBeanInfo(String className, Module module, - Map attributeHolderMap, - MBeanOperationInfo[] dOperations, Set> jmxInterfaces) { + private static MBeanInfo generateMBeanInfo(final Module module, + final Map attributeHolderMap, + final MBeanOperationInfo[] dOperations, final Set> jmxInterfaces) { String dDescription = findDescription(module.getClass(), jmxInterfaces); MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[0]; @@ -151,12 +150,12 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { for (AttributeHolder attributeHolder : attributeHolderMap.values()) { attributes.add(attributeHolder.toMBeanAttributeInfo()); } - return new MBeanInfo(className, dDescription, + return new MBeanInfo(module.getClass().getName(), dDescription, attributes.toArray(new MBeanAttributeInfo[0]), dConstructors, dOperations, new MBeanNotificationInfo[0]); } - static String findDescription(Class clazz, Set> jmxInterfaces) { + static String findDescription(final Class clazz, final Set> jmxInterfaces) { List descriptions = AnnotationsHelper .findClassAnnotationInSuperClassesAndIfcs(clazz, Description.class, jmxInterfaces); return AnnotationsHelper.aggregateDescriptions(descriptions); @@ -168,10 +167,9 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { // inspect all exported interfaces ending with MXBean, extract getters & // setters into attribute holder - private static Map buildMBeanInfo(Module module, - boolean writable, ModuleIdentifier moduleIdentifier, - Set> jmxInterfaces, MBeanServer internalServer, - ObjectName internalObjectName) { + private Map buildMBeanInfo(final boolean writable, final ModuleIdentifier moduleIdentifier, + final Set> jmxInterfaces, + final ObjectName internalObjectName) { // internal variables for describing MBean elements Set methods = new HashSet<>(); @@ -193,7 +191,7 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { for (MBeanAttributeInfo a : internalInfo.getAttributes()) { attributeMap.put(a.getName(), a); } - Map attributeHolderMap = new HashMap<>(); + Map attributeHolderMapLocal = new HashMap<>(); for (Method method : methods) { if (method.getParameterTypes().length == 1 @@ -216,11 +214,11 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { } AttributeHolder attributeHolder = new AttributeHolder( attribName, module, attributeMap.get(attribName) - .getType(), writable, ifc, description); - attributeHolderMap.put(attribName, attributeHolder); + .getType(), writable, ifc, description); + attributeHolderMapLocal.put(attribName, attributeHolder); } } - return attributeHolderMap; + return attributeHolderMapLocal; } // DynamicMBean methods @@ -231,10 +229,10 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { } @Override - public Object getAttribute(String attributeName) + public Object getAttribute(final String attributeName) throws AttributeNotFoundException, MBeanException, ReflectionException { - if (attributeName.equals("MBeanInfo")) { + if ("MBeanInfo".equals(attributeName)) { return getMBeanInfo(); } @@ -263,15 +261,15 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { return obj; } - private Object fixDependencyListAttribute(Object attribute) { - if (attribute.getClass().isArray() == false) { + private Object fixDependencyListAttribute(final Object attribute) { + if (!attribute.getClass().isArray()) { throw new IllegalArgumentException("Unexpected attribute type, should be an array, but was " + attribute.getClass()); } for (int i = 0; i < Array.getLength(attribute); i++) { Object on = Array.get(attribute, i); - if (on instanceof ObjectName == false) { + if (!(on instanceof ObjectName)) { throw new IllegalArgumentException("Unexpected attribute type, should be an ObjectName, but was " + on.getClass()); } on = fixObjectName((ObjectName) on); @@ -282,8 +280,8 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { return attribute; } - private boolean isDependencyListAttr(String attributeName, Object attribute) { - if (attributeHolderMap.containsKey(attributeName) == false) { + private boolean isDependencyListAttr(final String attributeName, final Object attribute) { + if (!attributeHolderMap.containsKey(attributeName)) { return false; } @@ -295,7 +293,7 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { return isDepList; } - protected ObjectName fixObjectName(ObjectName on) { + protected ObjectName fixObjectName(final ObjectName on) { if (!ObjectNameUtil.ON_DOMAIN.equals(on.getDomain())) { throw new IllegalArgumentException("Wrong domain, expected " + ObjectNameUtil.ON_DOMAIN + " setter on " + on); @@ -310,7 +308,7 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { } @Override - public AttributeList getAttributes(String[] attributes) { + public AttributeList getAttributes(final String[] attributes) { AttributeList result = new AttributeList(); for (String attributeName : attributes) { try { @@ -318,14 +316,14 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { result.add(new Attribute(attributeName, value)); } catch (Exception e) { - logger.debug("Getting attribute {} failed", attributeName, e); + LOG.debug("Getting attribute {} failed", attributeName, e); } } return result; } @Override - public Object invoke(String actionName, Object[] params, String[] signature) + public Object invoke(final String actionName, final Object[] params, final String[] signature) throws MBeanException, ReflectionException { if ("getAttribute".equals(actionName) && params.length == 1 && signature[0].equals(String.class.getName())) { @@ -342,9 +340,9 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { && signature[0].equals(AttributeList.class.getName())) { return setAttributes((AttributeList) params[0]); } else { - logger.debug("Operation not found {} ", actionName); + LOG.debug("Operation not found {} ", actionName); throw new UnsupportedOperationException( - format("Operation not found on %s. Method invoke is only supported for getInstance and getAttribute(s) " + String.format("Operation not found on %s. Method invoke is only supported for getInstance and getAttribute(s) " + "method, got actionName %s, params %s, signature %s ", moduleIdentifier, actionName, params, signature)); } @@ -356,7 +354,7 @@ abstract class AbstractDynamicWrapper implements DynamicMBeanModuleWrapper { } @Override - public final boolean equals(Object other) { + public final boolean equals(final Object other) { return module.equals(other); }