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

com.hazelcast.internal.dynamicconfig.search.ConfigSearch Maven / Gradle / Ivy

There is a newer version: 62
Show newest version
/*
 * Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.internal.dynamicconfig.search;

import com.hazelcast.config.AtomicLongConfig;
import com.hazelcast.config.AtomicReferenceConfig;
import com.hazelcast.config.CacheSimpleConfig;
import com.hazelcast.config.CardinalityEstimatorConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.ConfigPatternMatcher;
import com.hazelcast.config.CountDownLatchConfig;
import com.hazelcast.config.DurableExecutorConfig;
import com.hazelcast.config.ExecutorConfig;
import com.hazelcast.config.FlakeIdGeneratorConfig;
import com.hazelcast.config.ListConfig;
import com.hazelcast.config.LockConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MultiMapConfig;
import com.hazelcast.config.PNCounterConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.config.ReliableTopicConfig;
import com.hazelcast.config.ReplicatedMapConfig;
import com.hazelcast.config.RingbufferConfig;
import com.hazelcast.config.ScheduledExecutorConfig;
import com.hazelcast.config.SemaphoreConfig;
import com.hazelcast.config.SetConfig;
import com.hazelcast.config.TopicConfig;
import com.hazelcast.internal.dynamicconfig.ConfigurationService;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Config search factory, which allows to build searchers for the given config types.
 *
 * @since 3.11
 * @see Searcher
 */
@SuppressWarnings({"checkstyle:methodcount", "checkstyle:executablestatementcount"})
public final class ConfigSearch {
    private static final Map CONFIG_SUPPLIERS = new ConcurrentHashMap();

    private ConfigSearch() {
    }

