Java Code Examples for com.alipay.sofa.rpc.common.utils.CommonUtils#isEmpty()

The following examples show how to use com.alipay.sofa.rpc.common.utils.CommonUtils#isEmpty() . 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: AbstractCluster.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isAvailable() {
    if (destroyed || !initialized) {
        return false;
    }
    List<ProviderGroup> providerGroups = addressHolder.getProviderGroups();
    if (CommonUtils.isEmpty(providerGroups)) {
        return false;
    }
    for (ProviderGroup entry : providerGroups) {
        List<ProviderInfo> providerInfos = entry.getProviderInfos();
        for (ProviderInfo providerInfo : providerInfos) {
            ClientTransport transport = connectionHolder.getAvailableClientTransport(providerInfo);
            if (transport != null && transport.isAvailable()) {
                return true; // 只有有1个可用 即可返回
            } else {
                connectionHolder.setUnavailable(providerInfo, transport);
            }
        }
    }
    return false;
}
 
Example 2
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * Convert child data to attribute list.
 *
 * @param config       the interface config
 * @param overridePath the override path
 * @param currentData  the current data
 * @return the attribute list
 * @throws UnsupportedEncodingException decode exception
 */
static List<Map<String, String>> convertOverrideToAttributes(AbstractInterfaceConfig config,
                                                             String overridePath,
                                                             List<ChildData> currentData)
    throws UnsupportedEncodingException {
    List<Map<String, String>> attributes = new ArrayList<Map<String, String>>();
    if (CommonUtils.isEmpty(currentData)) {
        return attributes;
    }

    for (ChildData childData : currentData) {
        String url = URLDecoder.decode(childData.getPath().substring(overridePath.length() + 1),
            "UTF-8");
        if (config instanceof ConsumerConfig) {
            //If child data contains system local host, convert config to attribute
            if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(SystemInfo.getLocalHost())
                && url.contains("://" + SystemInfo.getLocalHost() + "?")) {
                attributes.add(convertConfigToAttribute(overridePath, childData, false));
            }
        }
    }
    return attributes;
}
 
Example 3
Source File: ZookeeperConfigObserver.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 接口配置修改子节点Data列表
 *
 * @param config      接口配置
 * @param configPath  配置Path
 * @param currentData 子节点Data列表
 */
public void updateConfigAll(AbstractInterfaceConfig config, String configPath, List<ChildData> currentData) {
    if (CommonUtils.isEmpty(currentData)) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive updateAll data is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            for (ChildData data : currentData) {
                LOGGER.infoWithApp(config.getAppName(), "Receive updateAll data: path=["
                    + data.getPath() + "], data=[" + StringSerializer.decode(data.getData()) + "]"
                    + ", stat=[" + data.getStat() + "]");
            }
        }
        List<ConfigListener> configListeners = configListenerMap.get(config);
        if (CommonUtils.isNotEmpty(configListeners)) {
            List<Map<String, String>> attributes = ZookeeperRegistryHelper.convertConfigToAttributes(configPath,
                currentData);
            for (ConfigListener listener : configListeners) {
                for (Map<String, String> attribute : attributes) {
                    listener.configChanged(attribute);
                }
            }
        }
    }
}
 
Example 4
Source File: MeshRegistry.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected String fillProtocolAndVersion(SubscribeServiceResult subscribeServiceResult, String targetURL,
                                        String serviceName, String protocol) {

    String meshPort = judgeMeshPort(protocol);

    final List<String> datas = subscribeServiceResult.getDatas();

    if (CommonUtils.isEmpty(datas)) {
        targetURL = targetURL + ":" + meshPort;
    } else {
        for (String data : subscribeServiceResult.getDatas()) {
            final int indexOfParam = data.indexOf("?");
            if (indexOfParam != -1) {
                String param = data.substring(indexOfParam + 1);
                targetURL = targetURL + ":" + meshPort;
                targetURL = targetURL + "?" + param;
            } else {
                targetURL = targetURL + ":" + meshPort;
            }
            break;
        }
    }
    return targetURL;
}
 
Example 5
Source File: ProviderGroup.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 增加多个服务列表
 *
 * @param providerInfos 要增加的服务分组列表
 * @return 当前服务分组 provider group
 */
