io.shardingjdbc.spring.namespace.parser.MasterSlaveDataSourceBeanDefinitionParser Maven / Gradle / Ivy
The newest version!
/*
* Copyright 1999-2015 dangdang.com.
*
* 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.shardingjdbc.spring.namespace.parser;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType;
import io.shardingjdbc.spring.datasource.SpringMasterSlaveDataSource;
import io.shardingjdbc.spring.namespace.constants.MasterSlaveDataSourceBeanDefinitionParserTag;
import io.shardingjdbc.spring.namespace.constants.ShardingDataSourceBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Master-slave data source parser for spring namespace.
*
* @author zhangliang
*/
public class MasterSlaveDataSourceBeanDefinitionParser extends AbstractBeanDefinitionParser {
@Override
//CHECKSTYLE:OFF
protected AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) {
//CHECKSTYLE:ON
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(SpringMasterSlaveDataSource.class);
factory.addConstructorArgValue(parseId(element));
String masterDataSourceName = parseMasterDataSourceRef(element);
factory.addConstructorArgValue(masterDataSourceName);
factory.addConstructorArgReference(masterDataSourceName);
factory.addConstructorArgValue(parseSlaveDataSources(element));
String strategyRef = parseStrategyRef(element);
if (!Strings.isNullOrEmpty(strategyRef)) {
factory.addConstructorArgReference(strategyRef);
} else {
factory.addConstructorArgValue(parseStrategyType(element));
}
factory.addConstructorArgValue(parseConfigMap(element, parserContext, factory.getBeanDefinition()));
return factory.getBeanDefinition();
}
private String parseId(final Element element) {
return element.getAttribute(ID_ATTRIBUTE);
}
private String parseMasterDataSourceRef(final Element element) {
return element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.MASTER_DATA_SOURCE_NAME_ATTRIBUTE);
}
private Map parseSlaveDataSources(final Element element) {
List slaveDataSources = Splitter.on(",").trimResults().splitToList(element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.SLAVE_DATA_SOURCE_NAMES_ATTRIBUTE));
Map result = new ManagedMap<>(slaveDataSources.size());
for (String each : slaveDataSources) {
result.put(each, new RuntimeBeanReference(each));
}
return result;
}
private String parseStrategyRef(final Element element) {
return element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.STRATEGY_REF_ATTRIBUTE);
}
private MasterSlaveLoadBalanceAlgorithmType parseStrategyType(final Element element) {
String result = element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.STRATEGY_TYPE_ATTRIBUTE);
return Strings.isNullOrEmpty(result) ? MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType() : MasterSlaveLoadBalanceAlgorithmType.valueOf(result);
}
private Map parseConfigMap(final Element element, final ParserContext parserContext, final BeanDefinition beanDefinition) {
Element dataElement = DomUtils.getChildElementByTagName(element, ShardingDataSourceBeanDefinitionParserTag.CONFIG_MAP_TAG);
return null == dataElement ? Collections.>emptyMap() : parserContext.getDelegate().parseMapElement(dataElement, beanDefinition);
}
}