[Improvement][common] Add UnsupportedOperationException for utils construct (#3381)
* [Improvement][common] Add UnsupportedOperationException for utils construct * Fix checkstyle
This commit is contained in:
parent
632d52cd69
commit
ac4ed94061
|
|
@ -14,6 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common;
|
||||
|
||||
import org.apache.dolphinscheduler.common.enums.ExecutionStatus;
|
||||
|
|
@ -27,7 +28,7 @@ import java.util.regex.Pattern;
|
|||
public final class Constants {
|
||||
|
||||
private Constants() {
|
||||
throw new IllegalStateException("Constants class");
|
||||
throw new UnsupportedOperationException("Construct Constants");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -14,13 +14,19 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.commons.beanutils.BeanMap;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Provides utility methods and decorators for {@link Collection} instances.
|
||||
|
|
@ -37,8 +43,9 @@ import java.util.*;
|
|||
public class CollectionUtils {
|
||||
|
||||
private CollectionUtils() {
|
||||
throw new IllegalStateException("CollectionUtils class");
|
||||
throw new UnsupportedOperationException("Construct CollectionUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Collection} containing <i>a</i> minus a subset of
|
||||
* <i>b</i>. Only the elements of <i>b</i> that satisfy the predicate
|
||||
|
|
@ -71,7 +78,7 @@ public class CollectionUtils {
|
|||
/**
|
||||
* String to map
|
||||
*
|
||||
* @param str string
|
||||
* @param str string
|
||||
* @param separator separator
|
||||
* @return string to map
|
||||
*/
|
||||
|
|
@ -82,7 +89,7 @@ public class CollectionUtils {
|
|||
/**
|
||||
* String to map
|
||||
*
|
||||
* @param str string
|
||||
* @param str string
|
||||
* @param separator separator
|
||||
* @param keyPrefix prefix
|
||||
* @return string to map
|
||||
|
|
@ -112,7 +119,6 @@ public class CollectionUtils {
|
|||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper class to easily access cardinality properties of two collections.
|
||||
*
|
||||
|
|
@ -137,8 +143,8 @@ public class CollectionUtils {
|
|||
* @param b the second collection
|
||||
*/
|
||||
public CardinalityHelper(final Iterable<? extends O> a, final Iterable<? extends O> b) {
|
||||
cardinalityA = CollectionUtils.<O>getCardinalityMap(a);
|
||||
cardinalityB = CollectionUtils.<O>getCardinalityMap(b);
|
||||
cardinalityA = CollectionUtils.getCardinalityMap(a);
|
||||
cardinalityB = CollectionUtils.getCardinalityMap(b);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -227,7 +233,7 @@ public class CollectionUtils {
|
|||
* Only those elements present in the collection will appear as
|
||||
* keys in the map.
|
||||
*
|
||||
* @param <O> the type of object in the returned {@link Map}. This is a super type of O
|
||||
* @param <O> the type of object in the returned {@link Map}. This is a super type of O
|
||||
* @param coll the collection to get the cardinality map for, must not be null
|
||||
* @return the populated cardinality map
|
||||
*/
|
||||
|
|
@ -239,9 +245,9 @@ public class CollectionUtils {
|
|||
return count;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes certain attributes of each object in the list
|
||||
*
|
||||
* @param originList origin list
|
||||
* @param exclusionSet exclusion set
|
||||
* @param <T> T
|
||||
|
|
@ -258,8 +264,8 @@ public class CollectionUtils {
|
|||
Map<String, Object> instanceMap;
|
||||
for (T instance : originList) {
|
||||
Map<String, Object> dataMap = new BeanMap(instance);
|
||||
instanceMap = new LinkedHashMap<>(16,0.75f,true);
|
||||
for (Map.Entry<String, Object> entry: dataMap.entrySet()) {
|
||||
instanceMap = new LinkedHashMap<>(16, 0.75f, true);
|
||||
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
|
||||
if (exclusionSet.contains(entry.getKey())) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,124 +14,129 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
import org.apache.dolphinscheduler.common.enums.ResUploadType;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.apache.hadoop.security.UserGroupInformation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* common utils
|
||||
*/
|
||||
public class CommonUtils {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);
|
||||
|
||||
private static final Base64 BASE64 = new Base64();
|
||||
private static final Base64 BASE64 = new Base64();
|
||||
|
||||
private CommonUtils() {
|
||||
throw new IllegalStateException("CommonUtils class");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return get the path of system environment variables
|
||||
*/
|
||||
public static String getSystemEnvPath() {
|
||||
String envPath = PropertyUtils.getString(Constants.DOLPHINSCHEDULER_ENV_PATH);
|
||||
if (StringUtils.isEmpty(envPath)) {
|
||||
URL envDefaultPath = CommonUtils.class.getClassLoader().getResource(Constants.ENV_PATH);
|
||||
|
||||
if (envDefaultPath != null){
|
||||
envPath = envDefaultPath.getPath();
|
||||
logger.debug("env path :{}", envPath);
|
||||
}else{
|
||||
envPath = "/etc/profile";
|
||||
}
|
||||
private CommonUtils() {
|
||||
throw new UnsupportedOperationException("Construct CommonUtils");
|
||||
}
|
||||
|
||||
return envPath;
|
||||
}
|
||||
/**
|
||||
* @return get the path of system environment variables
|
||||
*/
|
||||
public static String getSystemEnvPath() {
|
||||
String envPath = PropertyUtils.getString(Constants.DOLPHINSCHEDULER_ENV_PATH);
|
||||
if (StringUtils.isEmpty(envPath)) {
|
||||
URL envDefaultPath = CommonUtils.class.getClassLoader().getResource(Constants.ENV_PATH);
|
||||
|
||||
/**
|
||||
*
|
||||
* @return is develop mode
|
||||
*/
|
||||
public static boolean isDevelopMode() {
|
||||
return PropertyUtils.getBoolean(Constants.DEVELOPMENT_STATE, true);
|
||||
}
|
||||
if (envDefaultPath != null) {
|
||||
envPath = envDefaultPath.getPath();
|
||||
logger.debug("env path :{}", envPath);
|
||||
} else {
|
||||
envPath = "/etc/profile";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* if upload resource is HDFS and kerberos startup is true , else false
|
||||
* @return true if upload resource is HDFS and kerberos startup
|
||||
*/
|
||||
public static boolean getKerberosStartupState(){
|
||||
String resUploadStartupType = PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE);
|
||||
ResUploadType resUploadType = ResUploadType.valueOf(resUploadStartupType);
|
||||
Boolean kerberosStartupState = PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE,false);
|
||||
return resUploadType == ResUploadType.HDFS && kerberosStartupState;
|
||||
}
|
||||
|
||||
/**
|
||||
* load kerberos configuration
|
||||
* @throws Exception errors
|
||||
*/
|
||||
public static void loadKerberosConf()throws Exception{
|
||||
if (CommonUtils.getKerberosStartupState()) {
|
||||
System.setProperty(Constants.JAVA_SECURITY_KRB5_CONF, PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH));
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.set(Constants.HADOOP_SECURITY_AUTHENTICATION, Constants.KERBEROS);
|
||||
UserGroupInformation.setConfiguration(configuration);
|
||||
UserGroupInformation.loginUserFromKeytab(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME),
|
||||
PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH));
|
||||
return envPath;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* encode password
|
||||
* @param password
|
||||
* @return
|
||||
*/
|
||||
public static String encodePassword(String password) {
|
||||
if(StringUtils.isEmpty(password)){return StringUtils.EMPTY; }
|
||||
//if encryption is not turned on, return directly
|
||||
boolean encryptionEnable = PropertyUtils.getBoolean(Constants.DATASOURCE_ENCRYPTION_ENABLE,false);
|
||||
if ( !encryptionEnable){ return password; }
|
||||
|
||||
// Using Base64 + salt to process password
|
||||
String salt = PropertyUtils.getString(Constants.DATASOURCE_ENCRYPTION_SALT,Constants.DATASOURCE_ENCRYPTION_SALT_DEFAULT);
|
||||
String passwordWithSalt = salt + new String(BASE64.encode(password.getBytes(StandardCharsets.UTF_8))) ;
|
||||
return new String(BASE64.encode(passwordWithSalt.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* decode password
|
||||
* @param password
|
||||
* @return
|
||||
*/
|
||||
public static String decodePassword(String password) {
|
||||
if(StringUtils.isEmpty(password)){return StringUtils.EMPTY ; }
|
||||
|
||||
//if encryption is not turned on, return directly
|
||||
boolean encryptionEnable = PropertyUtils.getBoolean(Constants.DATASOURCE_ENCRYPTION_ENABLE,false);
|
||||
if ( !encryptionEnable){ return password; }
|
||||
|
||||
// Using Base64 + salt to process password
|
||||
String salt = PropertyUtils.getString(Constants.DATASOURCE_ENCRYPTION_SALT,Constants.DATASOURCE_ENCRYPTION_SALT_DEFAULT);
|
||||
String passwordWithSalt = new String(BASE64.decode(password), StandardCharsets.UTF_8) ;
|
||||
if(!passwordWithSalt.startsWith(salt)){
|
||||
logger.warn("There is a password and salt mismatch: {} ",password);
|
||||
return password;
|
||||
/**
|
||||
* @return is develop mode
|
||||
*/
|
||||
public static boolean isDevelopMode() {
|
||||
return PropertyUtils.getBoolean(Constants.DEVELOPMENT_STATE, true);
|
||||
}
|
||||
return new String(BASE64.decode(passwordWithSalt.substring(salt.length())), StandardCharsets.UTF_8) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* if upload resource is HDFS and kerberos startup is true , else false
|
||||
*
|
||||
* @return true if upload resource is HDFS and kerberos startup
|
||||
*/
|
||||
public static boolean getKerberosStartupState() {
|
||||
String resUploadStartupType = PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE);
|
||||
ResUploadType resUploadType = ResUploadType.valueOf(resUploadStartupType);
|
||||
Boolean kerberosStartupState = PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false);
|
||||
return resUploadType == ResUploadType.HDFS && kerberosStartupState;
|
||||
}
|
||||
|
||||
/**
|
||||
* load kerberos configuration
|
||||
*
|
||||
* @throws Exception errors
|
||||
*/
|
||||
public static void loadKerberosConf() throws Exception {
|
||||
if (CommonUtils.getKerberosStartupState()) {
|
||||
System.setProperty(Constants.JAVA_SECURITY_KRB5_CONF, PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH));
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.set(Constants.HADOOP_SECURITY_AUTHENTICATION, Constants.KERBEROS);
|
||||
UserGroupInformation.setConfiguration(configuration);
|
||||
UserGroupInformation.loginUserFromKeytab(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME),
|
||||
PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* encode password
|
||||
*/
|
||||
public static String encodePassword(String password) {
|
||||
if (StringUtils.isEmpty(password)) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
//if encryption is not turned on, return directly
|
||||
boolean encryptionEnable = PropertyUtils.getBoolean(Constants.DATASOURCE_ENCRYPTION_ENABLE, false);
|
||||
if (!encryptionEnable) {
|
||||
return password;
|
||||
}
|
||||
|
||||
// Using Base64 + salt to process password
|
||||
String salt = PropertyUtils.getString(Constants.DATASOURCE_ENCRYPTION_SALT, Constants.DATASOURCE_ENCRYPTION_SALT_DEFAULT);
|
||||
String passwordWithSalt = salt + new String(BASE64.encode(password.getBytes(StandardCharsets.UTF_8)));
|
||||
return new String(BASE64.encode(passwordWithSalt.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* decode password
|
||||
*/
|
||||
public static String decodePassword(String password) {
|
||||
if (StringUtils.isEmpty(password)) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
//if encryption is not turned on, return directly
|
||||
boolean encryptionEnable = PropertyUtils.getBoolean(Constants.DATASOURCE_ENCRYPTION_ENABLE, false);
|
||||
if (!encryptionEnable) {
|
||||
return password;
|
||||
}
|
||||
|
||||
// Using Base64 + salt to process password
|
||||
String salt = PropertyUtils.getString(Constants.DATASOURCE_ENCRYPTION_SALT, Constants.DATASOURCE_ENCRYPTION_SALT_DEFAULT);
|
||||
String passwordWithSalt = new String(BASE64.decode(password), StandardCharsets.UTF_8);
|
||||
if (!passwordWithSalt.startsWith(salt)) {
|
||||
logger.warn("There is a password and salt mismatch: {} ", password);
|
||||
return password;
|
||||
}
|
||||
return new String(BASE64.decode(passwordWithSalt.substring(salt.length())), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,37 +14,40 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ConnectionUtils {
|
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(ConnectionUtils.class);
|
||||
public static final Logger logger = LoggerFactory.getLogger(ConnectionUtils.class);
|
||||
|
||||
private ConnectionUtils() {
|
||||
throw new IllegalStateException("ConnectionUtils class");
|
||||
}
|
||||
|
||||
/**
|
||||
* release resource
|
||||
* @param resources resources
|
||||
*/
|
||||
public static void releaseResource(AutoCloseable... resources) {
|
||||
|
||||
if (resources == null || resources.length == 0) {
|
||||
return;
|
||||
private ConnectionUtils() {
|
||||
throw new UnsupportedOperationException("Construct ConnectionUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* release resource
|
||||
*
|
||||
* @param resources resources
|
||||
*/
|
||||
public static void releaseResource(AutoCloseable... resources) {
|
||||
|
||||
if (resources == null || resources.length == 0) {
|
||||
return;
|
||||
}
|
||||
Arrays.stream(resources).filter(Objects::nonNull)
|
||||
.forEach(resource -> {
|
||||
try {
|
||||
resource.close();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
Arrays.stream(resources).filter(Objects::nonNull)
|
||||
.forEach(resource -> {
|
||||
try {
|
||||
resource.close();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,10 @@ public class DateUtils {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
|
||||
|
||||
private DateUtils() {
|
||||
throw new UnsupportedOperationException("Construct DateUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* date to local datetime
|
||||
*
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@ import org.apache.dolphinscheduler.common.enums.DependResult;
|
|||
import org.apache.dolphinscheduler.common.enums.DependentRelation;
|
||||
import org.apache.dolphinscheduler.common.model.DateInterval;
|
||||
import org.apache.dolphinscheduler.common.utils.dependent.DependentDateUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
|
@ -29,32 +27,35 @@ import java.util.List;
|
|||
|
||||
public class DependentUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DependentUtils.class);
|
||||
private DependentUtils() {
|
||||
throw new UnsupportedOperationException("Construct DependentUtils");
|
||||
}
|
||||
|
||||
public static DependResult getDependResultForRelation(DependentRelation relation,
|
||||
List<DependResult> dependResultList){
|
||||
List<DependResult> dependResultList) {
|
||||
|
||||
DependResult dependResult = DependResult.SUCCESS;
|
||||
|
||||
switch (relation){
|
||||
switch (relation) {
|
||||
case AND:
|
||||
if(dependResultList.contains(DependResult.FAILED)){
|
||||
if (dependResultList.contains(DependResult.FAILED)) {
|
||||
dependResult = DependResult.FAILED;
|
||||
} if(dependResultList.contains(DependResult.WAITING)){
|
||||
}
|
||||
if (dependResultList.contains(DependResult.WAITING)) {
|
||||
dependResult = DependResult.WAITING;
|
||||
}
|
||||
break;
|
||||
case OR:
|
||||
if(dependResultList.contains(DependResult.SUCCESS)){
|
||||
if (dependResultList.contains(DependResult.SUCCESS)) {
|
||||
dependResult = DependResult.SUCCESS;
|
||||
}else if(dependResultList.contains(DependResult.WAITING)){
|
||||
} else if (dependResultList.contains(DependResult.WAITING)) {
|
||||
dependResult = DependResult.WAITING;
|
||||
}else{
|
||||
} else {
|
||||
dependResult = DependResult.FAILED;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
break;
|
||||
}
|
||||
return dependResult;
|
||||
}
|
||||
|
|
@ -62,36 +63,37 @@ public class DependentUtils {
|
|||
|
||||
/**
|
||||
* get date interval list by business date and date value.
|
||||
*
|
||||
* @param businessDate business date
|
||||
* @param dateValue date value
|
||||
* @return date interval list by business date and date value.
|
||||
*/
|
||||
public static List<DateInterval> getDateIntervalList(Date businessDate, String dateValue){
|
||||
public static List<DateInterval> getDateIntervalList(Date businessDate, String dateValue) {
|
||||
List<DateInterval> result = new ArrayList<>();
|
||||
switch (dateValue){
|
||||
switch (dateValue) {
|
||||
case "currentHour":
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 0);
|
||||
break;
|
||||
case "last1Hour":
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 1);
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 1);
|
||||
break;
|
||||
case "last2Hours":
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 2);
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 2);
|
||||
break;
|
||||
case "last3Hours":
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 3);
|
||||
result = DependentDateUtils.getLastHoursInterval(businessDate, 3);
|
||||
break;
|
||||
case "last24Hours":
|
||||
result = DependentDateUtils.getSpecialLastDayInterval(businessDate);
|
||||
break;
|
||||
case "today":
|
||||
result = DependentDateUtils.getTodayInterval(businessDate);
|
||||
result = DependentDateUtils.getTodayInterval(businessDate);
|
||||
break;
|
||||
case "last1Days":
|
||||
result = DependentDateUtils.getLastDayInterval(businessDate, 1);
|
||||
result = DependentDateUtils.getLastDayInterval(businessDate, 1);
|
||||
break;
|
||||
case "last2Days":
|
||||
result = DependentDateUtils.getLastDayInterval(businessDate, 2);
|
||||
result = DependentDateUtils.getLastDayInterval(businessDate, 2);
|
||||
break;
|
||||
case "last3Days":
|
||||
result = DependentDateUtils.getLastDayInterval(businessDate, 3);
|
||||
|
|
@ -144,5 +146,4 @@ public class DependentUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
|
@ -23,14 +24,16 @@ import org.apache.commons.codec.digest.DigestUtils;
|
|||
*/
|
||||
public class EncryptionUtils {
|
||||
|
||||
private EncryptionUtils() {
|
||||
throw new UnsupportedOperationException("Construct EncryptionUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param rawStr raw string
|
||||
* @return md5(rawStr)
|
||||
*/
|
||||
public static String getMd5(String rawStr) {
|
||||
return DigestUtils.md5Hex(null == rawStr ? StringUtils.EMPTY : rawStr);
|
||||
return DigestUtils.md5Hex(null == rawStr ? StringUtils.EMPTY : rawStr);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,12 +14,15 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
|
||||
|
||||
public class EnumUtils {
|
||||
|
||||
private EnumUtils() {
|
||||
throw new UnsupportedOperationException("Construct EnumUtils");
|
||||
}
|
||||
|
||||
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) {
|
||||
if (enumName == null) {
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import static org.apache.dolphinscheduler.common.Constants.DATA_BASEDIR_PATH;
|
||||
|
|
@ -21,6 +22,9 @@ import static org.apache.dolphinscheduler.common.Constants.RESOURCE_VIEW_SUFFIXS
|
|||
import static org.apache.dolphinscheduler.common.Constants.RESOURCE_VIEW_SUFFIXS_DEFAULT_VALUE;
|
||||
import static org.apache.dolphinscheduler.common.Constants.YYYYMMDDHHMMSS;
|
||||
|
||||
import org.apache.commons.io.Charsets;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
|
@ -36,9 +40,6 @@ import java.nio.charset.Charset;
|
|||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.commons.io.Charsets;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -46,12 +47,17 @@ import org.slf4j.LoggerFactory;
|
|||
* file utils
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
|
||||
|
||||
public static final String DATA_BASEDIR = PropertyUtils.getString(DATA_BASEDIR_PATH,"/tmp/dolphinscheduler");
|
||||
public static final String DATA_BASEDIR = PropertyUtils.getString(DATA_BASEDIR_PATH, "/tmp/dolphinscheduler");
|
||||
|
||||
public static final ThreadLocal<Logger> taskLoggerThreadLocal = new ThreadLocal<>();
|
||||
|
||||
private FileUtils() {
|
||||
throw new UnsupportedOperationException("Construct FileUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* get file suffix
|
||||
*
|
||||
|
|
@ -80,7 +86,7 @@ public class FileUtils {
|
|||
String fileName = String.format("%s/download/%s/%s", DATA_BASEDIR, DateUtils.getCurrentTime(YYYYMMDDHHMMSS), filename);
|
||||
|
||||
File file = new File(fileName);
|
||||
if (!file.getParentFile().exists()){
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
|
|
@ -97,7 +103,7 @@ public class FileUtils {
|
|||
public static String getUploadFilename(String tenantCode, String filename) {
|
||||
String fileName = String.format("%s/%s/resources/%s", DATA_BASEDIR, tenantCode, filename);
|
||||
File file = new File(fileName);
|
||||
if (!file.getParentFile().exists()){
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
|
|
@ -106,6 +112,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* directory of process execution
|
||||
*
|
||||
* @param projectId project id
|
||||
* @param processDefineId process definition id
|
||||
* @param processInstanceId process instance id
|
||||
|
|
@ -114,9 +121,9 @@ public class FileUtils {
|
|||
*/
|
||||
public static String getProcessExecDir(int projectId, int processDefineId, int processInstanceId, int taskInstanceId) {
|
||||
String fileName = String.format("%s/exec/process/%s/%s/%s/%s", DATA_BASEDIR, Integer.toString(projectId),
|
||||
Integer.toString(processDefineId), Integer.toString(processInstanceId),Integer.toString(taskInstanceId));
|
||||
Integer.toString(processDefineId), Integer.toString(processInstanceId), Integer.toString(taskInstanceId));
|
||||
File file = new File(fileName);
|
||||
if (!file.getParentFile().exists()){
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
|
|
@ -125,6 +132,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* directory of process instances
|
||||
*
|
||||
* @param projectId project id
|
||||
* @param processDefineId process definition id
|
||||
* @param processInstanceId process instance id
|
||||
|
|
@ -150,6 +158,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* create directory and user
|
||||
*
|
||||
* @param execLocalPath execute local path
|
||||
* @param userName user name
|
||||
* @throws IOException errors
|
||||
|
|
@ -190,12 +199,11 @@ public class FileUtils {
|
|||
OSUtils.taskLoggerThreadLocal.remove();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* write content to file ,if parent path not exists, it will do one's utmost to mkdir
|
||||
*
|
||||
* @param content content
|
||||
* @param filePath target file path
|
||||
* @param content content
|
||||
* @param filePath target file path
|
||||
* @return true if write success
|
||||
*/
|
||||
public static boolean writeContent2File(String content, String filePath) {
|
||||
|
|
@ -231,13 +239,13 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* Writes a String to a file creating the file if it does not exist.
|
||||
*
|
||||
* <p>
|
||||
* NOTE: As from v1.3, the parent directories of the file will be created
|
||||
* if they do not exist.
|
||||
*
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @throws IOException in case of an I/O error
|
||||
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
|
||||
* @since 2.4
|
||||
|
|
@ -248,13 +256,13 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* Writes a String to a file creating the file if it does not exist.
|
||||
*
|
||||
* <p>
|
||||
* NOTE: As from v1.3, the parent directories of the file will be created
|
||||
* if they do not exist.
|
||||
*
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @throws IOException in case of an I/O error
|
||||
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
|
||||
*/
|
||||
|
|
@ -265,9 +273,9 @@ public class FileUtils {
|
|||
/**
|
||||
* Writes a String to a file creating the file if it does not exist.
|
||||
*
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @param append if {@code true}, then the String will be added to the
|
||||
* end of the file rather than overwriting
|
||||
* @throws IOException in case of an I/O error
|
||||
|
|
@ -287,15 +295,14 @@ public class FileUtils {
|
|||
/**
|
||||
* Writes a String to a file creating the file if it does not exist.
|
||||
*
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param encoding the encoding to use, {@code null} means platform default
|
||||
* @param append if {@code true}, then the String will be added to the
|
||||
* end of the file rather than overwriting
|
||||
* @throws IOException in case of an I/O error
|
||||
* @throws UnsupportedCharsetException
|
||||
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
|
||||
* supported by the VM
|
||||
* @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
|
||||
* supported by the VM
|
||||
* @since 2.1
|
||||
*/
|
||||
public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {
|
||||
|
|
@ -305,8 +312,8 @@ public class FileUtils {
|
|||
/**
|
||||
* Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
|
||||
*
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
public static void writeStringToFile(File file, String data) throws IOException {
|
||||
|
|
@ -316,8 +323,8 @@ public class FileUtils {
|
|||
/**
|
||||
* Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
|
||||
*
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param file the file to write
|
||||
* @param data the content to write to the file
|
||||
* @param append if {@code true}, then the String will be added to the
|
||||
* end of the file rather than overwriting
|
||||
* @throws IOException in case of an I/O error
|
||||
|
|
@ -340,7 +347,7 @@ public class FileUtils {
|
|||
* An exception is thrown if the file exists but cannot be written to.
|
||||
* An exception is thrown if the parent directory cannot be created.
|
||||
*
|
||||
* @param file the file to open for output, must not be {@code null}
|
||||
* @param file the file to open for output, must not be {@code null}
|
||||
* @return a new {@link FileOutputStream} for the specified file
|
||||
* @throws IOException if the file object is a directory
|
||||
* @throws IOException if the file cannot be written to
|
||||
|
|
@ -364,7 +371,7 @@ public class FileUtils {
|
|||
* An exception is thrown if the file exists but cannot be written to.
|
||||
* An exception is thrown if the parent directory cannot be created.
|
||||
*
|
||||
* @param file the file to open for output, must not be {@code null}
|
||||
* @param file the file to open for output, must not be {@code null}
|
||||
* @param append if {@code true}, then bytes will be added to the
|
||||
* end of the file rather than overwriting
|
||||
* @return a new {@link FileOutputStream} for the specified file
|
||||
|
|
@ -384,15 +391,15 @@ public class FileUtils {
|
|||
} else {
|
||||
File parent = file.getParentFile();
|
||||
if (parent != null && !parent.mkdirs() && !parent.isDirectory()) {
|
||||
throw new IOException("Directory '" + parent + "' could not be created");
|
||||
throw new IOException("Directory '" + parent + "' could not be created");
|
||||
}
|
||||
}
|
||||
return new FileOutputStream(file, append);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* deletes a directory recursively
|
||||
*
|
||||
* @param dir directory
|
||||
* @throws IOException in case deletion is unsuccessful
|
||||
*/
|
||||
|
|
@ -420,17 +427,18 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* Gets all the parent subdirectories of the parentDir directory
|
||||
*
|
||||
* @param parentDir parent dir
|
||||
* @return all dirs
|
||||
*/
|
||||
public static File[] getAllDir(String parentDir){
|
||||
if(parentDir == null || "".equals(parentDir)) {
|
||||
public static File[] getAllDir(String parentDir) {
|
||||
if (parentDir == null || "".equals(parentDir)) {
|
||||
throw new RuntimeException("parentDir can not be empty");
|
||||
}
|
||||
|
||||
File file = new File(parentDir);
|
||||
if(!file.exists() || !file.isDirectory()) {
|
||||
throw new RuntimeException("parentDir not exist, or is not a directory:"+parentDir);
|
||||
if (!file.exists() || !file.isDirectory()) {
|
||||
throw new RuntimeException("parentDir not exist, or is not a directory:" + parentDir);
|
||||
}
|
||||
|
||||
return file.listFiles(File::isDirectory);
|
||||
|
|
@ -438,6 +446,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* Get Content
|
||||
*
|
||||
* @param inputStream input stream
|
||||
* @return string of input stream
|
||||
*/
|
||||
|
|
@ -447,15 +456,14 @@ public class FileUtils {
|
|||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length= inputStream.read(buffer)) != -1) {
|
||||
output.write(buffer,0,length);
|
||||
while ((length = inputStream.read(buffer)) != -1) {
|
||||
output.write(buffer, 0, length);
|
||||
}
|
||||
return output.toString();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
logger.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,9 +14,11 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.config.AuthSchemes;
|
||||
import org.apache.http.client.config.CookieSpecs;
|
||||
|
|
@ -30,148 +32,148 @@ import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* http utils
|
||||
*/
|
||||
public class HttpUtils {
|
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
|
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
|
||||
private HttpUtils() {
|
||||
throw new UnsupportedOperationException("Construct HttpUtils");
|
||||
}
|
||||
|
||||
private HttpUtils() {
|
||||
public static CloseableHttpClient getInstance() {
|
||||
return HttpClientInstance.httpClient;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static CloseableHttpClient getInstance(){
|
||||
return HttpClientInstance.httpClient;
|
||||
}
|
||||
|
||||
private static class HttpClientInstance{
|
||||
private static final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
|
||||
}
|
||||
private static class HttpClientInstance {
|
||||
private static final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
|
||||
}
|
||||
|
||||
|
||||
private static PoolingHttpClientConnectionManager cm;
|
||||
private static PoolingHttpClientConnectionManager cm;
|
||||
|
||||
private static SSLContext ctx = null;
|
||||
private static SSLContext ctx = null;
|
||||
|
||||
private static SSLConnectionSocketFactory socketFactory;
|
||||
private static SSLConnectionSocketFactory socketFactory;
|
||||
|
||||
private static RequestConfig requestConfig;
|
||||
private static RequestConfig requestConfig;
|
||||
|
||||
private static Registry<ConnectionSocketFactory> socketFactoryRegistry;
|
||||
private static Registry<ConnectionSocketFactory> socketFactoryRegistry;
|
||||
|
||||
private static X509TrustManager xtm = new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
private static X509TrustManager xtm = new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
static {
|
||||
try {
|
||||
ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
|
||||
ctx.init(null, new TrustManager[] { xtm }, null);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
logger.error("SSLContext init with NoSuchAlgorithmException", e);
|
||||
} catch (KeyManagementException e) {
|
||||
logger.error("SSLContext init with KeyManagementException", e);
|
||||
}
|
||||
socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
|
||||
/** set timeout、request time、socket timeout */
|
||||
requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES)
|
||||
.setExpectContinueEnabled(Boolean.TRUE)
|
||||
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
|
||||
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
|
||||
.setConnectTimeout(Constants.HTTP_CONNECT_TIMEOUT).setSocketTimeout(Constants.SOCKET_TIMEOUT)
|
||||
.setConnectionRequestTimeout(Constants.HTTP_CONNECTION_REQUEST_TIMEOUT).setRedirectsEnabled(true)
|
||||
.build();
|
||||
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
|
||||
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
cm.setDefaultMaxPerRoute(60);
|
||||
cm.setMaxTotal(100);
|
||||
static {
|
||||
try {
|
||||
ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
|
||||
ctx.init(null, new TrustManager[]{xtm}, null);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
logger.error("SSLContext init with NoSuchAlgorithmException", e);
|
||||
} catch (KeyManagementException e) {
|
||||
logger.error("SSLContext init with KeyManagementException", e);
|
||||
}
|
||||
socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
|
||||
/** set timeout、request time、socket timeout */
|
||||
requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES)
|
||||
.setExpectContinueEnabled(Boolean.TRUE)
|
||||
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
|
||||
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
|
||||
.setConnectTimeout(Constants.HTTP_CONNECT_TIMEOUT).setSocketTimeout(Constants.SOCKET_TIMEOUT)
|
||||
.setConnectionRequestTimeout(Constants.HTTP_CONNECTION_REQUEST_TIMEOUT).setRedirectsEnabled(true)
|
||||
.build();
|
||||
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
|
||||
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
cm.setDefaultMaxPerRoute(60);
|
||||
cm.setMaxTotal(100);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get http request content
|
||||
*
|
||||
* @param url url
|
||||
* @return http get request response content
|
||||
*/
|
||||
public static String get(String url) {
|
||||
CloseableHttpClient httpclient = HttpUtils.getInstance();
|
||||
|
||||
|
||||
/**
|
||||
* get http request content
|
||||
* @param url url
|
||||
* @return http get request response content
|
||||
*/
|
||||
public static String get(String url){
|
||||
CloseableHttpClient httpclient = HttpUtils.getInstance();
|
||||
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
return getResponseContentString(httpget,httpclient);
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
return getResponseContentString(httpget, httpclient);
|
||||
}
|
||||
|
||||
/**
|
||||
* get http response content
|
||||
*
|
||||
* @param httpget httpget
|
||||
* @param httpget httpget
|
||||
* @param httpClient httpClient
|
||||
* @return http get request response content
|
||||
*/
|
||||
public static String getResponseContentString(HttpGet httpget, CloseableHttpClient httpClient) {
|
||||
String responseContent = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
response = httpClient.execute(httpget);
|
||||
// check response status is 200
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
responseContent = EntityUtils.toString(entity, Constants.UTF_8);
|
||||
} else {
|
||||
logger.warn("http entity is null");
|
||||
}
|
||||
} else {
|
||||
logger.error("http get:{} response status code is not 200!", response.getStatusLine().getStatusCode());
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
logger.error(ioe.getMessage(), ioe);
|
||||
} finally {
|
||||
try {
|
||||
if (response != null) {
|
||||
EntityUtils.consume(response.getEntity());
|
||||
response.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
if (!httpget.isAborted()) {
|
||||
httpget.releaseConnection();
|
||||
httpget.abort();
|
||||
}
|
||||
String responseContent = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
response = httpClient.execute(httpget);
|
||||
// check response status is 200
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
responseContent = EntityUtils.toString(entity, Constants.UTF_8);
|
||||
} else {
|
||||
logger.warn("http entity is null");
|
||||
}
|
||||
} else {
|
||||
logger.error("http get:{} response status code is not 200!", response.getStatusLine().getStatusCode());
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
logger.error(ioe.getMessage(), ioe);
|
||||
} finally {
|
||||
try {
|
||||
if (response != null) {
|
||||
EntityUtils.consume(response.getEntity());
|
||||
response.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
if (!httpget.isAborted()) {
|
||||
httpget.releaseConnection();
|
||||
httpget.abort();
|
||||
}
|
||||
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
|
|
@ -18,14 +17,17 @@
|
|||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
public class IOUtils {
|
||||
|
||||
public static void closeQuietly(Closeable closeable){
|
||||
if(closeable != null){
|
||||
private IOUtils() {
|
||||
throw new UnsupportedOperationException("Construct IOUtils");
|
||||
}
|
||||
|
||||
public static void closeQuietly(Closeable closeable) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException ignore) {
|
||||
|
|
|
|||
|
|
@ -14,46 +14,50 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
/**
|
||||
* http utils
|
||||
*/
|
||||
public class IpUtils {
|
||||
|
||||
public static final String DOT = ".";
|
||||
private IpUtils() {
|
||||
throw new UnsupportedOperationException("Construct IpUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* ip str to long <p>
|
||||
*
|
||||
* @param ipStr ip string
|
||||
* @return ip to long
|
||||
*/
|
||||
public static Long ipToLong(String ipStr) {
|
||||
String[] ipSet = ipStr.split("\\" + DOT);
|
||||
public static final String DOT = ".";
|
||||
|
||||
return Long.parseLong(ipSet[0]) << 24 | Long.parseLong(ipSet[1]) << 16 | Long.parseLong(ipSet[2]) << 8 | Long.parseLong(ipSet[3]);
|
||||
}
|
||||
/**
|
||||
* ip str to long <p>
|
||||
*
|
||||
* @param ipStr ip string
|
||||
* @return ip to long
|
||||
*/
|
||||
public static Long ipToLong(String ipStr) {
|
||||
String[] ipSet = ipStr.split("\\" + DOT);
|
||||
|
||||
/**
|
||||
* long to ip
|
||||
* @param ipLong the long number converted from IP
|
||||
* @return String
|
||||
*/
|
||||
public static String longToIp(long ipLong) {
|
||||
long[] ipNumbers = new long[4];
|
||||
long tmp = 0xFF;
|
||||
ipNumbers[0] = ipLong >> 24 & tmp;
|
||||
ipNumbers[1] = ipLong >> 16 & tmp;
|
||||
ipNumbers[2] = ipLong >> 8 & tmp;
|
||||
ipNumbers[3] = ipLong & tmp;
|
||||
return Long.parseLong(ipSet[0]) << 24 | Long.parseLong(ipSet[1]) << 16 | Long.parseLong(ipSet[2]) << 8 | Long.parseLong(ipSet[3]);
|
||||
}
|
||||
|
||||
String sb = ipNumbers[0] + DOT +
|
||||
ipNumbers[1] + DOT +
|
||||
ipNumbers[2] + DOT +
|
||||
ipNumbers[3];
|
||||
return sb;
|
||||
}
|
||||
/**
|
||||
* long to ip
|
||||
*
|
||||
* @param ipLong the long number converted from IP
|
||||
* @return String
|
||||
*/
|
||||
public static String longToIp(long ipLong) {
|
||||
long[] ipNumbers = new long[4];
|
||||
long tmp = 0xFF;
|
||||
ipNumbers[0] = ipLong >> 24 & tmp;
|
||||
ipNumbers[1] = ipLong >> 16 & tmp;
|
||||
ipNumbers[2] = ipLong >> 8 & tmp;
|
||||
ipNumbers[3] = ipLong & tmp;
|
||||
|
||||
return ipNumbers[0] + DOT
|
||||
+ ipNumbers[1] + DOT
|
||||
+ ipNumbers[2] + DOT
|
||||
+ ipNumbers[3];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,25 +14,39 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import static com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT;
|
||||
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
|
||||
import static com.fasterxml.jackson.databind.DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL;
|
||||
import static com.fasterxml.jackson.databind.MapperFeature.REQUIRE_SETTERS_FOR_GETTERS;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
|
||||
import static com.fasterxml.jackson.databind.MapperFeature.REQUIRE_SETTERS_FOR_GETTERS;
|
||||
|
||||
/**
|
||||
* json utils
|
||||
|
|
@ -49,13 +63,12 @@ public class JSONUtils {
|
|||
.configure(ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)
|
||||
.configure(READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
|
||||
.configure(REQUIRE_SETTERS_FOR_GETTERS, true)
|
||||
.setTimeZone(TimeZone.getDefault())
|
||||
;
|
||||
.setTimeZone(TimeZone.getDefault());
|
||||
|
||||
private JSONUtils() {
|
||||
throw new UnsupportedOperationException("Construct JSONUtils");
|
||||
}
|
||||
|
||||
|
||||
public static ArrayNode createArrayNode() {
|
||||
return objectMapper.createArrayNode();
|
||||
}
|
||||
|
|
@ -94,9 +107,9 @@ public class JSONUtils {
|
|||
* the fields of the specified object are generics, just the object itself should not be a
|
||||
* generic type.
|
||||
*
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param json the string from which the object is to be deserialized
|
||||
* @param clazz the class of T
|
||||
* @param <T> T
|
||||
* @param <T> T
|
||||
* @return an object of type T from the string
|
||||
* classOfT
|
||||
*/
|
||||
|
|
@ -116,9 +129,9 @@ public class JSONUtils {
|
|||
/**
|
||||
* json to list
|
||||
*
|
||||
* @param json json string
|
||||
* @param json json string
|
||||
* @param clazz class
|
||||
* @param <T> T
|
||||
* @param <T> T
|
||||
* @return list
|
||||
*/
|
||||
public static <T> List<T> toList(String json, Class<T> clazz) {
|
||||
|
|
@ -137,7 +150,6 @@ public class JSONUtils {
|
|||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* check json object valid
|
||||
*
|
||||
|
|
@ -160,13 +172,12 @@ public class JSONUtils {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method for finding a JSON Object field with specified name in this
|
||||
* node or its child nodes, and returning value it has.
|
||||
* If no matching field is found in this node or its descendants, returns null.
|
||||
*
|
||||
* @param jsonNode json node
|
||||
* @param jsonNode json node
|
||||
* @param fieldName Name of field to look for
|
||||
* @return Value of first matching node found, if any; null if none
|
||||
*/
|
||||
|
|
@ -180,7 +191,6 @@ public class JSONUtils {
|
|||
return node.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* json to map
|
||||
* <p>
|
||||
|
|
@ -195,7 +205,8 @@ public class JSONUtils {
|
|||
}
|
||||
|
||||
try {
|
||||
return objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});
|
||||
return objectMapper.readValue(json, new TypeReference<Map<String, String>>() {
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.error("json to map exception!", e);
|
||||
}
|
||||
|
|
@ -206,11 +217,11 @@ public class JSONUtils {
|
|||
/**
|
||||
* json to map
|
||||
*
|
||||
* @param json json
|
||||
* @param json json
|
||||
* @param classK classK
|
||||
* @param classV classV
|
||||
* @param <K> K
|
||||
* @param <V> V
|
||||
* @param <K> K
|
||||
* @param <V> V
|
||||
* @return to map
|
||||
*/
|
||||
public static <K, V> Map<K, V> toMap(String json, Class<K> classK, Class<V> classV) {
|
||||
|
|
@ -258,7 +269,6 @@ public class JSONUtils {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* json serializer
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -14,22 +14,28 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* logger utils
|
||||
* logger utils
|
||||
*/
|
||||
public class LoggerUtils {
|
||||
|
||||
private LoggerUtils() {
|
||||
throw new UnsupportedOperationException("Construct LoggerUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* rules for extracting application ID
|
||||
*/
|
||||
|
|
@ -53,29 +59,29 @@ public class LoggerUtils {
|
|||
/**
|
||||
* build job id
|
||||
*
|
||||
* @param affix Task Logger's prefix
|
||||
* @param processDefId process define id
|
||||
* @param affix Task Logger's prefix
|
||||
* @param processDefId process define id
|
||||
* @param processInstId process instance id
|
||||
* @param taskId task id
|
||||
* @param taskId task id
|
||||
* @return task id format
|
||||
*/
|
||||
public static String buildTaskId(String affix,
|
||||
int processDefId,
|
||||
int processInstId,
|
||||
int taskId){
|
||||
int processDefId,
|
||||
int processInstId,
|
||||
int taskId) {
|
||||
// - [taskAppId=TASK_79_4084_15210]
|
||||
return String.format(" - %s%s-%s-%s-%s]",TASK_APPID_LOG_FORMAT,affix,
|
||||
return String.format(" - %s%s-%s-%s-%s]", TASK_APPID_LOG_FORMAT, affix,
|
||||
processDefId,
|
||||
processInstId,
|
||||
taskId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* processing log
|
||||
* get yarn application id list
|
||||
* @param log log content
|
||||
* @param logger logger
|
||||
*
|
||||
* @param log log content
|
||||
* @param logger logger
|
||||
* @return app id list
|
||||
*/
|
||||
public static List<String> getAppIds(String log, Logger logger) {
|
||||
|
|
@ -87,7 +93,7 @@ public class LoggerUtils {
|
|||
// analyse logs to get all submit yarn application id
|
||||
while (matcher.find()) {
|
||||
String appId = matcher.group();
|
||||
if(!appIds.contains(appId)){
|
||||
if (!appIds.contains(appId)) {
|
||||
logger.info("find app id: {}", appId);
|
||||
appIds.add(appId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,27 +14,36 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import static org.apache.dolphinscheduler.common.Constants.DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.apache.dolphinscheduler.common.Constants.DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE;
|
||||
|
||||
/**
|
||||
* NetUtils
|
||||
*/
|
||||
public class NetUtils {
|
||||
|
||||
|
||||
private NetUtils() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
throw new UnsupportedOperationException("Construct NetUtils");
|
||||
}
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(NetUtils.class);
|
||||
|
|
|
|||
|
|
@ -14,8 +14,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
import org.apache.dolphinscheduler.common.shell.ShellExecutor;
|
||||
|
||||
import org.apache.commons.configuration.Configuration;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
|
@ -32,9 +38,6 @@ import java.util.Optional;
|
|||
import java.util.StringTokenizer;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.configuration.Configuration;
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
import org.apache.dolphinscheduler.common.shell.ShellExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -45,431 +48,444 @@ import oshi.hardware.HardwareAbstractionLayer;
|
|||
|
||||
/**
|
||||
* os utils
|
||||
*
|
||||
*/
|
||||
public class OSUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OSUtils.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(OSUtils.class);
|
||||
|
||||
public static final ThreadLocal<Logger> taskLoggerThreadLocal = new ThreadLocal<>();
|
||||
public static final ThreadLocal<Logger> taskLoggerThreadLocal = new ThreadLocal<>();
|
||||
|
||||
private static final SystemInfo SI = new SystemInfo();
|
||||
public static final String TWO_DECIMAL = "0.00";
|
||||
private static final SystemInfo SI = new SystemInfo();
|
||||
public static final String TWO_DECIMAL = "0.00";
|
||||
|
||||
/**
|
||||
* return -1 when the function can not get hardware env info
|
||||
* e.g {@link OSUtils#loadAverage()} {@link OSUtils#cpuUsage()}
|
||||
*/
|
||||
public static final double NEGATIVE_ONE = -1;
|
||||
/**
|
||||
* return -1 when the function can not get hardware env info
|
||||
* e.g {@link OSUtils#loadAverage()} {@link OSUtils#cpuUsage()}
|
||||
*/
|
||||
public static final double NEGATIVE_ONE = -1;
|
||||
|
||||
private static HardwareAbstractionLayer hal = SI.getHardware();
|
||||
private static HardwareAbstractionLayer hal = SI.getHardware();
|
||||
|
||||
private OSUtils() {}
|
||||
|
||||
/**
|
||||
* Initialization regularization, solve the problem of pre-compilation performance,
|
||||
* avoid the thread safety problem of multi-thread operation
|
||||
*/
|
||||
private static final Pattern PATTERN = Pattern.compile("\\s+");
|
||||
|
||||
|
||||
/**
|
||||
* get memory usage
|
||||
* Keep 2 decimal
|
||||
* @return percent %
|
||||
*/
|
||||
public static double memoryUsage() {
|
||||
GlobalMemory memory = hal.getMemory();
|
||||
double memoryUsage = (memory.getTotal() - memory.getAvailable() - memory.getSwapUsed()) * 0.1 / memory.getTotal() * 10;
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(memoryUsage));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get available physical memory size
|
||||
*
|
||||
* Keep 2 decimal
|
||||
* @return available Physical Memory Size, unit: G
|
||||
*/
|
||||
public static double availablePhysicalMemorySize() {
|
||||
GlobalMemory memory = hal.getMemory();
|
||||
double availablePhysicalMemorySize = (memory.getAvailable() + memory.getSwapUsed()) /1024.0/1024/1024;
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(availablePhysicalMemorySize));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* get total physical memory size
|
||||
*
|
||||
* Keep 2 decimal
|
||||
* @return available Physical Memory Size, unit: G
|
||||
*/
|
||||
public static double totalMemorySize() {
|
||||
GlobalMemory memory = hal.getMemory();
|
||||
double availablePhysicalMemorySize = memory.getTotal() /1024.0/1024/1024;
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(availablePhysicalMemorySize));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* load average
|
||||
*
|
||||
* @return load average
|
||||
*/
|
||||
public static double loadAverage() {
|
||||
double loadAverage = hal.getProcessor().getSystemLoadAverage();
|
||||
if (Double.isNaN(loadAverage)) {
|
||||
return NEGATIVE_ONE;
|
||||
private OSUtils() {
|
||||
throw new UnsupportedOperationException("Construct OSUtils");
|
||||
}
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(loadAverage));
|
||||
}
|
||||
/**
|
||||
* Initialization regularization, solve the problem of pre-compilation performance,
|
||||
* avoid the thread safety problem of multi-thread operation
|
||||
*/
|
||||
private static final Pattern PATTERN = Pattern.compile("\\s+");
|
||||
|
||||
/**
|
||||
* get cpu usage
|
||||
*
|
||||
* @return cpu usage
|
||||
*/
|
||||
public static double cpuUsage() {
|
||||
CentralProcessor processor = hal.getProcessor();
|
||||
double cpuUsage = processor.getSystemCpuLoad();
|
||||
if (Double.isNaN(cpuUsage)) {
|
||||
return NEGATIVE_ONE;
|
||||
/**
|
||||
* get memory usage
|
||||
* Keep 2 decimal
|
||||
*
|
||||
* @return percent %
|
||||
*/
|
||||
public static double memoryUsage() {
|
||||
GlobalMemory memory = hal.getMemory();
|
||||
double memoryUsage = (memory.getTotal() - memory.getAvailable() - memory.getSwapUsed()) * 0.1 / memory.getTotal() * 10;
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(memoryUsage));
|
||||
}
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(cpuUsage));
|
||||
}
|
||||
/**
|
||||
* get available physical memory size
|
||||
* <p>
|
||||
* Keep 2 decimal
|
||||
*
|
||||
* @return available Physical Memory Size, unit: G
|
||||
*/
|
||||
public static double availablePhysicalMemorySize() {
|
||||
GlobalMemory memory = hal.getMemory();
|
||||
double availablePhysicalMemorySize = (memory.getAvailable() + memory.getSwapUsed()) / 1024.0 / 1024 / 1024;
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(availablePhysicalMemorySize));
|
||||
|
||||
public static List<String> getUserList() {
|
||||
try {
|
||||
if (isMacOS()) {
|
||||
return getUserListFromMac();
|
||||
} else if (isWindows()) {
|
||||
return getUserListFromWindows();
|
||||
} else {
|
||||
return getUserListFromLinux();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
/**
|
||||
* get total physical memory size
|
||||
* <p>
|
||||
* Keep 2 decimal
|
||||
*
|
||||
* @return available Physical Memory Size, unit: G
|
||||
*/
|
||||
public static double totalMemorySize() {
|
||||
GlobalMemory memory = hal.getMemory();
|
||||
double availablePhysicalMemorySize = memory.getTotal() / 1024.0 / 1024 / 1024;
|
||||
|
||||
/**
|
||||
* get user list from linux
|
||||
*
|
||||
* @return user list
|
||||
*/
|
||||
private static List<String> getUserListFromLinux() throws IOException {
|
||||
List<String> userList = new ArrayList<>();
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(availablePhysicalMemorySize));
|
||||
}
|
||||
|
||||
try (BufferedReader bufferedReader = new BufferedReader(
|
||||
new InputStreamReader(new FileInputStream("/etc/passwd")))) {
|
||||
String line;
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
if (line.contains(":")) {
|
||||
String[] userInfo = line.split(":");
|
||||
userList.add(userInfo[0]);
|
||||
/**
|
||||
* load average
|
||||
*
|
||||
* @return load average
|
||||
*/
|
||||
public static double loadAverage() {
|
||||
double loadAverage = hal.getProcessor().getSystemLoadAverage();
|
||||
if (Double.isNaN(loadAverage)) {
|
||||
return NEGATIVE_ONE;
|
||||
}
|
||||
}
|
||||
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(loadAverage));
|
||||
}
|
||||
|
||||
return userList;
|
||||
}
|
||||
|
||||
/**
|
||||
* get user list from mac
|
||||
* @return user list
|
||||
*/
|
||||
private static List<String> getUserListFromMac() throws IOException {
|
||||
String result = exeCmd("dscl . list /users");
|
||||
if (StringUtils.isNotEmpty(result)) {
|
||||
return Arrays.asList(result.split( "\n"));
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* get user list from windows
|
||||
* @return user list
|
||||
* @throws IOException
|
||||
*/
|
||||
private static List<String> getUserListFromWindows() throws IOException {
|
||||
String result = exeCmd("net user");
|
||||
String[] lines = result.split("\n");
|
||||
|
||||
int startPos = 0;
|
||||
int endPos = lines.length - 2;
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
if (lines[i].isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
if (lines[i].charAt(0) == '-') {
|
||||
for (int j = 0; j < lines[i].length(); j++) {
|
||||
if (lines[i].charAt(i) == '-') {
|
||||
count++;
|
||||
}
|
||||
/**
|
||||
* get cpu usage
|
||||
*
|
||||
* @return cpu usage
|
||||
*/
|
||||
public static double cpuUsage() {
|
||||
CentralProcessor processor = hal.getProcessor();
|
||||
double cpuUsage = processor.getSystemCpuLoad();
|
||||
if (Double.isNaN(cpuUsage)) {
|
||||
return NEGATIVE_ONE;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == lines[i].length()) {
|
||||
startPos = i + 1;
|
||||
break;
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
||||
df.setRoundingMode(RoundingMode.HALF_UP);
|
||||
return Double.parseDouble(df.format(cpuUsage));
|
||||
}
|
||||
|
||||
List<String> users = new ArrayList<>();
|
||||
while (startPos <= endPos) {
|
||||
users.addAll(Arrays.asList(PATTERN.split(lines[startPos])));
|
||||
startPos++;
|
||||
public static List<String> getUserList() {
|
||||
try {
|
||||
if (isMacOS()) {
|
||||
return getUserListFromMac();
|
||||
} else if (isWindows()) {
|
||||
return getUserListFromWindows();
|
||||
} else {
|
||||
return getUserListFromLinux();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
/**
|
||||
* get user list from linux
|
||||
*
|
||||
* @return user list
|
||||
*/
|
||||
private static List<String> getUserListFromLinux() throws IOException {
|
||||
List<String> userList = new ArrayList<>();
|
||||
|
||||
try (BufferedReader bufferedReader = new BufferedReader(
|
||||
new InputStreamReader(new FileInputStream("/etc/passwd")))) {
|
||||
String line;
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
if (line.contains(":")) {
|
||||
String[] userInfo = line.split(":");
|
||||
userList.add(userInfo[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return userList;
|
||||
}
|
||||
|
||||
/**
|
||||
* get user list from mac
|
||||
*
|
||||
* @return user list
|
||||
*/
|
||||
private static List<String> getUserListFromMac() throws IOException {
|
||||
String result = exeCmd("dscl . list /users");
|
||||
if (StringUtils.isNotEmpty(result)) {
|
||||
return Arrays.asList(result.split("\n"));
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* get user list from windows
|
||||
*
|
||||
* @return user list
|
||||
*/
|
||||
private static List<String> getUserListFromWindows() throws IOException {
|
||||
String result = exeCmd("net user");
|
||||
String[] lines = result.split("\n");
|
||||
|
||||
int startPos = 0;
|
||||
int endPos = lines.length - 2;
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
if (lines[i].isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
if (lines[i].charAt(0) == '-') {
|
||||
for (int j = 0; j < lines[i].length(); j++) {
|
||||
if (lines[i].charAt(i) == '-') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count == lines[i].length()) {
|
||||
startPos = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
List<String> users = new ArrayList<>();
|
||||
while (startPos <= endPos) {
|
||||
users.addAll(Arrays.asList(PATTERN.split(lines[startPos])));
|
||||
startPos++;
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
/**
|
||||
* create user
|
||||
*
|
||||
* @param userName user name
|
||||
* @return true if creation was successful, otherwise false
|
||||
*/
|
||||
public static boolean createUser(String userName) {
|
||||
try {
|
||||
String userGroup = OSUtils.getGroup();
|
||||
if (StringUtils.isEmpty(userGroup)) {
|
||||
String errorLog = String.format("%s group does not exist for this operating system.", userGroup);
|
||||
LoggerUtils.logError(Optional.ofNullable(logger), errorLog);
|
||||
LoggerUtils.logError(Optional.ofNullable(taskLoggerThreadLocal.get()), errorLog);
|
||||
return false;
|
||||
}
|
||||
if (isMacOS()) {
|
||||
createMacUser(userName, userGroup);
|
||||
} else if (isWindows()) {
|
||||
createWindowsUser(userName, userGroup);
|
||||
} else {
|
||||
createLinuxUser(userName, userGroup);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LoggerUtils.logError(Optional.ofNullable(logger), e);
|
||||
LoggerUtils.logError(Optional.ofNullable(taskLoggerThreadLocal.get()), e);
|
||||
}
|
||||
|
||||
/**
|
||||
* create user
|
||||
* @param userName user name
|
||||
* @return true if creation was successful, otherwise false
|
||||
*/
|
||||
public static boolean createUser(String userName) {
|
||||
try {
|
||||
String userGroup = OSUtils.getGroup();
|
||||
if (StringUtils.isEmpty(userGroup)) {
|
||||
String errorLog = String.format("%s group does not exist for this operating system.", userGroup);
|
||||
LoggerUtils.logError(Optional.ofNullable(logger), errorLog);
|
||||
LoggerUtils.logError(Optional.ofNullable(taskLoggerThreadLocal.get()), errorLog);
|
||||
return false;
|
||||
}
|
||||
if (isMacOS()) {
|
||||
createMacUser(userName, userGroup);
|
||||
} else if (isWindows()) {
|
||||
createWindowsUser(userName, userGroup);
|
||||
} else {
|
||||
createLinuxUser(userName, userGroup);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LoggerUtils.logError(Optional.ofNullable(logger), e);
|
||||
LoggerUtils.logError(Optional.ofNullable(taskLoggerThreadLocal.get()), e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* create linux user
|
||||
*
|
||||
* @param userName user name
|
||||
* @param userGroup user group
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
private static void createLinuxUser(String userName, String userGroup) throws IOException {
|
||||
String infoLog1 = String.format("create linux os user : %s", userName);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog1);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog1);
|
||||
|
||||
/**
|
||||
* create linux user
|
||||
* @param userName user name
|
||||
* @param userGroup user group
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
private static void createLinuxUser(String userName, String userGroup) throws IOException {
|
||||
String infoLog1 = String.format("create linux os user : %s", userName);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog1);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog1);
|
||||
|
||||
String cmd = String.format("sudo useradd -g %s %s", userGroup, userName);
|
||||
String infoLog2 = String.format("execute cmd : %s", cmd);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog2);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog2);
|
||||
OSUtils.exeCmd(cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* create mac user (Supports Mac OSX 10.10+)
|
||||
* @param userName user name
|
||||
* @param userGroup user group
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
private static void createMacUser(String userName, String userGroup) throws IOException {
|
||||
|
||||
Optional<Logger> optionalLogger = Optional.ofNullable(logger);
|
||||
Optional<Logger> optionalTaskLogger = Optional.ofNullable(taskLoggerThreadLocal.get());
|
||||
|
||||
String infoLog1 = String.format("create mac os user : %s", userName);
|
||||
LoggerUtils.logInfo(optionalLogger, infoLog1);
|
||||
LoggerUtils.logInfo(optionalTaskLogger, infoLog1);
|
||||
|
||||
String createUserCmd = String.format("sudo sysadminctl -addUser %s -password %s", userName, userName);
|
||||
String infoLog2 = String.format("create user command : %s", createUserCmd);
|
||||
LoggerUtils.logInfo(optionalLogger, infoLog2);
|
||||
LoggerUtils.logInfo(optionalTaskLogger, infoLog2);
|
||||
OSUtils.exeCmd(createUserCmd);
|
||||
|
||||
String appendGroupCmd = String.format("sudo dseditgroup -o edit -a %s -t user %s", userName, userGroup);
|
||||
String infoLog3 = String.format("append user to group : %s", appendGroupCmd);
|
||||
LoggerUtils.logInfo(optionalLogger, infoLog3);
|
||||
LoggerUtils.logInfo(optionalTaskLogger, infoLog3);
|
||||
OSUtils.exeCmd(appendGroupCmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* create windows user
|
||||
* @param userName user name
|
||||
* @param userGroup user group
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
private static void createWindowsUser(String userName, String userGroup) throws IOException {
|
||||
String infoLog1 = String.format("create windows os user : %s", userName);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog1);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog1);
|
||||
|
||||
String userCreateCmd = String.format("net user \"%s\" /add", userName);
|
||||
String infoLog2 = String.format("execute create user command : %s", userCreateCmd);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog2);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog2);
|
||||
OSUtils.exeCmd(userCreateCmd);
|
||||
|
||||
String appendGroupCmd = String.format("net localgroup \"%s\" \"%s\" /add", userGroup, userName);
|
||||
String infoLog3 = String.format("execute append user to group : %s", appendGroupCmd);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog3);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog3);
|
||||
OSUtils.exeCmd(appendGroupCmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get system group information
|
||||
* @return system group info
|
||||
* @throws IOException errors
|
||||
*/
|
||||
public static String getGroup() throws IOException {
|
||||
if (isWindows()) {
|
||||
String currentProcUserName = System.getProperty("user.name");
|
||||
String result = exeCmd(String.format("net user \"%s\"", currentProcUserName));
|
||||
String line = result.split("\n")[22];
|
||||
String group = PATTERN.split(line)[1];
|
||||
if (group.charAt(0) == '*') {
|
||||
return group.substring(1);
|
||||
} else {
|
||||
return group;
|
||||
}
|
||||
} else {
|
||||
String result = exeCmd("groups");
|
||||
if (StringUtils.isNotEmpty(result)) {
|
||||
String[] groupInfo = result.split(" ");
|
||||
return groupInfo[0];
|
||||
}
|
||||
String cmd = String.format("sudo useradd -g %s %s", userGroup, userName);
|
||||
String infoLog2 = String.format("execute cmd : %s", cmd);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog2);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog2);
|
||||
OSUtils.exeCmd(cmd);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* create mac user (Supports Mac OSX 10.10+)
|
||||
*
|
||||
* @param userName user name
|
||||
* @param userGroup user group
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
private static void createMacUser(String userName, String userGroup) throws IOException {
|
||||
|
||||
/**
|
||||
* Execute the corresponding command of Linux or Windows
|
||||
*
|
||||
* @param command command
|
||||
* @return result of execute command
|
||||
* @throws IOException errors
|
||||
*/
|
||||
public static String exeCmd(String command) throws IOException {
|
||||
StringTokenizer st = new StringTokenizer(command);
|
||||
String[] cmdArray = new String[st.countTokens()];
|
||||
for (int i = 0; st.hasMoreTokens(); i++) {
|
||||
cmdArray[i] = st.nextToken();
|
||||
Optional<Logger> optionalLogger = Optional.ofNullable(logger);
|
||||
Optional<Logger> optionalTaskLogger = Optional.ofNullable(taskLoggerThreadLocal.get());
|
||||
|
||||
String infoLog1 = String.format("create mac os user : %s", userName);
|
||||
LoggerUtils.logInfo(optionalLogger, infoLog1);
|
||||
LoggerUtils.logInfo(optionalTaskLogger, infoLog1);
|
||||
|
||||
String createUserCmd = String.format("sudo sysadminctl -addUser %s -password %s", userName, userName);
|
||||
String infoLog2 = String.format("create user command : %s", createUserCmd);
|
||||
LoggerUtils.logInfo(optionalLogger, infoLog2);
|
||||
LoggerUtils.logInfo(optionalTaskLogger, infoLog2);
|
||||
OSUtils.exeCmd(createUserCmd);
|
||||
|
||||
String appendGroupCmd = String.format("sudo dseditgroup -o edit -a %s -t user %s", userName, userGroup);
|
||||
String infoLog3 = String.format("append user to group : %s", appendGroupCmd);
|
||||
LoggerUtils.logInfo(optionalLogger, infoLog3);
|
||||
LoggerUtils.logInfo(optionalTaskLogger, infoLog3);
|
||||
OSUtils.exeCmd(appendGroupCmd);
|
||||
}
|
||||
return exeShell(cmdArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the shell
|
||||
* @param command command
|
||||
* @return result of execute the shell
|
||||
* @throws IOException errors
|
||||
*/
|
||||
public static String exeShell(String[] command) throws IOException {
|
||||
return ShellExecutor.execCommand(command);
|
||||
}
|
||||
/**
|
||||
* create windows user
|
||||
*
|
||||
* @param userName user name
|
||||
* @param userGroup user group
|
||||
* @throws IOException in case of an I/O error
|
||||
*/
|
||||
private static void createWindowsUser(String userName, String userGroup) throws IOException {
|
||||
String infoLog1 = String.format("create windows os user : %s", userName);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog1);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog1);
|
||||
|
||||
/**
|
||||
* get process id
|
||||
* @return process id
|
||||
*/
|
||||
public static int getProcessID() {
|
||||
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
|
||||
return Integer.parseInt(runtimeMXBean.getName().split("@")[0]);
|
||||
}
|
||||
String userCreateCmd = String.format("net user \"%s\" /add", userName);
|
||||
String infoLog2 = String.format("execute create user command : %s", userCreateCmd);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog2);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog2);
|
||||
OSUtils.exeCmd(userCreateCmd);
|
||||
|
||||
/**
|
||||
* whether is macOS
|
||||
* @return true if mac
|
||||
*/
|
||||
public static boolean isMacOS() {
|
||||
return getOSName().startsWith("Mac");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* whether is windows
|
||||
* @return true if windows
|
||||
*/
|
||||
public static boolean isWindows() {
|
||||
return getOSName().startsWith("Windows");
|
||||
}
|
||||
|
||||
/**
|
||||
* get current OS name
|
||||
* @return current OS name
|
||||
*/
|
||||
public static String getOSName() {
|
||||
return System.getProperty("os.name");
|
||||
}
|
||||
|
||||
/**
|
||||
* check memory and cpu usage
|
||||
* @param systemCpuLoad systemCpuLoad
|
||||
* @param systemReservedMemory systemReservedMemory
|
||||
* @return check memory and cpu usage
|
||||
*/
|
||||
public static Boolean checkResource(double systemCpuLoad, double systemReservedMemory){
|
||||
// system load average
|
||||
double loadAverage = OSUtils.loadAverage();
|
||||
// system available physical memory
|
||||
double availablePhysicalMemorySize = OSUtils.availablePhysicalMemorySize();
|
||||
|
||||
if(loadAverage > systemCpuLoad || availablePhysicalMemorySize < systemReservedMemory){
|
||||
logger.warn("load is too high or availablePhysicalMemorySize(G) is too low, it's availablePhysicalMemorySize(G):{},loadAvg:{}", availablePhysicalMemorySize , loadAverage);
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
String appendGroupCmd = String.format("net localgroup \"%s\" \"%s\" /add", userGroup, userName);
|
||||
String infoLog3 = String.format("execute append user to group : %s", appendGroupCmd);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog3);
|
||||
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog3);
|
||||
OSUtils.exeCmd(appendGroupCmd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check memory and cpu usage
|
||||
* @param conf conf
|
||||
* @param isMaster is master
|
||||
* @return check memory and cpu usage
|
||||
*/
|
||||
public static Boolean checkResource(Configuration conf, Boolean isMaster){
|
||||
double systemCpuLoad;
|
||||
double systemReservedMemory;
|
||||
/**
|
||||
* get system group information
|
||||
*
|
||||
* @return system group info
|
||||
* @throws IOException errors
|
||||
*/
|
||||
public static String getGroup() throws IOException {
|
||||
if (isWindows()) {
|
||||
String currentProcUserName = System.getProperty("user.name");
|
||||
String result = exeCmd(String.format("net user \"%s\"", currentProcUserName));
|
||||
String line = result.split("\n")[22];
|
||||
String group = PATTERN.split(line)[1];
|
||||
if (group.charAt(0) == '*') {
|
||||
return group.substring(1);
|
||||
} else {
|
||||
return group;
|
||||
}
|
||||
} else {
|
||||
String result = exeCmd("groups");
|
||||
if (StringUtils.isNotEmpty(result)) {
|
||||
String[] groupInfo = result.split(" ");
|
||||
return groupInfo[0];
|
||||
}
|
||||
}
|
||||
|
||||
if(Boolean.TRUE.equals(isMaster)){
|
||||
systemCpuLoad = conf.getDouble(Constants.MASTER_MAX_CPULOAD_AVG, Constants.DEFAULT_MASTER_CPU_LOAD);
|
||||
systemReservedMemory = conf.getDouble(Constants.MASTER_RESERVED_MEMORY, Constants.DEFAULT_MASTER_RESERVED_MEMORY);
|
||||
}else{
|
||||
systemCpuLoad = conf.getDouble(Constants.WORKER_MAX_CPULOAD_AVG, Constants.DEFAULT_WORKER_CPU_LOAD);
|
||||
systemReservedMemory = conf.getDouble(Constants.WORKER_RESERVED_MEMORY, Constants.DEFAULT_WORKER_RESERVED_MEMORY);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the corresponding command of Linux or Windows
|
||||
*
|
||||
* @param command command
|
||||
* @return result of execute command
|
||||
* @throws IOException errors
|
||||
*/
|
||||
public static String exeCmd(String command) throws IOException {
|
||||
StringTokenizer st = new StringTokenizer(command);
|
||||
String[] cmdArray = new String[st.countTokens()];
|
||||
for (int i = 0; st.hasMoreTokens(); i++) {
|
||||
cmdArray[i] = st.nextToken();
|
||||
}
|
||||
return exeShell(cmdArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the shell
|
||||
*
|
||||
* @param command command
|
||||
* @return result of execute the shell
|
||||
* @throws IOException errors
|
||||
*/
|
||||
public static String exeShell(String[] command) throws IOException {
|
||||
return ShellExecutor.execCommand(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* get process id
|
||||
*
|
||||
* @return process id
|
||||
*/
|
||||
public static int getProcessID() {
|
||||
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
|
||||
return Integer.parseInt(runtimeMXBean.getName().split("@")[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* whether is macOS
|
||||
*
|
||||
* @return true if mac
|
||||
*/
|
||||
public static boolean isMacOS() {
|
||||
return getOSName().startsWith("Mac");
|
||||
}
|
||||
|
||||
/**
|
||||
* whether is windows
|
||||
*
|
||||
* @return true if windows
|
||||
*/
|
||||
public static boolean isWindows() {
|
||||
return getOSName().startsWith("Windows");
|
||||
}
|
||||
|
||||
/**
|
||||
* get current OS name
|
||||
*
|
||||
* @return current OS name
|
||||
*/
|
||||
public static String getOSName() {
|
||||
return System.getProperty("os.name");
|
||||
}
|
||||
|
||||
/**
|
||||
* check memory and cpu usage
|
||||
*
|
||||
* @param systemCpuLoad systemCpuLoad
|
||||
* @param systemReservedMemory systemReservedMemory
|
||||
* @return check memory and cpu usage
|
||||
*/
|
||||
public static Boolean checkResource(double systemCpuLoad, double systemReservedMemory) {
|
||||
// system load average
|
||||
double loadAverage = OSUtils.loadAverage();
|
||||
// system available physical memory
|
||||
double availablePhysicalMemorySize = OSUtils.availablePhysicalMemorySize();
|
||||
|
||||
if (loadAverage > systemCpuLoad || availablePhysicalMemorySize < systemReservedMemory) {
|
||||
logger.warn("load is too high or availablePhysicalMemorySize(G) is too low, it's availablePhysicalMemorySize(G):{},loadAvg:{}", availablePhysicalMemorySize, loadAverage);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check memory and cpu usage
|
||||
*
|
||||
* @param conf conf
|
||||
* @param isMaster is master
|
||||
* @return check memory and cpu usage
|
||||
*/
|
||||
public static Boolean checkResource(Configuration conf, Boolean isMaster) {
|
||||
double systemCpuLoad;
|
||||
double systemReservedMemory;
|
||||
|
||||
if (Boolean.TRUE.equals(isMaster)) {
|
||||
systemCpuLoad = conf.getDouble(Constants.MASTER_MAX_CPULOAD_AVG, Constants.DEFAULT_MASTER_CPU_LOAD);
|
||||
systemReservedMemory = conf.getDouble(Constants.MASTER_RESERVED_MEMORY, Constants.DEFAULT_MASTER_RESERVED_MEMORY);
|
||||
} else {
|
||||
systemCpuLoad = conf.getDouble(Constants.WORKER_MAX_CPULOAD_AVG, Constants.DEFAULT_WORKER_CPU_LOAD);
|
||||
systemReservedMemory = conf.getDouble(Constants.WORKER_RESERVED_MEMORY, Constants.DEFAULT_WORKER_RESERVED_MEMORY);
|
||||
}
|
||||
return checkResource(systemCpuLoad, systemReservedMemory);
|
||||
}
|
||||
return checkResource(systemCpuLoad,systemReservedMemory);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,231 +24,228 @@ 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
|
||||
*/
|
||||
public class ParameterUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class);
|
||||
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 ParameterUtils() {
|
||||
throw new UnsupportedOperationException("Construct ParameterUtils");
|
||||
}
|
||||
|
||||
//Get current time, schedule execute time
|
||||
String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME);
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
Date cronTime = null;
|
||||
//Get current time, schedule execute time
|
||||
String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME);
|
||||
|
||||
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();
|
||||
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) {
|
||||
parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true);
|
||||
}
|
||||
|
||||
return parameterString;
|
||||
}
|
||||
|
||||
// replace variable ${} form,refers to the replacement of system variables and custom variables
|
||||
parameterString = PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true);
|
||||
/**
|
||||
* 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;
|
||||
|
||||
// replace time $[...] form, eg. $[yyyyMMdd]
|
||||
if (cronTime != null) {
|
||||
parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true);
|
||||
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) {
|
||||
parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true);
|
||||
|
||||
}
|
||||
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();
|
||||
/**
|
||||
* 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 variable ${} form,refers to the replacement of system variables and custom variables
|
||||
parameterString = PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, 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) {
|
||||
|
||||
// replace time $[...] form, eg. $[yyyyMMdd]
|
||||
if (cronTime != null) {
|
||||
parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true);
|
||||
if (globalParamList == null || globalParamList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
return parameterString;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
allParamMap.putAll(globalMap);
|
||||
|
||||
/**
|
||||
* 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){
|
||||
Set<Map.Entry<String, String>> entries = allParamMap.entrySet();
|
||||
|
||||
if (globalParamList == null || globalParamList.isEmpty()) {
|
||||
return null;
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
/**
|
||||
* handle escapes
|
||||
*
|
||||
* @param inputString input string
|
||||
* @return string filter escapes
|
||||
*/
|
||||
public static String handleEscapes(String inputString) {
|
||||
|
||||
if (timeParams != null) {
|
||||
allParamMap.putAll(timeParams);
|
||||
if (StringUtils.isNotEmpty(inputString)) {
|
||||
return inputString.replace("%", "////%").replaceAll("[\n|\r\t]", "_");
|
||||
}
|
||||
return inputString;
|
||||
}
|
||||
|
||||
allParamMap.putAll(globalMap);
|
||||
/**
|
||||
* $[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();
|
||||
}
|
||||
|
||||
Set<Map.Entry<String, String>> entries = allParamMap.entrySet();
|
||||
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));
|
||||
|
||||
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);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
globalMap.putAll(resolveMap);
|
||||
|
||||
for (Property property : globalParamList){
|
||||
String val = globalMap.get(property.getProp());
|
||||
if (val != null){
|
||||
property.setValue(val);
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
return JSONUtils.toJsonString(globalParamList);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* $[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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,16 +14,17 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
|
||||
/**
|
||||
* utility methods for validating input
|
||||
*
|
||||
* utility methods for validating input
|
||||
*/
|
||||
public final class Preconditions {
|
||||
|
||||
private Preconditions() {}
|
||||
private Preconditions() {
|
||||
throw new UnsupportedOperationException("Construct Preconditions");
|
||||
}
|
||||
|
||||
/**
|
||||
* if obj is null will throw NPE
|
||||
|
|
@ -41,32 +42,30 @@ public final class Preconditions {
|
|||
|
||||
/**
|
||||
* if obj is null will throw NullPointerException with error message
|
||||
*
|
||||
* @param obj obj
|
||||
* @param errorMsg error message
|
||||
* @param <T> T
|
||||
* @return T
|
||||
*/
|
||||
public static <T> T checkNotNull(T obj, String errorMsg) {
|
||||
public static <T> T checkNotNull(T obj, String errorMsg) {
|
||||
if (obj == null) {
|
||||
throw new NullPointerException(errorMsg);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* if condition is false will throw an IllegalArgumentException with the given message
|
||||
*
|
||||
* @param condition condition
|
||||
* @param errorMsg error message
|
||||
*
|
||||
* @param errorMsg error message
|
||||
* @throws IllegalArgumentException Thrown, if the condition is violated.
|
||||
*/
|
||||
public static void checkArgument(boolean condition, Object errorMsg) {
|
||||
public static void checkArgument(boolean condition, Object errorMsg) {
|
||||
if (!condition) {
|
||||
throw new IllegalArgumentException(String.valueOf(errorMsg));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,13 +14,15 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import static org.apache.dolphinscheduler.common.Constants.COMMON_PROPERTIES_PATH;
|
||||
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
import org.apache.dolphinscheduler.common.enums.ResUploadType;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
|
@ -28,7 +30,8 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.apache.dolphinscheduler.common.Constants.COMMON_PROPERTIES_PATH;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* property utils
|
||||
|
|
@ -44,7 +47,7 @@ public class PropertyUtils {
|
|||
private static final Properties properties = new Properties();
|
||||
|
||||
private PropertyUtils() {
|
||||
throw new IllegalStateException("PropertyUtils class");
|
||||
throw new UnsupportedOperationException("Construct PropertyUtils");
|
||||
}
|
||||
|
||||
static {
|
||||
|
|
@ -68,10 +71,9 @@ public class PropertyUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return judge whether resource upload startup
|
||||
* @return judge whether resource upload startup
|
||||
*/
|
||||
public static Boolean getResUploadStartupState(){
|
||||
public static Boolean getResUploadStartupState() {
|
||||
String resUploadStartupType = PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE);
|
||||
ResUploadType resUploadType = ResUploadType.valueOf(resUploadStartupType);
|
||||
return resUploadType == ResUploadType.HDFS || resUploadType == ResUploadType.S3;
|
||||
|
|
@ -113,14 +115,13 @@ 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) {
|
||||
return getInt(key, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue default value
|
||||
* @return property value
|
||||
|
|
@ -134,7 +135,7 @@ public class PropertyUtils {
|
|||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.info(e.getMessage(),e);
|
||||
logger.info(e.getMessage(), e);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
|
@ -147,7 +148,7 @@ public class PropertyUtils {
|
|||
*/
|
||||
public static boolean getBoolean(String key) {
|
||||
String value = properties.getProperty(key.trim());
|
||||
if(null != value){
|
||||
if (null != value) {
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
|
@ -163,7 +164,7 @@ public class PropertyUtils {
|
|||
*/
|
||||
public static Boolean getBoolean(String key, boolean defaultValue) {
|
||||
String value = properties.getProperty(key.trim());
|
||||
if(null != value){
|
||||
if (null != value) {
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
|
|
@ -172,6 +173,7 @@ public class PropertyUtils {
|
|||
|
||||
/**
|
||||
* get property long value
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultVal default value
|
||||
* @return property value
|
||||
|
|
@ -182,16 +184,14 @@ public class PropertyUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key key
|
||||
* @return property value
|
||||
*/
|
||||
public static long getLong(String key) {
|
||||
return getLong(key,-1);
|
||||
return getLong(key, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultVal default value
|
||||
* @return property value
|
||||
|
|
@ -201,11 +201,11 @@ public class PropertyUtils {
|
|||
return val == null ? defaultVal : Double.parseDouble(val);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get array
|
||||
* @param key property name
|
||||
* @param splitStr separator
|
||||
* get array
|
||||
*
|
||||
* @param key property name
|
||||
* @param splitStr separator
|
||||
* @return property value through array
|
||||
*/
|
||||
public static String[] getArray(String key, String splitStr) {
|
||||
|
|
@ -217,18 +217,17 @@ public class PropertyUtils {
|
|||
String[] propertyArray = value.split(splitStr);
|
||||
return propertyArray;
|
||||
} catch (NumberFormatException e) {
|
||||
logger.info(e.getMessage(),e);
|
||||
logger.info(e.getMessage(), e);
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key key
|
||||
* @param type type
|
||||
* @param defaultValue default value
|
||||
* @param <T> T
|
||||
* @return get enum value
|
||||
* @return get enum value
|
||||
*/
|
||||
public <T extends Enum<T>> T getEnum(String key, Class<T> type,
|
||||
T defaultValue) {
|
||||
|
|
@ -238,6 +237,7 @@ public class PropertyUtils {
|
|||
|
||||
/**
|
||||
* get all properties with specified prefix, like: fs.
|
||||
*
|
||||
* @param prefix prefix to search
|
||||
* @return all properties with specified prefix
|
||||
*/
|
||||
|
|
@ -253,11 +253,9 @@ public class PropertyUtils {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public static void setValue(String key, String value) {
|
||||
properties.setProperty(key,value);
|
||||
properties.setProperty(key, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,15 +17,21 @@
|
|||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import com.github.rholder.retry.*;
|
||||
import org.apache.dolphinscheduler.common.Constants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.github.rholder.retry.RetryException;
|
||||
import com.github.rholder.retry.Retryer;
|
||||
import com.github.rholder.retry.RetryerBuilder;
|
||||
import com.github.rholder.retry.StopStrategies;
|
||||
import com.github.rholder.retry.WaitStrategies;
|
||||
|
||||
/**
|
||||
* The Retryer util.
|
||||
*/
|
||||
|
|
@ -35,7 +41,7 @@ public class RetryerUtils {
|
|||
private static Retryer<Boolean> defaultRetryerResultNoCheck;
|
||||
|
||||
private RetryerUtils() {
|
||||
|
||||
throw new UnsupportedOperationException("Construct RetryerUtils");
|
||||
}
|
||||
|
||||
private static Retryer<Boolean> getDefaultRetryerResultNoCheck() {
|
||||
|
|
|
|||
|
|
@ -14,10 +14,8 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
|
@ -29,113 +27,123 @@ import java.util.List;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Metadata related common classes
|
||||
*
|
||||
*/
|
||||
public class SchemaUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SchemaUtils.class);
|
||||
private static Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
||||
private static final Logger logger = LoggerFactory.getLogger(SchemaUtils.class);
|
||||
private static Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
||||
|
||||
/**
|
||||
* Gets upgradable schemas for all upgrade directories
|
||||
* @return all schema list
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getAllSchemaList() {
|
||||
List<String> schemaDirList = new ArrayList<>();
|
||||
File[] schemaDirArr = FileUtils.getAllDir("sql/upgrade");
|
||||
if(schemaDirArr == null || schemaDirArr.length == 0) {
|
||||
return null;
|
||||
}
|
||||
private SchemaUtils() {
|
||||
throw new UnsupportedOperationException("Construct SchemaUtils");
|
||||
}
|
||||
|
||||
for(File file : schemaDirArr) {
|
||||
schemaDirList.add(file.getName());
|
||||
}
|
||||
/**
|
||||
* Gets upgradable schemas for all upgrade directories
|
||||
*
|
||||
* @return all schema list
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getAllSchemaList() {
|
||||
List<String> schemaDirList = new ArrayList<>();
|
||||
File[] schemaDirArr = FileUtils.getAllDir("sql/upgrade");
|
||||
if (schemaDirArr == null || schemaDirArr.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Collections.sort(schemaDirList , new Comparator() {
|
||||
@Override
|
||||
public int compare(Object o1 , Object o2){
|
||||
try {
|
||||
String dir1 = String.valueOf(o1);
|
||||
String dir2 = String.valueOf(o2);
|
||||
String version1 = dir1.split("_")[0];
|
||||
String version2 = dir2.split("_")[0];
|
||||
if(version1.equals(version2)) {
|
||||
return 0;
|
||||
}
|
||||
for (File file : schemaDirArr) {
|
||||
schemaDirList.add(file.getName());
|
||||
}
|
||||
|
||||
if(SchemaUtils.isAGreatVersion(version1, version2)) {
|
||||
return 1;
|
||||
}
|
||||
Collections.sort(schemaDirList, new Comparator() {
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
try {
|
||||
String dir1 = String.valueOf(o1);
|
||||
String dir2 = String.valueOf(o2);
|
||||
String version1 = dir1.split("_")[0];
|
||||
String version2 = dir2.split("_")[0];
|
||||
if (version1.equals(version2)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
if (SchemaUtils.isAGreatVersion(version1, version2)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
return -1;
|
||||
|
||||
return schemaDirList;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Determine whether schemaVersion is higher than version
|
||||
* @param schemaVersion schema version
|
||||
* @param version version
|
||||
* @return Determine whether schemaVersion is higher than version
|
||||
*/
|
||||
public static boolean isAGreatVersion(String schemaVersion, String version) {
|
||||
if(StringUtils.isEmpty(schemaVersion) || StringUtils.isEmpty(version)) {
|
||||
throw new RuntimeException("schemaVersion or version is empty");
|
||||
}
|
||||
return schemaDirList;
|
||||
}
|
||||
|
||||
String[] schemaVersionArr = schemaVersion.split("\\.");
|
||||
String[] versionArr = version.split("\\.");
|
||||
int arrLength = Math.min(schemaVersionArr.length, versionArr.length);
|
||||
for(int i = 0 ; i < arrLength ; i++) {
|
||||
if(Integer.parseInt(schemaVersionArr[i]) > Integer.parseInt(versionArr[i])) {
|
||||
return true;
|
||||
}else if(Integer.parseInt(schemaVersionArr[i]) < Integer.parseInt(versionArr[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Determine whether schemaVersion is higher than version
|
||||
*
|
||||
* @param schemaVersion schema version
|
||||
* @param version version
|
||||
* @return Determine whether schemaVersion is higher than version
|
||||
*/
|
||||
public static boolean isAGreatVersion(String schemaVersion, String version) {
|
||||
if (StringUtils.isEmpty(schemaVersion) || StringUtils.isEmpty(version)) {
|
||||
throw new RuntimeException("schemaVersion or version is empty");
|
||||
}
|
||||
|
||||
// If the version and schema version is the same from 0 up to the arrlength-1 element,whoever has a larger arrLength has a larger version number
|
||||
return schemaVersionArr.length > versionArr.length;
|
||||
}
|
||||
String[] schemaVersionArr = schemaVersion.split("\\.");
|
||||
String[] versionArr = version.split("\\.");
|
||||
int arrLength = Math.min(schemaVersionArr.length, versionArr.length);
|
||||
for (int i = 0; i < arrLength; i++) {
|
||||
if (Integer.parseInt(schemaVersionArr[i]) > Integer.parseInt(versionArr[i])) {
|
||||
return true;
|
||||
} else if (Integer.parseInt(schemaVersionArr[i]) < Integer.parseInt(versionArr[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current software version number of the system
|
||||
* @return current software version
|
||||
*/
|
||||
public static String getSoftVersion() {
|
||||
String soft_version;
|
||||
try {
|
||||
soft_version = FileUtils.readFile2Str(new FileInputStream(new File("sql/soft_version")));
|
||||
soft_version = replaceBlank(soft_version);
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
throw new RuntimeException("Failed to get the product version description file. The file could not be found", e);
|
||||
}
|
||||
return soft_version;
|
||||
}
|
||||
// If the version and schema version is the same from 0 up to the arrlength-1 element,whoever has a larger arrLength has a larger version number
|
||||
return schemaVersionArr.length > versionArr.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips the string of space carriage returns and tabs
|
||||
* @param str string
|
||||
* @return string removed blank
|
||||
*/
|
||||
public static String replaceBlank(String str) {
|
||||
String dest = "";
|
||||
if (str!=null) {
|
||||
/**
|
||||
* Gets the current software version number of the system
|
||||
*
|
||||
* @return current software version
|
||||
*/
|
||||
public static String getSoftVersion() {
|
||||
String softVersion;
|
||||
try {
|
||||
softVersion = FileUtils.readFile2Str(new FileInputStream(new File("sql/soft_version")));
|
||||
softVersion = replaceBlank(softVersion);
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
throw new RuntimeException("Failed to get the product version description file. The file could not be found", e);
|
||||
}
|
||||
return softVersion;
|
||||
}
|
||||
|
||||
Matcher m = p.matcher(str);
|
||||
dest = m.replaceAll("");
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
/**
|
||||
* Strips the string of space carriage returns and tabs
|
||||
*
|
||||
* @param str string
|
||||
* @return string removed blank
|
||||
*/
|
||||
public static String replaceBlank(String str) {
|
||||
String dest = "";
|
||||
if (str != null) {
|
||||
|
||||
Matcher m = p.matcher(str);
|
||||
dest = m.replaceAll("");
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,21 +14,25 @@
|
|||
* 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.dolphinscheduler.common.Constants;
|
||||
|
||||
/**
|
||||
* sensitive log Util
|
||||
* sensitive log Util
|
||||
*/
|
||||
public class SensitiveLogUtils {
|
||||
|
||||
private SensitiveLogUtils() {
|
||||
throw new UnsupportedOperationException("Construct SensitiveLogUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dataSourcePwd data source password
|
||||
* @return String
|
||||
*/
|
||||
public static String maskDataSourcePwd(String dataSourcePwd){
|
||||
public static String maskDataSourcePwd(String dataSourcePwd) {
|
||||
|
||||
if (StringUtils.isNotEmpty(dataSourcePwd)) {
|
||||
dataSourcePwd = Constants.PASSWORD_DEFAULT;
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
|
@ -22,7 +23,9 @@ import java.util.stream.StreamSupport;
|
|||
|
||||
public class StreamUtils {
|
||||
|
||||
private StreamUtils() { }
|
||||
private StreamUtils() {
|
||||
throw new UnsupportedOperationException("Construct StreamUtils");
|
||||
}
|
||||
|
||||
public static <T> Stream<T> asStream(Iterator<T> sourceIterator) {
|
||||
return asStream(sourceIterator, false);
|
||||
|
|
|
|||
|
|
@ -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,18 +33,18 @@ 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){
|
||||
public static String replaceNRTtoUnderline(String src) {
|
||||
return src.replaceAll("[\n|\r|\t]", "_");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,13 +14,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dolphinscheduler.common.utils;
|
||||
|
||||
import org.apache.dolphinscheduler.common.enums.TaskType;
|
||||
import org.apache.dolphinscheduler.common.task.AbstractParameters;
|
||||
import org.apache.dolphinscheduler.common.task.conditions.ConditionsParameters;
|
||||
import org.apache.dolphinscheduler.common.task.dependent.DependentParameters;
|
||||
import org.apache.dolphinscheduler.common.task.datax.DataxParameters;
|
||||
import org.apache.dolphinscheduler.common.task.dependent.DependentParameters;
|
||||
import org.apache.dolphinscheduler.common.task.flink.FlinkParameters;
|
||||
import org.apache.dolphinscheduler.common.task.http.HttpParameters;
|
||||
import org.apache.dolphinscheduler.common.task.mr.MapreduceParameters;
|
||||
|
|
@ -31,60 +32,65 @@ import org.apache.dolphinscheduler.common.task.spark.SparkParameters;
|
|||
import org.apache.dolphinscheduler.common.task.sql.SqlParameters;
|
||||
import org.apache.dolphinscheduler.common.task.sqoop.SqoopParameters;
|
||||
import org.apache.dolphinscheduler.common.task.subprocess.SubProcessParameters;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* task parameters utils
|
||||
*/
|
||||
public class TaskParametersUtils {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(TaskParametersUtils.class);
|
||||
private static Logger logger = LoggerFactory.getLogger(TaskParametersUtils.class);
|
||||
|
||||
/**
|
||||
* get task parameters
|
||||
* @param taskType task type
|
||||
* @param parameter parameter
|
||||
* @return task parameters
|
||||
*/
|
||||
public static AbstractParameters getParameters(String taskType, String parameter) {
|
||||
try {
|
||||
switch (EnumUtils.getEnum(TaskType.class,taskType)) {
|
||||
case SUB_PROCESS:
|
||||
return JSONUtils.parseObject(parameter, SubProcessParameters.class);
|
||||
case WATERDROP:
|
||||
return JSONUtils.parseObject(parameter, ShellParameters.class);
|
||||
case SHELL:
|
||||
return JSONUtils.parseObject(parameter, ShellParameters.class);
|
||||
case PROCEDURE:
|
||||
return JSONUtils.parseObject(parameter, ProcedureParameters.class);
|
||||
case SQL:
|
||||
return JSONUtils.parseObject(parameter, SqlParameters.class);
|
||||
case MR:
|
||||
return JSONUtils.parseObject(parameter, MapreduceParameters.class);
|
||||
case SPARK:
|
||||
return JSONUtils.parseObject(parameter, SparkParameters.class);
|
||||
case PYTHON:
|
||||
return JSONUtils.parseObject(parameter, PythonParameters.class);
|
||||
case DEPENDENT:
|
||||
return JSONUtils.parseObject(parameter, DependentParameters.class);
|
||||
case FLINK:
|
||||
return JSONUtils.parseObject(parameter, FlinkParameters.class);
|
||||
case HTTP:
|
||||
return JSONUtils.parseObject(parameter, HttpParameters.class);
|
||||
case DATAX:
|
||||
return JSONUtils.parseObject(parameter, DataxParameters.class);
|
||||
case CONDITIONS:
|
||||
return JSONUtils.parseObject(parameter, ConditionsParameters.class);
|
||||
case SQOOP:
|
||||
return JSONUtils.parseObject(parameter, SqoopParameters.class);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
private TaskParametersUtils() {
|
||||
throw new UnsupportedOperationException("Construct TaskParametersUtils");
|
||||
}
|
||||
|
||||
/**
|
||||
* get task parameters
|
||||
*
|
||||
* @param taskType task type
|
||||
* @param parameter parameter
|
||||
* @return task parameters
|
||||
*/
|
||||
public static AbstractParameters getParameters(String taskType, String parameter) {
|
||||
try {
|
||||
switch (EnumUtils.getEnum(TaskType.class, taskType)) {
|
||||
case SUB_PROCESS:
|
||||
return JSONUtils.parseObject(parameter, SubProcessParameters.class);
|
||||
case WATERDROP:
|
||||
return JSONUtils.parseObject(parameter, ShellParameters.class);
|
||||
case SHELL:
|
||||
return JSONUtils.parseObject(parameter, ShellParameters.class);
|
||||
case PROCEDURE:
|
||||
return JSONUtils.parseObject(parameter, ProcedureParameters.class);
|
||||
case SQL:
|
||||
return JSONUtils.parseObject(parameter, SqlParameters.class);
|
||||
case MR:
|
||||
return JSONUtils.parseObject(parameter, MapreduceParameters.class);
|
||||
case SPARK:
|
||||
return JSONUtils.parseObject(parameter, SparkParameters.class);
|
||||
case PYTHON:
|
||||
return JSONUtils.parseObject(parameter, PythonParameters.class);
|
||||
case DEPENDENT:
|
||||
return JSONUtils.parseObject(parameter, DependentParameters.class);
|
||||
case FLINK:
|
||||
return JSONUtils.parseObject(parameter, FlinkParameters.class);
|
||||
case HTTP:
|
||||
return JSONUtils.parseObject(parameter, HttpParameters.class);
|
||||
case DATAX:
|
||||
return JSONUtils.parseObject(parameter, DataxParameters.class);
|
||||
case CONDITIONS:
|
||||
return JSONUtils.parseObject(parameter, ConditionsParameters.class);
|
||||
case SQOOP:
|
||||
return JSONUtils.parseObject(parameter, SqoopParameters.class);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue