Java Code Examples for org.jboss.dmr.ModelType#PROPERTY

The following examples show how to use org.jboss.dmr.ModelType#PROPERTY . 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: RejectAttributeChecker.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Check an attribute for expressions.
 *
 * @param node the attribute value
 * @return whether an expression was found or not
 */
private boolean checkForExpression(final ModelNode node) {
    if (!node.isDefined()) {
        return false;
    }

    final ModelNode resolved = node.clone();
    if (node.getType() == ModelType.EXPRESSION || node.getType() == ModelType.STRING) {
        return checkForExpression(resolved.asString());
    } else if (node.getType() == ModelType.OBJECT) {
        for (Property prop : resolved.asPropertyList()) {
            if (checkForExpression(prop.getValue())) {
                return true;
            }
        }
    } else if (node.getType() == ModelType.LIST) {
        for (ModelNode current : resolved.asList()) {
            if (checkForExpression(current)) {
                return true;
            }
        }
    } else if (node.getType() == ModelType.PROPERTY) {
        return checkForExpression(resolved.asProperty().getValue());
    }
    return false;
}
 
Example 2
Source File: AttributeTransformationRequirementChecker.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Check an attribute for expressions.
 *
 * @param node the attribute value
 * @return whether an expression was found or not
 */
private boolean checkForExpression(final ModelNode node) {
    if (!node.isDefined()) {
        return false;
    }

    final ModelNode resolved = node.clone();
    if (node.getType() == ModelType.EXPRESSION || node.getType() == ModelType.STRING) {
        return checkForExpression(resolved.asString());
    } else if (node.getType() == ModelType.OBJECT) {
        for (Property prop : resolved.asPropertyList()) {
            if(checkForExpression(prop.getValue())) {
                return true;
            }
        }
    } else if (node.getType() == ModelType.LIST) {
        for (ModelNode current : resolved.asList()) {
            if(checkForExpression(current)) {
                return true;
            }
        }
    } else if (node.getType() == ModelType.PROPERTY) {
        return checkForExpression(resolved.asProperty().getValue());
    }
    return false;
}
 
