diff --git a/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SmsAutowiredConfig.java b/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SmsAutowiredConfig.java index 37a68ed4..e4a8013f 100644 --- a/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SmsAutowiredConfig.java +++ b/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SmsAutowiredConfig.java @@ -6,9 +6,12 @@ import org.dromara.sms.autoimmit.utils.RedisUtils; import org.dromara.sms.autoimmit.utils.SpringUtil; import org.dromara.sms.comm.config.SmsBanner; 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.factory.BeanFactory; 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.context.annotation.Bean; import org.springframework.core.env.Environment; @@ -25,6 +28,10 @@ public class SmsAutowiredConfig { this.springUtil = springUtil; } + @Bean + @ConfigurationProperties(prefix = "sms.sql") + protected SmsSqlConfig smsSqlConfig(){return BeanFactory.getSmsSqlConfig();} + @Bean @ConfigurationProperties(prefix = "sms") //指定配置文件注入属性前缀 protected SmsConfig smsConfig(){ @@ -50,10 +57,17 @@ public class SmsAutowiredConfig { } @Bean - protected SupplierConfig supplierConfig(){ + @ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "config_file") + protected SupplierConfig supplierConfig(SmsConfig smsConfig){ return new SupplierConfig(); } + @Bean + @ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "sql_config") + protected void supplierSqlConfig(){ + new SupplierSqlConfig(); + } + void init(){ /* 如果配置中启用了redis,则注入redis工具*/ diff --git a/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SupplierConfig.java b/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SupplierConfig.java index 01238d04..3f3274e1 100644 --- a/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SupplierConfig.java +++ b/sms-aggregation-autoimmit/src/main/java/org/dromara/sms/autoimmit/config/SupplierConfig.java @@ -3,6 +3,7 @@ package org.dromara.sms.autoimmit.config; import kim.wind.emay.config.EmayConfig; import org.dromara.sms.aliyun.config.AlibabaConfig; 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.huawei.config.HuaweiConfig; import org.dromara.sms.jdcloud.config.JdCloudConfig; @@ -13,6 +14,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; public class SupplierConfig { + + /** 阿里差异化配置*/ @Bean @ConfigurationProperties(prefix = "sms.alibaba") diff --git a/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/factory/BeanFactory.java b/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/factory/BeanFactory.java index c4df841a..9a6141fb 100644 --- a/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/factory/BeanFactory.java +++ b/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/factory/BeanFactory.java @@ -48,7 +48,7 @@ public class BeanFactory { /** 获取请求用的对象*/ public static ForestConfiguration getForestConfiguration() { if (forestConfiguration == null){ - forestConfiguration = Forest.config().setBackendName("httpclient").setLogEnabled(smsConfig.getHttpLog()); + forestConfiguration = Forest.config().setBackendName("httpclient").setLogEnabled(getSmsConfig().getHttpLog()); } return forestConfiguration; } diff --git a/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/JDBCTool.java b/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/JDBCTool.java index 75dbaaa3..b90389fc 100644 --- a/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/JDBCTool.java +++ b/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/JDBCTool.java @@ -3,6 +3,7 @@ package org.dromara.sms.comm.utils; import cn.hutool.core.util.StrUtil; import org.dromara.sms.comm.config.SmsSqlConfig; import org.dromara.sms.comm.exception.SmsSqlException; +import org.dromara.sms.comm.factory.BeanFactory; import java.sql.*; import java.util.Hashtable; @@ -17,15 +18,13 @@ import java.util.Map; **/ public class JDBCTool { - private final SmsSqlConfig config; - 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 final SmsSqlConfig config; public JDBCTool(SmsSqlConfig config) { - if (config == null){ + if (config == null) { throw new SmsSqlException("The configuration file failed to be loaded. Procedure"); } this.config = config; @@ -34,6 +33,14 @@ public class JDBCTool { } } + public static Map selectConfig() { + Map 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 *

