提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.framework.file.core.client; |
H |
2 |
|
|
3 |
import cn.hutool.core.lang.Assert; |
|
4 |
import cn.hutool.core.util.ReflectUtil; |
|
5 |
import com.iailab.module.infra.framework.file.core.enums.FileStorageEnum; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
|
|
8 |
import java.util.concurrent.ConcurrentHashMap; |
|
9 |
import java.util.concurrent.ConcurrentMap; |
|
10 |
|
|
11 |
/** |
|
12 |
* 文件客户端的工厂实现类 |
|
13 |
* |
|
14 |
* @author iailab |
|
15 |
*/ |
|
16 |
@Slf4j |
|
17 |
public class FileClientFactoryImpl implements FileClientFactory { |
|
18 |
|
|
19 |
/** |
|
20 |
* 文件客户端 Map |
|
21 |
* key:配置编号 |
|
22 |
*/ |
|
23 |
private final ConcurrentMap<Long, AbstractFileClient<?>> clients = new ConcurrentHashMap<>(); |
|
24 |
|
|
25 |
@Override |
|
26 |
public FileClient getFileClient(Long configId) { |
|
27 |
AbstractFileClient<?> client = clients.get(configId); |
|
28 |
if (client == null) { |
|
29 |
log.error("[getFileClient][配置编号({}) 找不到客户端]", configId); |
|
30 |
} |
|
31 |
return client; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
@SuppressWarnings("unchecked") |
|
36 |
public <Config extends FileClientConfig> void createOrUpdateFileClient(Long configId, Integer storage, Config config) { |
|
37 |
AbstractFileClient<Config> client = (AbstractFileClient<Config>) clients.get(configId); |
|
38 |
if (client == null) { |
|
39 |
client = this.createFileClient(configId, storage, config); |
|
40 |
client.init(); |
|
41 |
clients.put(client.getId(), client); |
|
42 |
} else { |
|
43 |
client.refresh(config); |
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
@SuppressWarnings("unchecked") |
|
48 |
private <Config extends FileClientConfig> AbstractFileClient<Config> createFileClient( |
|
49 |
Long configId, Integer storage, Config config) { |
|
50 |
FileStorageEnum storageEnum = FileStorageEnum.getByStorage(storage); |
|
51 |
Assert.notNull(storageEnum, String.format("文件配置(%s) 为空", storageEnum)); |
|
52 |
// 创建客户端 |
|
53 |
return (AbstractFileClient<Config>) ReflectUtil.newInstance(storageEnum.getClientClass(), configId, config); |
|
54 |
} |
|
55 |
|
|
56 |
} |