Java源码示例:org.jboss.as.controller.SimpleAttributeDefinitionBuilder

示例1
@Override
public void registerChildren(ManagementResourceRegistration profileSub1Reg) {
    super.registerChildren(profileSub1Reg);
    ResourceDefinition profileSub1RegType1Def = ResourceBuilder.Factory.create(PathElement.pathElement("type1", "*"),
            new NonResolvingResourceDescriptionResolver())
            .addReadOnlyAttribute(createAttribute("name", ModelType.STRING))
            .addReadOnlyAttribute(createAttribute("value", ModelType.INT))
            .build();
    profileSub1Reg.registerSubModel(profileSub1RegType1Def);

    ResourceDefinition profileSub1RegType2Def = ResourceBuilder.Factory.create(PathElement.pathElement("type2", "other"),
            new NonResolvingResourceDescriptionResolver())
            .addReadOnlyAttribute(createAttribute("name", ModelType.STRING))
            .addReadOnlyAttribute(SimpleAttributeDefinitionBuilder.create("default", ModelType.STRING).setRequired(false).setDefaultValue(new ModelNode("Default string")).build())
            .build();
    profileSub1Reg.registerSubModel(profileSub1RegType2Def);
}
 
示例2
public static AttributeDefinition createAttribute(String name, ModelType type, String groupName, boolean runtimeOnly, boolean alias, boolean allowNull) {
    SimpleAttributeDefinitionBuilder attribute = SimpleAttributeDefinitionBuilder.create(name, type);
    if (runtimeOnly) {
        attribute.setStorageRuntime();
    }
    if(groupName != null && ! groupName.isEmpty()) {
        attribute.setAttributeGroup(groupName);
    }
    if(alias) {
        attribute.addFlag(AttributeAccess.Flag.ALIAS);
    }
    attribute.setAllowExpression(true);
    if (allowNull) {
        attribute.setRequired(false);
    }
    return attribute.build();
}
 
示例3
/**
 * Tests that a valid operation passes validation
 */
@Test
public void testValidArgs() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.LONG)
                    .setRequired(true)
                    .setValidator(new LongRangeValidator(0L, 2L))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(1);
    KernelServices services = createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
            .setBootOperations(Collections.singletonList(operation))
            .build();

    services.validateOperation(operation);
}
 
示例4
@Test
public void testMissingRequiredParam() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.LONG)
                    .setRequired(true)
                    .build()
    );
    ModelNode operation = createAddOperation();
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(Collections.singletonList(operation))
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例5
@Test
public void testMissingRequiredParam2() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.LONG)
                    .build()
    );
    ModelNode operation = createAddOperation();
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(Collections.singletonList(operation))
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例6
@Test
public void testWrongParamType() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.LONG)
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set("Hello");
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(Collections.singletonList(operation))
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例7
@Test
public void testBigDecimalRangeTooSmall() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.BIG_DECIMAL)
                    .setValidator(new BigDecimalRangeValidator(10, Integer.MAX_VALUE))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(new BigDecimal("5"));
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(Collections.singletonList(operation))
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例8
@Test
public void testBigDecimalRangeTooLarge() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.BIG_DECIMAL)
                    .setValidator(new BigDecimalRangeValidator(Integer.MIN_VALUE, 10))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(new BigDecimal("15"));
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(Collections.singletonList(operation))
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例9
@Test
public void testIntRangeTooSmall() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.INT)
                    .setValidator(new IntRangeValidator(10))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(5);
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(operation)
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例10
@Test
public void testIntRangeTooLarge() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.INT)
                    .setValidator(new IntRangeValidator(Integer.MIN_VALUE, 10))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(15);
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(operation)
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例11
@Test
public void testStringTooShort() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.STRING)
                    .setValidator(new StringLengthValidator(3))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set("Yo");
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(operation)
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例12
@Test
public void testStringTooLong() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.STRING)
                    .setValidator(new StringLengthValidator(0, 1))
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set("Yo");
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(operation)
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例13
@Test
public void testBytesTooShort() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.BYTES)
                    .setMinSize(3)
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(new byte[] {1, 2});
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(operation)
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        // ok
    }
}
 
