| | |
| | | package com.ruoyi.oss.factory; |
| | | |
| | | import cn.hutool.core.lang.Assert; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.ruoyi.common.utils.JsonUtils; |
| | | import com.ruoyi.common.utils.RedisUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.reflect.ReflectUtils; |
| | | import com.ruoyi.oss.constant.CloudConstant; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | 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; |
| | |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class OssFactory { |
| | | |
| | | private static ISysConfigService sysConfigService; |
| | | |
| | | static { |
| | | OssFactory.sysConfigService = SpringUtils.getBean(ISysConfigService.class); |
| | | RedisUtils.subscribe(CloudConstant.CACHE_CONFIG_KEY, String.class, msg -> { |
| | | refreshService(msg); |
| | | log.info("订阅刷新OSS配置 => " + msg); |
| | | }); |
| | | } |
| | | |
| | | private static final Map<String, ICloudStorageService> SERVICES = new ConcurrentHashMap<>(); |
| | | /** |
| | | * 服务实例缓存 |
| | | */ |
| | | private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>(); |
| | | |
| | | public static ICloudStorageService instance() { |
| | | String type = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY); |
| | | /** |
| | | * 获取默认实例 |
| | | */ |
| | | public static ICloudStorageStrategy instance() { |
| | | // 获取redis 默认类型 |
| | | String type = Convert.toStr(RedisUtils.getCacheObject(CloudConstant.CACHE_CONFIG_KEY)); |
| | | if (StringUtils.isEmpty(type)) { |
| | | throw new OssException("文件存储服务类型无法找到!"); |
| | | } |
| | | return instance(type); |
| | | } |
| | | |
| | | public static ICloudStorageService instance(String type) { |
| | | ICloudStorageService service = SERVICES.get(type); |
| | | /** |
| | | * 根据类型获取实例 |
| | | */ |
| | | public static ICloudStorageStrategy instance(String type) { |
| | | ICloudStorageStrategy service = SERVICES.get(type); |
| | | if (service == null) { |
| | | service = (ICloudStorageService) SpringUtils.getBean(CloudServiceEnumd.getServiceClass(type)); |
| | | refreshService(type); |
| | | service = SERVICES.get(type); |
| | | } |
| | | return service; |
| | | } |
| | | |
| | | public static void register(String type, ICloudStorageService iCloudStorageService) { |
| | | Assert.notNull(type, "type can't be null"); |
| | | SERVICES.put(type, iCloudStorageService); |
| | | private static void refreshService(String type) { |
| | | Object json = RedisUtils.getCacheObject(CloudConstant.SYS_OSS_KEY + type); |
| | | CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class); |
| | | if (properties == null) { |
| | | throw new OssException("系统异常, '" + type + "'配置信息不存在!"); |
| | | } |
| | | // 获取redis配置信息 创建对象 并缓存 |
| | | ICloudStorageStrategy service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type)); |
| | | ((AbstractCloudStorageStrategy)service).init(properties); |
| | | SERVICES.put(type, service); |
| | | } |
| | | |
| | | } |