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

io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Copyright 2016-2018 shardingsphere.io.
 * 

* 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 io.shardingsphere.jdbc.orchestration.internal.config; import com.google.common.base.Strings; import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; import io.shardingsphere.core.api.config.ShardingRuleConfiguration; import io.shardingsphere.jdbc.orchestration.internal.json.DataSourceJsonConverter; import io.shardingsphere.jdbc.orchestration.internal.json.GsonFactory; import io.shardingsphere.jdbc.orchestration.internal.json.MasterSlaveRuleConfigurationConverter; import io.shardingsphere.jdbc.orchestration.internal.json.ShardingRuleConfigurationConverter; import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter; import javax.sql.DataSource; import java.util.Map; import java.util.Properties; /** * Configuration service. * * @author caohao * @author zhangliang */ public final class ConfigurationService { private final ConfigurationNode configNode; private final RegistryCenter regCenter; public ConfigurationService(final String name, final RegistryCenter regCenter) { configNode = new ConfigurationNode(name); this.regCenter = regCenter; } /** * Persist sharding configuration. * * @param dataSourceMap data source map * @param shardingRuleConfig sharding rule configuration * @param configMap config map * @param props sharding properties * @param isOverwrite is overwrite registry center's configuration */ public void persistShardingConfiguration( final Map dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Map configMap, final Properties props, final boolean isOverwrite) { persistDataSourceConfiguration(dataSourceMap, isOverwrite); persistShardingRuleConfiguration(shardingRuleConfig, isOverwrite); persistShardingConfigMap(configMap, isOverwrite); persistShardingProperties(props, isOverwrite); } /** * Adjust has data source configuration or not in registry center. * * @return has data source configuration or not */ public boolean hasDataSourceConfiguration() { return regCenter.isExisted(configNode.getFullPath(ConfigurationNode.DATA_SOURCE_NODE_PATH)); } /** * Adjust has sharding rule configuration or not in registry center. * * @return has sharding rule configuration or not */ public boolean hasShardingRuleConfiguration() { return regCenter.isExisted(configNode.getFullPath(ConfigurationNode.SHARDING_RULE_NODE_PATH)); } /** * Adjust has sharding config map or not in registry center. * * @return has sharding config map or not */ public boolean hasShardingConfigMap() { return regCenter.isExisted(configNode.getFullPath(ConfigurationNode.SHARDING_CONFIG_MAP_NODE_PATH)); } /** * Adjust has sharding properties or not in registry center. * * @return has sharding properties or not */ public boolean hasShardingProperties() { return regCenter.isExisted(configNode.getFullPath(ConfigurationNode.SHARDING_PROPS_NODE_PATH)); } private void persistDataSourceConfiguration(final Map dataSourceMap, final boolean isOverwrite) { if (isOverwrite || !hasDataSourceConfiguration()) { regCenter.persist(configNode.getFullPath(ConfigurationNode.DATA_SOURCE_NODE_PATH), DataSourceJsonConverter.toJson(dataSourceMap)); } } private void persistShardingRuleConfiguration(final ShardingRuleConfiguration shardingRuleConfig, final boolean isOverwrite) { if (isOverwrite || !hasShardingRuleConfiguration()) { regCenter.persist(configNode.getFullPath(ConfigurationNode.SHARDING_RULE_NODE_PATH), ShardingRuleConfigurationConverter.toJson(shardingRuleConfig)); } } private void persistShardingConfigMap(final Map configMap, final boolean isOverwrite) { if (isOverwrite || !hasShardingConfigMap()) { regCenter.persist(configNode.getFullPath(ConfigurationNode.SHARDING_CONFIG_MAP_NODE_PATH), GsonFactory.getGson().toJson(configMap)); } } private void persistShardingProperties(final Properties props, final boolean isOverwrite) { if (isOverwrite || !hasShardingProperties()) { regCenter.persist(configNode.getFullPath(ConfigurationNode.SHARDING_PROPS_NODE_PATH), GsonFactory.getGson().toJson(props)); } } /** * Persist master-slave configuration. * * @param dataSourceMap data source map * @param masterSlaveRuleConfig master-slave rule configuration * @param configMap config map * @param isOverwrite is overwrite registry center's configuration */ public void persistMasterSlaveConfiguration( final Map dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Map configMap, final boolean isOverwrite) { persistDataSourceConfiguration(dataSourceMap, isOverwrite); persistMasterSlaveRuleConfiguration(masterSlaveRuleConfig, isOverwrite); persistMasterSlaveConfigMap(configMap, isOverwrite); } /** * Adjust has master-slave rule configuration or not in registry center. * * @return has master-slave rule configuration or not */ public boolean hasMasterSlaveRuleConfiguration() { return regCenter.isExisted(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_RULE_NODE_PATH)); } /** * Adjust has master-slave config map or not in registry center. * * @return has master-slave config map or not */ public boolean hasMasterSlaveConfigMap() { return regCenter.isExisted(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_CONFIG_MAP_NODE_PATH)); } private void persistMasterSlaveRuleConfiguration(final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final boolean isOverwrite) { if (isOverwrite || !hasMasterSlaveRuleConfiguration()) { regCenter.persist(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_RULE_NODE_PATH), MasterSlaveRuleConfigurationConverter.toJson(masterSlaveRuleConfig)); } } private void persistMasterSlaveConfigMap(final Map configMap, final boolean isOverwrite) { if (isOverwrite || !hasMasterSlaveConfigMap()) { regCenter.persist(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_CONFIG_MAP_NODE_PATH), GsonFactory.getGson().toJson(configMap)); } } /** * Load data source configuration. * * @return data source configuration map */ public Map loadDataSourceMap() { return DataSourceJsonConverter.fromJson(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.DATA_SOURCE_NODE_PATH))); } /** * Load sharding rule configuration. * * @return sharding rule configuration */ public ShardingRuleConfiguration loadShardingRuleConfiguration() { return ShardingRuleConfigurationConverter.fromJson(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.SHARDING_RULE_NODE_PATH))); } /** * Load sharding config map. * * @return sharding config map */ @SuppressWarnings("unchecked") public Map loadShardingConfigMap() { return GsonFactory.getGson().fromJson(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.SHARDING_CONFIG_MAP_NODE_PATH)), Map.class); } /** * Load sharding properties configuration. * * @return sharding properties */ public Properties loadShardingProperties() { String data = regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.SHARDING_PROPS_NODE_PATH)); return Strings.isNullOrEmpty(data) ? new Properties() : GsonFactory.getGson().fromJson(data, Properties.class); } /** * Load master-slave rule configuration. * * @return master-slave rule configuration */ public MasterSlaveRuleConfiguration loadMasterSlaveRuleConfiguration() { return MasterSlaveRuleConfigurationConverter.fromJson(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_RULE_NODE_PATH))); } /** * Load master-slave config map. * * @return master-slave config map */ @SuppressWarnings("unchecked") public Map loadMasterSlaveConfigMap() { return GsonFactory.getGson().fromJson(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_CONFIG_MAP_NODE_PATH)), Map.class); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy