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

The following examples show how to use com.baidu.disconf.client.common.annotations.DisconfFileItem. These examples are extracted from open source projects. 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 Project: disconf   Source File: MethodUtils.java    License: 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 2
Source Project: disconf   Source File: DisconfAutowareConfig.java    License: 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 3
Source Project: disconf   Source File: MethodUtils.java    License: 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 4
Source Project: disconf   Source File: DisconfAutowareConfig.java    License: 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 5
Source Project: ehousechina   Source File: JedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.port")
public String getPort() {
	return port;
}
 
Example 6
Source Project: ehousechina   Source File: JedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.host")
public String getHost() {
	return host;
}
 
Example 7
Source Project: ehousechina   Source File: JedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.port")
public String getPort() {
	return port;
}
 
Example 8
Source Project: ehousechina   Source File: JedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "redis.host")
public String getHost() {
	return host;
}
 
Example 9
Source Project: disconf   Source File: ReflectionScanStatic.java    License: 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 10
Source Project: disconf   Source File: DisconfAspectJ.java    License: 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 Project: disconf   Source File: StaticConf.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar", associateField = "staticvar")
public static int getStaticVar() {
    return staticvar;
}
 
Example 12
Source Project: disconf   Source File: StaticConf.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar2", associateField = "staticvar2")
public static double getStaticvar2() {
    return staticvar2;
}
 
Example 13
Source Project: disconf   Source File: IgnoreConfB.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "varA2")
public Long getVarA2() {
    return varA2;
}
 
Example 14
Source Project: disconf   Source File: ConfA.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "confa.varA2")
public Long getVarA2() {
    return varA2;
}
 
Example 15
Source Project: disconf   Source File: ReflectionScanStatic.java    License: 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 16
Source Project: disconf   Source File: DisconfAspectJ.java    License: 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 Project: disconf   Source File: StaticConf.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar", associateField = "staticvar")
public static int getStaticVar() {
    return staticvar;
}
 
Example 18
Source Project: disconf   Source File: StaticConf.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "staticvar2", associateField = "staticvar2")
public static double getStaticvar2() {
    return staticvar2;
}
 
Example 19
Source Project: disconf   Source File: IgnoreConfB.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "varA2")
public Long getVarA2() {
    return varA2;
}
 
Example 20
Source Project: disconf   Source File: ConfA.java    License: Apache License 2.0 4 votes vote down vote up
@DisconfFileItem(name = "confa.varA2")
public Long getVarA2() {
    return varA2;
}
 
Example 21
Source Project: blog_demos   Source File: JedisConfig.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 地址, 分布式文件配置
 *
 * @return
 */
@DisconfFileItem(name = "redis.host", associateField = "host")
public String getHost() {
    return host;
}
 
Example 22
Source Project: blog_demos   Source File: JedisConfig.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 端口, 分布式文件配置
 *
 * @return
 */
@DisconfFileItem(name = "redis.port", associateField = "port")
public int getPort() {
    return port;
}
 
Example 23
Source Project: disconf   Source File: IgnoreConfB.java    License: 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 24
Source Project: disconf   Source File: ConfA.java    License: 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 25
Source Project: disconf   Source File: IgnoreConfB.java    License: 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 Project: disconf   Source File: ConfA.java    License: 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;
}