Search in sources :

Example 1 with DisconfFile

use of com.baidu.disconf.client.common.annotations.DisconfFile in project disconf by knightliao.

the class DisconfAspectJ method decideAccess.

/**
 * 获取配置文件数据, 只有开启disconf远程才会进行切面
 *
 * @throws Throwable
 */
@Around("anyPublicMethod() && @annotation(disconfFileItem)")
public Object decideAccess(ProceedingJoinPoint pjp, DisconfFileItem disconfFileItem) throws Throwable {
    if (DisClientConfig.getInstance().ENABLE_DISCONF) {
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        Method method = ms.getMethod();
        // 
        // 文件名
        // 
        Class<?> cls = method.getDeclaringClass();
        DisconfFile disconfFile = cls.getAnnotation(DisconfFile.class);
        // 
        // Field名
        // 
        Field field = MethodUtils.getFieldFromMethod(method, cls.getDeclaredFields(), DisConfigTypeEnum.FILE);
        if (field != null) {
            // 
            // 请求仓库配置数据
            // 
            DisconfStoreProcessor disconfStoreProcessor = DisconfStoreProcessorFactory.getDisconfStoreFileProcessor();
            Object ret = disconfStoreProcessor.getConfig(disconfFile.filename(), disconfFileItem.name());
            if (ret != null) {
                LOGGER.debug("using disconf store value: " + disconfFile.filename() + " (" + disconfFileItem.name() + " , " + ret + ")");
                return ret;
            }
        }
    }
    Object rtnOb;
    try {
        // 返回原值
        rtnOb = pjp.proceed();
    } catch (Throwable t) {
        LOGGER.info(t.getMessage());
        throw t;
    }
    return rtnOb;
}
Also used : Field(java.lang.reflect.Field) MethodSignature(org.aspectj.lang.reflect.MethodSignature) DisconfFile(com.baidu.disconf.client.common.annotations.DisconfFile) Method(java.lang.reflect.Method) DisconfStoreProcessor(com.baidu.disconf.client.store.DisconfStoreProcessor) Around(org.aspectj.lang.annotation.Around)

Example 2 with DisconfFile

use of com.baidu.disconf.client.common.annotations.DisconfFile 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 DisconfFile

use of com.baidu.disconf.client.common.annotations.DisconfFile in project disconf by knightliao.

the class ScanDynamicStoreAdapter method processFiles.

/**
 * 获取回调对应的配置文件列表
 */
private static void processFiles(Map<DisconfKey, List<IDisconfUpdate>> inverseMap, DisconfUpdateService disconfUpdateService, IDisconfUpdate iDisconfUpdate) {
    // 反索引
    List<Class<?>> classes = Arrays.asList(disconfUpdateService.classes());
    for (Class<?> curClass : classes) {
        // 获取其注解
        DisconfFile disconfFile = curClass.getAnnotation(DisconfFile.class);
        if (disconfFile == null) {
            LOGGER.error("cannot find DisconfFile annotation for class when set callback: {} ", curClass.toString());
            continue;
        }
        DisconfKey disconfKey = new DisconfKey(DisConfigTypeEnum.FILE, disconfFile.filename());
        addOne2InverseMap(disconfKey, inverseMap, iDisconfUpdate);
    }
    // 反索引
    List<String> fileKeyList = Arrays.asList(disconfUpdateService.confFileKeys());
    for (String fileKey : fileKeyList) {
        DisconfKey disconfKey = new DisconfKey(DisConfigTypeEnum.FILE, fileKey);
        addOne2InverseMap(disconfKey, inverseMap, iDisconfUpdate);
    }
}
Also used : DisconfFile(com.baidu.disconf.client.common.annotations.DisconfFile) DisconfKey(com.baidu.disconf.client.common.model.DisconfKey)

Example 4 with DisconfFile

use of com.baidu.disconf.client.common.annotations.DisconfFile in project disconf by knightliao.

the class ScanPrinterUtils method printFile.

/**
 */
public static void printFile(Set<Class<?>> classdata) {
    for (Class<?> item : classdata) {
        LOGGER.info(item.toString());
        DisconfFile disconfFile = item.getAnnotation(DisconfFile.class);
        LOGGER.info("\tfile name: " + disconfFile.filename());
        LOGGER.info("\tenv: " + disconfFile.env());
        LOGGER.info("\tversion: " + disconfFile.env());
    }
}
Also used : DisconfFile(com.baidu.disconf.client.common.annotations.DisconfFile)

Example 5 with DisconfFile

use of com.baidu.disconf.client.common.annotations.DisconfFile in project disconf by knightliao.

the class ReflectionScanStatic method analysis4DisconfFile.

/**
 * 分析出配置文件与配置文件中的Field的Method的MAP
 */
private void analysis4DisconfFile(ScanStaticModel scanModel) {
    Map<Class<?>, Set<Method>> disconfFileItemMap = new HashMap<Class<?>, Set<Method>>();
    // 
    // 配置文件MAP
    // 
    Set<Class<?>> classdata = scanModel.getDisconfFileClassSet();
    for (Class<?> classFile : classdata) {
        disconfFileItemMap.put(classFile, new HashSet<Method>());
    }
    // 
    // 将配置文件与配置文件中的配置项进行关联
    // 
    Set<Method> af1 = scanModel.getDisconfFileItemMethodSet();
    for (Method method : af1) {
        Class<?> thisClass = method.getDeclaringClass();
        if (disconfFileItemMap.containsKey(thisClass)) {
            Set<Method> fieldSet = disconfFileItemMap.get(thisClass);
            fieldSet.add(method);
            disconfFileItemMap.put(thisClass, fieldSet);
        } else {
            LOGGER.error("cannot find CLASS ANNOTATION " + DisconfFile.class.getName() + " for disconf file item: " + method.toString());
        }
    }
    // 
    // 最后的校验
    // 
    Iterator<Class<?>> iterator = disconfFileItemMap.keySet().iterator();
    while (iterator.hasNext()) {
        Class<?> classFile = iterator.next();
        // 校验是否所有配置文件都含有配置
        if (disconfFileItemMap.get(classFile).isEmpty()) {
            LOGGER.info("disconf file hasn't any items: " + classFile.getName());
            continue;
        }
        // 校验配置文件类型是否合适(目前只支持.properties类型)
        DisconfFile disconfFile = classFile.getAnnotation(DisconfFile.class);
        boolean fileTypeRight = ScanVerify.isDisconfFileTypeRight(disconfFile);
        if (!fileTypeRight) {
            LOGGER.warn("now do not support this file type" + disconfFile.toString());
            continue;
        }
    }
    // 设置
    scanModel.setDisconfFileItemMap(disconfFileItemMap);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) DisconfFile(com.baidu.disconf.client.common.annotations.DisconfFile) Method(java.lang.reflect.Method)

Aggregations

DisconfFile (com.baidu.disconf.client.common.annotations.DisconfFile)5 Method (java.lang.reflect.Method)3 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 DisconfFileItem (com.baidu.disconf.client.common.annotations.DisconfFileItem)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 DisconfKey (com.baidu.disconf.client.common.model.DisconfKey)1 DisconfStoreProcessor (com.baidu.disconf.client.store.DisconfStoreProcessor)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1