Search in sources :

Example 1 with ConfigDesc

use of com.weibo.api.motan.config.annotation.ConfigDesc in project motan by weibocom.

the class AbstractConfig method appendConfigParams.

/**
     * 将config 参数录入Map中
     * 
     * @param parameters
     */
@SuppressWarnings("unchecked")
protected void appendConfigParams(Map<String, String> parameters, String prefix) {
    Method[] methods = this.getClass().getMethods();
    for (Method method : methods) {
        try {
            String name = method.getName();
            if (isConfigMethod(method)) {
                int idx = name.startsWith("get") ? 3 : 2;
                String prop = name.substring(idx, idx + 1).toLowerCase() + name.substring(idx + 1);
                String key = prop;
                ConfigDesc configDesc = method.getAnnotation(ConfigDesc.class);
                if (configDesc != null && !StringUtils.isBlank(configDesc.key())) {
                    key = configDesc.key();
                }
                Object value = method.invoke(this);
                if (value == null || StringUtils.isBlank(String.valueOf(value))) {
                    if (configDesc != null && configDesc.required()) {
                        throw new MotanFrameworkException(String.format("%s.%s should not be null or empty", this.getClass().getSimpleName(), key), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
                    }
                    continue;
                }
                if (prefix != null && prefix.length() > 0) {
                    key = prefix + "." + key;
                }
                parameters.put(key, String.valueOf(value).trim());
            } else if ("getParameters".equals(name) && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0 && method.getReturnType() == Map.class) {
                Map<String, String> map = (Map<String, String>) method.invoke(this);
                if (map != null && map.size() > 0) {
                    String pre = prefix != null && prefix.length() > 0 ? prefix + "." : "";
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        parameters.put(pre + entry.getKey(), entry.getValue());
                    }
                }
            }
        } catch (Exception e) {
            throw new MotanFrameworkException(String.format("Error when append params for config: %s.%s", this.getClass().getSimpleName(), method.getName()), e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
        }
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ConfigDesc(com.weibo.api.motan.config.annotation.ConfigDesc) Method(java.lang.reflect.Method) Map(java.util.Map) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Aggregations

ConfigDesc (com.weibo.api.motan.config.annotation.ConfigDesc)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1