public ProviderGroup addAll(Collection<ProviderInfo> providerInfos) {
    if (CommonUtils.isEmpty(providerInfos)) {
        return this;
    }
    ConcurrentHashSet<ProviderInfo> tmp = new ConcurrentHashSet<ProviderInfo>(this.providerInfos);
    tmp.addAll(providerInfos); // 排重
    this.providerInfos = new ArrayList<ProviderInfo>(tmp);
    return this;
}
 
Example 6
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private String[] getMethodParameterNames(Method method) {
    String[] parameterNames = localVariableTableParameterNameDiscoverer.getParameterNames(method);
    if (CommonUtils.isEmpty(parameterNames)) {
        java.lang.reflect.Parameter[] parameters = method.getParameters();
        parameterNames = new String[parameters.length];
        int ix = 0;
        for (java.lang.reflect.Parameter parameter : parameters) {
            parameterNames[ix] = parameter.getName();
            ix++;
        }
    }
    return parameterNames;
}
 
Example 7
Source File: ProviderHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Compare two provider list, return add list and remove list
 *
 * @param oldList old Provider list
 * @param newList new provider list
 * @param add     provider list need add
 * @param remove  provider list need remove
 */
public static void compareProviders(List<ProviderInfo> oldList, List<ProviderInfo> newList,
                                    List<ProviderInfo> add, List<ProviderInfo> remove) {
    // 比较老列表和当前列表
    if (CommonUtils.isEmpty(oldList)) {
        // 空变成非空
        if (CommonUtils.isNotEmpty(newList)) {
            add.addAll(newList);
        }
        // 空到空,忽略
    } else {
        // 非空变成空
        if (CommonUtils.isEmpty(newList)) {
            remove.addAll(oldList);
        } else {
            // 非空变成非空,比较
            if (CommonUtils.isNotEmpty(oldList)) {
                List<ProviderInfo> tmpList = new ArrayList<ProviderInfo>(newList);
                // 遍历老的
                for (ProviderInfo oldProvider : oldList) {
                    if (tmpList.contains(oldProvider)) {
                        tmpList.remove(oldProvider);
                    } else {
                        // 新的没有,老的有,删掉
                        remove.add(oldProvider);
                    }
                }
                add.addAll(tmpList);
            }
        }
    }
}
 
Example 8
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert child data to attribute list.
 *
 * @param configPath  the config path
 * @param currentData the current data
 * @return the attribute list
 */
static List<Map<String, String>> convertConfigToAttributes(String configPath,
                                                           List<ChildData> currentData) {
    List<Map<String, String>> attributes = new ArrayList<Map<String, String>>();
    if (CommonUtils.isEmpty(currentData)) {
        return attributes;
    }

    for (ChildData childData : currentData) {
        attributes.add(convertConfigToAttribute(configPath, childData, false));
    }
    return attributes;
}
 
Example 9
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert url to provider list.
 *
 * @param providerPath
 * @param currentData  the current data
 * @return the list
 * @throws UnsupportedEncodingException decode exception
 */
static List<ProviderInfo> convertUrlsToProviders(String providerPath,
                                                 List<ChildData> currentData) throws UnsupportedEncodingException {
    List<ProviderInfo> providerInfos = new ArrayList<ProviderInfo>();
    if (CommonUtils.isEmpty(currentData)) {
        return providerInfos;
    }

    for (ChildData childData : currentData) {
        providerInfos.add(convertUrlToProvider(providerPath, childData));
    }
    return providerInfos;
}
 
Example 10
Source File: SystemLogger.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private String toString(String format, Object[] args) {
    if (CommonUtils.isEmpty(args)) {
        return format;
    }
    String s = format.replaceAll("\\{\\}", "%s");
    String[] ss = new String[args.length];
    for (int i = 0; i < args.length; i++) {
        ss[i] = StringUtils.toString(args[i]);
    }
    return String.format(s, ss);
}
 
Example 11
Source File: TracerChecker.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static List<String> extractFields(List<JSONObject> jsonObjects, String fieldName) {

        List<String> result = new ArrayList<String>();
        if (CommonUtils.isEmpty(jsonObjects)) {
            return result;
        } else {
            for (JSONObject jsonObject : jsonObjects) {
                result.add(extractField(jsonObject, fieldName));
            }
        }

        return result;
    }
 