示例14
@Test
public void testBytesTooLong() throws Exception {
    getMainExtension().setAddAttributes(
            SimpleAttributeDefinitionBuilder.create("test", ModelType.BYTES)
                    .setMaxSize(1)
                    .build()
    );
    ModelNode operation = createAddOperation();
    operation.get("test").set(new byte[] {1, 2});
    try {
        createKernelServicesBuilder(AdditionalInitialization.MANAGEMENT)
                .setBootOperations(operation)
                .build();
        Assert.fail("Not valid");
    } catch (Exception expected) {
        //
    }
}
 
示例15
/**
 * Initialize the subsystem with a few common attributes.
 *
 * @param context the extension context
 * @param version the model version
 * @return the subsystem registration for further use
 */
protected RegistrationResult initializeSubsystem(final ExtensionContext context, ModelVersion version) {
    final SubsystemRegistration subsystem = context.registerSubsystem(subsystemName, version);
    final ManagementResourceRegistration registration = subsystem.registerSubsystemModel(definition);

    // Test attribute
    registration.registerReadWriteAttribute(TEST_ATTRIBUTE, null, new BasicAttributeWriteHandler(TEST_ATTRIBUTE));

    // Other basic handlers
    final AttributeDefinition integer = SimpleAttributeDefinitionBuilder.create("int", ModelType.INT, true).setAllowExpression(allowExpressions).build();
    final AttributeDefinition string = SimpleAttributeDefinitionBuilder.create("string", ModelType.STRING, true).setAllowExpression(allowExpressions).build();
    registration.registerReadWriteAttribute(integer, null, new BasicAttributeWriteHandler(integer));
    registration.registerReadWriteAttribute(string, null, new BasicAttributeWriteHandler(string));
    registration.registerOperationHandler(GenericSubsystemDescribeHandler.DEFINITION, GenericSubsystemDescribeHandler.INSTANCE);

    return new RegistrationResult() {
        @Override
        public SubsystemRegistration getSubsystemRegistration() {
            return subsystem;
        }

        @Override
        public ManagementResourceRegistration getResourceRegistration() {
            return registration;
        }
    };
}
 
示例16
void addAttribute(String name, AccessConstraintDefinition...constraints) {
    SimpleAttributeDefinitionBuilder builder = new SimpleAttributeDefinitionBuilder(name, ModelType.STRING, true);
    if (constraints != null) {
        builder.setAccessConstraints(constraints);
    }
    builder.setAllowExpression(true);
    attributes.add(builder.build());
}
 
示例17
private static AttributeDefinition createAttributeDefinition(String name, ModelType type, AccessConstraintDefinition... accessConstraints) {
    SimpleAttributeDefinitionBuilder builder = SimpleAttributeDefinitionBuilder.create(name, type)
            .setStorageRuntime()
            .setRuntimeServiceNotRequired();
    if (accessConstraints != null) {
        for (AccessConstraintDefinition acd : accessConstraints) {
            builder = builder.addAccessConstraint(acd);
        }
    }
    return builder.build();
}
 
示例18
private static SimpleAttributeDefinition createWorkerAttribute(String name, Attribute attribute, int defaultValue) {
    return SimpleAttributeDefinitionBuilder.create(name, ModelType.INT, true)
            .setDefaultValue(new ModelNode().set(defaultValue))
            .setAlternatives(SERVER_ATTR_NAMES)
            .setXmlName(attribute.getLocalName())
            .setValidator(new IntRangeValidator(1))
            .setAllowExpression(true)
            .setDeprecated(ModelVersion.create(2,0))
            .build();
}
 
