yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.qianwen.smartman.modules.notify.manager;
 
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.notify.NotifierProvidersMapManager;
import com.qianwen.core.notify.NotifyResultProducer;
import com.qianwen.core.notify.NotifyType;
import com.qianwen.core.notify.notifier.Notifier;
import com.qianwen.core.notify.notifier.NotifierEventDispatcher;
import com.qianwen.core.notify.notifier.NotifierManager;
import com.qianwen.core.notify.notifier.NotifierProperties;
import com.qianwen.core.notify.notifier.NotifierProvider;
import com.qianwen.core.notify.notifier.NotifyConfigManager;
import com.qianwen.core.notify.template.Template;
import com.qianwen.core.tool.utils.Func;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 
@ConditionalOnBean({NotifyResultProducer.class, NotifierProvidersMapManager.class, NotifyConfigManager.class})
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/notify/manager/DefaultNotifierManager.class */
public class DefaultNotifierManager implements NotifierManager {
    private static final Logger log = LoggerFactory.getLogger(DefaultNotifierManager.class);
    private NotifyResultProducer notifyResultProducer;
    private NotifierProvidersMapManager notifierProvidersMapManager;
    private NotifyConfigManager configManager;
    private Map<String, Notifier> notifiers = new ConcurrentHashMap();
 
    public DefaultNotifierManager(NotifyResultProducer notifyResultProducer, NotifierProvidersMapManager notifierProvidersMapManager, NotifyConfigManager configManager) {
        this.notifyResultProducer = notifyResultProducer;
        this.notifierProvidersMapManager = notifierProvidersMapManager;
        this.configManager = configManager;
    }
 
    protected NotifierProperties getProperties(NotifyType notifyType, String id) {
        return this.configManager.getNotifyConfig(notifyType, id);
    }
 
    public Notifier<? extends Template> getNotifier(NotifyType type, String id) {
        Notifier reallyNotifier;
        NotifierProperties notifierProperties = getReallyNotifierProperties(type, id);
        Notifier notifierCached = this.notifiers.get(id);
        if (Func.isEmpty(notifierCached) || !Func.equals(notifierProperties, notifierCached.getNotifierProperties())) {
            synchronized (DefaultNotifierManager.class) {
                reallyNotifier = createNotifier(notifierProperties);
                Notifier oldNotifier = this.notifiers.put(id, reallyNotifier);
                if (Func.isNotEmpty(oldNotifier)) {
                    oldNotifier.close();
                }
            }
            return reallyNotifier;
        }
        return this.notifiers.get(id);
    }
 
    protected NotifierProperties getReallyNotifierProperties(NotifyType type, String id) {
        NotifierProperties notifier = (NotifierProperties) CacheUtil.get("blade:notify", "notifier:id:", id, () -> {
            return getProperties(type, id);
        }, false);
        return notifier;
    }
 
    protected Notifier createNotifier(NotifierProperties properties) {
        Map<String, NotifierProvider> providerMap = (Map) Optional.ofNullable(this.notifierProvidersMapManager.getProviders().get(properties.getType())).orElseThrow(() -> {
            return new UnsupportedOperationException("不支持的通知类型:" + properties.getType());
        });
        NotifierProvider provider = (NotifierProvider) Optional.ofNullable(providerMap.get(properties.getProvider())).orElseThrow(() -> {
            return new UnsupportedOperationException("不支持的服务商:" + properties.getProvider());
        });
        NotifierEventDispatcher dispatcher = new NotifierEventDispatcher(this.notifyResultProducer, provider.createNotifier(properties));
        return dispatcher;
    }
 
    public void reload(String id) {
        this.notifiers.remove(id).close();
    }
}