|
1 | 1 | package io.jenkins.plugins.service;
|
2 | 2 |
|
| 3 | +import io.jenkins.plugins.DingTalkGlobalConfig; |
| 4 | +import io.jenkins.plugins.DingTalkRobotConfig; |
| 5 | +import io.jenkins.plugins.enums.MsgTypeEnum; |
3 | 6 | import io.jenkins.plugins.model.MessageModel;
|
| 7 | +import io.jenkins.plugins.sdk.DingTalkSender; |
| 8 | +import java.net.Proxy; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Optional; |
| 12 | +import java.util.concurrent.ConcurrentHashMap; |
4 | 13 |
|
5 | 14 | /**
|
6 | 15 | * 发送消息
|
7 | 16 | *
|
8 | 17 | * @author liuwei
|
9 | 18 | */
|
10 |
| -public interface DingTalkService { |
| 19 | +public class DingTalkService { |
11 | 20 |
|
12 |
| - String send(String robot, MessageModel msg); |
| 21 | + private static volatile DingTalkService instance; |
| 22 | + private final Map<String, DingTalkSender> senders = new ConcurrentHashMap<>(); |
| 23 | + |
| 24 | + public static DingTalkService getInstance() { |
| 25 | + if (instance == null) { |
| 26 | + synchronized (DingTalkService.class) { |
| 27 | + if (instance == null) { |
| 28 | + instance = new DingTalkService(); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return instance; |
| 33 | + } |
| 34 | + |
| 35 | + private DingTalkSender getSender(String robotId) { |
| 36 | + DingTalkSender sender = senders.get(robotId); |
| 37 | + if (sender == null) { |
| 38 | + synchronized (senders) { |
| 39 | + sender = senders.get(robotId); |
| 40 | + if (sender == null) { |
| 41 | + DingTalkGlobalConfig globalConfig = DingTalkGlobalConfig.getInstance(); |
| 42 | + Proxy proxy = globalConfig.getProxy(); |
| 43 | + ArrayList<DingTalkRobotConfig> robotConfigs = globalConfig.getRobotConfigs(); |
| 44 | + Optional<DingTalkRobotConfig> robotConfigOptional = robotConfigs.stream() |
| 45 | + .filter(item -> robotId.equals(item.getId())) |
| 46 | + .findAny(); |
| 47 | + if (robotConfigOptional.isPresent()) { |
| 48 | + DingTalkRobotConfig robotConfig = robotConfigOptional.get(); |
| 49 | + sender = new DingTalkSender(robotConfig, proxy); |
| 50 | + senders.put(robotId, sender); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return sender; |
| 56 | + } |
| 57 | + |
| 58 | + public void resetSenders() { |
| 59 | + senders.clear(); |
| 60 | + } |
| 61 | + |
| 62 | + public String send(String robotId, MessageModel msg) { |
| 63 | + MsgTypeEnum type = msg.getType(); |
| 64 | + DingTalkSender sender = getSender(robotId); |
| 65 | + if (sender == null) { |
| 66 | + return String.format("ID 为 %s 的机器人不存在。", robotId); |
| 67 | + } |
| 68 | + if (type == null) { |
| 69 | + return "消息类型【type】不能为空"; |
| 70 | + } |
| 71 | + switch (type) { |
| 72 | + case TEXT: |
| 73 | + return sender.sendText(msg); |
| 74 | + case LINK: |
| 75 | + return sender.sendLink(msg); |
| 76 | + case MARKDOWN: |
| 77 | + return sender.sendMarkdown(msg); |
| 78 | + case ACTION_CARD: |
| 79 | + return sender.sendActionCard(msg); |
| 80 | + default: |
| 81 | + return String.format("错误的消息类型:%s", type.name()); |
| 82 | + } |
| 83 | + } |
13 | 84 | }
|
0 commit comments