Java Code Examples for org.jboss.dmr.ModelNode#asLong()

The following examples show how to use org.jboss.dmr.ModelNode#asLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DMRDriver.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
private static Object toObject(ModelNode value) throws ProtocolException {
    switch (value.getType()) {
        case BIG_DECIMAL:
            return value.asBigDecimal();
        case BIG_INTEGER:
            return value.asBigInteger();
        case BOOLEAN:
            return value.asBoolean();
        case BYTES:
            return value.asBytes();
        case DOUBLE:
            return value.asDouble();
        case INT:
            return value.asInt();
        case LONG:
            return value.asLong();
        case OBJECT:
            return value.asObject();
        case PROPERTY:
            return value.asProperty();
        case STRING:
            return value.asString();
        case UNDEFINED:
            return null;
        case LIST:
            return toObjectList(value.asList());
        default:
            throw new ProtocolException("cannot handle an attribute of type [" + value.getType() + "]");
    }
}
 
Example 2
Source File: JmxManagementInterface.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Object object(ModelNode node) {
    switch (node.getType()) {
        case BIG_DECIMAL:
            return node.asBigDecimal();
        case BIG_INTEGER:
            return node.asBigInteger();
        case BOOLEAN:
            return node.asBoolean();
        case BYTES:
            return node.asBytes();
        case DOUBLE:
            return node.asDouble();
        case EXPRESSION:
            return node.asExpression();
        case INT:
            return node.asInt();
        case LIST:
            return node.asList();
        case LONG:
            return node.asLong();
        case PROPERTY:
            return node.asProperty();
        case STRING:
            return node.asString();
        case UNDEFINED:
            return null;
        default:
            throw new UnsupportedOperationException("Can't convert '" + node.getType() + "' to object");
    }
}
 
Example 3
Source File: LongRangeValidator.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void validateParameter(String parameterName, ModelNode value) throws OperationFailedException {
    super.validateParameter(parameterName, value);
    if (value.isDefined() && value.getType() != ModelType.EXPRESSION) {
        long val = value.asLong();
        if (val < min) {
            throw new OperationFailedException(ControllerLogger.ROOT_LOGGER.invalidMinValue(val, parameterName, min));
        }
        else if (val > max) {
            throw new OperationFailedException(ControllerLogger.ROOT_LOGGER.invalidMaxValue(val, parameterName, max));
        }
    }
}
 
Example 4
Source File: TypeConverters.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
Object fromModelNode(final ModelNode node) {
    return node.asLong();
}
 
Example 5
Source File: AttributeDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private ModelNode convertToExpectedType(final ModelNode node) {
    ModelType nodeType = node.getType();
    if (nodeType == type || nodeType == ModelType.UNDEFINED || nodeType == ModelType.EXPRESSION || Util.isExpression(node.asString())) {
        return node;
    }
    switch (type) {
        case BIG_DECIMAL:
            return new ModelNode(node.asBigDecimal());
        case BIG_INTEGER:
            return new ModelNode(node.asBigInteger());
        case BOOLEAN:
            return new ModelNode(node.asBoolean());
        case BYTES:
            return new ModelNode(node.asBytes());
        case DOUBLE:
            return new ModelNode(node.asDouble());
        case INT:
            return new ModelNode(node.asInt());
        case LIST:
            return new ModelNode().set(node.asList());
        case LONG:
            return new ModelNode(node.asLong());
        case PROPERTY:
            return new ModelNode().set(node.asProperty());
        case TYPE:
            return new ModelNode(node.asType());
        case STRING:
            return new ModelNode(node.asString());
        case OBJECT:
            // Check for LIST of PROPERTY. If that is found convert.
            // But only convert if that specifically is found in order
            // to avoid odd unintended conversions (e.g. LIST of STRING, which DMR can convert to OBJECT)
            if (nodeType == ModelType.LIST) {
                if (node.asInt() == 0) {
                    return new ModelNode().setEmptyObject();
                }
                ModelNode first = node.get(0);
                if (first.getType() != ModelType.PROPERTY) {
                    return node;
                }
                // Now we know at least the first element is property, so
                // we assume the rest are as well.
                List<Property> propertyList;
                try {
                    propertyList = node.asPropertyList();
                } catch (IllegalArgumentException iae) {
                    // ignore. The validator allowed this node or we wouldn't be here,
                    // so just fall through and return the unconverted node
                    // Note this isn't expected to be a real world case
                    return node;
                }
                ModelNode result = new ModelNode().setEmptyObject();
                for (Property prop : propertyList) {
                    result.get(prop.getName()).set(prop.getValue());
                }
                return result;
            }
            return node;
        default:
            return node;
    }
}
 
