Search in sources :

Example 1 with ExceptionFunction

use of org.wildfly.common.function.ExceptionFunction in project wildfly by wildfly.

the class ProtocolMetricsHandler method executeRuntimeStep.

@Override
protected void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
    String name = Operations.getAttributeName(operation);
    String protocolName = context.getCurrentAddressValue();
    ServiceName channelServiceName = JGroupsRequirement.CHANNEL.getServiceName(context, UnaryCapabilityNameResolver.PARENT);
    ExceptionFunction<JChannel, ModelNode, Exception> function = new ExceptionFunction<JChannel, ModelNode, Exception>() {

        @Override
        public ModelNode apply(JChannel channel) throws Exception {
            int index = protocolName.lastIndexOf('.');
            Protocol protocol = channel.getProtocolStack().findProtocol((index < 0) ? protocolName : protocolName.substring(index + 1));
            if (protocol == null) {
                throw new IllegalArgumentException(protocolName);
            }
            Attribute attribute = getAttribute(protocol.getClass(), name);
            if (attribute == null) {
                throw new OperationFailedException(JGroupsLogger.ROOT_LOGGER.unknownMetric(name));
            }
            FieldType type = FieldType.valueOf(attribute.getType());
            ModelNode result = new ModelNode();
            Object value = attribute.read(protocol);
            if (value != null) {
                type.setValue(result, value);
            }
            return result;
        }
    };
    FunctionExecutor<JChannel> executor = this.executors.get(channelServiceName);
    try {
        ModelNode value = (executor != null) ? executor.execute(function) : null;
        if (value != null) {
            context.getResult().set(value);
        }
    } catch (Exception e) {
        context.getFailureDescription().set(e.getLocalizedMessage());
    } finally {
        context.completeStep(OperationContext.ResultHandler.NOOP_RESULT_HANDLER);
    }
}
Also used : JChannel(org.jgroups.JChannel) ManagedAttribute(org.jgroups.annotations.ManagedAttribute) OperationFailedException(org.jboss.as.controller.OperationFailedException) ExceptionFunction(org.wildfly.common.function.ExceptionFunction) PrivilegedActionException(java.security.PrivilegedActionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OperationFailedException(org.jboss.as.controller.OperationFailedException) ServiceName(org.jboss.msc.service.ServiceName) AccessibleObject(java.lang.reflect.AccessibleObject) ModelNode(org.jboss.dmr.ModelNode) Protocol(org.jgroups.stack.Protocol)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 ModelNode (org.jboss.dmr.ModelNode)1 ServiceName (org.jboss.msc.service.ServiceName)1 JChannel (org.jgroups.JChannel)1 ManagedAttribute (org.jgroups.annotations.ManagedAttribute)1 Protocol (org.jgroups.stack.Protocol)1 ExceptionFunction (org.wildfly.common.function.ExceptionFunction)1