查询封装 + * * @param sql 要查询的sql语句 * @author :Wind - */ - public Map select(String sql) { + */ + public Map select(String sql) { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; - Map map = new Hashtable<>(); + Map map = new Hashtable<>(); try { conn = getConn(); preparedStatement = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); @@ -68,10 +76,7 @@ public class JDBCTool { while (resultSet.next()) { String data = resultSet.getString(config.getConfigName()); String supplier = resultSet.getString(config.getSupplierFieldName()); - map.put(supplier,data); - } - if (map.size() != 1){ - throw new SmsSqlException(StrUtil.format(ERR_CONFIG,String.valueOf(map.size()))); + map.put(supplier, data); } } catch (SQLException e) { throw new SmsSqlException(e.getMessage()); @@ -114,14 +119,13 @@ public class JDBCTool { } } - public Map select(){ + public Map select() { String format = StrUtil.format(SELECT, config.getConfigName(), config.getSupplierFieldName(), - config.getDatabaseName(), + config.getTableName(), config.getStartName(), config.getIsStart()); return select(format); } - } diff --git a/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/SmsUtil.java b/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/SmsUtil.java index 92334010..83ef38d8 100644 --- a/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/SmsUtil.java +++ b/sms-aggregation-comm/src/main/java/org/dromara/sms/comm/utils/SmsUtil.java @@ -1,5 +1,10 @@ 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.SecureRandom; import java.util.List; @@ -95,4 +100,30 @@ public class SmsUtil { return str.toString(); } + /** + * jsonForObject + *

将json字符串转化为指定的对象 + * @author :Wind + */ + public static T jsonForObject(String json, Class 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 + *

拷贝bean,只有源对象不为null才会拷贝 + * @param t 源对象 + * @param m 目标对象 + * @author :Wind + */ + public static void copyBean(T t,M m){ + if (t != null){ + BeanUtil.copyProperties(t, m); + } + } + } diff --git a/sms-aggregation-core/src/main/java/org/dromara/sms/core/SupplierSqlConfig.java b/sms-aggregation-core/src/main/java/org/dromara/sms/core/SupplierSqlConfig.java new file mode 100644 index 00000000..8da617fc --- /dev/null +++ b/sms-aggregation-core/src/main/java/org/dromara/sms/core/SupplierSqlConfig.java @@ -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 + *

处理sql配置源 + * @author :Wind + * 2023/4/11 19:42 + **/ +public class SupplierSqlConfig { + private static Map select; + + static { + select = JDBCTool.selectConfig(); + } + + /** + * SupplierSqlConfig + *

在类初始化是完成方法调用 + * @author :Wind + */ + public SupplierSqlConfig() { + alibaba(); + huawei(); + jingdong(); + tencent(); + uniSms(); + yunPian(); + cloopen(); + } + + /** + * alibaba + *

数据库读取并设置阿里云短信 + * @author :Wind + */ + public static void alibaba(){ + AlibabaConfig alibabaConfig = SmsUtil.jsonForObject(select.get(SupplierType.ALIBABA.getName()), AlibabaConfig.class); + SmsUtil.copyBean(alibabaConfig, SupplierFactory.getAlibabaConfig()); + } + + /** + * huawei + *

数据库读取并设置华为短信 + * @author :Wind + */ + public static void huawei(){ + HuaweiConfig huaweiConfig = SmsUtil.jsonForObject(select.get(SupplierType.HUAWEI.getName()), HuaweiConfig.class); + SmsUtil.copyBean(huaweiConfig, SupplierFactory.getHuaweiConfig()); + } + + /** + * jingdong + *

数据库读取并设置京东短信 + * @author :Wind + */ + public static void jingdong(){ + JdCloudConfig jdCloudConfig = SmsUtil.jsonForObject(select.get(SupplierType.JD_CLOUD.getName()), JdCloudConfig.class); + SmsUtil.copyBean(jdCloudConfig,SupplierFactory.getJdCloudConfig()); + } + + /** + * tencent + *

数据库读取并设置腾讯短信 + * @author :Wind + */ + public static void tencent(){ + TencentConfig tencentConfig = SmsUtil.jsonForObject(select.get(SupplierType.TENCENT.getName()), TencentConfig.class); + SmsUtil.copyBean(tencentConfig, SupplierFactory.getTencentConfig()); + } + + /** + * uniSms + *

数据库读取并设置合一短信配置 + * @author :Wind + */ + public static void uniSms(){ + UniConfig uniConfig = SmsUtil.jsonForObject(select.get(SupplierType.UNI_SMS.getName()), UniConfig.class); + SmsUtil.copyBean(uniConfig,SupplierFactory.getUniConfig()); + } + + /** + * yunPian + *

数据库读取并设置云片短信 + * @author :Wind + */ + public static void yunPian(){ + YunpianConfig yunpianConfig = SmsUtil.jsonForObject(select.get(SupplierType.YUNPIAN.getName()), YunpianConfig.class); + SmsUtil.copyBean(yunpianConfig,SupplierFactory.getYunpianConfig()); + } + + /** + * cloopen + *

数据库读取并设置荣联云短信 + * @author :Wind + */ + public static void cloopen(){ + CloopenConfig cloopenConfig = SmsUtil.jsonForObject(select.get(SupplierType.CLOOPEN.getName()), CloopenConfig.class); + SmsUtil.copyBean(cloopenConfig,SupplierFactory.getCloopenConfig()); + } + +}