This is an automated email from the ASF dual-hosted git repository.
leonbao pushed a commit to branch 1.3.4-prepare
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git
The following commit(s) were added to refs/heads/1.3.4-prepare by this push:
new e3644b6 [FIX#4033]cherry pick from dev to resolve that `$[]``
conflicts with mysql keywords (#4142)
e3644b6 is described below
commit e3644b6df94841ea8ad17a15852a24284a43f0a2
Author: lgcareer <[email protected]>
AuthorDate: Wed Dec 2 17:15:20 2020 +0800
[FIX#4033]cherry pick from dev to resolve that `$[]`` conflicts with mysql
keywords (#4142)
* [FIX#4033]cherry pick from dev to resolve that `$[]`` conflicts with
mysql keywords
* [FIX#4033]cherry pick from dev to resolve that `$[]`` conflicts with
mysql keywords
* [FIX#4033]cherry pick from dev to resolve that `$[]`` conflicts with
mysql keywords
Co-authored-by: Kirs <[email protected]>
---
.../common/utils/ParameterUtils.java | 422 +++++++++++----------
.../dolphinscheduler/common/utils/StringUtils.java | 22 +-
.../utils/placeholder/TimePlaceholderUtils.java | 61 ++-
.../common/utils/ParameterUtilsTest.java | 84 ++--
.../placeholder/TimePlaceholderUtilsTest.java | 76 ++--
pom.xml | 1 +
6 files changed, 379 insertions(+), 287 deletions(-)
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java
index 84c60db..1227b4e 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java
@@ -14,9 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.common.utils;
-import com.alibaba.fastjson.JSON;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.DataType;
@@ -24,233 +24,259 @@ import
org.apache.dolphinscheduler.common.process.Property;
import org.apache.dolphinscheduler.common.utils.placeholder.BusinessTimeUtils;
import org.apache.dolphinscheduler.common.utils.placeholder.PlaceholderUtils;
import
org.apache.dolphinscheduler.common.utils.placeholder.TimePlaceholderUtils;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.time.DateUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.sql.PreparedStatement;
-import java.text.ParseException;
-import java.util.*;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* parameter parse utils
*/
public class ParameterUtils {
- private static final Logger logger =
LoggerFactory.getLogger(ParameterUtils.class);
-
- /**
- * convert parameters place holders
- *
- * @param parameterString parameter
- * @param parameterMap parameter map
- * @return convert parameters place holders
- */
- public static String convertParameterPlaceholders(String parameterString,
Map<String, String> parameterMap) {
- if (StringUtils.isEmpty(parameterString) || parameterMap == null) {
- return parameterString;
- }
+ private static final Logger logger =
LoggerFactory.getLogger(ParameterUtils.class);
- //Get current time, schedule execute time
- String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME);
+ private static final String DATE_PARSE_PATTERN = "\\$\\[([^\\]]+)]";
- Date cronTime = null;
+ private static final String DATE_START_PATTERN = "^[0-9]";
- if (StringUtils.isNotEmpty(cronTimeStr)) {
- try {
- cronTime = DateUtils.parseDate(cronTimeStr, new
String[]{Constants.PARAMETER_FORMAT_TIME});
- } catch (ParseException e) {
- logger.error("parse {} exception", cronTimeStr, e);
- }
- } else {
- cronTime = new Date();
+ private ParameterUtils() {
+ throw new UnsupportedOperationException("Construct ParameterUtils");
}
- // replace variable ${} form,refers to the replacement of system variables
and custom variables
- parameterString = PlaceholderUtils.replacePlaceholders(parameterString,
parameterMap, true);
-
- // replace time $[...] form, eg. $[yyyyMMdd]
- if (cronTime != null) {
- parameterString =
TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true);
+ /**
+ * convert parameters place holders
+ *
+ * @param parameterString parameter
+ * @param parameterMap parameter map
+ * @return convert parameters place holders
+ */
+ public static String convertParameterPlaceholders(String parameterString,
Map<String, String> parameterMap) {
+ if (StringUtils.isEmpty(parameterString) || parameterMap == null) {
+ return parameterString;
+ }
+
+ //Get current time, schedule execute time
+ String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME);
+
+ Date cronTime;
+
+ if (StringUtils.isNotEmpty(cronTimeStr)) {
+ cronTime = DateUtils.parse(cronTimeStr,
Constants.PARAMETER_FORMAT_TIME);
+ } else {
+ cronTime = new Date();
+ }
+
+ // replace variable ${} form,refers to the replacement of system
variables and custom variables
+ parameterString =
PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true);
+
+ // replace time $[...] form, eg. $[yyyyMMdd]
+ if (cronTime != null) {
+ return dateTemplateParse(parameterString, cronTime);
+ }
+
+ return parameterString;
}
- return parameterString;
- }
-
- /**
- * new
- * convert parameters place holders
- *
- * @param parameterString parameter
- * @param parameterMap parameter map
- * @return convert parameters place holders
- */
- public static String convertParameterPlaceholders2(String parameterString,
Map<String, String> parameterMap) {
- if (StringUtils.isEmpty(parameterString)) {
- return parameterString;
- }
- //Get current time, schedule execute time
- String cronTimeStr = parameterMap.get(Constants.PARAMETER_SHECDULE_TIME);
- Date cronTime = null;
-
- if (StringUtils.isNotEmpty(cronTimeStr)) {
- try {
- cronTime = DateUtils.parseDate(cronTimeStr, new
String[]{Constants.PARAMETER_FORMAT_TIME});
-
- } catch (ParseException e) {
- logger.error(String.format("parse %s exception", cronTimeStr), e);
- }
- } else {
- cronTime = new Date();
+ /**
+ * new
+ * convert parameters place holders
+ *
+ * @param parameterString parameter
+ * @param parameterMap parameter map
+ * @return convert parameters place holders
+ */
+ public static String convertParameterPlaceholders2(String parameterString,
Map<String, String> parameterMap) {
+ if (StringUtils.isEmpty(parameterString)) {
+ return parameterString;
+ }
+ //Get current time, schedule execute time
+ String cronTimeStr =
parameterMap.get(Constants.PARAMETER_SHECDULE_TIME);
+ Date cronTime = null;
+
+ if (StringUtils.isNotEmpty(cronTimeStr)) {
+ cronTime = DateUtils.parse(cronTimeStr,
Constants.PARAMETER_FORMAT_TIME);
+
+ } else {
+ cronTime = new Date();
+ }
+
+ // replace variable ${} form,refers to the replacement of system
variables and custom variables
+ parameterString =
PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true);
+
+ // replace time $[...] form, eg. $[yyyyMMdd]
+ if (cronTime != null) {
+ return dateTemplateParse(parameterString, cronTime);
+ }
+ return parameterString;
}
- // replace variable ${} form,refers to the replacement of system variables
and custom variables
- parameterString = PlaceholderUtils.replacePlaceholders(parameterString,
parameterMap, true);
+ /**
+ * set in parameter
+ *
+ * @param index index
+ * @param stmt preparedstatement
+ * @param dataType data type
+ * @param value value
+ * @throws Exception errors
+ */
+ public static void setInParameter(int index, PreparedStatement stmt,
DataType dataType, String value) throws Exception {
+ if (dataType.equals(DataType.VARCHAR)) {
+ stmt.setString(index, value);
+ } else if (dataType.equals(DataType.INTEGER)) {
+ stmt.setInt(index, Integer.parseInt(value));
+ } else if (dataType.equals(DataType.LONG)) {
+ stmt.setLong(index, Long.parseLong(value));
+ } else if (dataType.equals(DataType.FLOAT)) {
+ stmt.setFloat(index, Float.parseFloat(value));
+ } else if (dataType.equals(DataType.DOUBLE)) {
+ stmt.setDouble(index, Double.parseDouble(value));
+ } else if (dataType.equals(DataType.DATE)) {
+ stmt.setDate(index, java.sql.Date.valueOf(value));
+ } else if (dataType.equals(DataType.TIME)) {
+ stmt.setString(index, value);
+ } else if (dataType.equals(DataType.TIMESTAMP)) {
+ stmt.setTimestamp(index, java.sql.Timestamp.valueOf(value));
+ } else if (dataType.equals(DataType.BOOLEAN)) {
+ stmt.setBoolean(index, Boolean.parseBoolean(value));
+ }
+ }
- // replace time $[...] form, eg. $[yyyyMMdd]
- if (cronTime != null) {
- parameterString =
TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true);
+ /**
+ * curing user define parameters
+ *
+ * @param globalParamMap global param map
+ * @param globalParamList global param list
+ * @param commandType command type
+ * @param scheduleTime schedule time
+ * @return curing user define parameters
+ */
+ public static String curingGlobalParams(Map<String, String>
globalParamMap, List<Property> globalParamList,
+ CommandType commandType, Date
scheduleTime) {
+
+ if (globalParamList == null || globalParamList.isEmpty()) {
+ return null;
+ }
+
+ Map<String, String> globalMap = new HashMap<>();
+ if (globalParamMap != null) {
+ globalMap.putAll(globalParamMap);
+ }
+ Map<String, String> allParamMap = new HashMap<>();
+ //If it is a complement, a complement time needs to be passed in,
according to the task type
+ Map<String, String> timeParams = BusinessTimeUtils
+ .getBusinessTime(commandType, scheduleTime);
+ if (timeParams != null) {
+ allParamMap.putAll(timeParams);
+ }
+
+ allParamMap.putAll(globalMap);
+
+ Set<Map.Entry<String, String>> entries = allParamMap.entrySet();
+
+ Map<String, String> resolveMap = new HashMap<>();
+ for (Map.Entry<String, String> entry : entries) {
+ String val = entry.getValue();
+ if (val.startsWith("$")) {
+ String str = ParameterUtils.convertParameterPlaceholders(val,
allParamMap);
+ resolveMap.put(entry.getKey(), str);
+ }
+ }
+ globalMap.putAll(resolveMap);
+
+ for (Property property : globalParamList) {
+ String val = globalMap.get(property.getProp());
+ if (val != null) {
+ property.setValue(val);
+ }
+ }
+ return JSONUtils.toJsonString(globalParamList);
}
- return parameterString;
- }
-
-
- /**
- * set in parameter
- * @param index index
- * @param stmt preparedstatement
- * @param dataType data type
- * @param value value
- * @throws Exception errors
- */
- public static void setInParameter(int index, PreparedStatement stmt,
DataType dataType, String value)throws Exception{
- if (dataType.equals(DataType.VARCHAR)){
- stmt.setString(index,value);
- }else if (dataType.equals(DataType.INTEGER)){
- stmt.setInt(index, Integer.parseInt(value));
- }else if (dataType.equals(DataType.LONG)){
- stmt.setLong(index, Long.parseLong(value));
- }else if (dataType.equals(DataType.FLOAT)){
- stmt.setFloat(index, Float.parseFloat(value));
- }else if (dataType.equals(DataType.DOUBLE)){
- stmt.setDouble(index, Double.parseDouble(value));
- }else if (dataType.equals(DataType.DATE)){
- stmt.setDate(index, java.sql.Date.valueOf(value));
- }else if (dataType.equals(DataType.TIME)){
- stmt.setString(index, value);
- }else if (dataType.equals(DataType.TIMESTAMP)){
- stmt.setTimestamp(index, java.sql.Timestamp.valueOf(value));
- }else if (dataType.equals(DataType.BOOLEAN)){
- stmt.setBoolean(index,Boolean.parseBoolean(value));
- }
- }
-
- /**
- * curing user define parameters
- *
- * @param globalParamMap global param map
- * @param globalParamList global param list
- * @param commandType command type
- * @param scheduleTime schedule time
- * @return curing user define parameters
- */
- public static String curingGlobalParams(Map<String,String> globalParamMap,
List<Property> globalParamList,
- CommandType commandType, Date scheduleTime){
-
- if (globalParamList == null || globalParamList.isEmpty()) {
- return null;
+
+ /**
+ * handle escapes
+ *
+ * @param inputString input string
+ * @return string filter escapes
+ */
+ public static String handleEscapes(String inputString) {
+
+ if (StringUtils.isNotEmpty(inputString)) {
+ return inputString.replace("%", "////%").replaceAll("[\n|\r\t]",
"_");
+ }
+ return inputString;
}
- Map<String, String> globalMap = new HashMap<>();
- if (globalParamMap!= null){
- globalMap.putAll(globalParamMap);
+ /**
+ * $[yyyyMMdd] replace schedule time
+ */
+ public static String replaceScheduleTime(String text, Date scheduleTime) {
+ Map<String, Property> paramsMap = new HashMap<>();
+ //if getScheduleTime null ,is current date
+ if (null == scheduleTime) {
+ scheduleTime = new Date();
+ }
+
+ String dateTime =
org.apache.dolphinscheduler.common.utils.DateUtils.format(scheduleTime,
Constants.PARAMETER_FORMAT_TIME);
+ Property p = new Property();
+ p.setValue(dateTime);
+ p.setProp(Constants.PARAMETER_SHECDULE_TIME);
+ paramsMap.put(Constants.PARAMETER_SHECDULE_TIME, p);
+ text = ParameterUtils.convertParameterPlaceholders2(text,
convert(paramsMap));
+
+ return text;
}
- Map<String,String> allParamMap = new HashMap<>();
- //If it is a complement, a complement time needs to be passed in,
according to the task type
- Map<String,String> timeParams = BusinessTimeUtils
- .getBusinessTime(commandType, scheduleTime);
- if (timeParams != null) {
- allParamMap.putAll(timeParams);
+ /**
+ * format convert
+ *
+ * @param paramsMap params map
+ * @return Map of converted
+ * see org.apache.dolphinscheduler.server.utils.ParamUtils.convert
+ */
+ public static Map<String, String> convert(Map<String, Property> paramsMap)
{
+ Map<String, String> map = new HashMap<>();
+ Iterator<Map.Entry<String, Property>> iter =
paramsMap.entrySet().iterator();
+ while (iter.hasNext()) {
+ Map.Entry<String, Property> en = iter.next();
+ map.put(en.getKey(), en.getValue().getValue());
+ }
+ return map;
}
- allParamMap.putAll(globalMap);
+ private static String dateTemplateParse(String templateStr, Date date) {
+ if (templateStr == null) {
+ return null;
+ }
+ Pattern pattern = Pattern.compile(DATE_PARSE_PATTERN);
- Set<Map.Entry<String, String>> entries = allParamMap.entrySet();
+ StringBuffer newValue = new StringBuffer(templateStr.length());
- Map<String,String> resolveMap = new HashMap<>();
- for (Map.Entry<String,String> entry : entries){
- String val = entry.getValue();
- if (val.startsWith("$")){
- String str = ParameterUtils.convertParameterPlaceholders(val,
allParamMap);
- resolveMap.put(entry.getKey(),str);
- }
- }
- globalMap.putAll(resolveMap);
-
- for (Property property : globalParamList){
- String val = globalMap.get(property.getProp());
- if (val != null){
- property.setValue(val);
- }
- }
- return JSON.toJSONString(globalParamList);
- }
+ Matcher matcher = pattern.matcher(templateStr);
+ while (matcher.find()) {
+ String key = matcher.group(1);
+ if (Pattern.matches(DATE_START_PATTERN, key)) {
+ continue;
+ }
+ String value = TimePlaceholderUtils.getPlaceHolderTime(key, date);
+ assert value != null;
+ matcher.appendReplacement(newValue, value);
+ }
- /**
- * handle escapes
- * @param inputString input string
- * @return string filter escapes
- */
- public static String handleEscapes(String inputString){
+ matcher.appendTail(newValue);
- if(StringUtils.isNotEmpty(inputString)){
- return inputString.replace("%", "////%");
+ return newValue.toString();
}
- return inputString;
- }
-
-
- /**
- * $[yyyyMMdd] replace schedule time
- * @param text
- * @param scheduleTime
- * @return
- */
- public static String replaceScheduleTime(String text, Date scheduleTime) {
- Map<String, Property> paramsMap = new HashMap<>();
- //if getScheduleTime null ,is current date
- if (null == scheduleTime) {
- scheduleTime = new Date();
- }
-
- String dateTime =
org.apache.dolphinscheduler.common.utils.DateUtils.format(scheduleTime,
Constants.PARAMETER_FORMAT_TIME);
- Property p = new Property();
- p.setValue(dateTime);
- p.setProp(Constants.PARAMETER_SHECDULE_TIME);
- paramsMap.put(Constants.PARAMETER_SHECDULE_TIME, p);
- text = ParameterUtils.convertParameterPlaceholders2(text,
convert(paramsMap));
-
- return text;
- }
-
-
- /**
- * format convert
- * @param paramsMap params map
- * @return Map of converted
- * see org.apache.dolphinscheduler.server.utils.ParamUtils.convert
- */
- public static Map<String,String> convert(Map<String,Property> paramsMap){
- Map<String,String> map = new HashMap<>();
- Iterator<Map.Entry<String, Property>> iter =
paramsMap.entrySet().iterator();
- while (iter.hasNext()){
- Map.Entry<String, Property> en = iter.next();
- map.put(en.getKey(),en.getValue().getValue());
- }
- return map;
- }
+
}
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java
index af2817a..256f199 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java
@@ -14,11 +14,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.common.utils;
public class StringUtils {
+
public static final String EMPTY = "";
+ private StringUtils() {
+ throw new UnsupportedOperationException("Construct StringUtils");
+ }
+
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
@@ -27,14 +33,26 @@ public class StringUtils {
return !isEmpty(cs);
}
- public static boolean isBlank(String s){
+ public static boolean isBlank(String s) {
if (isEmpty(s)) {
return true;
}
return s.trim().length() == 0;
}
- public static boolean isNotBlank(String s){
+ public static boolean isNotBlank(String s) {
return !isBlank(s);
}
+
+ public static String replaceNRTtoUnderline(String src) {
+ if (isBlank(src)) {
+ return src;
+ } else {
+ return src.replaceAll("[\n|\r|\t]", "_");
+ }
+ }
+
+ public static String trim(String str) {
+ return str == null ? null : str.trim();
+ }
}
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java
index 35cb018..6a46bbe 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java
@@ -14,11 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.common.utils.placeholder;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.DateUtils;
-import org.apache.commons.lang.StringUtils;
+import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,8 +47,8 @@ public class TimePlaceholderUtils {
* Replaces all placeholders of format {@code ${name}} with the value
returned
* from the supplied {@link PropertyPlaceholderHelper.PlaceholderResolver}.
*
- * @param value the value containing the placeholders to
be replaced
- * @param date custom date
+ * @param value the value containing the
placeholders to be replaced
+ * @param date custom date
* @param ignoreUnresolvablePlaceholders ignore unresolvable placeholders
* @return the supplied value with placeholders replaced inline
*/
@@ -59,11 +60,11 @@ public class TimePlaceholderUtils {
return helper.replacePlaceholders(value, new
TimePlaceholderResolver(value, date));
}
-
/**
* Creates a new {@code PropertyPlaceholderHelper} that uses the supplied
prefix and suffix.
+ *
* @param ignoreUnresolvablePlaceholders indicates whether unresolvable
placeholders should
- * be ignored ({@code true}) or cause an exception ({@code false})
+ * be ignored ({@code true}) or
cause an exception ({@code false})
*/
private static PropertyPlaceholderHelper
getPropertyPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
return new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX,
PLACEHOLDER_SUFFIX, null, ignoreUnresolvablePlaceholders);
@@ -89,7 +90,7 @@ public class TimePlaceholderUtils {
* Change the sign in the expression to P (positive) N (negative)
*
* @param expression
- * @return eg. "-3+-6*(+8)-(-5) -> S3+S6*(P8)-(S5)"
+ * @return eg. "-3+-6*(+8)-(-5) -> S3+S6*(P8)-(S5)"
*/
private static String convert(String expression) {
char[] arr = expression.toCharArray();
@@ -262,7 +263,7 @@ public class TimePlaceholderUtils {
* Placeholder replacement resolver
*/
private static class TimePlaceholderResolver implements
- PropertyPlaceholderHelper.PlaceholderResolver {
+ PropertyPlaceholderHelper.PlaceholderResolver {
private final String value;
@@ -278,12 +279,28 @@ public class TimePlaceholderUtils {
try {
return calculateTime(placeholderName, date);
} catch (Exception ex) {
- logger.error("resolve placeholder '{}' in [ {} ]" ,
placeholderName, value, ex);
+ logger.error("resolve placeholder '{}' in [ {} ]",
placeholderName, value, ex);
return null;
}
}
}
+ /**
+ * return the formatted date according to the corresponding date format
+ *
+ * @param expression date expression
+ * @param date date
+ * @return reformat date
+ */
+ public static String getPlaceHolderTime(String expression, Date date) {
+ if (StringUtils.isBlank(expression)) {
+ return null;
+ }
+ if (null == date) {
+ return null;
+ }
+ return calculateTime(expression, date);
+ }
/**
* calculate time
@@ -320,9 +337,10 @@ public class TimePlaceholderUtils {
/**
* calculate time expresstion
+ *
* @param expression expresstion
- * @param date date
- * @return map with date, date format
+ * @param date date
+ * @return map with date, date format
*/
public static Map.Entry<Date, String> calcTimeExpression(String
expression, Date date) {
Map.Entry<Date, String> resultEntry;
@@ -346,8 +364,9 @@ public class TimePlaceholderUtils {
/**
* get first day of month
+ *
* @param expression expresstion
- * @param date date
+ * @param date date
* @return first day of month
*/
public static Map.Entry<Date, String> calcMonthBegin(String expression,
Date date) {
@@ -369,8 +388,9 @@ public class TimePlaceholderUtils {
/**
* get last day of month
+ *
* @param expression expresstion
- * @param date date
+ * @param date date
* @return last day of month
*/
public static Map.Entry<Date, String> calcMonthEnd(String expression, Date
date) {
@@ -392,8 +412,9 @@ public class TimePlaceholderUtils {
/**
* get first day of week
+ *
* @param expression expresstion
- * @param date date
+ * @param date date
* @return monday
*/
public static Map.Entry<Date, String> calcWeekStart(String expression,
Date date) {
@@ -414,8 +435,9 @@ public class TimePlaceholderUtils {
/**
* get last day of week
+ *
* @param expression expresstion
- * @param date date
+ * @param date date
* @return last day of week
*/
public static Map.Entry<Date, String> calcWeekEnd(String expression, Date
date) {
@@ -437,8 +459,9 @@ public class TimePlaceholderUtils {
/**
* calc months expression
+ *
* @param expression expresstion
- * @param date date
+ * @param date date
* @return calc months
*/
public static Map.Entry<Date, String> calcMonths(String expression, Date
date) {
@@ -461,7 +484,7 @@ public class TimePlaceholderUtils {
* calculate time expression
*
* @param expression expresstion
- * @param date date
+ * @param date date
* @return calculate time expression with date,format
*/
public static Map.Entry<Date, String> calcMinutes(String expression, Date
date) {
@@ -471,7 +494,7 @@ public class TimePlaceholderUtils {
if (Character.isDigit(expression.charAt(index + 1))) {
String addMinuteExpr = expression.substring(index + 1);
Date targetDate = org.apache.commons.lang.time.DateUtils
- .addMinutes(date, calcMinutes(addMinuteExpr));
+ .addMinutes(date, calcMinutes(addMinuteExpr));
String dateFormat = expression.substring(0, index);
return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
@@ -482,7 +505,7 @@ public class TimePlaceholderUtils {
if (Character.isDigit(expression.charAt(index + 1))) {
String addMinuteExpr = expression.substring(index + 1);
Date targetDate = org.apache.commons.lang.time.DateUtils
- .addMinutes(date, 0 - calcMinutes(addMinuteExpr));
+ .addMinutes(date, 0 - calcMinutes(addMinuteExpr));
String dateFormat = expression.substring(0, index);
return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
@@ -512,7 +535,7 @@ public class TimePlaceholderUtils {
} else {
calcExpression = String.format("60*24*(%s)%s",
minuteExpression.substring(0, index),
- minuteExpression.substring(index));
+ minuteExpression.substring(index));
}
return calculate(calcExpression);
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java
index abdc15c..796a5fa 100644
---
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java
@@ -14,23 +14,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.common.utils;
-import com.alibaba.fastjson.JSON;
-import org.apache.commons.lang.time.DateUtils;
+import static
org.apache.dolphinscheduler.common.utils.placeholder.TimePlaceholderUtils.replacePlaceholders;
+
+import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.DataType;
import org.apache.dolphinscheduler.common.enums.Direct;
import org.apache.dolphinscheduler.common.process.Property;
import org.apache.dolphinscheduler.common.utils.placeholder.PlaceholderUtils;
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.*;
-import static
org.apache.dolphinscheduler.common.Constants.PARAMETER_FORMAT_TIME;
-import static
org.apache.dolphinscheduler.common.utils.placeholder.TimePlaceholderUtils.replacePlaceholders;
-
public class ParameterUtilsTest {
public static final Logger logger =
LoggerFactory.getLogger(ParameterUtilsTest.class);
@@ -39,13 +45,13 @@ public class ParameterUtilsTest {
* Test convertParameterPlaceholders
*/
@Test
- public void testConvertParameterPlaceholders() throws Exception {
+ public void testConvertParameterPlaceholders() throws ParseException {
// parameterString,parameterMap is null
Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null,
null));
// parameterString is null,parameterMap is not null
- Map<String, String> parameterMap = new HashMap<String,String>();
- parameterMap.put("testParameter","testParameter");
+ Map<String, String> parameterMap = new HashMap<String, String>();
+ parameterMap.put("testParameter", "testParameter");
Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null,
parameterMap));
// parameterStringăparameterMap is not null
@@ -53,67 +59,79 @@ public class ParameterUtilsTest {
Assert.assertEquals(parameterString,
ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap));
//replace variable ${} form
- parameterMap.put("testParameter2","${testParameter}");
-
Assert.assertEquals(parameterString,PlaceholderUtils.replacePlaceholders(parameterString,
parameterMap, true));
+ parameterMap.put("testParameter2", "${testParameter}");
+ Assert.assertEquals(parameterString,
PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true));
// replace time $[...] form, eg. $[yyyyMMdd]
Date cronTime = new Date();
Assert.assertEquals(parameterString,
replacePlaceholders(parameterString, cronTime, true));
// replace time $[...] form, eg. $[yyyyMMdd]
- Date cronTimeStr = DateUtils.parseDate("20191220145900", new
String[]{PARAMETER_FORMAT_TIME});
+ Date cronTimeStr = DateUtils.stringToDate("2019-02-02 00:00:00");
Assert.assertEquals(parameterString,
replacePlaceholders(parameterString, cronTimeStr, true));
}
+ @Test
+ public void testConvertParameterPlaceholders2() {
+ String parameterString =
+ "${user} is userName, '$[1]' '$[add_months(yyyyMMdd,12*2)]'
'$[add_months(yyyyMMdd,-12*2)]' '$[add_months(yyyyMMdd,3)]'
'$[add_months(yyyyMMdd,-4)]' "
+ + "'$[yyyyMMdd+7*2]' '$[yyyyMMdd-7*2]' '$[yyyyMMdd+3]'
'$[0]' '$[yyyyMMdd-3]' '$[HHmmss+2/24]' '$[HHmmss-1/24]' '$[HHmmss+3/24/60]'
'$[HHmmss-2/24/60]' '$[3]'";
+ Map<String, String> parameterMap = new HashMap<>();
+ parameterMap.put("user", "Kris");
+ parameterMap.put(Constants.PARAMETER_DATETIME, "20201201123000");
+ parameterString =
ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap);
+ Assert.assertEquals("Kris is userName, '$[1]' '20221201' '20181201'
'20210301' '20200801' '20201215' '20201117' '20201204' '$[0]' '20201128'
'143000' '113000' '123300' '122800' '$[3]'",
+ parameterString);
+ }
+
/**
* Test curingGlobalParams
*/
@Test
- public void testCuringGlobalParams() throws Exception {
+ public void testCuringGlobalParams() {
//define globalMap
Map<String, String> globalParamMap = new HashMap<>();
- globalParamMap.put("globalParams1","Params1");
+ globalParamMap.put("globalParams1", "Params1");
//define globalParamList
List<Property> globalParamList = new ArrayList<>();
//define scheduleTime
- Date scheduleTime = DateUtils.parseDate("20191220145900", new
String[]{PARAMETER_FORMAT_TIME});
+ Date scheduleTime = DateUtils.stringToDate("2019-12-20 00:00:00");
//test globalParamList is null
String result = ParameterUtils.curingGlobalParams(globalParamMap,
globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime);
Assert.assertNull(result);
-
Assert.assertNull(ParameterUtils.curingGlobalParams(null,null,CommandType.START_CURRENT_TASK_PROCESS,null));
-
Assert.assertNull(ParameterUtils.curingGlobalParams(globalParamMap,null,CommandType.START_CURRENT_TASK_PROCESS,scheduleTime));
+ Assert.assertNull(ParameterUtils.curingGlobalParams(null, null,
CommandType.START_CURRENT_TASK_PROCESS, null));
+ Assert.assertNull(ParameterUtils.curingGlobalParams(globalParamMap,
null, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime));
//test globalParamList is not null
- Property property=new Property("testGlobalParam", Direct.IN,
DataType.VARCHAR,"testGlobalParam");
+ Property property = new Property("testGlobalParam", Direct.IN,
DataType.VARCHAR, "testGlobalParam");
globalParamList.add(property);
- String result2 =
ParameterUtils.curingGlobalParams(null,globalParamList,CommandType.START_CURRENT_TASK_PROCESS,scheduleTime);
- Assert.assertEquals(result2, JSON.toJSONString(globalParamList));
+ String result2 = ParameterUtils.curingGlobalParams(null,
globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime);
+ Assert.assertEquals(result2, JSONUtils.toJsonString(globalParamList));
- String result3 =
ParameterUtils.curingGlobalParams(globalParamMap,globalParamList,CommandType.START_CURRENT_TASK_PROCESS,null);
- Assert.assertEquals(result3, JSON.toJSONString(globalParamList));
+ String result3 = ParameterUtils.curingGlobalParams(globalParamMap,
globalParamList, CommandType.START_CURRENT_TASK_PROCESS, null);
+ Assert.assertEquals(result3, JSONUtils.toJsonString(globalParamList));
String result4 = ParameterUtils.curingGlobalParams(globalParamMap,
globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime);
- Assert.assertEquals(result4, JSON.toJSONString(globalParamList));
+ Assert.assertEquals(result4, JSONUtils.toJsonString(globalParamList));
//test var $ startsWith
- globalParamMap.put("bizDate","${system.biz.date}");
- globalParamMap.put("b1zCurdate","${system.biz.curdate}");
-
+ globalParamMap.put("bizDate", "${system.biz.date}");
+ globalParamMap.put("b1zCurdate", "${system.biz.curdate}");
- Property property2=new Property("testParamList1", Direct.IN,
DataType.VARCHAR,"testParamList");
- Property property3=new Property("testParamList2", Direct.IN,
DataType.VARCHAR,"{testParamList1}");
- Property property4=new Property("testParamList3", Direct.IN,
DataType.VARCHAR,"${b1zCurdate}");
+ Property property2 = new Property("testParamList1", Direct.IN,
DataType.VARCHAR, "testParamList");
+ Property property3 = new Property("testParamList2", Direct.IN,
DataType.VARCHAR, "{testParamList1}");
+ Property property4 = new Property("testParamList3", Direct.IN,
DataType.VARCHAR, "${b1zCurdate}");
globalParamList.add(property2);
globalParamList.add(property3);
globalParamList.add(property4);
String result5 = ParameterUtils.curingGlobalParams(globalParamMap,
globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime);
- Assert.assertEquals(result5,JSONUtils.toJsonString(globalParamList));
+ Assert.assertEquals(result5, JSONUtils.toJsonString(globalParamList));
}
/**
@@ -122,9 +140,9 @@ public class ParameterUtilsTest {
@Test
public void testHandleEscapes() throws Exception {
Assert.assertNull(ParameterUtils.handleEscapes(null));
- Assert.assertEquals("",ParameterUtils.handleEscapes(""));
- Assert.assertEquals("test
Parameter",ParameterUtils.handleEscapes("test Parameter"));
-
Assert.assertEquals("////%test////%Parameter",ParameterUtils.handleEscapes("%test%Parameter"));
+ Assert.assertEquals("", ParameterUtils.handleEscapes(""));
+ Assert.assertEquals("test Parameter",
ParameterUtils.handleEscapes("test Parameter"));
+ Assert.assertEquals("////%test////%Parameter",
ParameterUtils.handleEscapes("%test%Parameter"));
}
}
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java
index d204dfd..1037b9b 100644
---
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dolphinscheduler.common.utils.placeholder;
import org.apache.dolphinscheduler.common.utils.DateUtils;
@@ -25,44 +26,49 @@ import java.util.Date;
public class TimePlaceholderUtilsTest {
- Date date = null;
+ private Date date;
@Before
- public void init(){
- date = DateUtils.parse("20170101010101","yyyyMMddHHmmss");
+ public void init() {
+ date = DateUtils.parse("20170101010101", "yyyyMMddHHmmss");
+ }
+
+ @Test
+ public void replacePlaceholdersT() {
+
Assert.assertEquals("2017test12017:***2016-12-31,20170102,20170130,20161227,20161231",
TimePlaceholderUtils
+
.replacePlaceholders("$[yyyy]test1$[yyyy:***]$[yyyy-MM-dd-1],$[month_begin(yyyyMMdd,
1)],$[month_end(yyyyMMdd, -1)],$[week_begin(yyyyMMdd, 1)],$[week_end(yyyyMMdd,
-1)]",
+ date, true));
+
+
Assert.assertEquals("1483200061,1483290061,1485709261,1482771661,1483113600,1483203661",
TimePlaceholderUtils.replacePlaceholders("$[timestamp(yyyyMMdd00mmss)],"
+ + "$[timestamp(month_begin(yyyyMMddHHmmss, 1))],"
+ + "$[timestamp(month_end(yyyyMMddHHmmss, -1))],"
+ + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))],"
+ + "$[timestamp(week_end(yyyyMMdd000000, -1))],"
+ + "$[timestamp(yyyyMMddHHmmss)]",
+ date, true));
+ }
+
+ @Test
+ public void calcMinutesT() {
+ Assert.assertEquals("Sun Jan 01 01:01:01 CST 2017=yyyy",
TimePlaceholderUtils.calcMinutes("yyyy", date).toString());
+ Assert.assertEquals("Sun Jan 08 01:01:01 CST 2017=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd+7*1", date).toString());
+ Assert.assertEquals("Sun Dec 25 01:01:01 CST 2016=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd-7*1", date).toString());
+ Assert.assertEquals("Mon Jan 02 01:01:01 CST 2017=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd+1", date).toString());
+ Assert.assertEquals("Sat Dec 31 01:01:01 CST 2016=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd-1", date).toString());
+ Assert.assertEquals("Sun Jan 01 02:01:01 CST 2017=yyyyMMddHH",
TimePlaceholderUtils.calcMinutes("yyyyMMddHH+1/24", date).toString());
+ Assert.assertEquals("Sun Jan 01 00:01:01 CST 2017=yyyyMMddHH",
TimePlaceholderUtils.calcMinutes("yyyyMMddHH-1/24", date).toString());
+ }
+
+ @Test
+ public void calcMonthsT() {
+ Assert.assertEquals("Mon Jan 01 01:01:01 CST 2018=yyyyMMdd",
TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,12*1)", date).toString());
+ Assert.assertEquals("Fri Jan 01 01:01:01 CST 2016=yyyyMMdd",
TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,-12*1)", date).toString());
}
-// @Test
-// public void replacePlaceholdersT() {
-//
Assert.assertEquals("2017test12017:***2016-12-31,20170102,20170130,20161227,20161231",
TimePlaceholderUtils.replacePlaceholders("$[yyyy]test1$[yyyy:***]$[yyyy-MM-dd-1],$[month_begin(yyyyMMdd,
1)],$[month_end(yyyyMMdd, -1)],$[week_begin(yyyyMMdd, 1)],$[week_end(yyyyMMdd,
-1)]",
-// date, true));
-//
-//
Assert.assertEquals("1483200061,1483290061,1485709261,1482771661,1483113600,1483203661",
TimePlaceholderUtils.replacePlaceholders("$[timestamp(yyyyMMdd00mmss)],"
-// + "$[timestamp(month_begin(yyyyMMddHHmmss, 1))],"
-// + "$[timestamp(month_end(yyyyMMddHHmmss, -1))],"
-// + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))],"
-// + "$[timestamp(week_end(yyyyMMdd000000, -1))],"
-// + "$[timestamp(yyyyMMddHHmmss)]",
-// date, true));
-// }
-//
-//
-//
-// @Test
-// public void calcMinutesT() {
-// Assert.assertEquals("Sun Jan 01 01:01:01 CST 2017=yyyy",
TimePlaceholderUtils.calcMinutes("yyyy", date).toString());
-// Assert.assertEquals("Sun Jan 08 01:01:01 CST 2017=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd+7*1", date).toString());
-// Assert.assertEquals("Sun Dec 25 01:01:01 CST 2016=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd-7*1", date).toString());
-// Assert.assertEquals("Mon Jan 02 01:01:01 CST 2017=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd+1", date).toString());
-// Assert.assertEquals("Sat Dec 31 01:01:01 CST 2016=yyyyMMdd",
TimePlaceholderUtils.calcMinutes("yyyyMMdd-1", date).toString());
-// Assert.assertEquals("Sun Jan 01 02:01:01 CST 2017=yyyyMMddHH",
TimePlaceholderUtils.calcMinutes("yyyyMMddHH+1/24", date).toString());
-// Assert.assertEquals("Sun Jan 01 00:01:01 CST 2017=yyyyMMddHH",
TimePlaceholderUtils.calcMinutes("yyyyMMddHH-1/24", date).toString());
-// }
-//
-// @Test
-// public void calcMonthsT() {
-// Assert.assertEquals("Mon Jan 01 01:01:01 CST 2018=yyyyMMdd",
TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,12*1)", date).toString());
-// Assert.assertEquals("Fri Jan 01 01:01:01 CST 2016=yyyyMMdd",
TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,-12*1)", date).toString());
-// }
+ @Test
+ public void testGetPlaceHolderTime() {
+
+ Assert.assertEquals("20170101",
TimePlaceholderUtils.getPlaceHolderTime("yyyyMMdd", date));
+ }
}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 55c0cdc..072ec13 100644
--- a/pom.xml
+++ b/pom.xml
@@ -761,6 +761,7 @@
<include>**/common/utils/LoggerUtilsTest.java</include>
<include>**/common/utils/OSUtilsTest.java</include>
<include>**/common/utils/ParameterUtilsTest.java</include>
+
<include>**/common/utils/TimePlaceholderUtilsTest.java</include>
<include>**/common/utils/PreconditionsTest.java</include>
<include>**/common/utils/PropertyUtilsTest.java</include>
<include>**/common/utils/SchemaUtilsTest.java</include>