You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by li...@apache.org on 2021/01/15 13:12:03 UTC
[incubator-dolphinscheduler] branch dev updated:
[Improvement][alert] Refactor alert module to fix code smell (#4434)
This is an automated email from the ASF dual-hosted git repository.
lidongdai pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new a0b2feb [Improvement][alert] Refactor alert module to fix code smell (#4434)
a0b2feb is described below
commit a0b2febd62714e323404d5ca44c23c01f352c4ce
Author: Segun Ogundipe <da...@gmail.com>
AuthorDate: Fri Jan 15 14:11:54 2021 +0100
[Improvement][alert] Refactor alert module to fix code smell (#4434)
* chore: Refactore dolphinscheduler-alert to fix code smell
* chore: Refactor code to fix codestyle error
---
.../apache/dolphinscheduler/alert/AlertServer.java | 13 +-
.../alert/manager/DingTalkManager.java | 17 ++-
.../alert/plugin/EmailAlertPlugin.java | 18 ++-
.../alert/template/AlertTemplateFactory.java | 9 +-
.../alert/template/impl/DefaultHTMLTemplate.java | 73 +++++----
.../dolphinscheduler/alert/utils/Constants.java | 14 +-
.../alert/utils/DingTalkUtils.java | 57 +++----
.../alert/utils/EnterpriseWeChatUtils.java | 30 ++--
.../dolphinscheduler/alert/utils/ExcelUtils.java | 163 +++++++++++----------
.../dolphinscheduler/alert/utils/FuncUtils.java | 7 +-
.../dolphinscheduler/alert/utils/MailUtils.java | 125 +++++++++-------
.../alert/utils/PropertyUtils.java | 55 ++++---
.../alert/utils/DingTalkUtilsTest.java | 8 +-
.../alert/utils/ExcelUtilsTest.java | 10 +-
.../alert/utils/PropertyUtilsTest.java | 50 +++----
15 files changed, 362 insertions(+), 287 deletions(-)
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
index 347336c..bf791ac 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert;
import org.apache.dolphinscheduler.alert.plugin.EmailAlertPlugin;
@@ -25,11 +26,12 @@ import org.apache.dolphinscheduler.common.thread.Stopper;
import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.entity.Alert;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* alert of start
*/
@@ -40,8 +42,6 @@ public class AlertServer {
*/
private AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
- private AlertSender alertSender;
-
private static AlertServer instance;
private FilePluginManager alertPluginManager;
@@ -61,7 +61,7 @@ public class AlertServer {
alertPluginManager.addPlugin(new EmailAlertPlugin());
}
- public synchronized static AlertServer getInstance() {
+ public static synchronized AlertServer getInstance() {
if (null == instance) {
instance = new AlertServer();
}
@@ -78,12 +78,11 @@ public class AlertServer {
Thread.currentThread().interrupt();
}
List<Alert> alerts = alertDao.listWaitExecutionAlert();
- alertSender = new AlertSender(alerts, alertDao, alertPluginManager);
+ AlertSender alertSender = new AlertSender(alerts, alertDao, alertPluginManager);
alertSender.run();
}
}
-
public static void main(String[] args) {
AlertServer alertServer = AlertServer.getInstance();
alertServer.start();
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/manager/DingTalkManager.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/manager/DingTalkManager.java
index 6840794..871ad95 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/manager/DingTalkManager.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/manager/DingTalkManager.java
@@ -14,40 +14,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.manager;
import org.apache.dolphinscheduler.alert.utils.Constants;
import org.apache.dolphinscheduler.alert.utils.DingTalkUtils;
import org.apache.dolphinscheduler.plugin.model.AlertInfo;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* Ding Talk Manager
*/
public class DingTalkManager {
- private static final Logger logger = LoggerFactory.getLogger(EnterpriseWeChatManager.class);
+ private static final Logger logger = LoggerFactory.getLogger(DingTalkManager.class);
- public Map<String,Object> send(AlertInfo alert) {
- Map<String,Object> retMap = new HashMap<>();
+ public Map<String, Object> send(AlertInfo alert) {
+ Map<String, Object> retMap = new HashMap<>();
retMap.put(Constants.STATUS, false);
logger.info("send message {}", alert.getAlertData().getTitle());
try {
String msg = buildMessage(alert);
DingTalkUtils.sendDingTalkMsg(msg, Constants.UTF_8);
} catch (IOException e) {
- logger.error(e.getMessage(),e);
+ logger.error(e.getMessage(), e);
}
retMap.put(Constants.STATUS, true);
return retMap;
}
private String buildMessage(AlertInfo alert) {
- String msg = alert.getAlertData().getContent();
- return msg;
+ return alert.getAlertData().getContent();
}
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPlugin.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPlugin.java
index fbc600f..5bbc219 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPlugin.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPlugin.java
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.plugin;
import org.apache.dolphinscheduler.alert.manager.DingTalkManager;
@@ -28,14 +29,19 @@ import org.apache.dolphinscheduler.plugin.api.AlertPlugin;
import org.apache.dolphinscheduler.plugin.model.AlertData;
import org.apache.dolphinscheduler.plugin.model.AlertInfo;
import org.apache.dolphinscheduler.plugin.model.PluginName;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.*;
-
/**
* EmailAlertPlugin
- *
+ * <p>
* This plugin is a default plugin, and mix up email and enterprise wechat, because adapt with former alert behavior
*/
public class EmailAlertPlugin implements AlertPlugin {
@@ -125,10 +131,10 @@ public class EmailAlertPlugin implements AlertPlugin {
}
}
- if (DingTalkUtils.isEnableDingTalk) {
+ if (DingTalkUtils.IS_ENABLE_DING_TALK) {
logger.info("Ding Talk is enable.");
- dingTalkManager.send(info);
- }
+ dingTalkManager.send(info);
+ }
} else {
retMaps.put(Constants.MESSAGE, "alert send error.");
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java
index 965677e..d384631 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java
@@ -14,23 +14,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.template;
import org.apache.dolphinscheduler.alert.template.impl.DefaultHTMLTemplate;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* the alert template factory
*/
public class AlertTemplateFactory {
- private static final Logger logger = LoggerFactory.getLogger(AlertTemplateFactory.class);
-
- private AlertTemplateFactory(){}
+ private AlertTemplateFactory() {
+ }
/**
* get a template from alert.properties conf file
+ *
* @return a template, default is DefaultHTMLTemplate
*/
public static AlertTemplate getMessageTemplate() {
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java
index a01f301..f590849 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java
@@ -14,21 +14,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.template.impl;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
+import static org.apache.dolphinscheduler.common.utils.Preconditions.checkNotNull;
+
import org.apache.dolphinscheduler.alert.template.AlertTemplate;
import org.apache.dolphinscheduler.alert.utils.Constants;
import org.apache.dolphinscheduler.common.enums.ShowType;
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.dolphinscheduler.common.utils.*;
-import java.util.*;
-
-import static org.apache.dolphinscheduler.common.utils.Preconditions.*;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
/**
* the default html alert message template
@@ -37,33 +45,33 @@ public class DefaultHTMLTemplate implements AlertTemplate {
public static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplate.class);
-
@Override
- public String getMessageFromTemplate(String content, ShowType showType,boolean showAll) {
+ public String getMessageFromTemplate(String content, ShowType showType, boolean showAll) {
- switch (showType){
+ switch (showType) {
case TABLE:
- return getTableTypeMessage(content,showAll);
+ return getTableTypeMessage(content, showAll);
case TEXT:
- return getTextTypeMessage(content,showAll);
+ return getTextTypeMessage(content);
default:
- throw new IllegalArgumentException(String.format("not support showType: %s in DefaultHTMLTemplate",showType));
+ throw new IllegalArgumentException(String.format("not support showType: %s in DefaultHTMLTemplate", showType));
}
}
/**
* get alert message which type is TABLE
+ *
* @param content message content
* @param showAll weather to show all
* @return alert message
*/
- private String getTableTypeMessage(String content,boolean showAll){
+ private String getTableTypeMessage(String content, boolean showAll) {
- if (StringUtils.isNotEmpty(content)){
+ if (StringUtils.isNotEmpty(content)) {
List<LinkedHashMap> mapItemsList = JSONUtils.toList(content, LinkedHashMap.class);
- if(!showAll && mapItemsList.size() > Constants.NUMBER_1000){
- mapItemsList = mapItemsList.subList(0,Constants.NUMBER_1000);
+ if (!showAll && mapItemsList.size() > Constants.NUMBER_1000) {
+ mapItemsList = mapItemsList.subList(0, Constants.NUMBER_1000);
}
StringBuilder contents = new StringBuilder(200);
@@ -71,15 +79,15 @@ public class DefaultHTMLTemplate implements AlertTemplate {
boolean flag = true;
String title = "";
- for (LinkedHashMap mapItems : mapItemsList){
+ for (LinkedHashMap mapItems : mapItemsList) {
- Set<Map.Entry<String, Object>> entries = mapItems.entrySet();
+ Set<Entry<String, Object>> entries = mapItems.entrySet();
- Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
+ Iterator<Entry<String, Object>> iterator = entries.iterator();
StringBuilder t = new StringBuilder(Constants.TR);
StringBuilder cs = new StringBuilder(Constants.TR);
- while (iterator.hasNext()){
+ while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
t.append(Constants.TH).append(entry.getKey()).append(Constants.TH_END);
@@ -88,14 +96,14 @@ public class DefaultHTMLTemplate implements AlertTemplate {
}
t.append(Constants.TR_END);
cs.append(Constants.TR_END);
- if (flag){
+ if (flag) {
title = t.toString();
}
flag = false;
contents.append(cs);
}
- return getMessageFromHtmlTemplate(title,contents.toString());
+ return getMessageFromHtmlTemplate(title, contents.toString());
}
return content;
@@ -103,22 +111,22 @@ public class DefaultHTMLTemplate implements AlertTemplate {
/**
* get alert message which type is TEXT
+ *
* @param content message content
- * @param showAll weather to show all
* @return alert message
*/
- private String getTextTypeMessage(String content,boolean showAll){
+ private String getTextTypeMessage(String content) {
- if (StringUtils.isNotEmpty(content)){
+ if (StringUtils.isNotEmpty(content)) {
ArrayNode list = JSONUtils.parseArray(content);
StringBuilder contents = new StringBuilder(100);
- for (JsonNode jsonNode : list){
+ for (JsonNode jsonNode : list) {
contents.append(Constants.TR);
contents.append(Constants.TD).append(jsonNode.toString()).append(Constants.TD_END);
contents.append(Constants.TR_END);
}
- return getMessageFromHtmlTemplate(null,contents.toString());
+ return getMessageFromHtmlTemplate(null, contents.toString());
}
@@ -127,16 +135,17 @@ public class DefaultHTMLTemplate implements AlertTemplate {
/**
* get alert message from a html template
- * @param title message title
- * @param content message content
+ *
+ * @param title message title
+ * @param content message content
* @return alert message which use html template
*/
- private String getMessageFromHtmlTemplate(String title,String content){
+ private String getMessageFromHtmlTemplate(String title, String content) {
checkNotNull(content);
- String htmlTableThead = StringUtils.isEmpty(title) ? "" : String.format("<thead>%s</thead>\n",title);
+ String htmlTableThead = StringUtils.isEmpty(title) ? "" : String.format("<thead>%s</thead>%n", title);
- return Constants.HTML_HEADER_PREFIX +htmlTableThead + content + Constants.TABLE_BODY_HTML_TAIL;
+ return Constants.HTML_HEADER_PREFIX + htmlTableThead + content + Constants.TABLE_BODY_HTML_TAIL;
}
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java
index 465d9bf..c0f916a 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.utils;
/**
@@ -23,6 +24,7 @@ public class Constants {
private Constants() {
throw new IllegalStateException("Constants class");
}
+
/**
* alert properties path
*/
@@ -67,7 +69,7 @@ public class Constants {
public static final String MAIL_SMTP_SSL_ENABLE = "mail.smtp.ssl.enable";
- public static final String MAIL_SMTP_SSL_TRUST="mail.smtp.ssl.trust";
+ public static final String MAIL_SMTP_SSL_TRUST = "mail.smtp.ssl.trust";
public static final String TEXT_HTML_CHARSET_UTF_8 = "text/html;charset=utf-8";
@@ -156,7 +158,7 @@ public class Constants {
public static final String ENTERPRISE_WECHAT_AGENT_ID = "enterprise.wechat.agent.id";
public static final String ENTERPRISE_WECHAT_USERS = "enterprise.wechat.users";
-
+
public static final String DINGTALK_WEBHOOK = "dingtalk.webhook";
@@ -174,7 +176,13 @@ public class Constants {
public static final String DINGTALK_ENABLE = "dingtalk.isEnable";
- public static final String HTML_HEADER_PREFIX = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head><title>dolphinscheduler</title><meta name='Keywords' content=''><meta name='Description' content=''><style type=\"text/css\">table {margin-top:0px;padding-top:0px;border:1px solid;font-size: 14px;color: #333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}table th {border-width: 1px;padding: 8px;border [...]
+ public static final String HTML_HEADER_PREFIX = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>"
+ + "<html><head><title>dolphinscheduler</title><meta name='Keywords' content=''>"
+ + "<meta name='Description' content=''>"
+ + "<style type=\"text/css\">table {margin-top:0px;padding-top:0px;border:1px solid;font-size: 14px;color: #333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}"
+ + "table th {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;text-align: left;}"
+ + "table td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;text-align: left;}</style>"
+ + "/head><body style=\"margin:0;padding:0\"><table border=\"1px\" cellpadding=\"5px\" cellspacing=\"-10px\"> ";
public static final String TABLE_BODY_HTML_TAIL = "</table></body></html>";
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java
index 455d5de..50a62e4 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtils.java
@@ -14,10 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.dolphinscheduler.alert.utils;
+package org.apache.dolphinscheduler.alert.utils;
import org.apache.dolphinscheduler.common.utils.*;
+
import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
@@ -32,44 +33,50 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* DingTalkUtils utils
* support send msg to ding talk by robot message push function.
- * support proxy setting
+ * support PROXY setting
*/
public class DingTalkUtils {
public static final Logger logger = LoggerFactory.getLogger(DingTalkUtils.class);
- public static final boolean isEnableDingTalk = PropertyUtils.getBoolean(Constants.DINGTALK_ENABLE);
- private static final String dingTaskUrl = PropertyUtils.getString(Constants.DINGTALK_WEBHOOK);
- private static final String keyword = PropertyUtils.getString(Constants.DINGTALK_KEYWORD);
- private static final Boolean isEnableProxy = PropertyUtils.getBoolean(Constants.DINGTALK_PROXY_ENABLE);
- private static final String proxy = PropertyUtils.getString(Constants.DINGTALK_PROXY);
- private static final String user = PropertyUtils.getString(Constants.DINGTALK_USER);
- private static final String passwd = PropertyUtils.getString(Constants.DINGTALK_PASSWORD);
- private static final Integer port = PropertyUtils.getInt(Constants.DINGTALK_PORT);
+ public static final boolean IS_ENABLE_DING_TALK = PropertyUtils.getBoolean(Constants.DINGTALK_ENABLE);
+ private static final String DING_TASK_URL = PropertyUtils.getString(Constants.DINGTALK_WEBHOOK);
+ private static final String KEYWORD = PropertyUtils.getString(Constants.DINGTALK_KEYWORD);
+ private static final Boolean IS_ENABLE_PROXY = PropertyUtils.getBoolean(Constants.DINGTALK_PROXY_ENABLE);
+ private static final String PROXY = PropertyUtils.getString(Constants.DINGTALK_PROXY);
+ private static final String USER = PropertyUtils.getString(Constants.DINGTALK_USER);
+ private static final String PASSWD = PropertyUtils.getString(Constants.DINGTALK_PASSWORD);
+ private static final Integer PORT = PropertyUtils.getInt(Constants.DINGTALK_PORT);
+
+ private DingTalkUtils() {
+ throw new IllegalStateException(DingTalkUtils.class.getName());
+ }
/**
* send message interface
* only support text message format now.
+ *
* @param msg message context to send
* @param charset charset type
- * @return result of sending msg
+ * @return result of sending msg
* @throws IOException the IOException
*/
public static String sendDingTalkMsg(String msg, String charset) throws IOException {
- String msgToJson = textToJsonString(msg + "#" + keyword);
+ String msgToJson = textToJsonString(msg + "#" + KEYWORD);
HttpPost httpPost = constructHttpPost(msgToJson, charset);
CloseableHttpClient httpClient;
- if (isEnableProxy) {
+ if (Boolean.TRUE.equals(IS_ENABLE_PROXY)) {
httpClient = getProxyClient();
RequestConfig rcf = getProxyConfig();
httpPost.setConfig(rcf);
@@ -87,28 +94,26 @@ public class DingTalkUtils {
} finally {
response.close();
}
- logger.info("Ding Talk send [{}], resp:{%s}", msg, resp);
+ logger.info("Ding Talk send [{}], resp:{%s}", msg);
return resp;
- } finally {
+ } finally {
httpClient.close();
}
}
public static HttpPost constructHttpPost(String msg, String charset) {
- HttpPost post = new HttpPost(dingTaskUrl);
+ HttpPost post = new HttpPost(DING_TASK_URL);
StringEntity entity = new StringEntity(msg, charset);
post.setEntity(entity);
post.addHeader("Content-Type", "application/json; charset=utf-8");
return post;
}
-
public static CloseableHttpClient getProxyClient() {
- HttpHost httpProxy = new HttpHost(proxy, port);
+ HttpHost httpProxy = new HttpHost(PROXY, PORT);
CredentialsProvider provider = new BasicCredentialsProvider();
- provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(user, passwd));
- CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
- return httpClient;
+ provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(USER, PASSWD));
+ return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
}
public static CloseableHttpClient getDefaultClient() {
@@ -116,14 +121,14 @@ public class DingTalkUtils {
}
public static RequestConfig getProxyConfig() {
- HttpHost httpProxy = new HttpHost(proxy, port);
+ HttpHost httpProxy = new HttpHost(PROXY, PORT);
return RequestConfig.custom().setProxy(httpProxy).build();
}
public static String textToJsonString(String text) {
- Map<String, Object> items = new HashMap<String, Object>();
+ Map<String, Object> items = new HashMap<>();
items.put("msgtype", "text");
- Map<String, String> textContent = new HashMap<String, String>();
+ Map<String, String> textContent = new HashMap<>();
byte[] byt = StringUtils.getBytesUtf8(text);
String txt = StringUtils.newStringUtf8(byt);
textContent.put("content", txt);
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java
index b3cb5f7..aeb6671 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java
@@ -60,9 +60,13 @@ public class EnterpriseWeChatUtils {
private static final String ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_TEAM_SEND_MSG);
private static final String ENTERPRISE_WE_CHAT_USER_SEND_MSG = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_USER_SEND_MSG);
- private static final String agentIdRegExp = "\\{agentId}";
- private static final String msgRegExp = "\\{msg}";
- private static final String userRegExp = "\\{toUser}";
+ private static final String AGENT_ID_REG_EXP = "\\{agentId}";
+ private static final String MSG_REG_EXP = "\\{msg}";
+ private static final String USER_REG_EXP = "\\{toUser}";
+
+ private EnterpriseWeChatUtils() {
+ throw new IllegalStateException(EnterpriseWeChatUtils.class.getName());
+ }
/**
* get Enterprise WeChat is enable
@@ -120,8 +124,8 @@ public class EnterpriseWeChatUtils {
*/
public static String makeTeamSendMsg(String toParty, String agentId, String msg) {
return ENTERPRISE_WE_CHAT_TEAM_SEND_MSG.replaceAll("\\{toParty}", toParty)
- .replaceAll(agentIdRegExp, agentId)
- .replaceAll(msgRegExp, msg);
+ .replaceAll(AGENT_ID_REG_EXP, agentId)
+ .replaceAll(MSG_REG_EXP, msg);
}
/**
@@ -135,8 +139,8 @@ public class EnterpriseWeChatUtils {
public static String makeTeamSendMsg(Collection<String> toParty, String agentId, String msg) {
String listParty = FuncUtils.mkString(toParty, "|");
return ENTERPRISE_WE_CHAT_TEAM_SEND_MSG.replaceAll("\\{toParty}", listParty)
- .replaceAll(agentIdRegExp, agentId)
- .replaceAll(msgRegExp, msg);
+ .replaceAll(AGENT_ID_REG_EXP, agentId)
+ .replaceAll(MSG_REG_EXP, msg);
}
/**
@@ -148,9 +152,9 @@ public class EnterpriseWeChatUtils {
* @return Enterprise WeChat send message
*/
public static String makeUserSendMsg(String toUser, String agentId, String msg) {
- return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll("\\{toUser}", toUser)
- .replaceAll(agentIdRegExp, agentId)
- .replaceAll(msgRegExp, msg);
+ return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll(USER_REG_EXP, toUser)
+ .replaceAll(AGENT_ID_REG_EXP, agentId)
+ .replaceAll(MSG_REG_EXP, msg);
}
/**
@@ -163,9 +167,9 @@ public class EnterpriseWeChatUtils {
*/
public static String makeUserSendMsg(Collection<String> toUser, String agentId, String msg) {
String listUser = FuncUtils.mkString(toUser, "|");
- return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll(userRegExp, listUser)
- .replaceAll(agentIdRegExp, agentId)
- .replaceAll(msgRegExp, msg);
+ return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll(USER_REG_EXP, listUser)
+ .replaceAll(AGENT_ID_REG_EXP, agentId)
+ .replaceAll(MSG_REG_EXP, msg);
}
/**
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java
index 0825686..76ce66a 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/ExcelUtils.java
@@ -14,23 +14,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.utils;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
+
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.util.*;
-import org.apache.dolphinscheduler.common.utils.*;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* excel utils
@@ -38,19 +44,25 @@ import org.apache.dolphinscheduler.common.utils.*;
public class ExcelUtils {
private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
+
+ private ExcelUtils() {
+ throw new IllegalStateException(ExcelUtils.class.getName());
+ }
+
/**
* generate excel file
+ *
* @param content the content
* @param title the title
* @param xlsFilePath the xls path
*/
- public static void genExcelFile(String content,String title,String xlsFilePath){
+ public static void genExcelFile(String content, String title, String xlsFilePath) {
List<LinkedHashMap> itemsList;
//The JSONUtils.toList has been try catch ex
itemsList = JSONUtils.toList(content, LinkedHashMap.class);
- if (CollectionUtils.isEmpty(itemsList)){
+ if (CollectionUtils.isEmpty(itemsList)) {
logger.error("itemsList is null");
throw new RuntimeException("itemsList is null");
}
@@ -59,82 +71,79 @@ public class ExcelUtils {
List<String> headerList = new ArrayList<>();
- Iterator<Map.Entry<String, Object>> iter = headerMap.entrySet().iterator();
- while (iter.hasNext()){
- Map.Entry<String, Object> en = iter.next();
+ for (Map.Entry<String, Object> en : headerMap.entrySet()) {
headerList.add(en.getKey());
}
HSSFWorkbook wb = null;
FileOutputStream fos = null;
- try {
- // declare a workbook
- wb = new HSSFWorkbook();
- // generate a table
- HSSFSheet sheet = wb.createSheet();
- HSSFRow row = sheet.createRow(0);
- //set the height of the first line
- row.setHeight((short)500);
-
- //set Horizontal right
- CellStyle cellStyle = wb.createCellStyle();
- cellStyle.setAlignment(HorizontalAlignment.RIGHT);
-
- //setting excel headers
- for (int i = 0; i < headerList.size(); i++) {
- HSSFCell cell = row.createCell(i);
- cell.setCellStyle(cellStyle);
- cell.setCellValue(headerList.get(i));
- }
-
- //setting excel body
- int rowIndex = 1;
- for (LinkedHashMap<String, Object> itemsMap : itemsList){
- Object[] values = itemsMap.values().toArray();
- row = sheet.createRow(rowIndex);
- //setting excel body height
- row.setHeight((short)500);
- rowIndex++;
- for (int j = 0 ; j < values.length ; j++){
- HSSFCell cell1 = row.createCell(j);
- cell1.setCellStyle(cellStyle);
- cell1.setCellValue(String.valueOf(values[j]));
- }
- }
-
- for (int i = 0; i < headerList.size(); i++) {
- sheet.setColumnWidth(i, headerList.get(i).length() * 800);
- }
-
- File file = new File(xlsFilePath);
- if (!file.exists()) {
- file.mkdirs();
- }
-
- //setting file output
- fos = new FileOutputStream(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS);
-
- wb.write(fos);
-
- }catch (Exception e){
- logger.error("generate excel error",e);
- throw new RuntimeException("generate excel error",e);
- }finally {
- if (wb != null){
- try {
- wb.close();
- } catch (IOException e) {
- logger.error(e.getMessage(),e);
- }
- }
- if (fos != null){
- try {
- fos.close();
- } catch (IOException e) {
- logger.error(e.getMessage(),e);
- }
- }
- }
+ try {
+ // declare a workbook
+ wb = new HSSFWorkbook();
+ // generate a table
+ HSSFSheet sheet = wb.createSheet();
+ HSSFRow row = sheet.createRow(0);
+ //set the height of the first line
+ row.setHeight((short) 500);
+
+ //set Horizontal right
+ CellStyle cellStyle = wb.createCellStyle();
+ cellStyle.setAlignment(HorizontalAlignment.RIGHT);
+
+ //setting excel headers
+ for (int i = 0; i < headerList.size(); i++) {
+ HSSFCell cell = row.createCell(i);
+ cell.setCellStyle(cellStyle);
+ cell.setCellValue(headerList.get(i));
+ }
+
+ //setting excel body
+ int rowIndex = 1;
+ for (LinkedHashMap<String, Object> itemsMap : itemsList) {
+ Object[] values = itemsMap.values().toArray();
+ row = sheet.createRow(rowIndex);
+ //setting excel body height
+ row.setHeight((short) 500);
+ rowIndex++;
+ for (int j = 0; j < values.length; j++) {
+ HSSFCell cell1 = row.createCell(j);
+ cell1.setCellStyle(cellStyle);
+ cell1.setCellValue(String.valueOf(values[j]));
+ }
+ }
+
+ for (int i = 0; i < headerList.size(); i++) {
+ sheet.setColumnWidth(i, headerList.get(i).length() * 800);
+ }
+
+ File file = new File(xlsFilePath);
+ if (!file.exists()) {
+ file.mkdirs();
+ }
+
+ //setting file output
+ fos = new FileOutputStream(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS);
+
+ wb.write(fos);
+
+ } catch (Exception e) {
+ throw new RuntimeException("generate excel error", e);
+ } finally {
+ if (wb != null) {
+ try {
+ wb.close();
+ } catch (IOException e) {
+ logger.error(e.getMessage(), e);
+ }
+ }
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException e) {
+ logger.error(e.getMessage(), e);
+ }
+ }
+ }
}
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java
index d68532a..e78b4eb 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/FuncUtils.java
@@ -14,15 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.utils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
public class FuncUtils {
+ private FuncUtils() {
+ throw new IllegalStateException(FuncUtils.class.getName());
+ }
+
public static String mkString(Iterable<String> list, String split) {
- if (null == list || StringUtils.isEmpty(split)){
+ if (null == list || StringUtils.isEmpty(split)) {
return null;
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java
index 888c9db..f57481b 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java
@@ -14,23 +14,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.utils;
import org.apache.dolphinscheduler.alert.template.AlertTemplate;
import org.apache.dolphinscheduler.alert.template.AlertTemplateFactory;
import org.apache.dolphinscheduler.common.enums.ShowType;
-import org.apache.commons.mail.EmailException;
-import org.apache.commons.mail.HtmlEmail;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import javax.mail.*;
-import javax.mail.internet.*;
-import java.io.*;
-import java.util.*;
+import org.apache.commons.mail.EmailException;
+import org.apache.commons.mail.HtmlEmail;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.mail.Authenticator;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimeUtility;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* mail utils
@@ -55,7 +71,7 @@ public class MailUtils {
public static final Boolean MAIL_USE_SSL = PropertyUtils.getBoolean(Constants.MAIL_SMTP_SSL_ENABLE);
- public static final String xlsFilePath = PropertyUtils.getString(Constants.XLS_FILE_PATH,"/tmp/xls");
+ public static final String XLS_FILE_PATH = PropertyUtils.getString(Constants.XLS_FILE_PATH, "/tmp/xls");
public static final String STARTTLS_ENABLE = PropertyUtils.getString(Constants.MAIL_SMTP_STARTTLS_ENABLE);
@@ -67,23 +83,29 @@ public class MailUtils {
//Solve the problem of messy Chinese name in excel attachment
static {
- System.setProperty("mail.mime.splitlongparameters","false");
+ System.setProperty("mail.mime.splitlongparameters", "false");
+ }
+
+ private MailUtils() {
+ throw new IllegalStateException(MailUtils.class.getName());
}
/**
* send mail to receivers
+ *
* @param receivers the receiver list
* @param title the title
* @param content the content
* @param showType the show type
* @return the result map
*/
- public static Map<String,Object> sendMails(Collection<String> receivers, String title, String content,String showType) {
+ public static Map<String, Object> sendMails(Collection<String> receivers, String title, String content, String showType) {
return sendMails(receivers, null, title, content, showType);
}
/**
* send mail
+ *
* @param receivers the receiver list
* @param receiversCc cc list
* @param title the title
@@ -91,8 +113,8 @@ public class MailUtils {
* @param showType the show type
* @return the send result
*/
- public static Map<String,Object> sendMails(Collection<String> receivers, Collection<String> receiversCc, String title, String content, String showType) {
- Map<String,Object> retMap = new HashMap<>();
+ public static Map<String, Object> sendMails(Collection<String> receivers, Collection<String> receiversCc, String title, String content, String showType) {
+ Map<String, Object> retMap = new HashMap<>();
retMap.put(Constants.STATUS, false);
// if there is no receivers && no receiversCc, no need to process
@@ -111,14 +133,14 @@ public class MailUtils {
email.setMailSession(session);
email.setFrom(MAIL_SENDER);
email.setCharset(Constants.UTF_8);
- if (CollectionUtils.isNotEmpty(receivers)){
+ if (CollectionUtils.isNotEmpty(receivers)) {
// receivers mail
for (String receiver : receivers) {
email.addTo(receiver);
}
}
- if (CollectionUtils.isNotEmpty(receiversCc)){
+ if (CollectionUtils.isNotEmpty(receiversCc)) {
//cc
for (String receiverCc : receiversCc) {
email.addCc(receiverCc);
@@ -129,16 +151,16 @@ public class MailUtils {
} catch (Exception e) {
handleException(receivers, retMap, e);
}
- }else if (showType.equals(ShowType.ATTACHMENT.getDescp()) || showType.equals(ShowType.TABLEATTACHMENT.getDescp())) {
+ } else if (showType.equals(ShowType.ATTACHMENT.getDescp()) || showType.equals(ShowType.TABLEATTACHMENT.getDescp())) {
try {
- String partContent = (showType.equals(ShowType.ATTACHMENT.getDescp()) ? "Please see the attachment " + title + Constants.EXCEL_SUFFIX_XLS : htmlTable(content,false));
+ String partContent = (showType.equals(ShowType.ATTACHMENT.getDescp()) ? "Please see the attachment " + title + Constants.EXCEL_SUFFIX_XLS : htmlTable(content, false));
- attachment(receivers,receiversCc,title,content,partContent);
+ attachment(receivers, receiversCc, title, content, partContent);
retMap.put(Constants.STATUS, true);
return retMap;
- }catch (Exception e){
+ } catch (Exception e) {
handleException(receivers, retMap, e);
return retMap;
}
@@ -149,49 +171,52 @@ public class MailUtils {
/**
* html table content
+ *
* @param content the content
* @param showAll if show the whole content
* @return the html table form
*/
- private static String htmlTable(String content, boolean showAll){
- return alertTemplate.getMessageFromTemplate(content,ShowType.TABLE,showAll);
+ private static String htmlTable(String content, boolean showAll) {
+ return alertTemplate.getMessageFromTemplate(content, ShowType.TABLE, showAll);
}
/**
* html table content
+ *
* @param content the content
* @return the html table form
*/
- private static String htmlTable(String content){
- return htmlTable(content,true);
+ private static String htmlTable(String content) {
+ return htmlTable(content, true);
}
/**
* html text content
+ *
* @param content the content
* @return text in html form
*/
- private static String htmlText(String content){
- return alertTemplate.getMessageFromTemplate(content,ShowType.TEXT);
+ private static String htmlText(String content) {
+ return alertTemplate.getMessageFromTemplate(content, ShowType.TEXT);
}
/**
* send mail as Excel attachment
+ *
* @param receivers the receiver list
* @param title the title
- * @throws Exception
*/
- private static void attachment(Collection<String> receivers,Collection<String> receiversCc,String title,String content,String partContent)throws Exception{
+ private static void attachment(Collection<String> receivers, Collection<String> receiversCc, String title, String content, String partContent) throws Exception {
MimeMessage msg = getMimeMessage(receivers);
- attachContent(receiversCc, title, content,partContent, msg);
+ attachContent(receiversCc, title, content, partContent, msg);
}
/**
* get MimeMessage
+ *
* @param receivers receivers
* @return the MimeMessage
- * @throws MessagingException
*/
private static MimeMessage getMimeMessage(Collection<String> receivers) throws MessagingException {
@@ -223,7 +248,7 @@ public class MailUtils {
props.setProperty(Constants.MAIL_SMTP_AUTH, Constants.STRING_TRUE);
props.setProperty(Constants.MAIL_TRANSPORT_PROTOCOL, MAIL_PROTOCOL);
props.setProperty(Constants.MAIL_SMTP_STARTTLS_ENABLE, STARTTLS_ENABLE);
- if (SSL_ENABLE) {
+ if (Boolean.TRUE.equals(SSL_ENABLE)) {
props.setProperty(Constants.MAIL_SMTP_SSL_ENABLE, "true");
props.setProperty(Constants.MAIL_SMTP_SSL_TRUST, SSL_TRUST);
}
@@ -241,20 +266,19 @@ public class MailUtils {
/**
* attach content
+ *
* @param receiversCc the cc list
* @param title the title
* @param content the content
* @param partContent the partContent
* @param msg the message
- * @throws MessagingException
- * @throws IOException
*/
- private static void attachContent(Collection<String> receiversCc, String title, String content, String partContent,MimeMessage msg) throws MessagingException, IOException {
- /**
+ private static void attachContent(Collection<String> receiversCc, String title, String content, String partContent, MimeMessage msg) throws MessagingException, IOException {
+ /*
* set receiverCc
*/
- if(CollectionUtils.isNotEmpty(receiversCc)){
- for (String receiverCc : receiversCc){
+ if (CollectionUtils.isNotEmpty(receiversCc)) {
+ for (String receiverCc : receiversCc) {
msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(receiverCc));
}
}
@@ -267,16 +291,16 @@ public class MailUtils {
part1.setContent(partContent, Constants.TEXT_HTML_CHARSET_UTF_8);
// set attach file
MimeBodyPart part2 = new MimeBodyPart();
- File file = new File(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS);
+ File file = new File(XLS_FILE_PATH + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// make excel file
- ExcelUtils.genExcelFile(content,title,xlsFilePath);
+ ExcelUtils.genExcelFile(content, title, XLS_FILE_PATH);
part2.attachFile(file);
- part2.setFileName(MimeUtility.encodeText(title + Constants.EXCEL_SUFFIX_XLS,Constants.UTF_8,"B"));
+ part2.setFileName(MimeUtility.encodeText(title + Constants.EXCEL_SUFFIX_XLS, Constants.UTF_8, "B"));
// add components to collection
partList.addBodyPart(part1);
partList.addBodyPart(part2);
@@ -289,21 +313,21 @@ public class MailUtils {
/**
* the string object map
+ *
* @param title the title
* @param content the content
* @param showType the showType
* @param retMap the result map
* @param email the email
* @return the result map
- * @throws EmailException
*/
private static Map<String, Object> getStringObjectMap(String title, String content, String showType, Map<String, Object> retMap, HtmlEmail email) throws EmailException {
- /**
+ /*
* the subject of the message to be sent
*/
email.setSubject(title);
- /**
+ /*
* to send information, you can use HTML tags in mail content because of the use of HtmlEmail
*/
if (showType.equals(ShowType.TABLE.getDescp())) {
@@ -322,23 +346,24 @@ public class MailUtils {
/**
* file delete
+ *
* @param file the file to delete
*/
- public static void deleteFile(File file){
- if(file.exists()){
- if(file.delete()){
- logger.info("delete success: {}",file.getAbsolutePath() + file.getName());
- }else{
+ public static void deleteFile(File file) {
+ if (file.exists()) {
+ if (file.delete()) {
+ logger.info("delete success: {}", file.getAbsolutePath() + file.getName());
+ } else {
logger.info("delete fail: {}", file.getAbsolutePath() + file.getName());
}
- }else{
+ } else {
logger.info("file not exists: {}", file.getAbsolutePath() + file.getName());
}
}
-
/**
* handle exception
+ *
* @param receivers the receiver list
* @param retMap the result map
* @param e the exception
@@ -347,6 +372,4 @@ public class MailUtils {
logger.error("Send email to {} failed", receivers, e);
retMap.put(Constants.MESSAGE, "Send email to {" + String.join(",", receivers) + "} failed," + e.toString());
}
-
-
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java
index 91f7261..0eb2413 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java
+++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java
@@ -14,19 +14,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.alert.utils;
+import static org.apache.dolphinscheduler.alert.utils.Constants.ALERT_PROPERTIES_PATH;
+
import org.apache.dolphinscheduler.common.utils.IOUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.regex.PatternSyntaxException;
-import static org.apache.dolphinscheduler.alert.utils.Constants.ALERT_PROPERTIES_PATH;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* property utils
@@ -41,13 +43,11 @@ public class PropertyUtils {
private static final Properties properties = new Properties();
- private static final PropertyUtils propertyUtils = new PropertyUtils();
-
- private PropertyUtils(){
+ private PropertyUtils() {
init();
}
- private void init(){
+ private void init() {
String[] propertyFiles = new String[]{ALERT_PROPERTIES_PATH};
for (String fileName : propertyFiles) {
InputStream fis = null;
@@ -69,6 +69,7 @@ public class PropertyUtils {
/**
* get property value
+ *
* @param key property name
* @return the value
*/
@@ -95,7 +96,7 @@ public class PropertyUtils {
* get property value
*
* @param key property name
- * @return get property int value , if key == null, then return -1
+ * @return get property int value , if key == null, then return -1
*/
public static int getInt(String key) {
@@ -104,6 +105,7 @@ public class PropertyUtils {
/**
* get int value
+ *
* @param key the key
* @param defaultValue the default value
* @return the value related the key or the default value if the key not existed
@@ -117,15 +119,16 @@ public class PropertyUtils {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
- logger.info(e.getMessage(),e);
+ logger.info(e.getMessage(), e);
}
return defaultValue;
}
/**
* get property value
+ *
* @param key property name
- * @return the boolean result value
+ * @return the boolean result value
*/
public static Boolean getBoolean(String key) {
@@ -134,7 +137,7 @@ public class PropertyUtils {
}
String value = properties.getProperty(key.trim());
- if(null != value){
+ if (null != value) {
return Boolean.parseBoolean(value);
}
@@ -143,15 +146,17 @@ public class PropertyUtils {
/**
* get long value
+ *
* @param key the key
* @return if the value not existed, return -1, or will return the related value
*/
public static long getLong(String key) {
- return getLong(key,-1);
+ return getLong(key, -1);
}
/**
* get long value
+ *
* @param key the key
* @param defaultVal the default value
* @return the value related the key or the default value if the key not existed
@@ -166,7 +171,7 @@ public class PropertyUtils {
try {
return Long.parseLong(val);
} catch (NumberFormatException e) {
- logger.info(e.getMessage(),e);
+ logger.info(e.getMessage(), e);
}
return defaultVal;
@@ -174,16 +179,17 @@ public class PropertyUtils {
/**
* get double value
+ *
* @param key the key
* @return if the value not existed, return -1.0, or will return the related value
*/
public static double getDouble(String key) {
- String val = getString(key);
- return getDouble(key,-1.0);
+ return getDouble(key, -1.0);
}
/**
* get double value
+ *
* @param key the key
* @param defaultVal the default value
* @return the value related the key or the default value if the key not existed
@@ -198,17 +204,17 @@ public class PropertyUtils {
try {
return Double.parseDouble(val);
} catch (NumberFormatException e) {
- logger.info(e.getMessage(),e);
+ logger.info(e.getMessage(), e);
}
return defaultVal;
}
-
/**
- * get array
- * @param key property name
- * @param splitStr separator
+ * get array
+ *
+ * @param key property name
+ * @param splitStr separator
* @return the result array
*/
public static String[] getArray(String key, String splitStr) {
@@ -219,21 +225,22 @@ public class PropertyUtils {
try {
return value.split(splitStr);
} catch (PatternSyntaxException e) {
- logger.info(e.getMessage(),e);
+ logger.info(e.getMessage(), e);
}
return null;
}
/**
* get enum
+ *
* @param key the key
* @param type the class type
* @param defaultValue the default value
* @param <T> the generic class type
- * @return get enum value
+ * @return get enum value
*/
public static <T extends Enum<T>> T getEnum(String key, Class<T> type,
- T defaultValue) {
+ T defaultValue) {
String val = getString(key);
if (val == null) {
return defaultValue;
@@ -242,7 +249,7 @@ public class PropertyUtils {
try {
return Enum.valueOf(type, val);
} catch (IllegalArgumentException e) {
- logger.info(e.getMessage(),e);
+ logger.info(e.getMessage(), e);
}
return defaultValue;
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtilsTest.java
index 049881c..ac62c17 100644
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtilsTest.java
+++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/DingTalkUtilsTest.java
@@ -32,8 +32,6 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
-import static org.junit.Assert.*;
-
@PrepareForTest(PropertyUtils.class)
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.net.ssl.*")
@@ -97,8 +95,8 @@ public class DingTalkUtilsTest {
@Test
public void testProxyConfig() {
RequestConfig rc = DingTalkUtils.getProxyConfig();
- Assert.assertEquals(rc.getProxy().getPort(), 80);
- Assert.assertEquals(rc.getProxy().getHostName(), "proxy.com.cn");
+ Assert.assertEquals(80, rc.getProxy().getPort());
+ Assert.assertEquals("proxy.com.cn", rc.getProxy().getHostName());
}
@Test
@@ -114,7 +112,7 @@ public class DingTalkUtilsTest {
String msg = DingTalkUtils.textToJsonString("this is test:中文");
logger.info("test support utf8, actual:" + msg);
- logger.info("test support utf8, actual:" + DingTalkUtils.isEnableDingTalk);
+ logger.info("test support utf8, actual:" + DingTalkUtils.IS_ENABLE_DING_TALK);
String expect = "{\"text\":{\"content\":\"this is test:中文\"},\"msgtype\":\"text\"}";
Assert.assertEquals(expect, msg);
}
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/ExcelUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/ExcelUtilsTest.java
index 8ee6235..c483325 100644
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/ExcelUtilsTest.java
+++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/ExcelUtilsTest.java
@@ -17,6 +17,10 @@
package org.apache.dolphinscheduler.alert.utils;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@@ -25,8 +29,6 @@ import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.File;
-import static org.junit.Assert.assertTrue;
public class ExcelUtilsTest {
@@ -61,7 +63,7 @@ public class ExcelUtilsTest {
//Define dest file path
String xlsFilePath = rootPath + System.getProperty("file.separator");
- logger.info("xlsFilePath: "+xlsFilePath);
+ logger.info("XLS_FILE_PATH: " + xlsFilePath);
//Define correctContent
String correctContent = "[{\"name\":\"ds name\",\"value\":\"ds value\"}]";
@@ -99,4 +101,4 @@ public class ExcelUtilsTest {
File file = new File("/tmp/xls" + Constants.SINGLE_SLASH + "t" + Constants.EXCEL_SUFFIX_XLS);
file.delete();
}
-}
\ No newline at end of file
+}
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java
index 2a300c9..4e239b5 100644
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java
+++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java
@@ -41,11 +41,11 @@ public class PropertyUtilsTest {
//Expected "EMAIL"
String result = PropertyUtils.getString("alert.type");
logger.info(result);
- assertEquals(result, "EMAIL");
+ assertEquals("EMAIL", result);
//Expected "xxx.xxx.test"
result = PropertyUtils.getString("mail.server.host");
- assertEquals(result, "xxx.xxx.test");
+ assertEquals("xxx.xxx.test", result);
//If key is undefine in alert.properties, then return null
result = PropertyUtils.getString("abc");
@@ -88,23 +88,23 @@ public class PropertyUtilsTest {
//Expected 25
long result = PropertyUtils.getLong("mail.server.port");
- assertSame(result, 25L);
+ assertSame(25L, result);
//If key is null, then return -1
result = PropertyUtils.getLong(null);
- assertSame(result, -1L);
+ assertSame(-1L, result);
//If key is undefine in alert.properties, then return -1
result = PropertyUtils.getLong("abc");
- assertSame(result, -1L);
+ assertSame(-1L, result);
//If key is undefine in alert.properties, and there is a defaultval, then return defaultval
result = PropertyUtils.getLong("abc", 200);
- assertEquals(result, 200L);
+ assertEquals(200L, result);
//If the value can not parse to long ,it will log the error and return -1L
result = PropertyUtils.getLong("test.server.testnumber");
- assertSame(result, -1L);
+ assertSame(-1L, result);
}
/**
@@ -115,23 +115,23 @@ public class PropertyUtilsTest {
//Expected 3.0
double result = PropertyUtils.getDouble("test.server.factor");
- assertEquals(result, 3.0, 0);
+ assertEquals(3.0, result, 0);
//If key is null, then return -1.0
result = PropertyUtils.getDouble(null);
- assertEquals(result, -1.0, 0);
+ assertEquals(-1.0, result, 0);
//If key is undefine in alert.properties, then return -1
result = PropertyUtils.getDouble("abc");
- assertEquals(result, -1.0, 0);
+ assertEquals(-1.0, result, 0);
//If key is undefine in alert.properties, and there is a defaultval, then return defaultval
result = PropertyUtils.getDouble("abc", 5.0);
- assertEquals(result, 5.0, 0);
+ assertEquals(5.0, result, 0);
//If the value can not parse to double ,it will log the error and return -1.0
result = PropertyUtils.getDouble("test.server.testnumber");
- assertEquals(result, -1.0, 0);
+ assertEquals(-1.0, result, 0);
}
/**
@@ -145,9 +145,9 @@ public class PropertyUtilsTest {
assertEquals(result.length, 3);
//Equal array values
- assertEquals(result[0], "xxx.xxx.test1");
- assertEquals(result[1], "xxx.xxx.test2");
- assertEquals(result[2], "xxx.xxx.test3");
+ assertEquals("xxx.xxx.test1", result[0]);
+ assertEquals("xxx.xxx.test2", result[1]);
+ assertEquals("xxx.xxx.test3", result[2]);
//If key is null, then return -1
result = PropertyUtils.getArray(null, ",");
@@ -170,23 +170,23 @@ public class PropertyUtilsTest {
//Expected 25
int result = PropertyUtils.getInt("mail.server.port");
- assertSame(result, 25);
+ assertSame(25, result);
//If key is null, then return -1
result = PropertyUtils.getInt(null);
- assertSame(result, -1);
+ assertSame(-1, result);
//If key is undefine in alert.properties, then return -1
result = PropertyUtils.getInt("abc");
- assertSame(result, -1);
+ assertSame(-1, result);
//If key is undefine in alert.properties, and there is a defaultval, then return defaultval
result = PropertyUtils.getInt("abc", 300);
- assertEquals(result, 300);
+ assertEquals(300, result);
//If the value can not parse to int ,it will log the error and return -1
result = PropertyUtils.getInt("test.server.testnumber");
- assertSame(result, -1);
+ assertSame(-1, result);
}
/**
@@ -197,19 +197,19 @@ public class PropertyUtilsTest {
//Expected MASTER
ZKNodeType zkNodeType = PropertyUtils.getEnum("test.server.enum1", ZKNodeType.class,ZKNodeType.WORKER);
- assertEquals(zkNodeType, ZKNodeType.MASTER);
+ assertEquals(ZKNodeType.MASTER, zkNodeType);
//Expected DEAD_SERVER
zkNodeType = PropertyUtils.getEnum("test.server.enum2", ZKNodeType.class,ZKNodeType.WORKER);
- assertEquals(zkNodeType, ZKNodeType.DEAD_SERVER);
+ assertEquals(ZKNodeType.DEAD_SERVER, zkNodeType);
//If key is null, then return defaultval
zkNodeType = PropertyUtils.getEnum(null, ZKNodeType.class,ZKNodeType.WORKER);
- assertEquals(zkNodeType, ZKNodeType.WORKER);
+ assertEquals(ZKNodeType.WORKER, zkNodeType);
//If the value doesn't define in enum ,it will log the error and return -1
zkNodeType = PropertyUtils.getEnum("test.server.enum3", ZKNodeType.class,ZKNodeType.WORKER);
- assertEquals(zkNodeType, ZKNodeType.WORKER);
+ assertEquals(ZKNodeType.WORKER, zkNodeType);
}
-}
\ No newline at end of file
+}