    static {
        CONFIG_SUPPLIERS.put(MapConfig.class, new ConfigSupplier() {
            @Override
            public MapConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findMapConfig(name);
            }

            @Override
            public MapConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getMapConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getMapConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(CacheSimpleConfig.class, new ConfigSupplier() {
            @Override
            public CacheSimpleConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findCacheSimpleConfig(name);
            }

            @Override
            public CacheSimpleConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getCacheConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getCacheConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(QueueConfig.class, new ConfigSupplier() {
            @Override
            public QueueConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findQueueConfig(name);
            }

            @Override
            public QueueConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getQueueConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getQueueConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(LockConfig.class, new ConfigSupplier() {
            @Override
            public LockConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findLockConfig(name);
            }

            @Override
            public LockConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getLockConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getLockConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(ListConfig.class, new ConfigSupplier() {
            @Override
            public ListConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findListConfig(name);
            }

            @Override
            public ListConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getListConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getListConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(SetConfig.class, new ConfigSupplier() {
            @Override
            public SetConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findSetConfig(name);
            }

            @Override
            public SetConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getSetConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getSetConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(MultiMapConfig.class, new ConfigSupplier() {
            @Override
            public MultiMapConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findMultiMapConfig(name);
            }

            @Override
            public MultiMapConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getMultiMapConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getMultiMapConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(ReplicatedMapConfig.class, new ConfigSupplier() {
            @Override
            public ReplicatedMapConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                        @Nonnull String name) {
                return configurationService.findReplicatedMapConfig(name);
            }

            @Override
            public ReplicatedMapConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getReplicatedMapConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getReplicatedMapConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(RingbufferConfig.class, new ConfigSupplier() {
            @Override
            public RingbufferConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findRingbufferConfig(name);
            }

            @Override
            public RingbufferConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getRingbufferConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getRingbufferConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(AtomicLongConfig.class, new ConfigSupplier() {
            @Override
            public AtomicLongConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findAtomicLongConfig(name);
            }

            @Override
            public AtomicLongConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getAtomicLongConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getAtomicLongConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(AtomicReferenceConfig.class, new ConfigSupplier() {
            @Override
            public AtomicReferenceConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                          @Nonnull String name) {
                return configurationService.findAtomicReferenceConfig(name);
            }

            @Override
            public AtomicReferenceConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getAtomicReferenceConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getAtomicReferenceConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(CountDownLatchConfig.class, new ConfigSupplier() {
            @Override
            public CountDownLatchConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                         @Nonnull String name) {
                return configurationService.findCountDownLatchConfig(name);
            }

            @Override
            public CountDownLatchConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getCountDownLatchConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getCountDownLatchConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(TopicConfig.class, new ConfigSupplier() {
            @Override
            public TopicConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findTopicConfig(name);
            }

            @Override
            public TopicConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getTopicConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getTopicConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(ReliableTopicConfig.class, new ConfigSupplier() {
            @Override
            public ReliableTopicConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                        @Nonnull String name) {
                return configurationService.findReliableTopicConfig(name);
            }

            @Override
            public ReliableTopicConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getReliableTopicConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getReliableTopicConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(ExecutorConfig.class, new ConfigSupplier() {
            @Override
            public ExecutorConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findExecutorConfig(name);
            }

            @Override
            public ExecutorConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getExecutorConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getExecutorConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(DurableExecutorConfig.class, new ConfigSupplier() {
            @Override
            public DurableExecutorConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                          @Nonnull String name) {
                return configurationService.findDurableExecutorConfig(name);
            }

            @Override
            public DurableExecutorConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getDurableExecutorConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getDurableExecutorConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(ScheduledExecutorConfig.class, new ConfigSupplier() {
            @Override
            public ScheduledExecutorConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                            @Nonnull String name) {
                return configurationService.findScheduledExecutorConfig(name);
            }

            @Override
            public ScheduledExecutorConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getScheduledExecutorConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getScheduledExecutorConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(CardinalityEstimatorConfig.class, new ConfigSupplier() {
            @Override
            public CardinalityEstimatorConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                               @Nonnull String name) {
                return configurationService.findCardinalityEstimatorConfig(name);
            }

            @Override
            public CardinalityEstimatorConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getCardinalityEstimatorConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getCardinalityEstimatorConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(SemaphoreConfig.class, new ConfigSupplier() {
            @Override
            public SemaphoreConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findSemaphoreConfig(name);
            }

            @Override
            public SemaphoreConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getSemaphoreConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getSemaphoreConfigsAsMap();
            }
        });
        CONFIG_SUPPLIERS.put(FlakeIdGeneratorConfig.class, new ConfigSupplier() {
            @Override
            public FlakeIdGeneratorConfig getDynamicConfig(@Nonnull ConfigurationService configurationService,
                                                           @Nonnull String name) {
                return configurationService.findFlakeIdGeneratorConfig(name);
            }

            @Override
            public FlakeIdGeneratorConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getFlakeIdGeneratorConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getFlakeIdGeneratorConfigs();
            }
        });
        CONFIG_SUPPLIERS.put(PNCounterConfig.class, new ConfigSupplier() {
            @Override
            public PNCounterConfig getDynamicConfig(@Nonnull ConfigurationService configurationService, @Nonnull String name) {
                return configurationService.findPNCounterConfig(name);
            }

            @Override
            public PNCounterConfig getStaticConfig(@Nonnull Config staticConfig, @Nonnull String name) {
                return staticConfig.getPNCounterConfig(name);
            }

            @Override
            public Map getStaticConfigs(@Nonnull Config staticConfig) {
                return staticConfig.getPNCounterConfigs();
            }
        });
    }

    /**
     * Factory method for providing {@link ConfigSupplier} instances for the given config class
     *
     * @param cls class of the config we're looking for
     * @param  type of config class
     * @return {@link ConfigSupplier} config supplier for the given config type
     * @see com.hazelcast.spi.properties.GroupProperty#SEARCH_DYNAMIC_CONFIG_FIRST
     */
    @Nullable
    public static  ConfigSupplier supplierFor(@Nonnull Class cls) {
        return CONFIG_SUPPLIERS.get(cls);
    }

    /**
     * Factory method for creating {@link Searcher} instances
     *
     * @param staticConfig hazelcast static config
     * @param configurationService configuration service
     * @param isStaticFirst true if {@link Searcher} should look into static config first, and
     *                      false otherwise.
     * @param  type of config class
     * @return {@link Searcher} for the given config type and conditions
     * @see com.hazelcast.spi.properties.GroupProperty#SEARCH_DYNAMIC_CONFIG_FIRST
     * @see ConfigSupplier
     */
    @Nonnull
    public static 
    Searcher searcherFor(@Nonnull final Config staticConfig,
                            @Nonnull final ConfigurationService configurationService,
                            @Nonnull final ConfigPatternMatcher configPatternMatcher, boolean isStaticFirst) {
        return isStaticFirst
                ? new StaticFirstSearcher(configurationService, staticConfig, configPatternMatcher)
                : new DynamicFirstSearcher(configurationService, staticConfig, configPatternMatcher);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy