Search in sources :

Example 1 with DisconfItem

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

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

the class ReflectionScanStatic method scanBasicInfo.

/**
 * 扫描基本信息
 */
private ScanStaticModel scanBasicInfo(List<String> packNameList) {
    ScanStaticModel scanModel = new ScanStaticModel();
    // 
    // 扫描对象
    // 
    Reflections reflections = getReflection(packNameList);
    scanModel.setReflections(reflections);
    // 
    // 获取DisconfFile class
    // 
    Set<Class<?>> classdata = reflections.getTypesAnnotatedWith(DisconfFile.class);
    scanModel.setDisconfFileClassSet(classdata);
    // 
    // 获取DisconfFileItem method
    // 
    Set<Method> af1 = reflections.getMethodsAnnotatedWith(DisconfFileItem.class);
    scanModel.setDisconfFileItemMethodSet(af1);
    // 
    // 获取DisconfItem method
    // 
    af1 = reflections.getMethodsAnnotatedWith(DisconfItem.class);
    scanModel.setDisconfItemMethodSet(af1);
    // 
    // 获取DisconfActiveBackupService
    // 
    classdata = reflections.getTypesAnnotatedWith(DisconfActiveBackupService.class);
    scanModel.setDisconfActiveBackupServiceClassSet(classdata);
    // 
    // 获取DisconfUpdateService
    // 
    classdata = reflections.getTypesAnnotatedWith(DisconfUpdateService.class);
    scanModel.setDisconfUpdateService(classdata);
    // update pipeline
    Set<Class<? extends IDisconfUpdatePipeline>> iDisconfUpdatePipeline = reflections.getSubTypesOf(IDisconfUpdatePipeline.class);
    if (iDisconfUpdatePipeline != null && iDisconfUpdatePipeline.size() != 0) {
        scanModel.setiDisconfUpdatePipeline((Class<IDisconfUpdatePipeline>) iDisconfUpdatePipeline.toArray()[0]);
    }
    return scanModel;
}
Also used : DisconfActiveBackupService(com.baidu.disconf.client.common.annotations.DisconfActiveBackupService) IDisconfUpdatePipeline(com.baidu.disconf.client.common.update.IDisconfUpdatePipeline) ScanStaticModel(com.baidu.disconf.client.scan.inner.statically.model.ScanStaticModel) Method(java.lang.reflect.Method) DisconfUpdateService(com.baidu.disconf.client.common.annotations.DisconfUpdateService) Reflections(org.reflections.Reflections) DisconfItem(com.baidu.disconf.client.common.annotations.DisconfItem)

Example 3 with DisconfItem

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

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

the class ScanPrinterUtils method printItem.

/**
 */
public static void printItem(Set<Field> af1) {
    for (Field item : af1) {
        LOGGER.info(item.toString());
        DisconfItem disconfItem = item.getAnnotation(DisconfItem.class);
        LOGGER.info("\tkey: " + disconfItem.key());
        LOGGER.info("\tenv: " + disconfItem.env());
        LOGGER.info("\tversion: " + disconfItem.version());
    }
}
Also used : Field(java.lang.reflect.Field) DisconfItem(com.baidu.disconf.client.common.annotations.DisconfItem)

Aggregations

DisconfItem (com.baidu.disconf.client.common.annotations.DisconfItem)4 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)2 DisconfActiveBackupService (com.baidu.disconf.client.common.annotations.DisconfActiveBackupService)1 DisconfFileItem (com.baidu.disconf.client.common.annotations.DisconfFileItem)1 DisconfUpdateService (com.baidu.disconf.client.common.annotations.DisconfUpdateService)1 DisConfCommonModel (com.baidu.disconf.client.common.model.DisConfCommonModel)1 DisconfCenterItem (com.baidu.disconf.client.common.model.DisconfCenterItem)1 IDisconfUpdatePipeline (com.baidu.disconf.client.common.update.IDisconfUpdatePipeline)1 ScanStaticModel (com.baidu.disconf.client.scan.inner.statically.model.ScanStaticModel)1 Reflections (org.reflections.Reflections)1