Search in sources :

Example 1 with FileItemValue

use of com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue in project disconf by knightliao.

the class DisconfStoreFileProcessorImpl method inject2Instance.

/**
 */
@Override
public void inject2Instance(Object object, String fileName) {
    DisconfCenterFile disconfCenterFile = getInstance().getConfFileMap().get(fileName);
    // 校验是否存在
    if (disconfCenterFile == null) {
        LOGGER.error("cannot find " + fileName + " in store....");
        return;
    }
    // 
    if (object != null) {
        // 设置object
        disconfCenterFile.setObject(object);
    }
    // 根据类型设置值
    // 
    // 注入实体
    // 
    Map<String, FileItemValue> keMap = disconfCenterFile.getKeyMaps();
    for (String fileItem : keMap.keySet()) {
        // 根据类型设置值
        try {
            // 
            if (object == null) {
                if (keMap.get(fileItem).isStatic()) {
                    LOGGER.debug(fileItem + " is a static field. ");
                    keMap.get(fileItem).setValue4StaticFileItem(keMap.get(fileItem).getValue());
                }
            // 
            // 非静态类
            // 
            } else {
                LOGGER.debug(fileItem + " is a non-static field. ");
                if (keMap.get(fileItem).getValue() == null) {
                    // 如果仓库值为空,则实例 直接使用默认值
                    Object defaultValue = keMap.get(fileItem).getFieldDefaultValue(object);
                    keMap.get(fileItem).setValue(defaultValue);
                } else {
                    // 如果仓库里的值为非空,则实例使用仓库里的值
                    keMap.get(fileItem).setValue4FileItem(object, keMap.get(fileItem).getValue());
                }
            }
        } catch (Exception e) {
            LOGGER.error("inject2Instance fileName " + fileName + " " + e.toString(), e);
        }
    }
}
Also used : FileItemValue(com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue) DisconfCenterFile(com.baidu.disconf.client.common.model.DisconfCenterFile)

Example 2 with FileItemValue

use of com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue 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)

Example 3 with FileItemValue

use of com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue in project disconf by knightliao.

the class DisconfStoreFileProcessorImpl method inject2Store.

/**
 */
@Override
public void inject2Store(String fileName, DisconfValue disconfValue) {
    DisconfCenterFile disconfCenterFile = getInstance().getConfFileMap().get(fileName);
    // 校验是否存在
    if (disconfCenterFile == null) {
        LOGGER.error("cannot find " + fileName + " in store....");
        return;
    }
    if (disconfValue == null || disconfValue.getFileData() == null) {
        LOGGER.error("value is null for {}", fileName);
        return;
    }
    // 存储
    Map<String, FileItemValue> keMap = disconfCenterFile.getKeyMaps();
    if (keMap.size() > 0) {
        for (String fileItem : keMap.keySet()) {
            Object object = disconfValue.getFileData().get(fileItem);
            if (object == null) {
                LOGGER.error("cannot find {} to be injected. file content is: {}", fileItem, disconfValue.getFileData().toString());
                continue;
            }
            // 根据类型设置值
            try {
                Object value = keMap.get(fileItem).getFieldValueByType(object);
                keMap.get(fileItem).setValue(value);
            } catch (Exception e) {
                LOGGER.error("inject2Store filename: " + fileName + " " + e.toString(), e);
            }
        }
    }
    // 使用过 XML式配置
    if (disconfCenterFile.isTaggedWithNonAnnotationFile()) {
        if (disconfCenterFile.getSupportFileTypeEnum().equals(SupportFileTypeEnum.PROPERTIES)) {
            // 如果是采用XML进行配置的,则需要利用spring的reload将数据reload到bean里
            ReloadConfigurationMonitor.reload();
        }
        disconfCenterFile.setAdditionalKeyMaps(disconfValue.getFileData());
    }
}
Also used : FileItemValue(com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue) DisconfCenterFile(com.baidu.disconf.client.common.model.DisconfCenterFile)

Aggregations

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