From 05eabda3916faba01eb3e279b8fae6947bba23ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E8=93=A0?= Date: Fri, 13 Nov 2020 12:10:40 +0800 Subject: [PATCH] code smell --- .../plugin/alert/script/OSUtils.java | 4 ++++ .../plugin/alert/script/ProcessUtils.java | 16 +++++----------- .../plugin/alert/script/ScriptSender.java | 1 + .../plugin/alert/script/StreamGobbler.java | 5 +++-- .../script/ScriptAlertChannelFactoryTest.java | 1 - 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java index 57190e84b8..d7e6d23a88 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java @@ -22,6 +22,10 @@ package org.apache.dolphinscheduler.plugin.alert.script; */ public class OSUtils { + public OSUtils() { + throw new UnsupportedOperationException("Construct OSUtils"); + } + public static Boolean isWindows() { return System.getProperty("os.name").startsWith("Windows"); } diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java index 47d0d397e9..11fc869761 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java @@ -17,10 +17,8 @@ package org.apache.dolphinscheduler.plugin.alert.script; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,25 +39,21 @@ public class ProcessUtils { public static Integer executeScript(String... cmd) { int exitCode = -1; + ProcessBuilder processBuilder = new ProcessBuilder(cmd); try { Process process = processBuilder.start(); - InputStream in = process.getErrorStream(); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); - StringBuilder result = new StringBuilder(); - StreamGobbler inputStreamGobbler = - new StreamGobbler(process.getInputStream()); - StreamGobbler errorStreamGobbler = - new StreamGobbler(process.getErrorStream()); + StreamGobbler inputStreamGobbler = new StreamGobbler(process.getInputStream()); + StreamGobbler errorStreamGobbler = new StreamGobbler(process.getErrorStream()); inputStreamGobbler.start(); errorStreamGobbler.start(); return process.waitFor(); - } catch (IOException | InterruptedException e) { - logger.error("execute alert script error", e.getMessage()); + logger.error("execute alert script error {}", e.getMessage()); + Thread.currentThread().interrupt(); } return exitCode; diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java index f7613873b4..0a14623b7f 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java @@ -67,6 +67,7 @@ public class ScriptSender { return alertResult; } alertResult.setMessage("send script alert msg error,exitCode is " + exitCode); + logger.info("send script alert msg error,exitCode is {}", exitCode); return alertResult; } diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java index 1d8a516f37..cde0c7914d 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java @@ -38,6 +38,7 @@ public class StreamGobbler extends Thread { this.inputStream = inputStream; } + @Override public void run() { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader inputBufferReader = new BufferedReader(inputStreamReader); @@ -50,10 +51,10 @@ public class StreamGobbler extends Thread { output.append(System.getProperty("line.separator")); } if (output.length() > 0) { - logger.info(output.toString()); + logger.info("out put msg is{}",output.toString()); } } catch (IOException e) { - logger.error("I/O error occurs %S", e.getMessage()); + logger.error("I/O error occurs {}", e.getMessage()); } } diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java index 53127f7cf5..72f2197315 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java @@ -31,7 +31,6 @@ import org.junit.Test; */ public class ScriptAlertChannelFactoryTest { - @Test public void testGetParams() { ScriptAlertChannelFactory scriptAlertChannelFactory = new ScriptAlertChannelFactory();