org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler Maven / Gradle / Ivy
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.clustering.jgroups.subsystem;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
import java.util.Map;
import org.jboss.as.clustering.controller.FunctionExecutor;
import org.jboss.as.clustering.controller.FunctionExecutorRegistry;
import org.jboss.as.clustering.controller.UnaryCapabilityNameResolver;
import org.jboss.as.clustering.jgroups.logging.JGroupsLogger;
import org.jboss.as.controller.AbstractRuntimeOnlyHandler;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.msc.service.ServiceName;
import org.jgroups.JChannel;
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.Property;
import org.jgroups.stack.Protocol;
import org.jgroups.util.Util;
import org.wildfly.clustering.jgroups.spi.JGroupsRequirement;
import org.wildfly.common.function.ExceptionFunction;
/**
* A generic handler for protocol metrics based on reflection.
*
* @author Richard Achmatowicz (c) 2013 Red Hat Inc.
* @author Radoslav Husar
* @author Paul Ferraro
*/
public class ProtocolMetricsHandler extends AbstractRuntimeOnlyHandler {
interface Attribute {
String getName();
String getDescription();
Class> getType();
Object read(Object object) throws Exception;
}
abstract static class AbstractAttribute implements Attribute {
final A accessible;
AbstractAttribute(A accessible) {
this.accessible = accessible;
}
@Override
public String getName() {
if (this.accessible.isAnnotationPresent(ManagedAttribute.class)) {
String name = this.accessible.getAnnotation(ManagedAttribute.class).name();
if (!name.isEmpty()) return name;
}
if (this.accessible.isAnnotationPresent(Property.class)) {
String name = this.accessible.getAnnotation(Property.class).name();
if (!name.isEmpty()) return name;
}
return null;
}
@Override
public String getDescription() {
if (this.accessible.isAnnotationPresent(ManagedAttribute.class)) {
return this.accessible.getAnnotation(ManagedAttribute.class).description();
}
if (this.accessible.isAnnotationPresent(Property.class)) {
return this.accessible.getAnnotation(Property.class).description();
}
return this.accessible.toString();
}
@Override
public Object read(final Object object) throws Exception {
PrivilegedExceptionAction