|
@@ -3,7 +3,7 @@ package com.ruoyi.oss.factory;
|
|
|
import com.ruoyi.common.utils.JsonUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
|
|
-import com.ruoyi.common.utils.reflect.ReflectUtils;
|
|
|
+import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
import com.ruoyi.oss.constant.OssConstant;
|
|
|
import com.ruoyi.oss.enumd.OssEnumd;
|
|
|
import com.ruoyi.oss.exception.OssException;
|
|
@@ -12,9 +12,6 @@ import com.ruoyi.oss.service.IOssStrategy;
|
|
|
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-
|
|
|
/**
|
|
|
* 文件上传Factory
|
|
|
*
|
|
@@ -23,20 +20,16 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
@Slf4j
|
|
|
public class OssFactory {
|
|
|
|
|
|
- /**
|
|
|
- * 服务实例缓存
|
|
|
- */
|
|
|
- private static final Map<String, IOssStrategy> SERVICES = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
/**
|
|
|
* 初始化工厂
|
|
|
*/
|
|
|
public static void init() {
|
|
|
log.info("初始化OSS工厂");
|
|
|
RedisUtils.subscribe(OssConstant.CACHE_CONFIG_KEY, String.class, type -> {
|
|
|
- // 没有的实例不处理
|
|
|
- if (SERVICES.containsKey(type)) {
|
|
|
- refreshService(type);
|
|
|
+ AbstractOssStrategy strategy = getStrategy(type);
|
|
|
+ // 未初始化不处理
|
|
|
+ if (strategy.isInit) {
|
|
|
+ refresh(type);
|
|
|
log.info("订阅刷新OSS配置 => " + type);
|
|
|
}
|
|
|
});
|
|
@@ -58,24 +51,29 @@ public class OssFactory {
|
|
|
* 根据类型获取实例
|
|
|
*/
|
|
|
public static IOssStrategy instance(String type) {
|
|
|
- IOssStrategy service = SERVICES.get(type);
|
|
|
- if (service == null) {
|
|
|
- refreshService(type);
|
|
|
- service = SERVICES.get(type);
|
|
|
+ OssEnumd enumd = OssEnumd.find(type);
|
|
|
+ if (enumd == null) {
|
|
|
+ throw new OssException("文件存储服务类型无法找到!");
|
|
|
+ }
|
|
|
+ AbstractOssStrategy strategy = getStrategy(type);
|
|
|
+ if (!strategy.isInit) {
|
|
|
+ refresh(type);
|
|
|
}
|
|
|
- return service;
|
|
|
+ return strategy;
|
|
|
}
|
|
|
|
|
|
- private static void refreshService(String type) {
|
|
|
+ private static void refresh(String type) {
|
|
|
Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + type);
|
|
|
OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class);
|
|
|
if (properties == null) {
|
|
|
throw new OssException("系统异常, '" + type + "'配置信息不存在!");
|
|
|
}
|
|
|
- // 获取redis配置信息 创建对象 并缓存
|
|
|
- IOssStrategy service = (IOssStrategy) ReflectUtils.newInstance(OssEnumd.getServiceClass(type));
|
|
|
- ((AbstractOssStrategy) service).init(properties);
|
|
|
- SERVICES.put(type, service);
|
|
|
+ getStrategy(type).init(properties);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static AbstractOssStrategy getStrategy(String type) {
|
|
|
+ OssEnumd enumd = OssEnumd.find(type);
|
|
|
+ return (AbstractOssStrategy) SpringUtils.getBean(enumd.getBeanClass());
|
|
|
}
|
|
|
|
|
|
}
|