Search in sources :

Example 1 with ModelType

use of org.jboss.dmr.ModelType in project wildfly by wildfly.

the class ManagementResourceDefinition method registerOperations.

@Override
public void registerOperations(ManagementResourceRegistration resourceRegistration) {
    super.registerOperations(resourceRegistration);
    for (final String statisticName : statistics.getNames()) {
        final ModelType modelType = getModelType(statistics.getType(statisticName));
        if (statistics.isOperation(statisticName)) {
            AttributeDefinition attributeDefinition = new SimpleAttributeDefinitionBuilder(statisticName, modelType, true).setXmlName(statisticName).setAllowExpression(true).setFlags(AttributeAccess.Flag.STORAGE_RUNTIME).build();
            OperationStepHandler operationHandler = new AbstractMetricsHandler() {

                @Override
                void handle(final ModelNode response, OperationContext context, final ModelNode operation) {
                    Object result = statistics.getValue(statisticName, entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                    if (result != null) {
                        setResponse(response, result, modelType);
                    }
                }
            };
            SimpleOperationDefinition definition = new SimpleOperationDefinition(statisticName, descriptionResolver, attributeDefinition);
            resourceRegistration.registerOperationHandler(definition, operationHandler);
        }
    }
}
Also used : OperationContext(org.jboss.as.controller.OperationContext) OperationStepHandler(org.jboss.as.controller.OperationStepHandler) ModelType(org.jboss.dmr.ModelType) AttributeDefinition(org.jboss.as.controller.AttributeDefinition) ModelNode(org.jboss.dmr.ModelNode) SimpleOperationDefinition(org.jboss.as.controller.SimpleOperationDefinition) SimpleAttributeDefinitionBuilder(org.jboss.as.controller.SimpleAttributeDefinitionBuilder)

Example 2 with ModelType

use of org.jboss.dmr.ModelType in project wildfly by wildfly.

the class ManagementResourceDefinition method registerAttributes.

@Override
public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
    super.registerAttributes(resourceRegistration);
    for (final String statisticName : statistics.getNames()) {
        final ModelType modelType = getModelType(statistics.getType(statisticName));
        final SimpleAttributeDefinitionBuilder simpleAttributeDefinitionBuilder = new SimpleAttributeDefinitionBuilder(statisticName, modelType, true).setXmlName(statisticName).setAllowExpression(true).setFlags(AttributeAccess.Flag.STORAGE_RUNTIME);
        if (statistics.isAttribute(statisticName)) {
            // WFLY-561 improves usability by using "statistics-enabled" instead of "enabled"
            if (ENABLED_ATTRIBUTE.equals(statisticName)) {
                simpleAttributeDefinitionBuilder.setDeprecated(ENABLED_ATTRIBUTE_DEPRECATED_MODEL_VERSION);
            }
            OperationStepHandler readHandler = new AbstractMetricsHandler() {

                @Override
                void handle(final ModelNode response, OperationContext context, final ModelNode operation) {
                    Object result = statistics.getValue(statisticName, entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                    if (result != null) {
                        setResponse(response, result, modelType);
                    }
                }
            };
            // handle writeable attributes
            if (statistics.isWriteable(statisticName)) {
                OperationStepHandler writeHandler = new AbstractMetricsHandler() {

                    @Override
                    void handle(final ModelNode response, OperationContext context, final ModelNode operation) {
                        Object oldSetting = statistics.getValue(statisticName, entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                        {
                            final ModelNode value = operation.get(ModelDescriptionConstants.VALUE).resolve();
                            if (Boolean.class.equals(statistics.getType(statisticName))) {
                                statistics.setValue(statisticName, value.asBoolean(), entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                            } else if (Integer.class.equals(statistics.getType(statisticName))) {
                                statistics.setValue(statisticName, value.asInt(), entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                            } else if (Long.class.equals(statistics.getType(statisticName))) {
                                statistics.setValue(statisticName, value.asLong(), entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                            } else {
                                statistics.setValue(statisticName, value.asString(), entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                            }
                        }
                        final Object rollBackValue = oldSetting;
                        context.completeStep(new OperationContext.RollbackHandler() {

                            @Override
                            public void handleRollback(OperationContext context, ModelNode operation) {
                                statistics.setValue(statisticName, rollBackValue, entityManagerFactoryLookup, StatisticNameLookup.statisticNameLookup(statisticName), Path.path(PathAddress.pathAddress(operation.get(ADDRESS))));
                            }
                        });
                    }
                };
                resourceRegistration.registerReadWriteAttribute(simpleAttributeDefinitionBuilder.build(), readHandler, writeHandler);
            } else {
                resourceRegistration.registerMetric(simpleAttributeDefinitionBuilder.build(), readHandler);
            }
        }
    }
}
Also used : OperationContext(org.jboss.as.controller.OperationContext) OperationStepHandler(org.jboss.as.controller.OperationStepHandler) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) SimpleAttributeDefinitionBuilder(org.jboss.as.controller.SimpleAttributeDefinitionBuilder)

Example 3 with ModelType

use of org.jboss.dmr.ModelType in project wildfly by wildfly.

the class EjbJarRuntimeResourceTestBase method validatePool.

private void validatePool(ModelNode address, ModelNode resourceDescription, ModelNode resource) {
    for (String attr : POOL_ATTRIBUTES) {
        final ModelType expectedType = POOL_NAME.equals(attr) ? ModelType.STRING : ModelType.INT;
        assertTrue(resourceDescription.get(ATTRIBUTES, attr).isDefined());
        assertEquals(ModelType.STRING, resourceDescription.get(ATTRIBUTES, attr, DESCRIPTION).getType());
        assertEquals(expectedType, resourceDescription.get(ATTRIBUTES, attr, TYPE).asType());
        assertTrue(attr + " is not defined", resource.get(attr).isDefined());
        assertEquals(expectedType, resource.get(attr).getType());
    }
}
Also used : ModelType(org.jboss.dmr.ModelType)

Example 4 with ModelType

use of org.jboss.dmr.ModelType in project wildfly by wildfly.

the class ExpressionSupportSmokeTestCase method handleSimpleCollection.

private void handleSimpleCollection(PathAddress address, String attrName, ModelNode attrValue, ModelType valueType, Map<String, ModelNode> expressionAttrs, Map<String, ModelNode> otherAttrs, Map<String, ModelNode> expectedAttrs) {
    if (COMPLEX_TYPES.contains(valueType)) {
        // Too complicated
        noSimpleCollection++;
        logNoExpressions(address, attrName);
        otherAttrs.put(attrName, attrValue);
    } else {
        boolean hasExpression = false;
        ModelNode updated = new ModelNode();
        ModelNode expected = new ModelNode();
        for (ModelNode item : attrValue.asList()) {
            ModelType itemType = item.getType();
            if (itemType == ModelType.PROPERTY) {
                Property prop = item.asProperty();
                ModelNode propVal = prop.getValue();
                ModelType propValType = propVal.getType();
                if (propVal.isDefined() && propValType != ModelType.EXPRESSION) {
                    // Convert property value to expression
                    if (propValType == ModelType.STRING) {
                        checkForUnconvertedExpression(address, attrName, propVal);
                    }
                    String expression = "${exp.test:" + propVal.asString() + "}";
                    updated.get(prop.getName()).set(expression);
                    expected.get(prop.getName()).set(new ModelNode().set(new ValueExpression(expression)));
                    hasExpression = true;
                } else {
                    updated.get(prop.getName()).set(propVal);
                    expected.get(prop.getName()).set(propVal);
                }
            } else if (item.isDefined() && itemType != ModelType.EXPRESSION) {
                // Convert item to expression
                if (itemType == ModelType.STRING) {
                    checkForUnconvertedExpression(address, attrName, item);
                }
                String expression = "${exp.test:" + item.asString() + "}";
                updated.add(expression);
                expected.add(new ModelNode().set(new ValueExpression(expression)));
                hasExpression = true;
            } else {
                updated.add(item);
                expected.add(item);
            }
        }
        if (hasExpression) {
            simpleCollection++;
            logHandling("Added expression to SIMPLE " + attrValue.getType() + " attribute " + attrName + " at " + address.toModelNode().asString());
            expressionAttrs.put(attrName, updated);
            expectedAttrs.put(attrName, expected);
        } else {
            // We didn't change anything
            noSimpleCollection++;
            logNoExpressions(address, attrName);
            otherAttrs.put(attrName, attrValue);
            expectedAttrs.put(attrName, attrValue);
        }
    }
}
Also used : ValueExpression(org.jboss.dmr.ValueExpression) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 5 with ModelType

use of org.jboss.dmr.ModelType in project wildfly by wildfly.

the class AbstractExpressionSupportTestCase method handleSimpleCollection.

private void handleSimpleCollection(PathAddress address, String attrName, ModelNode attrValue, ModelType valueType, Map<String, ModelNode> expressionAttrs, Map<String, ModelNode> otherAttrs, Map<String, ModelNode> expectedAttrs) {
    if (COMPLEX_TYPES.contains(valueType)) {
        // Too complicated
        noSimpleCollection++;
        logNoExpressions(address, attrName);
        otherAttrs.put(attrName, attrValue);
    } else {
        boolean hasExpression = false;
        ModelNode updated = new ModelNode();
        ModelNode expected = new ModelNode();
        for (ModelNode item : attrValue.asList()) {
            ModelType itemType = item.getType();
            if (itemType == ModelType.PROPERTY) {
                Property prop = item.asProperty();
                ModelNode propVal = prop.getValue();
                ModelType propValType = propVal.getType();
                if (propVal.isDefined() && propValType != ModelType.EXPRESSION) {
                    // Convert property value to expression
                    if (propValType == ModelType.STRING) {
                        checkForUnconvertedExpression(address, attrName, propVal);
                    }
                    String expression = "${exp.test:" + propVal.asString() + "}";
                    updated.get(prop.getName()).set(expression);
                    expected.get(prop.getName()).set(new ModelNode().set(new ValueExpression(expression)));
                    hasExpression = true;
                } else {
                    updated.get(prop.getName()).set(propVal);
                    expected.get(prop.getName()).set(propVal);
                }
            } else if (item.isDefined() && itemType != ModelType.EXPRESSION) {
                // Convert item to expression
                if (itemType == ModelType.STRING) {
                    checkForUnconvertedExpression(address, attrName, item);
                }
                String expression = "${exp.test:" + item.asString() + "}";
                updated.add(expression);
                expected.add(new ModelNode().set(new ValueExpression(expression)));
                hasExpression = true;
            } else {
                updated.add(item);
                expected.add(item);
            }
        }
        if (hasExpression) {
            simpleCollection++;
            logHandling("Added expression to SIMPLE " + attrValue.getType() + " attribute " + attrName + " at " + address.toModelNode().asString());
            expressionAttrs.put(attrName, updated);
            expectedAttrs.put(attrName, expected);
        } else {
            // We didn't change anything
            noSimpleCollection++;
            logNoExpressions(address, attrName);
            otherAttrs.put(attrName, attrValue);
            expectedAttrs.put(attrName, attrValue);
        }
    }
}
Also used : ValueExpression(org.jboss.dmr.ValueExpression) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Aggregations

ModelType (org.jboss.dmr.ModelType)14 ModelNode (org.jboss.dmr.ModelNode)12 Property (org.jboss.dmr.Property)9 ValueExpression (org.jboss.dmr.ValueExpression)9 HashSet (java.util.HashSet)3 SimpleAttributeDefinitionBuilder (org.jboss.as.controller.SimpleAttributeDefinitionBuilder)3 AttributeDefinition (org.jboss.as.controller.AttributeDefinition)2 OperationContext (org.jboss.as.controller.OperationContext)2 OperationStepHandler (org.jboss.as.controller.OperationStepHandler)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 SimpleAttributeDefinition (org.jboss.as.controller.SimpleAttributeDefinition)1 SimpleOperationDefinition (org.jboss.as.controller.SimpleOperationDefinition)1