Search in sources :

Example 1 with Weight

use of com.alibaba.dubbo.registry.common.domain.Weight in project dubbo by alibaba.

the class OverrideUtils method overridesToWeights.

public static List<Weight> overridesToWeights(List<Override> overrides) {
    List<Weight> weights = new ArrayList<Weight>();
    if (overrides == null) {
        return weights;
    }
    for (Override o : overrides) {
        if (StringUtils.isEmpty(o.getParams())) {
            continue;
        } else {
            Map<String, String> params = StringUtils.parseQueryString(o.getParams());
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (entry.getKey().equals("weight")) {
                    Weight weight = new Weight();
                    weight.setAddress(o.getAddress());
                    weight.setId(o.getId());
                    weight.setService(o.getService());
                    weight.setWeight(Integer.valueOf(entry.getValue()));
                    weights.add(weight);
                }
            }
        }
    }
    return weights;
}
Also used : ArrayList(java.util.ArrayList) Override(com.alibaba.dubbo.registry.common.domain.Override) Map(java.util.Map) Weight(com.alibaba.dubbo.registry.common.domain.Weight)

Example 2 with Weight

use of com.alibaba.dubbo.registry.common.domain.Weight in project dubbo by alibaba.

the class Weights method show.

/**
     * load weight对象供编辑操作
     * @param id
     * @param context
     */
public void show(Long id, Map<String, Object> context) {
    Weight weight = OverrideUtils.overrideToWeight(overrideService.findById(id));
    context.put("weight", weight);
}
Also used : Weight(com.alibaba.dubbo.registry.common.domain.Weight)

Example 3 with Weight

use of com.alibaba.dubbo.registry.common.domain.Weight in project dubbo by alibaba.

the class Weights method create.

public boolean create(Map<String, Object> context) throws Exception {
    String addr = (String) context.get("address");
    String services = (String) context.get("multiservice");
    if (services == null || services.trim().length() == 0) {
        services = (String) context.get("service");
    }
    String weight = (String) context.get("weight");
    int w = Integer.parseInt(weight);
    Set<String> addresses = new HashSet<String>();
    BufferedReader reader = new BufferedReader(new StringReader(addr));
    while (true) {
        String line = reader.readLine();
        if (null == line)
            break;
        String[] split = line.split("[\\s,;]+");
        for (String s : split) {
            if (s.length() == 0)
                continue;
            String ip = s;
            String port = null;
            if (s.indexOf(":") != -1) {
                ip = s.substring(0, s.indexOf(":"));
                port = s.substring(s.indexOf(":") + 1, s.length());
                if (port.trim().length() == 0)
                    port = null;
            }
            if (!IP_PATTERN.matcher(ip).matches()) {
                context.put("message", "illegal IP: " + s);
                return false;
            }
            if (LOCAL_IP_PATTERN.matcher(ip).matches() || ALL_IP_PATTERN.matcher(ip).matches()) {
                context.put("message", "local IP or any host ip is illegal: " + s);
                return false;
            }
            if (port != null) {
                if (!NumberUtils.isDigits(port)) {
                    context.put("message", "illegal port: " + s);
                    return false;
                }
            }
            addresses.add(s);
        }
    }
    Set<String> aimServices = new HashSet<String>();
    reader = new BufferedReader(new StringReader(services));
    while (true) {
        String line = reader.readLine();
        if (null == line)
            break;
        String[] split = line.split("[\\s,;]+");
        for (String s : split) {
            if (s.length() == 0)
                continue;
            if (!super.currentUser.hasServicePrivilege(s)) {
                context.put("message", getMessage("HaveNoServicePrivilege", s));
                return false;
            }
            aimServices.add(s);
        }
    }
    for (String aimService : aimServices) {
        for (String a : addresses) {
            Weight wt = new Weight();
            wt.setUsername((String) context.get("operator"));
            wt.setAddress(Tool.getIP(a));
            wt.setService(aimService);
            wt.setWeight(w);
            overrideService.saveOverride(OverrideUtils.weightToOverride(wt));
        }
    }
    return true;
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Weight(com.alibaba.dubbo.registry.common.domain.Weight) HashSet(java.util.HashSet)

Aggregations

Weight (com.alibaba.dubbo.registry.common.domain.Weight)3 Override (com.alibaba.dubbo.registry.common.domain.Override)1 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1