Search in sources :

Example 1 with DefaultDiscardAttributeChecker

use of org.jboss.as.controller.transform.description.DiscardAttributeChecker.DefaultDiscardAttributeChecker in project wildfly by wildfly.

the class ChannelResourceDefinition method buildTransformation.

static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder parent) {
    if (JGroupsModel.VERSION_3_0_0.requiresTransformation(version)) {
        DynamicDiscardPolicy channelDiscardRejectPolicy = new DynamicDiscardPolicy() {

            @Override
            public DiscardPolicy checkResource(TransformationContext context, PathAddress address) {
                // Check whether all channel resources are used by the infinispan subsystem, and transformed
                // by its corresponding transformers; reject otherwise
                // n.b. we need to hard-code the values because otherwise we would end up with cyclical dependency
                String channelName = address.getLastElement().getValue();
                PathAddress rootAddress = address.subAddress(0, address.size() - 2);
                PathAddress subsystemAddress = rootAddress.append(PathElement.pathElement(ModelDescriptionConstants.SUBSYSTEM, "infinispan"));
                Resource infinispanResource;
                try {
                    infinispanResource = context.readResourceFromRoot(subsystemAddress);
                } catch (Resource.NoSuchResourceException ex) {
                    return DiscardPolicy.REJECT_AND_WARN;
                }
                ModelNode infinispanModel = Resource.Tools.readModel(infinispanResource);
                if (infinispanModel.hasDefined("cache-container")) {
                    for (ModelNode container : infinispanModel.get("cache-container").asList()) {
                        ModelNode cacheContainer = container.get(0);
                        if (cacheContainer.hasDefined("transport")) {
                            ModelNode transport = cacheContainer.get("transport").get("jgroups");
                            if (transport.hasDefined("channel")) {
                                String channel = transport.get("channel").asString();
                                if (channel.equals(channelName)) {
                                    return DiscardPolicy.SILENT;
                                }
                            } else {
                                // In that case, if this were the default channel, it can be discarded too
                                ModelNode subsystem = context.readResourceFromRoot(address.subAddress(0, address.size() - 1)).getModel();
                                if (subsystem.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName())) {
                                    if (subsystem.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName()).asString().equals(channelName)) {
                                        return DiscardPolicy.SILENT;
                                    }
                                }
                            }
                        }
                    }
                }
                // No references to this channel, we need to reject it.
                return DiscardPolicy.REJECT_AND_WARN;
            }
        };
        parent.addChildResource(WILDCARD_PATH, channelDiscardRejectPolicy);
    } else {
        ResourceTransformationDescriptionBuilder builder = parent.addChildResource(WILDCARD_PATH);
        if (JGroupsModel.VERSION_4_0_0.requiresTransformation(version)) {
            DiscardAttributeChecker discarder = new DefaultDiscardAttributeChecker(false, true) {

                @Override
                protected boolean isValueDiscardable(PathAddress address, String attributeName, ModelNode attributeValue, TransformationContext context) {
                    return !attributeValue.isDefined() || attributeValue.equals(new ModelNode(address.getLastElement().getValue()));
                }
            };
            builder.getAttributeBuilder().setDiscard(discarder, Attribute.CLUSTER.getDefinition()).addRejectCheck(RejectAttributeChecker.DEFINED, Attribute.CLUSTER.getDefinition());
        }
        ProtocolRegistration.buildTransformation(version, builder);
    }
}
Also used : DefaultDiscardAttributeChecker(org.jboss.as.controller.transform.description.DiscardAttributeChecker.DefaultDiscardAttributeChecker) DynamicDiscardPolicy(org.jboss.as.controller.transform.description.DynamicDiscardPolicy) PathAddress(org.jboss.as.controller.PathAddress) Resource(org.jboss.as.controller.registry.Resource) ResourceTransformationDescriptionBuilder(org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder) ModelNode(org.jboss.dmr.ModelNode) TransformationContext(org.jboss.as.controller.transform.TransformationContext) DiscardAttributeChecker(org.jboss.as.controller.transform.description.DiscardAttributeChecker) DefaultDiscardAttributeChecker(org.jboss.as.controller.transform.description.DiscardAttributeChecker.DefaultDiscardAttributeChecker)

Aggregations

PathAddress (org.jboss.as.controller.PathAddress)1 Resource (org.jboss.as.controller.registry.Resource)1 TransformationContext (org.jboss.as.controller.transform.TransformationContext)1 DiscardAttributeChecker (org.jboss.as.controller.transform.description.DiscardAttributeChecker)1 DefaultDiscardAttributeChecker (org.jboss.as.controller.transform.description.DiscardAttributeChecker.DefaultDiscardAttributeChecker)1 DynamicDiscardPolicy (org.jboss.as.controller.transform.description.DynamicDiscardPolicy)1 ResourceTransformationDescriptionBuilder (org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder)1 ModelNode (org.jboss.dmr.ModelNode)1