com.baidu.disconf.client.common.annotations.DisconfFileItem Java Examples

The following examples show how to use com.baidu.disconf.client.common.annotations.DisconfFileItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DisconfAutowareConfig.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 自动导入Static配置数据,能识别 DisconfFileItem 或 DisconfFileItem 的标识
 *
 * @Description: auto ware
 */
private static void autowareStaticConfig(Class<?> cls, Properties prop) throws Exception {

    if (null == prop) {
        throw new Exception("cannot autowareConfig null");
    }

    try {

        Field[] fields = cls.getDeclaredFields();

        for (Field field : fields) {

            if (field.isAnnotationPresent(DisconfFileItem.class)) {

                if (!Modifier.isStatic(field.getModifiers())) {
                    continue;
                }

                field.setAccessible(true);

                String name = field.getName();
                Object value = prop.getProperty(name, null);
                if (value != null) {
                    ClassUtils.setFieldValeByType(field, null, String.valueOf(value));
                }
            }
        }
    } catch (Exception e) {

        throw new Exception("error while autowire config file", e);
    }
}
 
Example #2
Source File: MethodUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 对于一个 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;
}
 
Example #3
Source File: DisconfAutowareConfig.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 自动导入Static配置数据,能识别 DisconfFileItem 或 DisconfFileItem 的标识
 *
 * @Description: auto ware
 */
private static void autowareStaticConfig(Class<?> cls, Properties prop) throws Exception {

    if (null == prop) {
        throw new Exception("cannot autowareConfig null");
    }

    try {

        Field[] fields = cls.getDeclaredFields();

        for (Field field : fields) {

            if (field.isAnnotationPresent(DisconfFileItem.class)) {

                if (!Modifier.isStatic(field.getModifiers())) {
                    continue;
                }

                field.setAccessible(true);

                String name = field.getName();
                Object value = prop.getProperty(name, null);
                if (value != null) {
                    ClassUtils.setFieldValeByType(field, null, String.valueOf(value));
                }
            }
        }
    } catch (Exception e) {

        throw new Exception("error while autowire config file", e);
    }
}
 
Example #4
Source File: MethodUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 对于一个 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;
}
 
Example #5
Source File: IgnoreConfB.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "varA2")
public Long getVarA2() {
    return varA2;
}
 
Example #6
Source File: ConfA.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "confa.varA2")
public Long getVarA2() {
    return varA2;
}
 
Example #7
Source File: IgnoreConfB.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "varA2")
public Long getVarA2() {
    return varA2;
}
 
Example #8
Source File: StaticConf.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar2", associateField = "staticvar2")
public static double getStaticvar2() {
    return staticvar2;
}
 
Example #9
Source File: StaticConf.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar", associateField = "staticvar")
public static int getStaticVar() {
    return staticvar;
}
 
Example #10
Source File: DisconfAspectJ.java    From disconf with Apache License 2.0 4 votes vote down vote up
/**
 * 获取配置文件数据, 只有开启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;
}
 
Example #11
Source File: ReflectionScanStatic.java    From disconf with Apache License 2.0 4 votes vote down vote up
/**
 * 扫描基本信息
 */
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;
}
 
Example #12
Source File: ConfA.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "confa.varA2")
public Long getVarA2() {
    return varA2;
}
 
Example #13
Source File: JedisConfig.java    From ehousechina with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.port")
public String getPort() {
	return port;
}
 
Example #14
Source File: StaticConf.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar2", associateField = "staticvar2")
public static double getStaticvar2() {
    return staticvar2;
}
 
Example #15
Source File: StaticConf.java    From disconf with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar", associateField = "staticvar")
public static int getStaticVar() {
    return staticvar;
}
 
Example #16
Source File: DisconfAspectJ.java    From disconf with Apache License 2.0 4 votes vote down vote up
/**
 * 获取配置文件数据, 只有开启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;
}
 
Example #17
Source File: ReflectionScanStatic.java    From disconf with Apache License 2.0 4 votes vote down vote up
/**
 * 扫描基本信息
 */
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;
}
 
Example #18
Source File: JedisConfig.java    From ehousechina with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.host")
public String getHost() {
	return host;
}
 
Example #19
Source File: JedisConfig.java    From ehousechina with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.port")
public String getPort() {
	return port;
}
 
Example #20
Source File: JedisConfig.java    From ehousechina with Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.host")
public String getHost() {
	return host;
}
 
Example #21
Source File: IgnoreConfB.java    From disconf with Apache License 2.0 2 votes vote down vote up
/**
 * name是配置文件中名字; associateField是此get方法相对应的Field名
 *
 * @return
 */
@DisconfFileItem(name = "varA", associateField = "varA")
public Long getVarA() {
    return varA;
}
 
Example #22
Source File: ConfA.java    From disconf with Apache License 2.0 2 votes vote down vote up
/**
 * name是配置文件中名字; associateField是此get方法相对应的Field名
 *
 * @return
 */
@DisconfFileItem(name = "confa.varA", associateField = "varA")
public Long getVarA() {
    return varA;
}
 
Example #23
Source File: JedisConfig.java    From blog_demos with Apache License 2.0 2 votes vote down vote up
/**
 * 端口, 分布式文件配置
 *
 * @return
 */
@DisconfFileItem(name = "redis.port", associateField = "port")
public int getPort() {
    return port;
}
 
Example #24
Source File: JedisConfig.java    From blog_demos with Apache License 2.0 2 votes vote down vote up
/**
 * 地址, 分布式文件配置
 *
 * @return
 */
@DisconfFileItem(name = "redis.host", associateField = "host")
public String getHost() {
    return host;
}
 
Example #25
Source File: IgnoreConfB.java    From disconf with Apache License 2.0 2 votes vote down vote up
/**
 * name是配置文件中名字; associateField是此get方法相对应的Field名
 *
 * @return
 */
@DisconfFileItem(name = "varA", associateField = "varA")
public Long getVarA() {
    return varA;
}
 
Example #26
Source File: ConfA.java    From disconf with Apache License 2.0 2 votes vote down vote up
/**
 * name是配置文件中名字; associateField是此get方法相对应的Field名
 *
 * @return
 */
@DisconfFileItem(name = "confa.varA", associateField = "varA")
public Long getVarA() {
    return varA;
}