Search in sources :

Example 1 with DisConfCommonModel

use of com.baidu.disconf.client.common.model.DisConfCommonModel 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 2 with DisConfCommonModel

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

the class StaticScannerItemMgrImpl method transformScanItem.

/**
     * 转换配置项
     */
private static DisconfCenterItem transformScanItem(Method method) {
    DisconfCenterItem disconfCenterItem = new DisconfCenterItem();
    // class
    Class<?> cls = method.getDeclaringClass();
    // fields
    Field[] expectedFields = cls.getDeclaredFields();
    // field
    Field field = MethodUtils.getFieldFromMethod(method, expectedFields, DisConfigTypeEnum.ITEM);
    if (field == null) {
        return null;
    }
    // 获取标注
    DisconfItem disconfItem = method.getAnnotation(DisconfItem.class);
    // 去掉空格
    String key = disconfItem.key().replace(" ", "");
    // get setter method
    Method setterMethod = MethodUtils.getSetterMethodFromField(cls, field);
    disconfCenterItem.setSetMethod(setterMethod);
    // field
    disconfCenterItem.setField(field);
    // key
    disconfCenterItem.setKey(key);
    // access
    field.setAccessible(true);
    // object
    disconfCenterItem.setObject(null);
    // value
    if (Modifier.isStatic(field.getModifiers())) {
        try {
            disconfCenterItem.setValue(field.get(null));
        } catch (Exception e) {
            LOGGER.error(e.toString());
            disconfCenterItem.setValue(null);
        }
    } else {
        disconfCenterItem.setValue(null);
    }
    //
    // disConfCommonModel
    DisConfCommonModel disConfCommonModel = makeDisConfCommonModel(disconfItem.app(), disconfItem.env(), disconfItem.version());
    disconfCenterItem.setDisConfCommonModel(disConfCommonModel);
    // Disconf-web url
    String url = DisconfWebPathMgr.getRemoteUrlParameter(DisClientSysConfig.getInstance().CONF_SERVER_STORE_ACTION, disConfCommonModel.getApp(), disConfCommonModel.getVersion(), disConfCommonModel.getEnv(), key, DisConfigTypeEnum.ITEM);
    disconfCenterItem.setRemoteServerUrl(url);
    return disconfCenterItem;
}
Also used : Field(java.lang.reflect.Field) DisConfCommonModel(com.baidu.disconf.client.common.model.DisConfCommonModel) DisconfCenterItem(com.baidu.disconf.client.common.model.DisconfCenterItem) Method(java.lang.reflect.Method) DisconfItem(com.baidu.disconf.client.common.annotations.DisconfItem)

Example 3 with DisConfCommonModel

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

the class DisconfItemCoreProcessorImpl method updateOneConfItem.

/**
     * 更新一个配置
     */
private void updateOneConfItem(String keyName, DisconfCenterItem disconfCenterItem) throws Exception {
    if (disconfCenterItem == null) {
        throw new Exception("cannot find disconfCenterItem " + keyName);
    }
    String value = null;
    //
    if (DisClientConfig.getInstance().ENABLE_DISCONF) {
        //
        try {
            String url = disconfCenterItem.getRemoteServerUrl();
            value = fetcherMgr.getValueFromServer(url);
            if (value != null) {
                LOGGER.debug("value: " + value);
            }
        } catch (Exception e) {
            LOGGER.error("cannot use remote configuration: " + keyName, e);
            LOGGER.info("using local variable: " + keyName);
        }
        LOGGER.debug("download ok.");
    }
    //
    // 注入到仓库中
    //
    disconfStoreProcessor.inject2Store(keyName, new DisconfValue(value, null));
    LOGGER.debug("inject ok.");
    //
    if (DisClientConfig.getInstance().ENABLE_DISCONF) {
        if (watchMgr != null) {
            DisConfCommonModel disConfCommonModel = disconfStoreProcessor.getCommonModel(keyName);
            watchMgr.watchPath(this, disConfCommonModel, keyName, DisConfigTypeEnum.ITEM, value);
            LOGGER.debug("watch ok.");
        } else {
            LOGGER.warn("cannot monitor {} because watch mgr is null", keyName);
        }
    }
}
Also used : DisConfCommonModel(com.baidu.disconf.client.common.model.DisConfCommonModel) DisconfValue(com.baidu.disconf.client.store.processor.model.DisconfValue)