Example 12
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 根据多个获取属性值,知道获取到为止
 *
 * @param map  原始map
 * @param keys 多个key
 * @return 属性值
 */
static String getValue(Map<String, String> map, String... keys) {
    if (CommonUtils.isEmpty(map)) {
        return null;
    }
    for (String key : keys) {
        String val = map.get(key);
        if (val != null) {
            return val;
        }
    }
    return null;
}
 
Example 13
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * convert map to url pair
 *
 * @param map
 * @return url paramters
 */
private static String convertMap2Pair(Map<String, String> map) {

    if (CommonUtils.isEmpty(map)) {
        return StringUtils.EMPTY;
    }

    StringBuilder sb = new StringBuilder(128);
    for (Map.Entry<String, String> entry : map.entrySet()) {
        sb.append(getKeyPairs(entry.getKey(), entry.getValue()));
    }

    return sb.toString();
}
 
Example 14
Source File: NacosRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert instances to providers list.
 *
 * @param allInstances the all instances 
 * @return the list
 */
static List<ProviderInfo> convertInstancesToProviders(List<Instance> allInstances) {
    List<ProviderInfo> providerInfos = new ArrayList<ProviderInfo>();
    if (CommonUtils.isEmpty(allInstances)) {
        return providerInfos;
    }

    for (Instance instance : allInstances) {
        String url = convertInstanceToUrl(instance);
        ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url);
        providerInfos.add(providerInfo);
    }
    return providerInfos;
}
 
Example 15
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 根据多个key删除属性值,全部删掉
 *
 * @param map  原始map
 * @param keys 多个key
 */
static void removeOldKeys(Map<String, String> map, String... keys) {
    if (CommonUtils.isEmpty(map)) {
        return;
    }
    for (String key : keys) {
        map.remove(key);
    }
}
 
Example 16
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 根据多个获取属性值,知道获取到为止
 *
 * @param map  原始map
 * @param keys 多个key
 * @return 属性值
 */
static String getValue(Map<String, String> map, String... keys) {
    if (CommonUtils.isEmpty(map)) {
        return null;
    }
    for (String key : keys) {
        String val = map.get(key);
        if (val != null) {
            return val;
        }
    }
    return null;
}
 
Example 17
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * convert map to url pair
 *
 * @param map
 * @return url paramters
 */
private static String convertMap2Pair(Map<String, String> map) {

    if (CommonUtils.isEmpty(map)) {
        return StringUtils.EMPTY;
    }

    StringBuilder sb = new StringBuilder(128);
    for (Map.Entry<String, String> entry : map.entrySet()) {
        sb.append(getKeyPairs(entry.getKey(), entry.getValue()));
    }

    return sb.toString();
}
 
Example 18
Source File: FilterInvoker.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 取得方法的特殊参数配置
 *
 * @param methodName   方法名
 * @param paramKey     参数关键字
 * @param defaultValue 默认值
 * @return 都找不到为false boolean method param
 */
protected boolean getBooleanMethodParam(String methodName, String paramKey, boolean defaultValue) {
    if (CommonUtils.isEmpty(configContext)) {
        return defaultValue;
    }
    Boolean o = (Boolean) configContext.get(buildMethodKey(methodName, paramKey));
    if (o == null) {
        o = (Boolean) configContext.get(paramKey);
        return o == null ? defaultValue : o;
    } else {
        return o;
    }
}
 
Example 19
Source File: ConsulUtils.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static List<String> buildServiceIds(ProviderConfig<?> config) {
    List<ServerConfig> servers = config.getServer();
    if (CommonUtils.isEmpty(servers)) {
        return Collections.emptyList();
    }
    return servers.stream()
            .map(server -> buildServiceId(config, server))
            .collect(Collectors.toList());
}
 
Example 20
Source File: ProviderGroup.java    From sofa-rpc with Apache License 2.0 2 votes vote down vote up
/**
 * Is empty boolean.
 *
 * @return the boolean
 */
public boolean isEmpty() {
    return CommonUtils.isEmpty(providerInfos);
}