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 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 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 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(); } }