疯狂的狮子li
2021-09-03 2d6c306ae113a7dd549a8e415ca075b754470dfc
ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java
@@ -11,6 +11,8 @@
import com.ruoyi.oss.exception.OssException;
import com.ruoyi.oss.properties.CloudStorageProperties;
import com.ruoyi.oss.service.ICloudStorageStrategy;
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -20,17 +22,29 @@
 *
 * @author Lion Li
 */
@Slf4j
public class OssFactory {
    private static RedisCache redisCache;
   private static RedisCache redisCache;
   static {
      OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
      redisCache.subscribe(CloudConstant.CACHE_CONFIG_KEY, String.class, msg -> {
         refreshService(msg);
         log.info("订阅刷新OSS配置 => " + msg);
      });
   }
   /**
    * 服务实例缓存
    */
   private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>();
   /**
    * 获取默认实例
    */
   public static ICloudStorageStrategy instance() {
      // 获取redis 默认类型
      String type = Convert.toStr(redisCache.getCacheObject(CloudConstant.CACHE_CONFIG_KEY));
      if (StringUtils.isEmpty(type)) {
         throw new OssException("文件存储服务类型无法找到!");
@@ -38,27 +52,28 @@
      return instance(type);
   }
   /**
    * 根据类型获取实例
    */
   public static ICloudStorageStrategy instance(String type) {
      ICloudStorageStrategy service = SERVICES.get(type);
      if (service == null) {
         Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
         CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
         String beanName = CloudServiceEnumd.getServiceName(type);
         ICloudStorageStrategy bean = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties);
         SpringUtils.registerBean(beanName, bean);
         service = SpringUtils.getBean(beanName);
         SERVICES.put(type, bean);
         refreshService(type);
         service = SERVICES.get(type);
      }
      return service;
   }
   public static void destroy(String type) {
      ICloudStorageStrategy service = SERVICES.get(type);
      if (service == null) {
         return;
   private static void refreshService(String type) {
      Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
      CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
      if (properties == null) {
         throw new OssException("系统异常, '" + type + "'配置信息不存在!");
      }
      SpringUtils.unregisterBean(CloudServiceEnumd.getServiceName(type));
      SERVICES.remove(type);
      // 获取redis配置信息 创建对象 并缓存
      ICloudStorageStrategy service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type));
      ((AbstractCloudStorageStrategy)service).init(properties);
      SERVICES.put(type, service);
   }
}