示例#1
0
  /**
   * 支持匹配的Service
   *
   * @param servicePattern
   * @param serviceName
   */
  static boolean isSerivceNameMatched(String servicePattern, String serviceName) {
    final int pip = servicePattern.indexOf('/');
    final int pi = serviceName.indexOf('/');
    if (pip != -1) { // pattern有group
      if (pi == -1) return false; // servicename无group

      String gp = servicePattern.substring(0, pip);
      servicePattern = servicePattern.substring(pip + 1);

      String g = serviceName.substring(0, pi);
      if (!gp.equals(g)) return false;
    }
    if (pi != -1) serviceName = serviceName.substring(pi + 1);

    final int vip = servicePattern.lastIndexOf(':');
    final int vi = serviceName.lastIndexOf(':');
    if (vip != -1) { // pattern有group
      if (vi == -1) return false;

      String vp = servicePattern.substring(vip + 1);
      servicePattern = servicePattern.substring(0, vip);

      String v = serviceName.substring(vi + 1);
      if (!vp.equals(v)) return false;
    }
    if (vi != -1) serviceName = serviceName.substring(0, vi);

    return ParseUtils.isMatchGlobPattern(servicePattern, serviceName);
  }
示例#2
0
 public ASTNode toAST(Context ctx) throws LensException {
   String hql = toHQL();
   ParseDriver pd = new ParseDriver();
   ASTNode tree;
   try {
     log.info("HQL:{}", hql);
     tree = pd.parse(hql, ctx);
   } catch (ParseException e) {
     throw new LensException(e);
   }
   return ParseUtils.findRootNonNullToken(tree);
 }
示例#3
0
  static Map<String, String> appendMethodsToUrls(
      Map<String, String> serviceUrls, Map<String, Set<String>> url2Methods) {
    // 为URL上加上方法参数
    Map<String, String> results = new HashMap<String, String>();
    for (Map.Entry<String, Set<String>> entry : url2Methods.entrySet()) {
      String url = entry.getKey();
      String query = serviceUrls.get(url);

      Set<String> methodNames = entry.getValue();
      if (methodNames != null && methodNames.size() > 0) {
        String ms = StringUtils.join(methodNames.toArray(new String[0]), ParseUtils.METHOD_SPLIT);
        query = ParseUtils.replaceParameter(query, "methods", ms);
      }
      results.put(url, query);
    }
    return results;
  }
  /**
   * Are all of the "defining parameters" required to create an AU available?
   *
   * @return true If so
   */
  private boolean verifyDefiningParameters() {
    KeyedList parameters;
    int size;

    if (!isCreateCommand()) {
      return true;
    }

    parameters = ParseUtils.getDynamicFields(getXmlUtils(), getRequestDocument(), AP_MD_AUDEFINING);
    size = parameters.size();

    for (int i = 0; i < size; i++) {
      if (StringUtil.isNullString((String) parameters.getValue(i))) {
        return false;
      }
    }
    return true;
  }
示例#5
0
  public static boolean matchRoute(
      String consumerAddress,
      String consumerQueryUrl,
      Route route,
      Map<String, List<String>> clusters) {
    RouteRule rule = RouteRule.parseQuitely(route);
    Map<String, RouteRule.MatchPair> when =
        RouteRuleUtils.expandCondition(
            rule.getWhenCondition(), "consumer.cluster", "consumer.host", clusters);
    Map<String, String> consumerSample = ParseUtils.parseQuery("consumer.", consumerQueryUrl);

    final int index = consumerAddress.lastIndexOf(":");
    String consumerHost = null;
    if (index != -1) {
      consumerHost = consumerAddress.substring(0, index);
    } else {
      consumerHost = consumerAddress;
    }
    consumerSample.put("consumer.host", consumerHost);

    return RouteRuleUtils.isMatchCondition(when, consumerSample, consumerSample);
  }
示例#6
0
  // FIXME clusters和routes的合并,可以在clusters或routes变化时预先做
  // FIXME 从Util方法中分离出Cache的操作
  public static Map<String, String> route(
      String serviceName,
      String consumerAddress,
      String consumerQueryUrl,
      Map<String, String> serviceUrls,
      List<Route> routes,
      Map<String, List<String>> clusters,
      List<Route> routed) {
    if (serviceUrls == null || serviceUrls.size() == 0) {
      return serviceUrls;
    }
    if (routes == null || routes.isEmpty()) {
      return serviceUrls;
    }

    Map<Long, RouteRule> rules = route2RouteRule(routes, clusters);

    final Map<String, String> consumerSample = ParseUtils.parseQuery("consumer.", consumerQueryUrl);
    final int index = consumerAddress.lastIndexOf(":");
    final String consumerHost;
    if (consumerAddress != null && index != -1) {
      consumerHost = consumerAddress.substring(0, index);
    } else {
      consumerHost = consumerAddress;
    }
    consumerSample.put("consumer.host", consumerHost);

    Map<String, Map<String, String>> url2ProviderSample =
        new HashMap<String, Map<String, String>>();
    for (Map.Entry<String, String> entry : serviceUrls.entrySet()) {
      URI uri;
      try {
        uri = new URI(entry.getKey());
      } catch (URISyntaxException e) {
        throw new IllegalStateException(
            "fail to parse url(" + entry.getKey() + "):" + e.getMessage(), e);
      }
      Map<String, String> sample = new HashMap<String, String>();
      sample.putAll(ParseUtils.parseQuery("provider.", entry.getValue()));
      sample.put("provider.protocol", uri.getScheme());
      sample.put("provider.host", uri.getHost());
      sample.put("provider.port", String.valueOf(uri.getPort()));

      url2ProviderSample.put(entry.getKey(), sample);
    }

    Map<String, Set<String>> url2Methods = new HashMap<String, Set<String>>();

    // consumer可以通过consumer.methods Key指定需要的方法
    String methodsString = consumerSample.get("consumer.methods");
    String[] methods =
        methodsString == null || methodsString.length() == 0
            ? new String[] {Route.ALL_METHOD}
            : methodsString.split(ParseUtils.METHOD_SPLIT);
    for (String method : methods) {
      consumerSample.put("method", method);
      // NOTE:
      // <*方法>只配置 <no method key>
      // method1方法匹配 <no method key> 和 <method = method1>, 此时要把<no method key>的Route的优先级降低即可
      if (routes != null && routes.size() > 0) {
        for (Route route : routes) {
          if (isSerivceNameMatched(route.getService(), serviceName)) {
            RouteRule rule = rules.get(route.getId());
            // 当满足when条件时
            if (rule != null
                && RouteRuleUtils.isMatchCondition(
                    rule.getWhenCondition(), consumerSample, consumerSample)) {
              if (routed != null && !routed.contains(route)) {
                routed.add(route);
              }
              Map<String, RouteRule.MatchPair> then = rule.getThenCondition();
              if (then != null) {
                Map<String, Map<String, String>> tmp =
                    getUrlsMatchedCondition(then, consumerSample, url2ProviderSample);
                // 如果规则的结果是空,则该规则无效,使用所有Provider
                if (route.isForce() || !tmp.isEmpty()) {
                  url2ProviderSample = tmp;
                }
              }
            }
          }
        }
      }
      for (String url : url2ProviderSample.keySet()) {
        Set<String> mts = url2Methods.get(url);
        if (mts == null) {
          mts = new HashSet<String>();
          url2Methods.put(url, mts);
        }
        mts.add(method);
      }
    } // end of for methods

    return appendMethodsToUrls(serviceUrls, url2Methods);
  }