Multi service registry center
This commit is contained in:
@@ -27,6 +27,7 @@ public final class Consts {
|
||||
}
|
||||
|
||||
public static final class S {
|
||||
public static final String DEFAULT = "default";
|
||||
public static final String TRUE = "true";
|
||||
public static final String FALSE = "false";
|
||||
public static final String TRUE1 = "1";
|
||||
|
||||
34
fizz-common/src/main/java/we/util/FileUtils.java
Normal file
34
fizz-common/src/main/java/we/util/FileUtils.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package we.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public abstract class FileUtils {
|
||||
|
||||
private FileUtils() {
|
||||
}
|
||||
|
||||
public static String getAppRootDir() {
|
||||
return new File(Consts.S.EMPTY).getAbsolutePath();
|
||||
}
|
||||
|
||||
public static String getAbsoluteDir(Class<?> cls) {
|
||||
return new File(Objects.requireNonNull(cls.getResource(Consts.S.EMPTY)).getPath()).getAbsolutePath();
|
||||
}
|
||||
|
||||
public static String getAbsolutePath(Class<?> cls) {
|
||||
String absoluteDir = getAbsoluteDir(cls);
|
||||
return absoluteDir + File.separatorChar + cls.getSimpleName() + ".class";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param file relative to src or resource dir, eg: we/util/FileUtils.class and application.yml
|
||||
*/
|
||||
public static String getAbsolutePath(String file) {
|
||||
return new File(Objects.requireNonNull(FileUtils.class.getClassLoader().getResource(file)).getPath()).getAbsolutePath();
|
||||
}
|
||||
}
|
||||
70
fizz-common/src/main/java/we/util/PropertiesUtils.java
Normal file
70
fizz-common/src/main/java/we/util/PropertiesUtils.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package we.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.PropertyAccessor;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public abstract class PropertiesUtils {
|
||||
|
||||
private PropertiesUtils() {
|
||||
}
|
||||
|
||||
public static String normalize(String propertyName) {
|
||||
char[] chars = propertyName.toCharArray();
|
||||
StringBuilder b = new StringBuilder(chars.length);
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char c = chars[i];
|
||||
if (c == Consts.S.DASH) {
|
||||
b.append(Character.toUpperCase(chars[++i]));
|
||||
} else {
|
||||
b.append(c);
|
||||
}
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public static Properties remove(Properties properties, String prefix) {
|
||||
Properties result = new Properties();
|
||||
properties.forEach(
|
||||
(k, v) -> {
|
||||
String s = k.toString();
|
||||
int idx = s.indexOf(prefix);
|
||||
if (idx > -1) {
|
||||
s = s.substring(prefix.length() + 1);
|
||||
}
|
||||
result.setProperty(s, v.toString());
|
||||
}
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void setBeanPropertyValue(Object bean, Properties properties) {
|
||||
setBeanPropertyValue(bean, properties, null);
|
||||
}
|
||||
|
||||
public static void setBeanPropertyValue(Object bean, Properties properties, Map<String, Class<?>> propertyTypeHint) {
|
||||
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(bean);
|
||||
for (String propertyName : properties.stringPropertyNames()) {
|
||||
if (beanWrapper.isWritableProperty(propertyName)) {
|
||||
beanWrapper.setPropertyValue(propertyName, properties.get(propertyName));
|
||||
} else if (propertyTypeHint != null) {
|
||||
int dotPos = propertyName.lastIndexOf(Consts.S.DOT);
|
||||
if (dotPos > -1) {
|
||||
String prefix = propertyName.substring(0, dotPos);
|
||||
Class<?> aClass = propertyTypeHint.get(prefix);
|
||||
if (aClass != null && Map.class.isAssignableFrom(aClass)) {
|
||||
String newPropertyName = prefix + PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR + propertyName.substring(dotPos + 1) + PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR;
|
||||
beanWrapper.setPropertyValue(newPropertyName, properties.get(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
fizz-common/src/main/java/we/util/YmlUtils.java
Normal file
34
fizz-common/src/main/java/we/util/YmlUtils.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package we.util;
|
||||
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public abstract class YmlUtils {
|
||||
|
||||
private YmlUtils() {
|
||||
}
|
||||
|
||||
public static Properties file2properties(String file) {
|
||||
Resource resource = new ClassPathResource(file);
|
||||
return getProperties(resource);
|
||||
}
|
||||
|
||||
public static Properties string2properties(String config) {
|
||||
Resource resource = new ByteArrayResource(config.getBytes());
|
||||
return getProperties(resource);
|
||||
}
|
||||
|
||||
private static Properties getProperties(Resource resource) {
|
||||
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
|
||||
yamlPropertiesFactoryBean.setResources(resource);
|
||||
return yamlPropertiesFactoryBean.getObject();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user