Search in sources :

Example 1 with DisconfFileItem

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);
    }
}
Also used : DisInnerConfigAnnotation(com.baidu.disconf.client.config.inner.DisInnerConfigAnnotation) Field(java.lang.reflect.Field) DisconfFileItem(com.baidu.disconf.client.common.annotations.DisconfFileItem)

Example 2 with DisconfFileItem

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;
}
Also used : Field(java.lang.reflect.Field) DisconfFileItem(com.baidu.disconf.client.common.annotations.DisconfFileItem) DisconfItem(com.baidu.disconf.client.common.annotations.DisconfItem)

Example 3 with DisconfFileItem

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;
}
Also used : Field(java.lang.reflect.Field) DisConfCommonModel(com.baidu.disconf.client.common.model.DisConfCommonModel) FileItemValue(com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue) HashMap(java.util.HashMap) DisconfFile(com.baidu.disconf.client.common.annotations.DisconfFile) DisconfFileItem(com.baidu.disconf.client.common.annotations.DisconfFileItem) DisconfCenterFile(com.baidu.disconf.client.common.model.DisconfCenterFile) Method(java.lang.reflect.Method)

Aggregations

DisconfFileItem (com.baidu.disconf.client.common.annotations.DisconfFileItem)3 Field (java.lang.reflect.Field)3 DisconfFile (com.baidu.disconf.client.common.annotations.DisconfFile)1 DisconfItem (com.baidu.disconf.client.common.annotations.DisconfItem)1 DisConfCommonModel (com.baidu.disconf.client.common.model.DisConfCommonModel)1 DisconfCenterFile (com.baidu.disconf.client.common.model.DisconfCenterFile)1 FileItemValue (com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue)1 DisInnerConfigAnnotation (com.baidu.disconf.client.config.inner.DisInnerConfigAnnotation)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1