All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.as.clustering.infinispan.subsystem.SharedStateCacheResourceDefinition Maven / Gradle / Ivy

There is a newer version: 34.0.0.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.jboss.as.clustering.infinispan.subsystem;

import java.util.Set;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.cache.PartitionHandlingConfiguration;
import org.infinispan.configuration.cache.SitesConfiguration;
import org.infinispan.configuration.cache.SitesConfigurationBuilder;
import org.infinispan.configuration.cache.StateTransferConfiguration;
import org.jboss.as.clustering.controller.ManagementResourceRegistration;
import org.jboss.as.clustering.controller.ResourceDescriptor;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.RequirementServiceBuilder;
import org.jboss.dmr.ModelNode;
import org.wildfly.clustering.server.util.MapEntry;
import org.wildfly.subsystem.service.ServiceDependency;
import org.wildfly.subsystem.service.capture.FunctionExecutorRegistry;

/**
 * Base class for cache resources which require common cache attributes, clustered cache attributes
 * and shared cache attributes.
 *
 * @author Richard Achmatowicz (c) 2011 Red Hat Inc.
 */
public class SharedStateCacheResourceDefinition extends ClusteredCacheResourceDefinition {

    static final Set REQUIRED_CHILDREN = Set.of(PartitionHandlingResourceDefinition.PATH, StateTransferResourceDefinition.PATH, BackupsResourceDefinition.PATH);

    private static class ResourceDescriptorConfigurator implements UnaryOperator {
        private final UnaryOperator configurator;

        ResourceDescriptorConfigurator(UnaryOperator configurator) {
            this.configurator = configurator;
        }

        @Override
        public ResourceDescriptor apply(ResourceDescriptor descriptor) {
            return this.configurator.apply(descriptor).addRequiredChildren(REQUIRED_CHILDREN);
        }
    }

    private final FunctionExecutorRegistry> executors;

    SharedStateCacheResourceDefinition(PathElement path, UnaryOperator configurator, CacheMode mode, FunctionExecutorRegistry> executors) {
        super(path, new ResourceDescriptorConfigurator(configurator), mode, executors);
        this.executors = executors;
    }

    @Override
    public ManagementResourceRegistration register(ManagementResourceRegistration parent) {
        ManagementResourceRegistration registration = super.register(parent);

        new PartitionHandlingResourceDefinition().register(registration);
        new StateTransferResourceDefinition().register(registration);
        new BackupsResourceDefinition(this.executors).register(registration);

        return registration;
    }

    @Override
    public MapEntry, Stream>>> resolve(OperationContext context, ModelNode model) throws OperationFailedException {
        PathAddress address = context.getCurrentAddress();
        String containerName = address.getParent().getLastElement().getValue();
        String cacheName = address.getLastElement().getValue();

        ServiceDependency partitionHandling = ServiceDependency.on(PartitionHandlingResourceDefinition.SERVICE_DESCRIPTOR, containerName, cacheName);
        ServiceDependency stateTransfer = ServiceDependency.on(StateTransferResourceDefinition.SERVICE_DESCRIPTOR, containerName, cacheName);
        ServiceDependency backups = ServiceDependency.on(BackupsResourceDefinition.SERVICE_DESCRIPTOR, containerName, cacheName);

        return super.resolve(context, model).map(consumer -> consumer.andThen(new Consumer<>() {
            @Override
            public void accept(ConfigurationBuilder builder) {
                builder.clustering().partitionHandling().read(partitionHandling.get());
                builder.clustering().stateTransfer().read(stateTransfer.get());

                SitesConfigurationBuilder sitesBuilder = builder.sites();
                sitesBuilder.read(backups.get());
            }
        }), stream -> Stream.concat(stream, Stream.of(partitionHandling, stateTransfer, backups)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy