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

org.zodiac.sentinel.base.model.SentinelRule Maven / Gradle / Ivy

package org.zodiac.sentinel.base.model;

import java.util.Arrays;
import java.util.Optional;

import org.springframework.util.StringUtils;

import com.alibaba.csp.sentinel.slots.block.AbstractRule;
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;

public enum SentinelRule {

    /**
     * flow.
     */
    FLOW("flow", FlowRule.class),
    /**
     * degrade.
     */
    DEGRADE("degrade", DegradeRule.class),
    /**
     * param flow.
     */
    PARAM_FLOW("param-flow", ParamFlowRule.class),
    /**
     * system.
     */
    SYSTEM("system", SystemRule.class),
    /**
     * authority.
     */
    AUTHORITY("authority", AuthorityRule.class),
    /**
     * gateway flow.
     */
    GW_FLOW("gw-flow",
            "com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule"),
    /**
     * api.
     */
    GW_API_GROUP("gw-api-group",
            "com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition");

    /**
     * alias for {@link AbstractRule}.
     */
    private final String name;

    /**
     * concrete {@link AbstractRule} class.
     */
    private Class clazz;

    /**
     * concrete {@link AbstractRule} class name.
     */
    private String clazzName;

    SentinelRule(String name, Class clazz) {
        this.name = name;
        this.clazz = clazz;
    }

    SentinelRule(String name, String clazzName) {
        this.name = name;
        this.clazzName = clazzName;
    }

    public String getName() {
        return name;
    }

    public Class getClazz() {
        if (clazz != null) {
            return clazz;
        } else {
            try {
                return Class.forName(clazzName);
            }
            catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static Optional getByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return Optional.empty();
        }
        return Arrays.stream(SentinelRule.values())
                .filter(ruleType -> name.equals(ruleType.getName())).findFirst();
    }

    public static Optional getByClass(Class clazz) {
        return Arrays.stream(SentinelRule.values())
                .filter(ruleType -> clazz == ruleType.getClazz()).findFirst();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy