|
|
@ -22,7 +22,7 @@ import static org.apache.dolphinscheduler.common.Constants.COMMON_PROPERTIES_PAT |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.spi.enums.ResUploadType; |
|
|
|
import org.apache.dolphinscheduler.spi.enums.ResUploadType; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.directory.api.util.Strings; |
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
@ -35,6 +35,7 @@ import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
public class PropertyUtils { |
|
|
|
public class PropertyUtils { |
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class); |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class); |
|
|
|
|
|
|
|
|
|
|
|
private static final Properties properties = new Properties(); |
|
|
|
private static final Properties properties = new Properties(); |
|
|
@ -92,7 +93,8 @@ public class PropertyUtils { |
|
|
|
* @return property value with upper case |
|
|
|
* @return property value with upper case |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String getUpperCaseString(String key) { |
|
|
|
public static String getUpperCaseString(String key) { |
|
|
|
return properties.getProperty(key.trim()).toUpperCase(); |
|
|
|
String val = getString(key); |
|
|
|
|
|
|
|
return StringUtils.isEmpty(val) ? val : val.toUpperCase(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -103,8 +105,8 @@ public class PropertyUtils { |
|
|
|
* @return property value |
|
|
|
* @return property value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String getString(String key, String defaultVal) { |
|
|
|
public static String getString(String key, String defaultVal) { |
|
|
|
String val = properties.getProperty(key.trim()); |
|
|
|
String val = getString(key); |
|
|
|
return val == null ? defaultVal : val; |
|
|
|
return StringUtils.isEmpty(val) ? defaultVal : val; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -124,7 +126,7 @@ public class PropertyUtils { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static int getInt(String key, int defaultValue) { |
|
|
|
public static int getInt(String key, int defaultValue) { |
|
|
|
String value = getString(key); |
|
|
|
String value = getString(key); |
|
|
|
if (value == null) { |
|
|
|
if (StringUtils.isEmpty(value)) { |
|
|
|
return defaultValue; |
|
|
|
return defaultValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -143,12 +145,7 @@ public class PropertyUtils { |
|
|
|
* @return property value |
|
|
|
* @return property value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean getBoolean(String key) { |
|
|
|
public static boolean getBoolean(String key) { |
|
|
|
String value = properties.getProperty(key.trim()); |
|
|
|
return getBoolean(key, false); |
|
|
|
if (null != value) { |
|
|
|
|
|
|
|
return Boolean.parseBoolean(value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -159,24 +156,93 @@ public class PropertyUtils { |
|
|
|
* @return property value |
|
|
|
* @return property value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static Boolean getBoolean(String key, boolean defaultValue) { |
|
|
|
public static Boolean getBoolean(String key, boolean defaultValue) { |
|
|
|
String value = properties.getProperty(key.trim()); |
|
|
|
String value = getString(key); |
|
|
|
if (null != value) { |
|
|
|
return StringUtils.isEmpty(value) ? defaultValue : Boolean.parseBoolean(value); |
|
|
|
return Boolean.parseBoolean(value); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* get property long value |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param key key |
|
|
|
|
|
|
|
* @param defaultValue default value |
|
|
|
|
|
|
|
* @return property value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static long getLong(String key, long defaultValue) { |
|
|
|
|
|
|
|
String value = getString(key); |
|
|
|
|
|
|
|
if (StringUtils.isEmpty(value)) { |
|
|
|
|
|
|
|
return defaultValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return Long.parseLong(value); |
|
|
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
|
|
logger.info(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
return defaultValue; |
|
|
|
return defaultValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* get property long value |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param key key |
|
|
|
* @param key key |
|
|
|
* @param defaultVal default value |
|
|
|
|
|
|
|
* @return property value |
|
|
|
* @return property value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static long getLong(String key, long defaultVal) { |
|
|
|
public static long getLong(String key) { |
|
|
|
String val = getString(key); |
|
|
|
return getLong(key, -1); |
|
|
|
return val == null ? defaultVal : Long.parseLong(val); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param key key |
|
|
|
|
|
|
|
* @param defaultValue default value |
|
|
|
|
|
|
|
* @return property value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static double getDouble(String key, double defaultValue) { |
|
|
|
|
|
|
|
String value = getString(key); |
|
|
|
|
|
|
|
if (StringUtils.isEmpty(value)) { |
|
|
|
|
|
|
|
return defaultValue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return Double.parseDouble(value); |
|
|
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
|
|
logger.info(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return defaultValue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* get array |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param key property name |
|
|
|
|
|
|
|
* @param splitStr separator |
|
|
|
|
|
|
|
* @return property value through array |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static String[] getArray(String key, String splitStr) { |
|
|
|
|
|
|
|
String value = getString(key); |
|
|
|
|
|
|
|
if (StringUtils.isEmpty(value)) { |
|
|
|
|
|
|
|
return new String[0]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return value.split(splitStr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param key key |
|
|
|
|
|
|
|
* @param type type |
|
|
|
|
|
|
|
* @param defaultValue default value |
|
|
|
|
|
|
|
* @param <T> T |
|
|
|
|
|
|
|
* @return get enum value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static <T extends Enum<T>> T getEnum(String key, Class<T> type, |
|
|
|
|
|
|
|
T defaultValue) { |
|
|
|
|
|
|
|
String value = getString(key); |
|
|
|
|
|
|
|
if (StringUtils.isEmpty(value)) { |
|
|
|
|
|
|
|
return defaultValue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return Enum.valueOf(type, value); |
|
|
|
|
|
|
|
} catch (IllegalArgumentException e) { |
|
|
|
|
|
|
|
logger.info(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return defaultValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -195,12 +261,17 @@ public class PropertyUtils { |
|
|
|
return matchedProperties; |
|
|
|
return matchedProperties; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* set value |
|
|
|
|
|
|
|
* @param key key |
|
|
|
|
|
|
|
* @param value value |
|
|
|
|
|
|
|
*/ |
|
|
|
public static void setValue(String key, String value) { |
|
|
|
public static void setValue(String key, String value) { |
|
|
|
properties.setProperty(key, value); |
|
|
|
properties.setProperty(key, value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Map<String, String> getPropertiesByPrefix(String prefix) { |
|
|
|
public static Map<String, String> getPropertiesByPrefix(String prefix) { |
|
|
|
if (Strings.isEmpty(prefix)) { |
|
|
|
if (StringUtils.isEmpty(prefix)) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
Set<Object> keys = properties.keySet(); |
|
|
|
Set<Object> keys = properties.keySet(); |
|
|
|