@ -14,10 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License .
* /
package org.apache.dolphinscheduler.common.utils ;
import org.apache.commons.lang.StringUtils ;
import org.apache.commons.lang.time.DateUtils ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.CommandType ;
import org.apache.dolphinscheduler.common.enums.DataType ;
@ -25,12 +24,17 @@ 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.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 org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
* parameter parse utils
@ -39,6 +43,10 @@ public class ParameterUtils {
private static final Logger logger = LoggerFactory . getLogger ( ParameterUtils . class ) ;
private ParameterUtils ( ) {
throw new UnsupportedOperationException ( "Construct ParameterUtils" ) ;
}
/ * *
* convert parameters place holders
*
@ -57,11 +65,7 @@ public class ParameterUtils {
Date cronTime = null ;
if ( StringUtils . isNotEmpty ( cronTimeStr ) ) {
try {
cronTime = DateUtils . parseDate ( cronTimeStr , new String [ ] { Constants . PARAMETER_FORMAT_TIME } ) ;
} catch ( ParseException e ) {
logger . error ( "parse {} exception" , cronTimeStr , e ) ;
}
cronTime = DateUtils . parse ( cronTimeStr , Constants . PARAMETER_FORMAT_TIME ) ;
} else {
cronTime = new Date ( ) ;
}
@ -94,12 +98,8 @@ public class ParameterUtils {
Date cronTime = null ;
if ( StringUtils . isNotEmpty ( cronTimeStr ) ) {
try {
cronTime = DateUtils . parseDate ( cronTimeStr , new String [ ] { Constants . PARAMETER_FORMAT_TIME } ) ;
cronTime = DateUtils . parse ( cronTimeStr , Constants . PARAMETER_FORMAT_TIME ) ;
} catch ( ParseException e ) {
logger . error ( String . format ( "parse %s exception" , cronTimeStr ) , e ) ;
}
} else {
cronTime = new Date ( ) ;
}
@ -115,34 +115,34 @@ public class ParameterUtils {
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 ) ) {
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 ) ) {
} else if ( dataType . equals ( DataType . LONG ) ) {
stmt . setLong ( index , Long . parseLong ( value ) ) ;
} else if ( dataType . equals ( DataType . FLOAT ) ) {
} else if ( dataType . equals ( DataType . FLOAT ) ) {
stmt . setFloat ( index , Float . parseFloat ( value ) ) ;
} else if ( dataType . equals ( DataType . DOUBLE ) ) {
} else if ( dataType . equals ( DataType . DOUBLE ) ) {
stmt . setDouble ( index , Double . parseDouble ( value ) ) ;
} else if ( dataType . equals ( DataType . DATE ) ) {
} else if ( dataType . equals ( DataType . DATE ) ) {
stmt . setDate ( index , java . sql . Date . valueOf ( value ) ) ;
} else if ( dataType . equals ( DataType . TIME ) ) {
} else if ( dataType . equals ( DataType . TIME ) ) {
stmt . setString ( index , value ) ;
} else if ( dataType . equals ( DataType . TIMESTAMP ) ) {
} 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 ) ) ;
} else if ( dataType . equals ( DataType . BOOLEAN ) ) {
stmt . setBoolean ( index , Boolean . parseBoolean ( value ) ) ;
}
}
@ -155,20 +155,20 @@ public class ParameterUtils {
* @param scheduleTime schedule time
* @return curing user define parameters
* /
public static String curingGlobalParams ( Map < String , String > globalParamMap , List < Property > globalParamList ,
CommandType commandType , Date scheduleTime ) {
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 ) {
if ( globalParamMap ! = null ) {
globalMap . putAll ( globalParamMap ) ;
}
Map < String , String > allParamMap = new HashMap < > ( ) ;
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
Map < String , String > timeParams = BusinessTimeUtils
. getBusinessTime ( commandType , scheduleTime ) ;
if ( timeParams ! = null ) {
@ -179,45 +179,41 @@ public class ParameterUtils {
Set < Map . Entry < String , String > > entries = allParamMap . entrySet ( ) ;
Map < String , String > resolveMap = new HashMap < > ( ) ;
for ( Map . Entry < String , String > entry : entries ) {
Map < String , String > resolveMap = new HashMap < > ( ) ;
for ( Map . Entry < String , String > entry : entries ) {
String val = entry . getValue ( ) ;
if ( val . startsWith ( "$" ) ) {
if ( val . startsWith ( "$" ) ) {
String str = ParameterUtils . convertParameterPlaceholders ( val , allParamMap ) ;
resolveMap . put ( entry . getKey ( ) , str ) ;
resolveMap . put ( entry . getKey ( ) , str ) ;
}
}
globalMap . putAll ( resolveMap ) ;
for ( Property property : globalParamList ) {
for ( Property property : globalParamList ) {
String val = globalMap . get ( property . getProp ( ) ) ;
if ( val ! = null ) {
if ( val ! = null ) {
property . setValue ( val ) ;
}
}
return JSONUtils . toJsonString ( globalParamList ) ;
}
/ * *
* handle escapes
*
* @param inputString input string
* @return string filter escapes
* /
public static String handleEscapes ( String inputString ) {
public static String handleEscapes ( String inputString ) {
if ( StringUtils . isNotEmpty ( inputString ) ) {
if ( StringUtils . isNotEmpty ( inputString ) ) {
return inputString . replace ( "%" , "////%" ) . replaceAll ( "[\n|\r\t]" , "_" ) ;
}
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 < > ( ) ;
@ -236,19 +232,19 @@ public class ParameterUtils {
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 < > ( ) ;
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 ( ) ) {
while ( iter . hasNext ( ) ) {
Map . Entry < String , Property > en = iter . next ( ) ;
map . put ( en . getKey ( ) , en . getValue ( ) . getValue ( ) ) ;
map . put ( en . getKey ( ) , en . getValue ( ) . getValue ( ) ) ;
}
return map ;
}