Example 6
Source File: OperationValidator.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void checkType(final ModelType modelType, final ModelNode value) {
    ModelNode resolved;
    try {
        resolved = expressionResolver.resolveExpressions(value);
    } catch (OperationFailedException e) {
        // Dealing with an unresolvable expression is beyond what this class can do.
        // So fall through and see what happens. Basically if modelType is EXPRESSION or STRING
        // it will pass, otherwise an IAE will be thrown
        resolved = value;
    }
    switch (modelType) {
        case BIG_DECIMAL:
            resolved.asBigDecimal();
            break;
        case BIG_INTEGER:
            resolved.asBigInteger();
            break;
        case BOOLEAN:
            resolved.asBoolean();
            break;
        case BYTES:
            resolved.asBytes();
            break;
        case DOUBLE:
            resolved.asDouble();
            break;
        case EXPRESSION:
            value.asString();
            break;
        case INT:
            resolved.asInt();
            break;
        case LIST:
            value.asList();
            break;
        case LONG:
            resolved.asLong();
            break;
        case OBJECT:
            value.asObject();
            break;
        case PROPERTY:
            value.asProperty();
            break;
        case STRING:
            value.asString();
            break;
        case TYPE:
            resolved.asType();
            break;
    }
}
 
Example 7
Source File: QueryOperationHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static boolean matchesFilter(final ModelNode resource, final ModelNode filter, final Operator operator) throws OperationFailedException {
    boolean isMatching = false;
    List<Property> filterProperties = filter.asPropertyList();
    List<Boolean> matches = new ArrayList<>(filterProperties.size());

    for (Property property : filterProperties) {

        final String filterName = property.getName();
        final ModelNode filterValue = property.getValue();

        boolean isEqual = false;

        if(!filterValue.isDefined() || filterValue.asString().equals(UNDEFINED))  {
            // query for undefined attributes
            isEqual = !resource.get(filterName).isDefined();
        }  else {

            final ModelType targetValueType = resource.get(filterName).getType();

            try {
                // query for attribute values (throws exception when types don't match)
                switch (targetValueType) {
                    case BOOLEAN:
                        isEqual = filterValue.asBoolean() == resource.get(filterName).asBoolean();
                        break;
                    case LONG:
                        isEqual = filterValue.asLong() == resource.get(filterName).asLong();
                        break;
                    case INT:
                        isEqual = filterValue.asInt() == resource.get(filterName).asInt();
                        break;
                    case DOUBLE:
                        isEqual = filterValue.asDouble() == resource.get(filterName).asDouble();
                        break;
                    default:
                        isEqual = filterValue.equals(resource.get(filterName));
                }
            } catch (IllegalArgumentException e) {
                throw ControllerLogger.MGMT_OP_LOGGER.selectFailedCouldNotConvertAttributeToType(filterName, targetValueType);
            }

        }

        if(isEqual) {
            matches.add(resource.get(filterName).equals(filterValue));
        }

    }

    if (Operator.AND.equals(operator)) {
        // all matches must be true
        isMatching = matches.size() == filterProperties.size();

    } else if(Operator.OR.equals(operator)){
        // at least one match must be true
        for (Boolean match : matches) {
            if (match) {
                isMatching = true;
                break;
            }
        }
    }
    else {
        // This is just to catch programming errors where a new case isn't added above
        throw new IllegalArgumentException(
                ControllerLogger.MGMT_OP_LOGGER.invalidValue(
                        operator.toString(),
                        OPERATOR,
                        Arrays.asList(Operator.values())
                )
        );
    }


    return isMatching;
}
 
Example 8
Source File: AbstractDeploymentManagerTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 4 votes vote down vote up
private long readLastEnabledTime(final ModelNode address) throws IOException {
    final ModelNode op = Operations.createReadAttributeOperation(address, "enabled-time");
    final ModelNode response = executeOp(op);
    return response.isDefined() ? response.asLong() : 0L;
}