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

org.zodiac.sentinel.base.datasource.model.AbstractDataSourceInfo Maven / Gradle / Ivy

package org.zodiac.sentinel.base.datasource.model;

import java.util.Objects;

import javax.validation.constraints.NotNull;

import org.springframework.core.env.Environment;
import org.zodiac.sentinel.base.datasource.DataSourceType;
import org.zodiac.sentinel.base.model.SentinelRule;

import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.fasterxml.jackson.annotation.JsonIgnore;

public abstract class AbstractDataSourceInfo implements DataSourceInfo {

    private final DataSourceType dataSourceType;

    private String dataType = "json";

    @NotNull
    private SentinelRule rule;

    private String converterClass;

    @JsonIgnore
    private final String factoryBeanName;

    @JsonIgnore
    private Environment environment;

    protected AbstractDataSourceInfo(DataSourceType dataSourceType, String factoryBeanName) {
        this.dataSourceType = dataSourceType;
        this.factoryBeanName = factoryBeanName;
    }

    @Override
    public DataSourceType getDataSourceType() {
        return dataSourceType;
    }

    @Override
    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    @Override
    public SentinelRule getRule() {
        return rule;
    }

    public void setRuleType(SentinelRule rule) {
        this.rule = rule;
    }

    @Override
    public String getConverterClass() {
        return converterClass;
    }

    public void setConverterClass(String converterClass) {
        this.converterClass = converterClass;
    }

    @Override
    public String getFactoryBeanName() {
        return factoryBeanName;
    }

    @Override
    public Environment getEnvironment() {
        return environment;
    }

    @Override
    public AbstractDataSourceInfo setEnvironment(Environment environment) {
        this.environment = environment;
        return this;
    }

    @Override
    public AbstractDataSourceInfo preCheck(String dataSourceName) {
        return this;
    }

    @Override
    public AbstractDataSourceInfo postRegister(AbstractDataSource dataSource) {
        switch (this.getRule()) {
        case FLOW:
            FlowRuleManager.register2Property(dataSource.getProperty());
            break;
        case DEGRADE:
            DegradeRuleManager.register2Property(dataSource.getProperty());
            break;
        case PARAM_FLOW:
            ParamFlowRuleManager.register2Property(dataSource.getProperty());
            break;
        case SYSTEM:
            SystemRuleManager.register2Property(dataSource.getProperty());
            break;
        case AUTHORITY:
            AuthorityRuleManager.register2Property(dataSource.getProperty());
            break;
        case GW_FLOW:
            GatewayRuleManager.register2Property(dataSource.getProperty());
            break;
        case GW_API_GROUP:
            GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
            break;
        default:
            break;
        }
        return this;
    }

    @Override
    public int hashCode() {
        return Objects.hash(converterClass, dataType, rule);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        AbstractDataSourceInfo other = (AbstractDataSourceInfo)obj;
        return Objects.equals(converterClass, other.converterClass) && Objects.equals(dataType, other.dataType)
            && rule == other.rule;
    }

    @Override
    public String toString() {
        return "AbstractSentinelDataSourceInfo [dataType=" + dataType + ", rule=" + rule + ", converterClass="
            + converterClass + "]";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy