Search in sources :

Example 1 with ClusterConfigException

use of com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException in project dal by ctripcorp.

the class DefaultIdGeneratorConfigXMLParser method parse.

private ClusterIdGeneratorConfig parse(String clusterName, Document doc) {
    Element root = doc.getDocumentElement();
    if (root == null || !ClusterConfigXMLConstants.ID_GENERATORS.equalsIgnoreCase(root.getTagName()))
        throw new ClusterConfigException("root element should be <IdGenerators>");
    List<Node> idGeneratorNodes = getChildNodes(root, ID_GENERATOR);
    if (idGeneratorNodes.size() > 1)
        throw new DalRuntimeException("the count of <IdGenerator> nodes should be only one");
    if (idGeneratorNodes.size() == 0)
        throw new DalRuntimeException("<IdGenerator> node does not exist");
    return parse(clusterName, idGeneratorNodes.get(0));
}
Also used : ClusterConfigException(com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node)

Example 2 with ClusterConfigException

use of com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException in project dal by ctripcorp.

the class ClusterConfigXMLParser method parse.

@Override
public ClusterConfig parse(String content, DalConfigCustomizedOption customizedOption) {
    StringReader reader = new StringReader(content);
    InputSource source = new InputSource(reader);
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        Document doc = factory.newDocumentBuilder().parse(source);
        return parse(doc, customizedOption);
    } catch (Throwable t) {
        throw new ClusterConfigException("parse cluster config error", t);
    }
}
Also used : ClusterConfigException(com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringReader(java.io.StringReader)

Example 3 with ClusterConfigException

use of com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException in project dal by ctripcorp.

the class ClusterConfigImpl method setDefaultStrategy.

public void setDefaultStrategy(ShardStrategy shardStrategy) {
    if (shardStrategy == null)
        return;
    if (defaultShardStrategy != null)
        throw new ClusterConfigException("default shard strategy already defined");
    defaultShardStrategy = shardStrategy;
    addShardStrategy(shardStrategy);
}
Also used : ClusterConfigException(com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)

Example 4 with ClusterConfigException

use of com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException in project dal by ctripcorp.

the class ClusterConfigXMLParser method parse.

private ClusterConfig parse(Document doc, DalConfigCustomizedOption customizedOption) throws IOException {
    Element root = doc.getDocumentElement();
    if (root == null || !DAL.equalsIgnoreCase(root.getTagName()))
        throw new ClusterConfigException("root element should be <DAL>");
    Node clusterNode = getChildNode(root, CLUSTER);
    if (clusterNode == null)
        throw new ClusterConfigException("cluster element not found");
    return parseCluster(clusterNode, customizedOption);
}
Also used : ClusterConfigException(com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)

Example 5 with ClusterConfigException

use of com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException in project dal by ctripcorp.

the class ClusterConfigXMLParser method parseCluster.

private ClusterConfig parseCluster(Node clusterNode, DalConfigCustomizedOption customizedOption) throws IOException {
    String name = getAttribute(clusterNode, NAME);
    if (StringUtils.isEmpty(name))
        throw new ClusterConfigException("cluster name undefined");
    ClusterType clusterType = ClusterType.NORMAL;
    String clusterTypeText = getAttribute(clusterNode, TYPE);
    if (!StringUtils.isEmpty(clusterTypeText))
        clusterType = ClusterType.parse(clusterTypeText);
    DatabaseCategory dbCategory = DatabaseCategory.parse(getAttribute(clusterNode, DB_CATEGORY));
    int version = Integer.parseInt(getAttribute(clusterNode, VERSION));
    ClusterConfigImpl clusterConfig = new ClusterConfigImpl(name, clusterType, dbCategory, version);
    clusterConfig.setCustomizedOption(customizedOption);
    Node databaseShardsNode = getChildNode(clusterNode, DATABASE_SHARDS);
    if (databaseShardsNode != null) {
        List<Node> databaseShardNodes = getChildNodes(databaseShardsNode, DATABASE_SHARD);
        for (Node databaseShardNode : databaseShardNodes) parseDatabaseShard(clusterConfig, databaseShardNode);
    }
    Node shardStrategiesNode = getChildNode(clusterNode, SHARD_STRATEGIES);
    if (shardStrategiesNode != null)
        parseShardStrategies(clusterConfig, shardStrategiesNode);
    Node idGeneratorsNode = getChildNode(clusterNode, ID_GENERATORS);
    if (idGeneratorsNode != null)
        parseIdGenerators(clusterConfig, idGeneratorsNode);
    Node routeStrategiesNode = getChildNode(clusterNode, ROUTE_STRATEGIES);
    if (!StringUtils.isEmpty(customizedOption.getRouteStrategy())) {
        initRouteStrategy(clusterConfig, customizedOption);
    } else if (routeStrategiesNode != null)
        parseRouteStrategies(clusterConfig, routeStrategiesNode);
    else
        initRouteStrategy(clusterConfig);
    parseDrcConfig(clusterConfig, clusterNode);
    return clusterConfig;
}
Also used : ClusterConfigException(com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException) DatabaseCategory(com.ctrip.framework.dal.cluster.client.database.DatabaseCategory) ClusterType(com.ctrip.framework.dal.cluster.client.cluster.ClusterType)

Aggregations

ClusterConfigException (com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)6 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ClusterType (com.ctrip.framework.dal.cluster.client.cluster.ClusterType)1 DatabaseCategory (com.ctrip.framework.dal.cluster.client.database.DatabaseCategory)1 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)1 StringReader (java.io.StringReader)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 InputSource (org.xml.sax.InputSource)1