use of com.baidu.disconf.client.common.annotations.DisconfFileItem in project disconf by knightliao.
the class DisconfAutowareConfig method autowareConfig.
/**
* 自动导入配置数据,能识别 DisconfFileItem 或 DisInnerConfigAnnotation 的标识
*
* @Description: auto ware
*/
private static void autowareConfig(final Object obj, Properties prop) throws Exception {
if (null == prop || obj == null) {
throw new Exception("cannot autowareConfig null");
}
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(DisconfFileItem.class) || field.isAnnotationPresent(DisInnerConfigAnnotation.class)) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
String name;
String value;
if (field.isAnnotationPresent(DisconfFileItem.class)) {
name = field.getName();
value = prop.getProperty(name, null);
} else {
// disconf使用的配置
DisInnerConfigAnnotation config = field.getAnnotation(DisInnerConfigAnnotation.class);
name = config.name();
String defaultValue = config.defaultValue();
value = prop.getProperty(name, defaultValue);
// using disconf as prefix to avoid env confusion
if (value.equals(defaultValue) && name != null) {
if (name.contains("disconf.")) {
String newName = name.substring(name.indexOf('.') + 1);
value = prop.getProperty(newName, defaultValue);
}
}
}
field.setAccessible(true);
if (null != value) {
try {
ClassUtils.setFieldValeByType(field, obj, value);
} catch (Exception e) {
LOGGER.error(String.format("invalid config: %s", name), e);
}
}
}
}
} catch (Exception e) {
throw new Exception("error while autowire config file", e);
}
}
use of com.baidu.disconf.client.common.annotations.DisconfFileItem in project disconf by knightliao.
the class MethodUtils method getFieldFromMethod.
/**
* 对于一个 get/is 方法,返回其相对应的Field
*/
public static Field getFieldFromMethod(Method method, Field[] expectedFields, DisConfigTypeEnum disConfigTypeEnum) {
String fieldName;
if (disConfigTypeEnum.equals(DisConfigTypeEnum.FILE)) {
DisconfFileItem disconfFileItem = method.getAnnotation(DisconfFileItem.class);
// 根据用户设定的注解来获取
fieldName = disconfFileItem.associateField();
} else {
DisconfItem disItem = method.getAnnotation(DisconfItem.class);
// 根据用户设定的注解来获取
fieldName = disItem.associateField();
}
//
if (StringUtils.isEmpty(fieldName)) {
// 从方法名 获取其 Field 名
fieldName = ClassUtils.getFieldNameByGetMethodName(method.getName());
}
// 确认此Field名是正确的
for (Field field : expectedFields) {
if (field.getName().equals(fieldName)) {
return field;
}
}
LOGGER.error(method.toString() + " cannot get its related field name. ");
return null;
}
use of com.baidu.disconf.client.common.annotations.DisconfFileItem in project disconf by knightliao.
the class StaticScannerFileMgrImpl method transformScanFile.
/**
* 转换配置文件
*/
private static DisconfCenterFile transformScanFile(Class<?> disconfFileClass, Set<Method> methods) {
DisconfCenterFile disconfCenterFile = new DisconfCenterFile();
//
// class
disconfCenterFile.setCls(disconfFileClass);
DisconfFile disconfFileAnnotation = disconfFileClass.getAnnotation(DisconfFile.class);
//
// file name
disconfCenterFile.setFileName(disconfFileAnnotation.filename());
// config file target dir path
disconfCenterFile.setTargetDirPath(disconfFileAnnotation.targetDirPath().trim());
// file type
disconfCenterFile.setSupportFileTypeEnum(SupportFileTypeEnum.getByFileName(disconfFileAnnotation.filename()));
//
// disConfCommonModel
DisConfCommonModel disConfCommonModel = makeDisConfCommonModel(disconfFileAnnotation.app(), disconfFileAnnotation.env(), disconfFileAnnotation.version());
disconfCenterFile.setDisConfCommonModel(disConfCommonModel);
// Remote URL
String url = DisconfWebPathMgr.getRemoteUrlParameter(DisClientSysConfig.getInstance().CONF_SERVER_STORE_ACTION, disConfCommonModel.getApp(), disConfCommonModel.getVersion(), disConfCommonModel.getEnv(), disconfCenterFile.getFileName(), DisConfigTypeEnum.FILE);
disconfCenterFile.setRemoteServerUrl(url);
// fields
Field[] expectedFields = disconfFileClass.getDeclaredFields();
//
// KEY & VALUE
//
Map<String, FileItemValue> keyMaps = new HashMap<String, FileItemValue>();
for (Method method : methods) {
// 获取指定的域
Field field = MethodUtils.getFieldFromMethod(method, expectedFields, DisConfigTypeEnum.FILE);
if (field == null) {
continue;
}
//
DisconfFileItem disconfFileItem = method.getAnnotation(DisconfFileItem.class);
String keyName = disconfFileItem.name();
// access
field.setAccessible(true);
// get setter method
Method setterMethod = MethodUtils.getSetterMethodFromField(disconfFileClass, field);
// static 则直接获取其值
if (Modifier.isStatic(field.getModifiers())) {
try {
FileItemValue fileItemValue = new FileItemValue(field.get(null), field, setterMethod);
keyMaps.put(keyName, fileItemValue);
} catch (Exception e) {
LOGGER.error(e.toString());
}
} else {
// 非static则为Null, 这里我们没有必要获取其Bean的值
FileItemValue fileItemValue = new FileItemValue(null, field, setterMethod);
keyMaps.put(keyName, fileItemValue);
}
}
// 设置
disconfCenterFile.setKeyMaps(keyMaps);
return disconfCenterFile;
}
Aggregations