import static java.lang.String.format;
+import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
-
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.ReflectionException;
-
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import static java.lang.String.format;
+
/**
* Contains common code for readable/rw dynamic mbean wrappers. Routes all
* requests (getAttribute, setAttribute, invoke) into the actual instance, but
* provides additional functionality - namely it disallows setting attribute on
* 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 ObjectName thisWrapperObjectName;
+ private final MBeanServer configMBeanServer;
+
+ private ModuleNotificationListener(final ObjectName objectNameInternal, final MBeanServer internalServer,
+ final ObjectName thisWrapperObjectName, final MBeanServer configMBeanServer) {
+ this.objectNameInternal = objectNameInternal;
+ this.internalServer = internalServer;
+ this.thisWrapperObjectName = thisWrapperObjectName;
+ this.configMBeanServer = configMBeanServer;
+ }
+
+ @Override
+ public void handleNotification(final Notification n, final 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);
+ }
+ }
+ }
+ }
+ }
+ private static final Logger LOG = LoggerFactory.getLogger(AbstractDynamicWrapper.class);
protected final boolean writable;
protected final Module module;
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;
* 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,
- final MBeanServer configMBeanServer) {
+ private final NotificationListener registerActualModule(final Module module,
+ final ObjectName thisWrapperObjectName,
+ final ObjectName objectNameInternal,
+ final MBeanServer internalServer,
+ final MBeanServer configMBeanServer) {
try {
internalServer.registerMBean(module, objectNameInternal);
} catch (InstanceAlreadyExistsException | MBeanRegistrationException
| NotCompliantMBeanException | IllegalStateException e) {
throw new IllegalStateException(
- "Error occured during mbean registration ", e);
+ "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, thisWrapperObjectName, configMBeanServer);
try {
configMBeanServer.addNotificationListener(
MBeanServerDelegate.DELEGATE_NAME, listener, null, null);
return listener;
}
- private static MBeanInfo generateMBeanInfo(String className, Module module,
- Map<String, AttributeHolder> attributeHolderMap,
- MBeanOperationInfo[] dOperations, Set<Class<?>> jmxInterfaces) {
+ private static MBeanInfo generateMBeanInfo(final String className, final Module module,
+ final Map<String, AttributeHolder> attributeHolderMap,
+ final MBeanOperationInfo[] dOperations, final Set<Class<?>> jmxInterfaces) {
String dDescription = findDescription(module.getClass(), jmxInterfaces);
MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[0];
dOperations, new MBeanNotificationInfo[0]);
}
- static String findDescription(Class<?> clazz, Set<Class<?>> jmxInterfaces) {
+ static String findDescription(final Class<?> clazz, final Set<Class<?>> jmxInterfaces) {
List<Description> descriptions = AnnotationsHelper
.findClassAnnotationInSuperClassesAndIfcs(clazz, Description.class, jmxInterfaces);
return AnnotationsHelper.aggregateDescriptions(descriptions);
// inspect all exported interfaces ending with MXBean, extract getters &
// setters into attribute holder
- private static Map<String, AttributeHolder> buildMBeanInfo(Module module,
- boolean writable, ModuleIdentifier moduleIdentifier,
- Set<Class<?>> jmxInterfaces, MBeanServer internalServer,
- ObjectName internalObjectName) {
+ private static Map<String, AttributeHolder> buildMBeanInfo(final Module module,
+ final boolean writable, final ModuleIdentifier moduleIdentifier,
+ final Set<Class<?>> jmxInterfaces, final MBeanServer internalServer,
+ final ObjectName internalObjectName) {
// internal variables for describing MBean elements
Set<Method> methods = new HashSet<>();
}
AttributeHolder attributeHolder = new AttributeHolder(
attribName, module, attributeMap.get(attribName)
- .getType(), writable, ifc, description);
+ .getType(), writable, ifc, description);
attributeHolderMap.put(attribName, attributeHolder);
}
}
}
@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();
}
} catch (InstanceNotFoundException e) {
new MBeanException(e);
}
+
if (obj instanceof ObjectName) {
AttributeHolder attributeHolder = attributeHolderMap
.get(attributeName);
}
return obj;
}
+
+
+ if (isDependencyListAttr(attributeName, obj)) {
+ obj = fixDependencyListAttribute(obj);
+ }
+
return obj;
+ }
+
+ private Object fixDependencyListAttribute(final Object attribute) {
+ if (attribute.getClass().isArray() == false) {
+ 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) {
+ throw new IllegalArgumentException("Unexpected attribute type, should be an ObjectName, but was " + on.getClass());
+ }
+ on = fixObjectName((ObjectName) on);
+
+ Array.set(attribute, i, on);
+ }
+ return attribute;
}
- protected ObjectName fixObjectName(ObjectName on) {
- if (!ObjectNameUtil.ON_DOMAIN.equals(on.getDomain()))
+ private boolean isDependencyListAttr(final String attributeName, final Object attribute) {
+ if (attributeHolderMap.containsKey(attributeName) == false) {
+ return false;
+ }
+
+ AttributeHolder attributeHolder = attributeHolderMap.get(attributeName);
+
+ boolean isDepList = true;
+ isDepList &= attributeHolder.getRequireInterfaceOrNull() != null;
+ isDepList &= attribute instanceof ObjectName[];
+ return isDepList;
+ }
+
+ 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);
+ }
// if on contains transaction name, remove it
String transactionName = ObjectNameUtil.getTransactionName(on);
- if (transactionName != null)
+ if (transactionName != null) {
return ObjectNameUtil.withoutTransactionName(on);
- else
+ } else {
return on;
+ }
}
@Override
- public AttributeList getAttributes(String[] attributes) {
+ public AttributeList getAttributes(final String[] attributes) {
AttributeList result = new AttributeList();
for (String attributeName : attributes) {
try {
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())) {
&& 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) "
+ "method, got actionName %s, params %s, signature %s ",
}
@Override
- public final boolean equals(Object other) {
+ public final boolean equals(final Object other) {
return module.equals(other);
}