Example 3
Source File: ModelTestUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Scans for entries of type STRING containing expression formatted strings. This is to trap where parsers
 * call ModelNode.set("${A}") when ModelNode.setExpression("${A}) should have been used
 *
 * @param model the model to check
 */
public static void scanForExpressionFormattedStrings(ModelNode model) {
    if (model.getType().equals(ModelType.STRING)) {
        if (EXPRESSION_PATTERN.matcher(model.asString()).matches()) {
            Assert.fail("ModelNode with type==STRING contains an expression formatted string: " + model.asString());
        }
    } else if (model.getType() == ModelType.OBJECT) {
        for (String key : model.keys()) {
            final ModelNode child = model.get(key);
            scanForExpressionFormattedStrings(child);
        }
    } else if (model.getType() == ModelType.LIST) {
        List<ModelNode> list = model.asList();
        for (ModelNode entry : list) {
            scanForExpressionFormattedStrings(entry);
        }

    } else if (model.getType() == ModelType.PROPERTY) {
        Property prop = model.asProperty();
        scanForExpressionFormattedStrings(prop.getValue());
    }
}
 
Example 4
Source File: ModelParserUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void compare(ModelNode node1, ModelNode node2) {
    Assert.assertEquals(node1.getType(), node2.getType());
    if (node1.getType() == ModelType.OBJECT) {
        final Set<String> keys1 = node1.keys();
        final Set<String> keys2 = node2.keys();
        Assert.assertEquals(node1 + "\n" + node2, keys1.size(), keys2.size());

        for (String key : keys1) {
            final ModelNode child1 = node1.get(key);
            Assert.assertTrue("Missing: " + key + "\n" + node1 + "\n" + node2, node2.has(key));
            final ModelNode child2 = node2.get(key);
            if (child1.isDefined()) {
                Assert.assertTrue(child1.toString(), child2.isDefined());
                compare(child1, child2);
            } else {
                Assert.assertFalse(child2.asString(), child2.isDefined());
            }
        }
    } else if (node1.getType() == ModelType.LIST) {
        List<ModelNode> list1 = node1.asList();
        List<ModelNode> list2 = node2.asList();
        Assert.assertEquals(list1 + "\n" + list2, list1.size(), list2.size());

        for (int i = 0; i < list1.size(); i++) {
            compare(list1.get(i), list2.get(i));
        }

    } else if (node1.getType() == ModelType.PROPERTY) {
        Property prop1 = node1.asProperty();
        Property prop2 = node2.asProperty();
        Assert.assertEquals(prop1 + "\n" + prop2, prop1.getName(), prop2.getName());
        compare(prop1.getValue(), prop2.getValue());

    } else {
        Assert.assertEquals("\n\"" + node1.asString() + "\"\n\"" + node2.asString() + "\"\n-----", node1.asString().trim(), node2.asString().trim());
    }
}
 
Example 5
Source File: ExpressionResolverImpl.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Examine the given model node, resolving any expressions found within, including within child nodes.
 *
 * @param node the node
 * @return a node with all expressions resolved
 * @throws OperationFailedException if an expression cannot be resolved
 */
private ModelNode resolveExpressionsRecursively(final ModelNode node) throws OperationFailedException {
    if (!node.isDefined()) {
        return node;
    }

    ModelType type = node.getType();
    ModelNode resolved;
    if (type == ModelType.EXPRESSION) {
        resolved = resolveExpressionStringRecursively(node.asExpression().getExpressionString(), lenient, true);
    } else if (type == ModelType.OBJECT) {
        resolved = node.clone();
        for (Property prop : resolved.asPropertyList()) {
            resolved.get(prop.getName()).set(resolveExpressionsRecursively(prop.getValue()));
        }
    } else if (type == ModelType.LIST) {
        resolved = node.clone();
        ModelNode list = new ModelNode();
        list.setEmptyList();
        for (ModelNode current : resolved.asList()) {
            list.add(resolveExpressionsRecursively(current));
        }
        resolved = list;
    } else if (type == ModelType.PROPERTY) {
        resolved = node.clone();
        resolved.set(resolved.asProperty().getName(), resolveExpressionsRecursively(resolved.asProperty().getValue()));
    } else {
        resolved = node;
    }

    return resolved;
}
 
Example 6
Source File: SimpleListAttributeDefinitionUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testBuilderCopyPreservesListValidator() throws OperationFailedException {

    // List can contain at most 2 items. The value of the item can not null
    ParameterValidator pv = new ModelTypeValidator(ModelType.PROPERTY, false, true);
    ListValidator lv = new ListValidator(pv, false, 1, 2);

    AttributeDefinition ad = SimpleAttributeDefinitionBuilder.create("x", ModelType.PROPERTY).build();
    SimpleListAttributeDefinition original = SimpleListAttributeDefinition.Builder.of("test", ad)
            .setListValidator(lv)
            .build();
    SimpleListAttributeDefinition copy = new SimpleListAttributeDefinition.Builder(original).build();

    ModelNode validValue = new ModelNode();
    validValue.add("foo", "bar");

    // too many elements
    ModelNode invalidValue = new ModelNode();
    invalidValue.add("foo", "bar");
    invalidValue.add("foo2", "baz");
    invalidValue.add("foo3", "bat");

    // undefined is not a valid element value
    ModelNode invalidValue2 = new ModelNode();
    invalidValue2.add(new ModelNode());

    // the original and copy attribute definition must validate the same way
    validateOperation(original, validValue, true);
    validateOperation(copy, validValue, true);

    validateOperation(original, invalidValue, false);
    validateOperation(copy, invalidValue, false);

    validateOperation(original, invalidValue2, false);
    validateOperation(copy, invalidValue2, false);
}
 
Example 7
Source File: ModelNodeFormatter.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static ModelNodeFormatterBase forType(ModelType type) {
    if(type == ModelType.STRING) {
        return STRING;
    }
    if(type == ModelType.BOOLEAN) {
        return BOOLEAN;
    }
    if(type == ModelType.OBJECT || type == ModelType.LIST) {
        return LIST;
    }
    if(type == ModelType.PROPERTY) {
        return PROPERTY;
    }
    return DEFAULT;
}
 
Example 8
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 9
Source File: PropertyValidator.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public PropertyValidator(boolean nullable, ParameterValidator valueValidator) {
    super(ModelType.PROPERTY, nullable);
    this.valueValidator = valueValidator;
}
 
Example 10
Source File: ResourceCompositeOperationHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected Map<String, ArgumentWithValue> getOperationArguments(CommandContext ctx, String opName) throws CommandLineException {
    Map<String, ArgumentWithValue> args = opArgs.get(opName);
    if(args != null) {
        return args;
    }

    final ModelNode descr = getOperationDescription(ctx, opName);
    if(descr.has(Util.REQUEST_PROPERTIES)) {
        args = new HashMap<String,ArgumentWithValue>();
        final List<Property> propList = descr.get(Util.REQUEST_PROPERTIES).asPropertyList();
        for (Property prop : propList) {
            CommandLineCompleter valueCompleter = null;
            ArgumentValueConverter valueConverter = null;
            if(propConverters != null) {
                valueConverter = propConverters.get(prop.getName());
            }
            if(valueCompleters != null) {
                valueCompleter = valueCompleters.get(prop.getName());
            }
            if(valueConverter == null) {
                valueConverter = ArgumentValueConverter.DEFAULT;
                final ModelType propType = getType(prop.getValue());
                if(propType != null) {
                    if(ModelType.BOOLEAN == propType) {
                        if(valueCompleter == null) {
                            valueCompleter = SimpleTabCompleter.BOOLEAN;
                        }
                    } else if(ModelType.STRING == propType) {
                        valueConverter = ArgumentValueConverter.NON_OBJECT;
                    } else if(prop.getName().endsWith("properties")) { // TODO this is bad but can't rely on proper descriptions
                        valueConverter = ArgumentValueConverter.PROPERTIES;
                    } else if(ModelType.LIST == propType) {
                        if(asType(descr.get(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                            valueConverter = ArgumentValueConverter.PROPERTIES;
                        } else {
                            valueConverter = ArgumentValueConverter.LIST;
                        }
                    }
                }
            }
            final ArgumentWithValue arg = new ArgumentWithValue(ResourceCompositeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
            args.put(arg.getFullName(), arg);
        }
    } else {
        args = Collections.emptyMap();
    }
    opArgs.put(opName, args);
    return args;
}
 
Example 11
Source File: ModelTestUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void compare(ModelNode node1, ModelNode node2, boolean ignoreUndefined, boolean ignoreType, Stack<String> stack) {
    if (! ignoreType) {
        Assert.assertEquals(getCompareStackAsString(stack) + " types", node1.getType(), node2.getType());
    }
    if (node1.getType() == ModelType.OBJECT) {
        ModelNode model1 = ignoreUndefined ? trimUndefinedChildren(node1) : node1;
        ModelNode model2 = ignoreUndefined ? trimUndefinedChildren(node2) : node2;
        final Set<String> keys1 = new TreeSet<String>(model1.keys());
        final Set<String> keys2 = new TreeSet<String>(model2.keys());

        // compare string representations of the keys to help see the difference
        if (!keys1.toString().equals(keys2.toString())){
            //Just to make debugging easier
            System.out.print("");
        }
        Assert.assertEquals(getCompareStackAsString(stack) + ": " + node1 + "\n" + node2, keys1.toString(), keys2.toString());
        Assert.assertTrue(keys1.containsAll(keys2));

        for (String key : keys1) {
            final ModelNode child1 = model1.get(key);
            Assert.assertTrue("Missing: " + key + "\n" + node1 + "\n" + node2, model2.has(key));
            final ModelNode child2 = model2.get(key);

            if (child1.isDefined()) {
                if (!ignoreUndefined) {
                    Assert.assertTrue(getCompareStackAsString(stack) + " key=" + key + "\n with child1 \n" + child1.toString() + "\n has child2 not defined\n node2 is:\n" + node2.toString(), child2.isDefined());
                }
                stack.push(key + "/");
                compare(child1, child2, ignoreUndefined, ignoreType, stack);
                stack.pop();
            } else if (!ignoreUndefined) {
                Assert.assertFalse(getCompareStackAsString(stack) + " key=" + key + "\n with child1 undefined has child2 \n" + child2.asString(), child2.isDefined());
            }
        }
    } else if (node1.getType() == ModelType.LIST) {
        List<ModelNode> list1 = node1.asList();
        List<ModelNode> list2 = node2.asList();
        Assert.assertEquals(list1 + "\n" + list2, list1.size(), list2.size());

        for (int i = 0; i < list1.size(); i++) {
            stack.push(i + "/");
            compare(list1.get(i), list2.get(i), ignoreUndefined, ignoreType, stack);
            stack.pop();
        }

    } else if (node1.getType() == ModelType.PROPERTY) {
        Property prop1 = node1.asProperty();
        Property prop2 = node2.asProperty();
        Assert.assertEquals(prop1 + "\n" + prop2, prop1.getName(), prop2.getName());
        stack.push(prop1.getName() + "/");
        compare(prop1.getValue(), prop2.getValue(), ignoreUndefined, ignoreType, stack);
        stack.pop();

    } else {
        Assert.assertEquals(getCompareStackAsString(stack) +
                    "\n\"" + node1.asString() + "\"\n\"" + node2.asString() + "\"\n-----", node1.asString().trim(), node2.asString().trim());
    }
}