示例19
private Attributes(ProcessType processType, boolean forDomain) {
    this.forDomain = forDomain;
    this.options = OPTIONS;
    if (processType.isServer()) {
        this.worker = WORKER;
        this.legacy = new AttributeDefinition[LEGACY_ATTRIBUTES.length];
        for (int i = 0; i < LEGACY_ATTRIBUTES.length; i++) {
            // Reject any defined value, but that means there can't be a default (as that gets validated)
            // Also, a default is incorrect on a server, as there really is no value
            this.legacy[i] = SimpleAttributeDefinitionBuilder.create(LEGACY_ATTRIBUTES[i])
                    .setValidator(WorkerThreadValidator.INSTANCE)
                    .setDefaultValue(null)
                    .build();
        }
    } else if (forDomain) {
        this.worker = SimpleAttributeDefinitionBuilder.create(WORKER).setCapabilityReference((CapabilityReferenceRecorder) null).build();
        this.legacy = LEGACY_ATTRIBUTES;
    } else {
        this.worker = WORKER;
        this.legacy = null;
    }
    int count = options.length + 1 + (legacy == null ? 0 : legacy.length);
    all = new AttributeDefinition[count];
    int idx = 0;
    if (legacy != null) {
        System.arraycopy(legacy, 0, all, 0, legacy.length);
        idx += legacy.length;
    }
    all[idx] = worker;
    System.arraycopy(options, 0, all, idx + 1, options.length);
}
 
示例20
private static SimpleAttributeDefinition createBooleanAttributeDefinition(String name) {
    return SimpleAttributeDefinitionBuilder.create(name, ModelType.BOOLEAN)
            .setDefaultValue(ModelNode.TRUE)
            .setRequired(false)
            .setAllowExpression(true)
            .setAttributeMarshaller(new WrappedAttributeMarshaller(Attribute.VALUE))
            .build();
}
 
示例21
static void registerIOExtension(ExtensionRegistry extensionRegistry, ManagementResourceRegistration rootRegistration) {
    ManagementResourceRegistration extReg = rootRegistration.registerSubModel(new SimpleResourceDefinition(PathElement.pathElement(EXTENSION, "org.wildfly.extension.io"),
            NonResolvingResourceDescriptionResolver.INSTANCE, new ModelOnlyAddStepHandler(), ModelOnlyRemoveStepHandler.INSTANCE));
    extReg.registerReadOnlyAttribute(new SimpleAttributeDefinitionBuilder("module", ModelType.STRING).build(), null);

    Extension ioe = new IOExtension();
    ioe.initialize(extensionRegistry.getExtensionContext("org.wildfly.extension.io", rootRegistration, ExtensionRegistryType.MASTER));

}
 
示例22
protected static SimpleAttributeDefinition createDefaultInterface(RuntimeCapability dependentCapability) {
    return new SimpleAttributeDefinitionBuilder(ModelDescriptionConstants.DEFAULT_INTERFACE, ModelType.STRING, false)
            .setAllowExpression(true)
            .setExpressionsDeprecated()
            .setValidator(new StringLengthValidator(1, Integer.MAX_VALUE, false, true))
            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES)
            .setCapabilityReference("org.wildfly.network.interface", dependentCapability)
            .build();
}
 
示例23
private static void registerOperation(final ManagementResourceRegistration resourceRegistration,
        final MeasurementUnit unit, final PathInfoHandler handler) {
    List<AttributeDefinition> replyParameters = new ArrayList<>();
    for (RelativePathSizeAttribute att : handler.relativePathAttributes) {
        ObjectTypeAttributeDefinition build = ObjectTypeAttributeDefinition.Builder.of(att.pathAttribute.getName(),
            SimpleAttributeDefinitionBuilder.create(USED_SPACE, ModelType.DOUBLE, false)
                .setUndefinedMetricValue(new ModelNode(0d))
                .setMeasurementUnit(unit)
                .setStorageRuntime()
                .build(),
            SimpleAttributeDefinitionBuilder.create(CREATION_TIME, ModelType.STRING, false)
                .setStorageRuntime()
                .build(),
            SimpleAttributeDefinitionBuilder.create(LAST_MODIFIED, ModelType.STRING, false)
                .setStorageRuntime()
                .build(),
            SimpleAttributeDefinitionBuilder.create(RESOLVED_PATH, ModelType.STRING, false)
                .setStorageRuntime()
                .build(),
            SimpleAttributeDefinitionBuilder.create(AVAILABLE_SPACE, ModelType.DOUBLE, false)
                .setMeasurementUnit(unit)
                .setStorageRuntime()
                .build())
            .build();
        replyParameters.add(build);
    }
    OperationDefinition operation = new SimpleOperationDefinitionBuilder(OPERATION_NAME, new DiskUsagePathResourceDescriptionResolver(OPERATION_NAME, replyParameters))
            .addParameter(UNIT_ATTRIBUTE)
            .setReadOnly()
            .setRuntimeOnly()
            .setReplyType(ModelType.OBJECT)
            .setReplyParameters(replyParameters.toArray(new AttributeDefinition[replyParameters.size()]))
            .build();
    resourceRegistration.registerOperationHandler(operation, handler);
}
 
