org.zodiac.sentinel.base.config.SentinelDataSourceConfigInfo Maven / Gradle / Ivy
package org.zodiac.sentinel.base.config;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.zodiac.sdk.toolkit.util.lang.ObjUtil;
import org.zodiac.sentinel.base.datasource.model.AbstractDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelApolloDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelFileDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelNacosDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelRedisDataSourceInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* @deprecated 使用各个数据源实现包的功能代替。
*
*/
public class SentinelDataSourceConfigInfo {
private final SentinelFileDataSourceInfo file = new SentinelFileDataSourceInfo();
private final SentinelApolloDataSourceInfo apollo = new SentinelApolloDataSourceInfo();
private final SentinelNacosDataSourceInfo nacos = new SentinelNacosDataSourceInfo();
private final SentinelRedisDataSourceInfo redis = new SentinelRedisDataSourceInfo();
public SentinelDataSourceConfigInfo() {
super();
}
public SentinelFileDataSourceInfo getFile() {
return file;
}
public SentinelApolloDataSourceInfo getApollo() {
return apollo;
}
public SentinelNacosDataSourceInfo getNacos() {
return nacos;
}
public SentinelRedisDataSourceInfo getRedis() {
return redis;
}
@JsonIgnore
public List getValidField() {
return Arrays.stream(this.getClass().getDeclaredFields()).map(field -> {
try {
if (ObjUtil.isNotEmptyObject(field.get(this))) {
return field.getName();
}
} catch (IllegalAccessException e) {
/*Won't happen.*/
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
}
@JsonIgnore
public AbstractDataSourceInfo getValidDataSourceInfo() {
List invalidFields = getValidField();
if (invalidFields.size() == 1) {
try {
this.getClass().getDeclaredField(invalidFields.get(0))
.setAccessible(true);
return (AbstractDataSourceInfo) this.getClass()
.getDeclaredField(invalidFields.get(0)).get(this);
} catch (IllegalAccessException e) {
/*Won't happen.*/
} catch (NoSuchFieldException e) {
/*Won't happen.*/
}
}
return null;
}
@Override
public int hashCode() {
return Objects.hash(apollo, file, nacos, redis);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SentinelDataSourceConfigInfo other = (SentinelDataSourceConfigInfo)obj;
return Objects.equals(apollo, other.apollo) && Objects.equals(file, other.file)
&& Objects.equals(nacos, other.nacos) && Objects.equals(redis, other.redis);
}
@Override
public String toString() {
return "SentinelDataSourceConfigInfo [file=" + file + ", apollo=" + apollo + ", nacos=" + nacos + ", redis="
+ redis + "]";
}
}