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

com.hazelcast.config.Config Maven / Gradle / Ivy

There is a newer version: 5.0-BETA-1
Show newest version
/*
 * Copyright (c) 2008-2013, 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.config;

import com.hazelcast.core.HazelcastException;
import com.hazelcast.core.ManagedContext;

import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.hazelcast.partition.strategy.StringPartitioningStrategy.getBaseName;
import static java.text.MessageFormat.format;

/**
 * Contains all the configuration to start a {@link com.hazelcast.core.HazelcastInstance}. A Config
 * can be created programmatically, but can also be configured using XML, see {@link com.hazelcast.config.XmlConfigBuilder}.
 *
 * Config instances can be shared between threads, but should not be modified after they are used to
 * create HazelcastInstances.
 */
public class Config {

    private URL configurationUrl;

    private File configurationFile;

    private ClassLoader classLoader;

    private Properties properties = new Properties();

    private String instanceName = null;

    private GroupConfig groupConfig = new GroupConfig();

    private NetworkConfig networkConfig = new NetworkConfig();

    private final Map mapConfigs = new ConcurrentHashMap();

    private final Map topicConfigs = new ConcurrentHashMap();

    private final Map queueConfigs = new ConcurrentHashMap();

    private final Map multiMapConfigs = new ConcurrentHashMap();

    private final Map listConfigs = new ConcurrentHashMap();

    private final Map setConfigs = new ConcurrentHashMap();

    private final Map executorConfigs = new ConcurrentHashMap();

    private final Map semaphoreConfigs = new ConcurrentHashMap();

    private final Map replicatedMapConfigs = new ConcurrentHashMap();

    private final Map wanReplicationConfigs = new ConcurrentHashMap();

    private final Map jobTrackerConfigs = new ConcurrentHashMap();

    private ServicesConfig servicesConfig = new ServicesConfig();

    private SecurityConfig securityConfig = new SecurityConfig();

    private final List listenerConfigs = new LinkedList();

    private PartitionGroupConfig partitionGroupConfig = new PartitionGroupConfig();

    private ManagementCenterConfig managementCenterConfig = new ManagementCenterConfig();

    private SerializationConfig serializationConfig = new SerializationConfig();

    private ManagedContext managedContext;

    private ConcurrentMap userContext = new ConcurrentHashMap();

    private MemberAttributeConfig memberAttributeConfig = new MemberAttributeConfig();

    private String licenseKey;

    public Config() {
    }

    public Config(String instanceName){
        this.instanceName = instanceName;
    }

    /**
     * Returns the class-loader that will be used in serialization.
     * 

If null, then thread context class-loader will be used instead. * * @return the class-loader */ public ClassLoader getClassLoader() { return classLoader; } /** * Sets the class-loader to be used during de-serialization * and as context class-loader of Hazelcast internal threads. * *

* If not set (or set to null); thread context class-loader * will be used in required places. * *

* Default value is null. * * @param classLoader class-loader to be used during de-serialization * @return Config instance */ public Config setClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; return this; } public String getProperty(String name) { String value = properties.getProperty(name); return value != null ? value : System.getProperty(name); } public Config setProperty(String name, String value) { properties.put(name, value); return this; } public MemberAttributeConfig getMemberAttributeConfig() { return memberAttributeConfig; } public void setMemberAttributeConfig(MemberAttributeConfig memberAttributeConfig) { this.memberAttributeConfig = memberAttributeConfig; } public Properties getProperties() { return properties; } public Config setProperties(Properties properties) { this.properties = properties; return this; } public String getInstanceName() { return instanceName; } public Config setInstanceName(String instanceName) { this.instanceName = instanceName; return this; } public GroupConfig getGroupConfig() { return groupConfig; } public Config setGroupConfig(GroupConfig groupConfig) { this.groupConfig = groupConfig; return this; } public NetworkConfig getNetworkConfig() { return networkConfig; } public Config setNetworkConfig(NetworkConfig networkConfig) { this.networkConfig = networkConfig; return this; } public MapConfig findMapConfig(String name){ name = getBaseName(name); MapConfig config; if ((config = lookupByPattern(mapConfigs, name)) != null) return config.getAsReadOnly(); return getMapConfig("default").getAsReadOnly(); } public MapConfig getMapConfig(String name) { name = getBaseName(name); MapConfig config; if ((config = lookupByPattern(mapConfigs, name)) != null) return config; MapConfig defConfig = mapConfigs.get("default"); if (defConfig == null) { defConfig = new MapConfig(); defConfig.setName("default"); addMapConfig(defConfig); } config = new MapConfig(defConfig); config.setName(name); addMapConfig(config); return config; } public Config addMapConfig(MapConfig mapConfig) { mapConfigs.put(mapConfig.getName(), mapConfig); return this; } /** * @return the mapConfigs */ public Map getMapConfigs() { return mapConfigs; } /** * @param mapConfigs the mapConfigs to set */ public Config setMapConfigs(Map mapConfigs) { this.mapConfigs.clear(); this.mapConfigs.putAll(mapConfigs); for (final Entry entry : this.mapConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public QueueConfig findQueueConfig(String name){ name = getBaseName(name); QueueConfig config; if ((config = lookupByPattern(queueConfigs, name)) != null) return config.getAsReadOnly(); return getQueueConfig("default").getAsReadOnly(); } public QueueConfig getQueueConfig(String name) { name = getBaseName(name); QueueConfig config; if ((config = lookupByPattern(queueConfigs, name)) != null) return config; QueueConfig defConfig = queueConfigs.get("default"); if (defConfig == null) { defConfig = new QueueConfig(); defConfig.setName("default"); addQueueConfig(defConfig); } config = new QueueConfig(defConfig); config.setName(name); addQueueConfig(config); return config; } public Config addQueueConfig(QueueConfig queueConfig) { queueConfigs.put(queueConfig.getName(), queueConfig); return this; } public Map getQueueConfigs() { return queueConfigs; } public Config setQueueConfigs(Map queueConfigs) { this.queueConfigs.clear(); this.queueConfigs.putAll(queueConfigs); for (Entry entry : queueConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public ListConfig findListConfig(String name){ name = getBaseName(name); ListConfig config; if ((config = lookupByPattern(listConfigs, name)) != null) return config.getAsReadOnly(); return getListConfig("default").getAsReadOnly(); } public ListConfig getListConfig(String name) { name = getBaseName(name); ListConfig config; if ((config = lookupByPattern(listConfigs, name)) != null) return config; ListConfig defConfig = listConfigs.get("default"); if (defConfig == null) { defConfig = new ListConfig(); defConfig.setName("default"); addListConfig(defConfig); } config = new ListConfig(defConfig); config.setName(name); addListConfig(config); return config; } public Config addListConfig(ListConfig listConfig) { listConfigs.put(listConfig.getName(), listConfig); return this; } public Map getListConfigs() { return listConfigs; } public Config setListConfigs(Map listConfigs) { this.listConfigs.clear(); this.listConfigs.putAll(listConfigs); for (Entry entry : listConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public SetConfig findSetConfig(String name){ name = getBaseName(name); SetConfig config; if ((config = lookupByPattern(setConfigs, name)) != null) return config.getAsReadOnly(); return getSetConfig("default").getAsReadOnly(); } public SetConfig getSetConfig(String name) { name = getBaseName(name); SetConfig config; if ((config = lookupByPattern(setConfigs, name)) != null) return config; SetConfig defConfig = setConfigs.get("default"); if (defConfig == null) { defConfig = new SetConfig(); defConfig.setName("default"); addSetConfig(defConfig); } config = new SetConfig(defConfig); config.setName(name); addSetConfig(config); return config; } public Config addSetConfig(SetConfig setConfig) { setConfigs.put(setConfig.getName(), setConfig); return this; } public Map getSetConfigs() { return setConfigs; } public Config setSetConfigs(Map setConfigs) { this.setConfigs.clear(); this.setConfigs.putAll(setConfigs); for (Entry entry : setConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public MultiMapConfig findMultiMapConfig(String name){ name = getBaseName(name); MultiMapConfig config; if ((config = lookupByPattern(multiMapConfigs, name)) != null) return config.getAsReadOnly(); return getMultiMapConfig("default").getAsReadOnly(); } public MultiMapConfig getMultiMapConfig(String name) { name = getBaseName(name); MultiMapConfig config; if ((config = lookupByPattern(multiMapConfigs, name)) != null) return config; MultiMapConfig defConfig = multiMapConfigs.get("default"); if (defConfig == null) { defConfig = new MultiMapConfig(); defConfig.setName("default"); addMultiMapConfig(defConfig); } config = new MultiMapConfig(defConfig); config.setName(name); addMultiMapConfig(config); return config; } public Config addMultiMapConfig(MultiMapConfig multiMapConfig) { multiMapConfigs.put(multiMapConfig.getName(), multiMapConfig); return this; } public Map getMultiMapConfigs() { return multiMapConfigs; } public Config setMultiMapConfigs(Map multiMapConfigs) { this.multiMapConfigs.clear(); this.multiMapConfigs.putAll(multiMapConfigs); for (final Entry entry : this.multiMapConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public ReplicatedMapConfig findReplicatedMapConfig(String name){ ReplicatedMapConfig config; if ((config = lookupByPattern(replicatedMapConfigs, name)) != null) return config.getAsReadOnly(); return getReplicatedMapConfig("default").getAsReadOnly(); } public ReplicatedMapConfig getReplicatedMapConfig(String name) { ReplicatedMapConfig config; if ((config = lookupByPattern(replicatedMapConfigs, name)) != null) return config; ReplicatedMapConfig defConfig = replicatedMapConfigs.get("default"); if (defConfig == null) { defConfig = new ReplicatedMapConfig(); defConfig.setName("default"); addReplicatedMapConfig(defConfig); } config = new ReplicatedMapConfig(defConfig); config.setName(name); addReplicatedMapConfig(config); return config; } public Config addReplicatedMapConfig(ReplicatedMapConfig replicatedMapConfig) { replicatedMapConfigs.put(replicatedMapConfig.getName(), replicatedMapConfig); return this; } public Map getReplicatedMapConfigs() { return replicatedMapConfigs; } public Config setReplicatedMapConfigs(Map replicatedMapConfigs) { this.replicatedMapConfigs.clear(); this.replicatedMapConfigs.putAll(replicatedMapConfigs); for (final Entry entry : this.replicatedMapConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public TopicConfig findTopicConfig(String name){ name = getBaseName(name); TopicConfig config; if ((config = lookupByPattern(topicConfigs, name)) != null) return config.getAsReadOnly(); return getTopicConfig("default").getAsReadOnly(); } public TopicConfig getTopicConfig(String name) { name = getBaseName(name); TopicConfig config; if ((config = lookupByPattern(topicConfigs, name)) != null) { return config; } TopicConfig defConfig = topicConfigs.get("default"); if (defConfig == null) { defConfig = new TopicConfig(); defConfig.setName("default"); addTopicConfig(defConfig); } config = new TopicConfig(defConfig); config.setName(name); addTopicConfig(config); return config; } public Config addTopicConfig(TopicConfig topicConfig) { topicConfigs.put(topicConfig.getName(), topicConfig); return this; } /** * @return the topicConfigs */ public Map getTopicConfigs() { return topicConfigs; } /** * @param mapTopicConfigs the topicConfigs to set */ public Config setTopicConfigs(Map mapTopicConfigs) { this.topicConfigs.clear(); this.topicConfigs.putAll(mapTopicConfigs); for (final Entry entry : this.topicConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public ExecutorConfig findExecutorConfig(String name){ name = getBaseName(name); ExecutorConfig config; if ((config = lookupByPattern(executorConfigs, name)) != null) return config.getAsReadOnly(); return getExecutorConfig("default").getAsReadOnly(); } /** * Returns the ExecutorConfig for the given name * * @param name name of the executor config * @return ExecutorConfig */ public ExecutorConfig getExecutorConfig(String name) { name = getBaseName(name); ExecutorConfig config; if ((config = lookupByPattern(executorConfigs, name)) != null) return config; ExecutorConfig defConfig = executorConfigs.get("default"); if (defConfig == null) { defConfig = new ExecutorConfig(); defConfig.setName("default"); addExecutorConfig(defConfig); } config = new ExecutorConfig(defConfig); config.setName(name); addExecutorConfig(config); return config; } /** * Adds a new ExecutorConfig by name * * @param executorConfig executor config to add * @return this config instance */ public Config addExecutorConfig(ExecutorConfig executorConfig) { this.executorConfigs.put(executorConfig.getName(), executorConfig); return this; } public Map getExecutorConfigs() { return executorConfigs; } public Config setExecutorConfigs(Map executorConfigs) { this.executorConfigs.clear(); this.executorConfigs.putAll(executorConfigs); for (Entry entry : executorConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public SemaphoreConfig findSemaphoreConfig(String name){ name = getBaseName(name); SemaphoreConfig config; if ((config = lookupByPattern(semaphoreConfigs, name)) != null) return config.getAsReadOnly(); return getSemaphoreConfig("default").getAsReadOnly(); } /** * Returns the SemaphoreConfig for the given name * * @param name name of the semaphore config * @return SemaphoreConfig */ public SemaphoreConfig getSemaphoreConfig(String name) { name = getBaseName(name); SemaphoreConfig config; if ((config = lookupByPattern(semaphoreConfigs, name)) != null) return config; SemaphoreConfig defConfig = semaphoreConfigs.get("default"); if (defConfig == null) { defConfig = new SemaphoreConfig(); defConfig.setName("default"); addSemaphoreConfig(defConfig); } config = new SemaphoreConfig(defConfig); config.setName(name); addSemaphoreConfig(config); return config; } /** * Adds a new SemaphoreConfig by name * * @param semaphoreConfig semaphore config to add * @return this config instance */ public Config addSemaphoreConfig(SemaphoreConfig semaphoreConfig) { this.semaphoreConfigs.put(semaphoreConfig.getName(), semaphoreConfig); return this; } /** * Returns the collection of semaphore configs. * * @return collection of semaphore configs. */ public Collection getSemaphoreConfigs() { return semaphoreConfigs.values(); } public Config setSemaphoreConfigs(Map semaphoreConfigs) { this.semaphoreConfigs.clear(); this.semaphoreConfigs.putAll(semaphoreConfigs); for (final Entry entry : this.semaphoreConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public WanReplicationConfig getWanReplicationConfig(String name) { return wanReplicationConfigs.get(name); } public Config addWanReplicationConfig(WanReplicationConfig wanReplicationConfig) { wanReplicationConfigs.put(wanReplicationConfig.getName(), wanReplicationConfig); return this; } public Map getWanReplicationConfigs() { return wanReplicationConfigs; } public Config setWanReplicationConfigs(Map wanReplicationConfigs) { this.wanReplicationConfigs.clear(); this.wanReplicationConfigs.putAll(wanReplicationConfigs); return this; } public JobTrackerConfig findJobTrackerConfig(String name) { name = getBaseName(name); JobTrackerConfig config; if ((config = lookupByPattern(jobTrackerConfigs, name)) != null) return config.getAsReadOnly(); return getJobTrackerConfig(name); } public JobTrackerConfig getJobTrackerConfig(String name) { name = getBaseName(name); JobTrackerConfig config; if ((config = lookupByPattern(jobTrackerConfigs, name)) != null) return config; JobTrackerConfig defConfig = jobTrackerConfigs.get("default"); if (defConfig == null) { defConfig = new JobTrackerConfig(); defConfig.setName("default"); addJobTrackerConfig(defConfig); } config = new JobTrackerConfig(defConfig); config.setName(name); addJobTrackerConfig(config); return config; } public Config addJobTrackerConfig(JobTrackerConfig jobTrackerConfig) { jobTrackerConfigs.put(jobTrackerConfig.getName(), jobTrackerConfig); return this; } public Map getJobTrackerConfigs() { return jobTrackerConfigs; } public Config setJobTrackerConfigs(Map jobTrackerConfigs) { this.jobTrackerConfigs.clear(); this.jobTrackerConfigs.putAll(jobTrackerConfigs); for (final Entry entry : this.jobTrackerConfigs.entrySet()) { entry.getValue().setName(entry.getKey()); } return this; } public ManagementCenterConfig getManagementCenterConfig() { return managementCenterConfig; } public Config setManagementCenterConfig(ManagementCenterConfig managementCenterConfig) { this.managementCenterConfig = managementCenterConfig; return this; } public ServicesConfig getServicesConfig() { return servicesConfig; } public Config setServicesConfig(ServicesConfig servicesConfig) { this.servicesConfig = servicesConfig; return this; } public SecurityConfig getSecurityConfig() { return securityConfig; } public Config setSecurityConfig(SecurityConfig securityConfig) { this.securityConfig = securityConfig; return this; } public Config addListenerConfig(ListenerConfig listenerConfig) { getListenerConfigs().add(listenerConfig); return this; } public List getListenerConfigs() { return listenerConfigs; } public Config setListenerConfigs(List listenerConfigs) { this.listenerConfigs.clear(); this.listenerConfigs.addAll(listenerConfigs); return this; } public SerializationConfig getSerializationConfig() { return serializationConfig; } public Config setSerializationConfig(SerializationConfig serializationConfig) { this.serializationConfig = serializationConfig; return this; } public PartitionGroupConfig getPartitionGroupConfig() { return partitionGroupConfig; } public Config setPartitionGroupConfig(PartitionGroupConfig partitionGroupConfig) { this.partitionGroupConfig = partitionGroupConfig; return this; } public ManagedContext getManagedContext() { return managedContext; } public Config setManagedContext(final ManagedContext managedContext) { this.managedContext = managedContext; return this; } public ConcurrentMap getUserContext() { return userContext; } public Config setUserContext(ConcurrentMap userContext) { if(userContext == null){ throw new IllegalArgumentException("userContext can't be null"); } this.userContext = userContext; return this; } /** * @return the configurationUrl */ public URL getConfigurationUrl() { return configurationUrl; } /** * @param configurationUrl the configurationUrl to set */ public Config setConfigurationUrl(URL configurationUrl) { this.configurationUrl = configurationUrl; return this; } /** * @return the configurationFile */ public File getConfigurationFile() { return configurationFile; } /** * @param configurationFile the configurationFile to set */ public Config setConfigurationFile(File configurationFile) { this.configurationFile = configurationFile; return this; } public String getLicenseKey() { return licenseKey; } public Config setLicenseKey(final String licenseKey) { this.licenseKey = licenseKey; return this; } private static T lookupByPattern(Map map, String name) { T t = map.get(name); if (t == null) { for (Map.Entry entry : map.entrySet()) { String pattern = entry.getKey(); T value = entry.getValue(); if (nameMatches(name, pattern)) { return value; } } } return t; } public static boolean nameMatches(final String name, final String pattern) { final int index = pattern.indexOf('*'); if (index == -1) { return name.equals(pattern); } else { final String firstPart = pattern.substring(0, index); final int indexFirstPart = name.indexOf(firstPart, 0); if (indexFirstPart == -1) { return false; } final String secondPart = pattern.substring(index + 1); final int indexSecondPart = name.indexOf(secondPart, index + 1); return indexSecondPart != -1; } } /** * @param config * @return true if config is compatible with this one, * false if config belongs to another group * @throws RuntimeException if map, queue, topic configs are incompatible */ public boolean isCompatible(final Config config) { if (config == null) { throw new IllegalArgumentException("Expected not null config"); } if (!this.groupConfig.getName().equals(config.getGroupConfig().getName())) { return false; } if (!this.groupConfig.getPassword().equals(config.getGroupConfig().getPassword())) { throw new HazelcastException("Incompatible group password"); } checkMapConfigCompatible(config); return true; } private void checkMapConfigCompatible(final Config config) { Set mapConfigNames = new HashSet(mapConfigs.keySet()); mapConfigNames.addAll(config.mapConfigs.keySet()); for (final String name : mapConfigNames) { final MapConfig thisMapConfig = lookupByPattern(mapConfigs, name); final MapConfig thatMapConfig = lookupByPattern(config.mapConfigs, name); if (thisMapConfig != null && thatMapConfig != null && !thisMapConfig.isCompatible(thatMapConfig)) { throw new HazelcastException(format("Incompatible map config this:\n{0}\nanother:\n{1}", thisMapConfig, thatMapConfig)); } } } @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("Config"); sb.append("{groupConfig=").append(groupConfig); sb.append(", properties=").append(properties); sb.append(", networkConfig=").append(networkConfig); sb.append(", mapConfigs=").append(mapConfigs); sb.append(", topicConfigs=").append(topicConfigs); sb.append(", queueConfigs=").append(queueConfigs); sb.append(", multiMapConfigs=").append(multiMapConfigs); sb.append(", executorConfigs=").append(executorConfigs); sb.append(", semaphoreConfigs=").append(semaphoreConfigs); sb.append(", wanReplicationConfigs=").append(wanReplicationConfigs); sb.append(", listenerConfigs=").append(listenerConfigs); sb.append(", partitionGroupConfig=").append(partitionGroupConfig); sb.append(", managementCenterConfig=").append(managementCenterConfig); sb.append(", securityConfig=").append(securityConfig); sb.append('}'); return sb.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy