Merge branch 'dev-test' of gitee.com:dromara/sms_aggregation into dev-test

Signed-off-by: 李泰然 <995608517@qq.com>
This commit is contained in:
李泰然 2023-04-11 15:24:40 +00:00 committed by Gitee
commit 6d99c5fcd2
6 changed files with 184 additions and 17 deletions

View File

@ -6,9 +6,12 @@ import org.dromara.sms.autoimmit.utils.RedisUtils;
import org.dromara.sms.autoimmit.utils.SpringUtil; import org.dromara.sms.autoimmit.utils.SpringUtil;
import org.dromara.sms.comm.config.SmsBanner; import org.dromara.sms.comm.config.SmsBanner;
import org.dromara.sms.comm.config.SmsConfig; import org.dromara.sms.comm.config.SmsConfig;
import org.dromara.sms.comm.config.SmsSqlConfig;
import org.dromara.sms.comm.delayedTime.DelayedTime; import org.dromara.sms.comm.delayedTime.DelayedTime;
import org.dromara.sms.comm.factory.BeanFactory; import org.dromara.sms.comm.factory.BeanFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.sms.core.SupplierSqlConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -25,6 +28,10 @@ public class SmsAutowiredConfig {
this.springUtil = springUtil; this.springUtil = springUtil;
} }
@Bean
@ConfigurationProperties(prefix = "sms.sql")
protected SmsSqlConfig smsSqlConfig(){return BeanFactory.getSmsSqlConfig();}
@Bean @Bean
@ConfigurationProperties(prefix = "sms") //指定配置文件注入属性前缀 @ConfigurationProperties(prefix = "sms") //指定配置文件注入属性前缀
protected SmsConfig smsConfig(){ protected SmsConfig smsConfig(){
@ -50,10 +57,17 @@ public class SmsAutowiredConfig {
} }
@Bean @Bean
protected SupplierConfig supplierConfig(){ @ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "config_file")
protected SupplierConfig supplierConfig(SmsConfig smsConfig){
return new SupplierConfig(); return new SupplierConfig();
} }
@Bean
@ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "sql_config")
protected void supplierSqlConfig(){
new SupplierSqlConfig();
}
void init(){ void init(){
/* 如果配置中启用了redis则注入redis工具*/ /* 如果配置中启用了redis则注入redis工具*/

View File

@ -3,6 +3,7 @@ package org.dromara.sms.autoimmit.config;
import kim.wind.emay.config.EmayConfig; import kim.wind.emay.config.EmayConfig;
import org.dromara.sms.aliyun.config.AlibabaConfig; import org.dromara.sms.aliyun.config.AlibabaConfig;
import org.dromara.sms.cloopen.config.CloopenConfig; import org.dromara.sms.cloopen.config.CloopenConfig;
import org.dromara.sms.comm.config.SmsConfig;
import org.dromara.sms.core.config.SupplierFactory; import org.dromara.sms.core.config.SupplierFactory;
import org.dromara.sms.huawei.config.HuaweiConfig; import org.dromara.sms.huawei.config.HuaweiConfig;
import org.dromara.sms.jdcloud.config.JdCloudConfig; import org.dromara.sms.jdcloud.config.JdCloudConfig;
@ -13,6 +14,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
public class SupplierConfig { public class SupplierConfig {
/** 阿里差异化配置*/ /** 阿里差异化配置*/
@Bean @Bean
@ConfigurationProperties(prefix = "sms.alibaba") @ConfigurationProperties(prefix = "sms.alibaba")

View File

@ -48,7 +48,7 @@ public class BeanFactory {
/** 获取请求用的对象*/ /** 获取请求用的对象*/
public static ForestConfiguration getForestConfiguration() { public static ForestConfiguration getForestConfiguration() {
if (forestConfiguration == null){ if (forestConfiguration == null){
forestConfiguration = Forest.config().setBackendName("httpclient").setLogEnabled(smsConfig.getHttpLog()); forestConfiguration = Forest.config().setBackendName("httpclient").setLogEnabled(getSmsConfig().getHttpLog());
} }
return forestConfiguration; return forestConfiguration;
} }

View File

@ -3,6 +3,7 @@ package org.dromara.sms.comm.utils;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import org.dromara.sms.comm.config.SmsSqlConfig; import org.dromara.sms.comm.config.SmsSqlConfig;
import org.dromara.sms.comm.exception.SmsSqlException; import org.dromara.sms.comm.exception.SmsSqlException;
import org.dromara.sms.comm.factory.BeanFactory;
import java.sql.*; import java.sql.*;
import java.util.Hashtable; import java.util.Hashtable;
@ -17,15 +18,13 @@ import java.util.Map;
**/ **/
public class JDBCTool { public class JDBCTool {
private final SmsSqlConfig config;
public static final String SELECT = "select {},{} from {} where {} = {}"; public static final String SELECT = "select {},{} from {} where {} = {}";
private static final String ERR_CONFIG = "One configuration was expected, but {} was found. Please check your database configuration"; private static final String ERR_CONFIG = "One configuration was expected, but {} was found. Please check your database configuration";
private final SmsSqlConfig config;
public JDBCTool(SmsSqlConfig config) { public JDBCTool(SmsSqlConfig config) {
if (config == null){ if (config == null) {
throw new SmsSqlException("The configuration file failed to be loaded. Procedure"); throw new SmsSqlException("The configuration file failed to be loaded. Procedure");
} }
this.config = config; this.config = config;
@ -34,6 +33,14 @@ public class JDBCTool {
} }
} }
public static Map<String, String> selectConfig() {
Map<String, String> select = BeanFactory.getJDBCTool().select();
if (select.size() == 0) {
throw new SmsSqlException("No valid configuration was scanned. Please check whether the configuration file or database link is normal");
}
return select;
}
/** /**
* 获取链接 * 获取链接
*/ */
@ -49,16 +56,17 @@ public class JDBCTool {
} }
/** /**
* select * select
* <p>查询封装 * <p>查询封装
*
* @param sql 要查询的sql语句 * @param sql 要查询的sql语句
* @author :Wind * @author :Wind
*/ */
public Map<String,String> select(String sql) { public Map<String, String> select(String sql) {
Connection conn = null; Connection conn = null;
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
Map<String,String> map = new Hashtable<>(); Map<String, String> map = new Hashtable<>();
try { try {
conn = getConn(); conn = getConn();
preparedStatement = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
@ -68,10 +76,7 @@ public class JDBCTool {
while (resultSet.next()) { while (resultSet.next()) {
String data = resultSet.getString(config.getConfigName()); String data = resultSet.getString(config.getConfigName());
String supplier = resultSet.getString(config.getSupplierFieldName()); String supplier = resultSet.getString(config.getSupplierFieldName());
map.put(supplier,data); map.put(supplier, data);
}
if (map.size() != 1){
throw new SmsSqlException(StrUtil.format(ERR_CONFIG,String.valueOf(map.size())));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new SmsSqlException(e.getMessage()); throw new SmsSqlException(e.getMessage());
@ -114,14 +119,13 @@ public class JDBCTool {
} }
} }
public Map<String,String> select(){ public Map<String, String> select() {
String format = StrUtil.format(SELECT, String format = StrUtil.format(SELECT,
config.getConfigName(), config.getConfigName(),
config.getSupplierFieldName(), config.getSupplierFieldName(),
config.getDatabaseName(), config.getTableName(),
config.getStartName(), config.getStartName(),
config.getIsStart()); config.getIsStart());
return select(format); return select(format);
} }
} }

View File

@ -1,5 +1,10 @@
package org.dromara.sms.comm.utils; package org.dromara.sms.comm.utils;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.dromara.sms.comm.exception.SmsSqlException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.List; import java.util.List;
@ -95,4 +100,30 @@ public class SmsUtil {
return str.toString(); return str.toString();
} }
/**
* jsonForObject
* <p>将json字符串转化为指定的对象
* @author :Wind
*/
public static <T> T jsonForObject(String json, Class<T> t) {
try {
return json == null||"".equals(json)?null: JSONObject.toJavaObject(JSONObject.parseObject(json), t);
} catch (JSONException e) {
throw new SmsSqlException("json sequence exception" + e.getMessage());
}
}
/**
* copyBean
* <p>拷贝bean只有源对象不为null才会拷贝
* @param t 源对象
* @param m 目标对象
* @author :Wind
*/
public static <T,M>void copyBean(T t,M m){
if (t != null){
BeanUtil.copyProperties(t, m);
}
}
} }

View File

@ -0,0 +1,115 @@
package org.dromara.sms.core;
import org.dromara.sms.aliyun.config.AlibabaConfig;
import org.dromara.sms.cloopen.config.CloopenConfig;
import org.dromara.sms.comm.enumerate.SupplierType;
import org.dromara.sms.comm.utils.JDBCTool;
import org.dromara.sms.comm.utils.SmsUtil;
import org.dromara.sms.core.config.SupplierFactory;
import org.dromara.sms.huawei.config.HuaweiConfig;
import org.dromara.sms.jdcloud.config.JdCloudConfig;
import org.dromara.sms.tencent.config.TencentConfig;
import org.dromara.sms.unisms.config.UniConfig;
import org.dromara.sms.yunpian.config.YunpianConfig;
import java.util.Map;
/**
* SupplierSqlConfig
* <p> 处理sql配置源
* @author :Wind
* 2023/4/11 19:42
**/
public class SupplierSqlConfig {
private static Map<String, String> select;
static {
select = JDBCTool.selectConfig();
}
/**
* SupplierSqlConfig
* <p>在类初始化是完成方法调用
* @author :Wind
*/
public SupplierSqlConfig() {
alibaba();
huawei();
jingdong();
tencent();
uniSms();
yunPian();
cloopen();
}
/**
* alibaba
* <p>数据库读取并设置阿里云短信
* @author :Wind
*/
public static void alibaba(){
AlibabaConfig alibabaConfig = SmsUtil.jsonForObject(select.get(SupplierType.ALIBABA.getName()), AlibabaConfig.class);
SmsUtil.copyBean(alibabaConfig, SupplierFactory.getAlibabaConfig());
}
/**
* huawei
* <p>数据库读取并设置华为短信
* @author :Wind
*/
public static void huawei(){
HuaweiConfig huaweiConfig = SmsUtil.jsonForObject(select.get(SupplierType.HUAWEI.getName()), HuaweiConfig.class);
SmsUtil.copyBean(huaweiConfig, SupplierFactory.getHuaweiConfig());
}
/**
* jingdong
* <p>数据库读取并设置京东短信
* @author :Wind
*/
public static void jingdong(){
JdCloudConfig jdCloudConfig = SmsUtil.jsonForObject(select.get(SupplierType.JD_CLOUD.getName()), JdCloudConfig.class);
SmsUtil.copyBean(jdCloudConfig,SupplierFactory.getJdCloudConfig());
}
/**
* tencent
* <p>数据库读取并设置腾讯短信
* @author :Wind
*/
public static void tencent(){
TencentConfig tencentConfig = SmsUtil.jsonForObject(select.get(SupplierType.TENCENT.getName()), TencentConfig.class);
SmsUtil.copyBean(tencentConfig, SupplierFactory.getTencentConfig());
}
/**
* uniSms
* <p>数据库读取并设置合一短信配置
* @author :Wind
*/
public static void uniSms(){
UniConfig uniConfig = SmsUtil.jsonForObject(select.get(SupplierType.UNI_SMS.getName()), UniConfig.class);
SmsUtil.copyBean(uniConfig,SupplierFactory.getUniConfig());
}
/**
* yunPian
* <p>数据库读取并设置云片短信
* @author :Wind
*/
public static void yunPian(){
YunpianConfig yunpianConfig = SmsUtil.jsonForObject(select.get(SupplierType.YUNPIAN.getName()), YunpianConfig.class);
SmsUtil.copyBean(yunpianConfig,SupplierFactory.getYunpianConfig());
}
/**
* cloopen
* <p>数据库读取并设置荣联云短信
* @author :Wind
*/
public static void cloopen(){
CloopenConfig cloopenConfig = SmsUtil.jsonForObject(select.get(SupplierType.CLOOPEN.getName()), CloopenConfig.class);
SmsUtil.copyBean(cloopenConfig,SupplierFactory.getCloopenConfig());
}
}