示例24
public LegacyResourceDefinition(ModelNode modelDescription) {
    this.description = modelDescription.get(ModelDescriptionConstants.MODEL_DESCRIPTION);
    ModelNode attributes = description.has(ModelDescriptionConstants.ATTRIBUTES) ? description.get(ModelDescriptionConstants.ATTRIBUTES) : new ModelNode();
    address = PathAddress.pathAddress(modelDescription.get(ModelDescriptionConstants.ADDRESS));

    if (attributes.isDefined()) {
        for (Property property : attributes.asPropertyList()) {
            String name = property.getName();
            SimpleAttributeDefinition def = SimpleAttributeDefinitionBuilder.create(name, property.getValue()).build();
            this.attributes.put(name, new AttributeAccess(
                    AttributeAccess.AccessType.READ_ONLY, AttributeAccess.Storage.CONFIGURATION, null, null, def)
            );
        }
    }
    ModelNode children = modelDescription.get(ModelDescriptionConstants.CHILDREN);
    if (!children.isDefined()) {
        return;
    }
    for (ModelNode child : children.asList()) {
        ResourceDefinition definition = new LegacyResourceDefinition(child);
        if (definition.getPathElement().isWildcard()) {
            this.wildcardChildren.add(definition);
        } else {
            this.singletonChildren.add(definition);
        }
    }
    description.remove(ModelDescriptionConstants.CHILDREN);
}
 
示例25
void addAttribute(String name, AccessConstraintDefinition...constraints) {
    SimpleAttributeDefinitionBuilder builder = new SimpleAttributeDefinitionBuilder(name, ModelType.STRING);
    if (constraints != null) {
        builder.setAccessConstraints(constraints);
    }
    attributes.add(builder.build());
}
 
示例26
void addReadOnlyAttribute(String name, AccessConstraintDefinition...constraints) {
    SimpleAttributeDefinitionBuilder builder = new SimpleAttributeDefinitionBuilder(name, ModelType.STRING);
    if (constraints != null) {
        builder.setAccessConstraints(constraints);
    }
    readOnlyAttributes.add(builder.build());
}
 
示例27
void addAttribute(String name, boolean allowNull, Boolean nullSignificant, ModelNode defaultValue, AccessConstraintDefinition...constraints) {
    SimpleAttributeDefinitionBuilder builder = new SimpleAttributeDefinitionBuilder(name, ModelType.STRING, allowNull);
    if (nullSignificant != null) {
        builder.setNullSignificant(nullSignificant);
    }
    if (defaultValue != null) {
        builder.setDefaultValue(defaultValue);
    }
    if (constraints != null) {
        builder.setAccessConstraints(constraints);
    }
    builder.setAllowExpression(true);
    attributes.add(builder.build());
}
 
示例28
@Override
public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
    super.registerAttributes(resourceRegistration);

    AttributeDefinition attributeDefinition = SimpleAttributeDefinitionBuilder
            .create(UNCONSTRAINED_READWRITE_ATTRIBUTE, ModelType.STRING, true)
            .setDefaultValue(new ModelNode(OLD_VALUE_OF_UNCONSTRAINED_READWRITE_ATTRIBUTE))
            .build();
    resourceRegistration.registerReadWriteAttribute(attributeDefinition, null,
            new ModelOnlyWriteAttributeHandler(attributeDefinition));

    attributeDefinition = SimpleAttributeDefinitionBuilder
            .create(SENSITIVE_CONSTRAINED_READWRITE_ATTRIBUTE, ModelType.STRING, true)
            .setDefaultValue(new ModelNode(OLD_VALUE_OF_SENSITIVE_CONSTRAINED_READWRITE_ATTRIBUTE))
            .setAccessConstraints(MY_SENSITIVE_CONSTRAINT)
            .build();
    resourceRegistration.registerReadWriteAttribute(attributeDefinition, null,
            new ModelOnlyWriteAttributeHandler(attributeDefinition));

    attributeDefinition = SimpleAttributeDefinitionBuilder
            .create(APPLICATION_CONSTRAINED_READWRITE_ATTRIBUTE, ModelType.STRING, true)
            .setDefaultValue(new ModelNode(OLD_VALUE_OF_APPLICATION_CONSTRAINED_READWRITE_ATTRIBUTE))
            .setAccessConstraints(MY_APPLICATION_CONSTRAINT)
            .build();
    resourceRegistration.registerReadWriteAttribute(attributeDefinition, null,
            new ModelOnlyWriteAttributeHandler(attributeDefinition));
}
 
示例29
void addAttribute(String name, AccessConstraintDefinition...constraints) {
    SimpleAttributeDefinitionBuilder builder = new SimpleAttributeDefinitionBuilder(name, ModelType.STRING);
    if (constraints != null) {
        builder.setAccessConstraints(constraints);
    }
    builder.setAllowExpression(true);
    attributes.add(builder.build());
}
 
示例30
@Override
public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
    super.registerAttributes(resourceRegistration);

    OperationStepHandler readAttributeHandler = useDefaultReadAttributeHandler
            ? null : new TestReadAttributeHandler(new ModelNode(VALUE_OF_UNCONSTRAINED_READONLY_ATTRIBUTE));
    ModelNode defaultValue = useDefaultReadAttributeHandler
            ? new ModelNode(VALUE_OF_UNCONSTRAINED_READONLY_ATTRIBUTE) : null;

    AttributeDefinition attributeDefinition = SimpleAttributeDefinitionBuilder
            .create(UNCONSTRAINED_READONLY_ATTRIBUTE, ModelType.STRING, true)
            .setDefaultValue(defaultValue)
            .build();
    resourceRegistration.registerReadOnlyAttribute(attributeDefinition, readAttributeHandler);

    readAttributeHandler = useDefaultReadAttributeHandler
            ? null : new TestReadAttributeHandler(new ModelNode(VALUE_OF_SENSITIVE_CONSTRAINED_READONLY_ATTRIBUTE));
    defaultValue = useDefaultReadAttributeHandler
            ? new ModelNode(VALUE_OF_SENSITIVE_CONSTRAINED_READONLY_ATTRIBUTE) : null;

    attributeDefinition = SimpleAttributeDefinitionBuilder
            .create(SENSITIVE_CONSTRAINED_READONLY_ATTRIBUTE, ModelType.STRING, true)
            .setDefaultValue(defaultValue)
            .setAccessConstraints(MY_SENSITIVE_CONSTRAINT)
            .build();
    resourceRegistration.registerReadOnlyAttribute(attributeDefinition, readAttributeHandler);

    readAttributeHandler = useDefaultReadAttributeHandler
            ? null : new TestReadAttributeHandler(new ModelNode(VALUE_OF_APPLICATION_CONSTRAINED_READONLY_ATTRIBUTE));
    defaultValue = useDefaultReadAttributeHandler
            ? new ModelNode(VALUE_OF_APPLICATION_CONSTRAINED_READONLY_ATTRIBUTE) : null;

    attributeDefinition = SimpleAttributeDefinitionBuilder
            .create(APPLICATION_CONSTRAINED_READONLY_ATTRIBUTE, ModelType.STRING, true)
            .setDefaultValue(defaultValue)
            .setAccessConstraints(MY_APPLICATION_CONSTRAINT)
            .build();
    resourceRegistration.registerReadOnlyAttribute(attributeDefinition, readAttributeHandler);
}