예제 #1
0
 public void saveOwner(Owner owner) {
   List<Override> overrides =
       overrideService.findByServiceAndAddress(owner.getService(), Constants.ANYHOST_VALUE);
   if (overrides == null || overrides.size() == 0) {
     Override override = new Override();
     override.setAddress(Constants.ANYHOST_VALUE);
     override.setService(owner.getService());
     override.setEnabled(true);
     override.setParams("owner=" + owner.getUsername());
     overrideService.saveOverride(override);
   } else {
     for (Override override : overrides) {
       Map<String, String> params = StringUtils.parseQueryString(override.getParams());
       String usernames = params.get("owner");
       if (usernames == null || usernames.length() == 0) {
         usernames = owner.getUsername();
       } else {
         usernames = usernames + "," + owner.getUsername();
       }
       params.put("owner", usernames);
       override.setParams(StringUtils.toQueryString(params));
       overrideService.updateOverride(override);
     }
   }
 }
예제 #2
0
 public void index(Map<String, Object> context) {
   final String service = StringUtils.trimToNull((String) context.get("service"));
   String address = (String) context.get("address");
   address = Tool.getIP(address);
   List<Weight> weights;
   if (service != null && service.length() > 0) {
     weights = OverrideUtils.overridesToWeights(overrideService.findByService(service));
   } else if (address != null && address.length() > 0) {
     weights = OverrideUtils.overridesToWeights(overrideService.findByAddress(address));
   } else {
     weights = OverrideUtils.overridesToWeights(overrideService.findAll());
   }
   context.put("weights", weights);
 }
예제 #3
0
  /**
   * 删除动作
   *
   * @param ids
   * @return
   */
  public boolean delete(Long[] ids, Map<String, Object> context) {
    for (Long id : ids) {
      Weight w = OverrideUtils.overrideToWeight(overrideService.findById(id));
      if (!super.currentUser.hasServicePrivilege(w.getService())) {
        context.put("message", getMessage("HaveNoServicePrivilege", w.getService()));
        return false;
      }
    }

    for (Long id : ids) {
      overrideService.deleteOverride(id);
    }
    return true;
  }
예제 #4
0
 public boolean update(Weight weight, Map<String, Object> context) {
   if (!super.currentUser.hasServicePrivilege(weight.getService())) {
     context.put("message", getMessage("HaveNoServicePrivilege", weight.getService()));
     return false;
   }
   weight.setAddress(Tool.getIP(weight.getAddress()));
   overrideService.updateOverride(OverrideUtils.weightToOverride(weight));
   return true;
 }
예제 #5
0
  public void disableProvider(Long id) {
    if (id == null) {
      throw new IllegalStateException("no provider id");
    }

    Provider oldProvider = findProvider(id);
    if (oldProvider == null) {
      throw new IllegalStateException("Provider was changed!");
    }

    if (oldProvider.isDynamic()) {
      // 保证disable的override唯一
      if (oldProvider.isEnabled()) {
        Override override = new Override();
        override.setAddress(oldProvider.getAddress());
        override.setService(oldProvider.getService());
        override.setEnabled(true);
        override.setParams(Constants.DISABLED_KEY + "=true");
        overrideService.saveOverride(override);
        return;
      }
      List<Override> oList =
          overrideService.findByServiceAndAddress(
              oldProvider.getService(), oldProvider.getAddress());

      for (Override o : oList) {
        Map<String, String> params = StringUtils.parseQueryString(o.getParams());
        if (params.containsKey(Constants.DISABLED_KEY)) {
          if (params.get(Constants.DISABLED_KEY).equals("false")) {
            overrideService.deleteOverride(o.getId());
          }
        }
      }
    } else {
      oldProvider.setEnabled(false);
      updateProvider(oldProvider);
    }
  }
예제 #6
0
  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;
  }
예제 #7
0
 /**
  * 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);
 }
예제 #8
0
 public void edit(Long id, Map<String, Object> context) {
   add(context);
   show(id, context);
   context.put("service", overrideService.findById(id).getService());
 }
예제 #9
0
 public List<Owner> findAll() {
   List<Provider> pList = providerService.findAll();
   List<Override> cList = overrideService.findAll();
   return toOverrideLiset(pList, cList);
 }
예제 #10
0
 public List<Owner> findByService(String serviceName) {
   List<Provider> pList = providerService.findByService(serviceName);
   List<Override> cList =
       overrideService.findByServiceAndAddress(serviceName, Constants.ANYHOST_VALUE);
   return toOverrideLiset(pList, cList);
 }