Example 4 with DisConfCommonModel

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

the class StaticScannerNonAnnotationFileMgrImpl method getDisconfCenterFile.

/**
     *
     */
public static DisconfCenterBaseModel getDisconfCenterFile(String fileName) {
    DisconfCenterFile disconfCenterFile = new DisconfCenterFile();
    fileName = fileName.trim();
    //
    // file name
    disconfCenterFile.setFileName(fileName);
    // 非注解式
    disconfCenterFile.setIsTaggedWithNonAnnotationFile(true);
    // file type
    disconfCenterFile.setSupportFileTypeEnum(SupportFileTypeEnum.getByFileName(fileName));
    //
    // disConfCommonModel
    DisConfCommonModel disConfCommonModel = makeDisConfCommonModel("", "", "");
    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);
    return disconfCenterFile;
}
Also used : DisConfCommonModel(com.baidu.disconf.client.common.model.DisConfCommonModel) DisconfCenterFile(com.baidu.disconf.client.common.model.DisconfCenterFile)

Example 5 with DisConfCommonModel

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

the class DisconfFileCoreProcessorImpl method updateOneConfFile.

/**
     * 更新 一個配置文件, 下载、注入到仓库、Watch 三步骤
     */
private void updateOneConfFile(String fileName, DisconfCenterFile disconfCenterFile) throws Exception {
    if (disconfCenterFile == null) {
        throw new Exception("cannot find disconfCenterFile " + fileName);
    }
    String filePath = fileName;
    Map<String, Object> dataMap = new HashMap<String, Object>();
    //
    if (DisClientConfig.getInstance().ENABLE_DISCONF) {
        //
        try {
            String url = disconfCenterFile.getRemoteServerUrl();
            filePath = fetcherMgr.downloadFileFromServer(url, fileName, disconfCenterFile.getFileDir());
        } catch (Exception e) {
            //
            // 下载失败了, 尝试使用本地的配置
            //
            LOGGER.error(e.toString(), e);
            LOGGER.warn("using local properties in class path: " + fileName);
            // change file path
            filePath = fileName;
        }
        LOGGER.debug("download ok.");
    }
    try {
        dataMap = FileTypeProcessorUtils.getKvMap(disconfCenterFile.getSupportFileTypeEnum(), disconfCenterFile.getFilePath());
    } catch (Exception e) {
        LOGGER.error("cannot get kv data for " + filePath, e);
    }
    //
    // 注入到仓库中
    //
    disconfStoreProcessor.inject2Store(fileName, new DisconfValue(null, dataMap));
    LOGGER.debug("inject ok.");
    //
    if (DisClientConfig.getInstance().ENABLE_DISCONF) {
        //
        // Watch
        //
        DisConfCommonModel disConfCommonModel = disconfStoreProcessor.getCommonModel(fileName);
        if (watchMgr != null) {
            watchMgr.watchPath(this, disConfCommonModel, fileName, DisConfigTypeEnum.FILE, GsonUtils.toJson(disconfCenterFile.getKV()));
            LOGGER.debug("watch ok.");
        } else {
            LOGGER.warn("cannot monitor {} because watch mgr is null", fileName);
        }
    }
}
Also used : DisConfCommonModel(com.baidu.disconf.client.common.model.DisConfCommonModel) HashMap(java.util.HashMap) DisconfValue(com.baidu.disconf.client.store.processor.model.DisconfValue)

Aggregations

DisConfCommonModel (com.baidu.disconf.client.common.model.DisConfCommonModel)5 DisconfCenterFile (com.baidu.disconf.client.common.model.DisconfCenterFile)2 DisconfValue (com.baidu.disconf.client.store.processor.model.DisconfValue)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 DisconfFile (com.baidu.disconf.client.common.annotations.DisconfFile)1 DisconfFileItem (com.baidu.disconf.client.common.annotations.DisconfFileItem)1 DisconfItem (com.baidu.disconf.client.common.annotations.DisconfItem)1 FileItemValue (com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue)1 DisconfCenterItem (com.baidu.disconf.client.common.model.DisconfCenterItem)1