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

org.zodiac.sentinel.base.datasource.converter.SentinelDataSourceConverter Maven / Gradle / Ivy

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

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.commons.util.Colls;
import org.zodiac.commons.util.lang.Strings;

import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class SentinelDataSourceConverter implements Converter> {

    private static final Logger LOG = LoggerFactory.getLogger(SentinelDataSourceConverter.class);

    private final ObjectMapper objectMapper;

    private final Class ruleClass;

    public SentinelDataSourceConverter(ObjectMapper objectMapper, Class ruleClass) {
        this.objectMapper = objectMapper;
        this.ruleClass = ruleClass;
    }

    @Override
    public Collection convert(String source) {
        Collection ruleCollection;

        if (ruleClass == FlowRule.class || ruleClass == DegradeRule.class
                || ruleClass == SystemRule.class || ruleClass == AuthorityRule.class
                || ruleClass == ParamFlowRule.class) {
            ruleCollection = Colls.list();
        } else {
            ruleCollection = Colls.set();
        }

        if (Strings.isEmpty(source)) {
            LOG.warn("converter can not convert rules because source is empty");
            return ruleCollection;
        }
        try {
            List sourceArray = objectMapper.readValue(source, new TypeReference>() {});

            for (Object obj : sourceArray) {
                try {
                    String item = objectMapper.writeValueAsString(obj);
                    Optional.ofNullable(convertRule(item))
                            .ifPresent(convertRule -> ruleCollection.add(convertRule));
                }
                catch (IOException e) {
                    LOG.error("sentinel rule convert error: " + e.getMessage(), e);
                    throw new IllegalArgumentException("sentinel rule convert error: " + e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new RuntimeException("convert error: " + e.getMessage(), e);
            }
        }
        return ruleCollection;
    }

    private Object convertRule(String ruleStr) throws IOException {
        return objectMapper.readValue(ruleStr, ruleClass);
    }

}