Java Code Examples for com.alibaba.dubbo.registry.common.route.RouteRule#MatchPair

The following examples show how to use com.alibaba.dubbo.registry.common.route.RouteRule#MatchPair . 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: Routes.java    From dubbox with Apache License 2.0 4 votes vote down vote up
/**
   * 显示路由详细信息
   * @param context
   */
  public void show(Map<String, Object> context) {
  	try {
       Route route = routeService.findRoute(Long.parseLong((String)context.get("id")));
       
       if (route == null) {
           throw new IllegalArgumentException("The route is not existed.");
       }
       if(route.getService()!=null && !route.getService().isEmpty()){
           context.put("service", route.getService());
       }
       
       RouteRule routeRule= RouteRule.parse(route);
       
       @SuppressWarnings("unchecked")
       Map<String, RouteRule.MatchPair>[] paramArray = new Map[] {
           routeRule.getWhenCondition(), routeRule.getThenCondition()};
       String[][][] namesArray = new String[][][] {when_names, then_names };
       
       for(int i=0; i<paramArray.length; ++i) {
           Map<String, RouteRule.MatchPair> param = paramArray[i];
           String[][] names = namesArray[i];
           for(String[] name : names) {
               RouteRule.MatchPair matchPair = param.get(name[0]);
               if(matchPair == null) {
                   continue;
               }
               
               if(!matchPair.getMatches().isEmpty()) {
                   String m = RouteRule.join(matchPair.getMatches());
                   context.put(name[1], m);
               }
               if(!matchPair.getUnmatches().isEmpty()) {
                   String u = RouteRule.join(matchPair.getUnmatches());
                   context.put(name[2], u);
               }
           }
       }
       context.put("route", route);
       context.put("methods", CollectionUtils.sort(new ArrayList<String>(providerService.findMethodsByService(route.getService()))));
  	} catch (ParseException e) {
	e.printStackTrace();
}
  }
 
Example 2
Source File: RouteRuleUtilsTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void test_isMatchCondition()throws Exception {
    Map<String, RouteRule.MatchPair> condition = new HashMap<String, RouteRule.MatchPair>();
    Map<String, String> valueParams = new HashMap<String, String>();
    Map<String, String> kv = new HashMap<String, String>();
    
    boolean output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    {
        // key1
        Set<String> matches   = new HashSet<String>();
        matches.add("value1_1");
        matches.add("value1_2");
        matches.add("value1_3");
        Set<String> unmatches = new HashSet<String>();
        unmatches.add("v1_1");
        unmatches.add("v1_2");
        unmatches.add("v1_3");
        
        RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key1", p);
        
        // key2 
        matches   = new HashSet<String>();
        matches.add("value2_1");
        matches.add("value2_2");
        matches.add("value2_3");
        unmatches = new HashSet<String>();
        unmatches.add("v2_1");
        unmatches.add("v2_2");
        unmatches.add("v2_3");
        
        p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key2", p);
    }
    
    kv.put("kkk", "vvv");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "vvvXXX");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "value1_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key2", "value2_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    kv.put("key1", "v1_2");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
}
 
Example 3
Source File: Routes.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
/**
   * 显示路由详细信息
   * @param context
   */
  public void show(Map<String, Object> context) {
  	try {
       Route route = routeService.findRoute(Long.parseLong((String)context.get("id")));
       
       if (route == null) {
           throw new IllegalArgumentException("The route is not existed.");
       }
       if(route.getService()!=null && !route.getService().isEmpty()){
           context.put("service", route.getService());
       }
       
       RouteRule routeRule= RouteRule.parse(route);
       
       @SuppressWarnings("unchecked")
       Map<String, RouteRule.MatchPair>[] paramArray = new Map[] {
           routeRule.getWhenCondition(), routeRule.getThenCondition()};
       String[][][] namesArray = new String[][][] {when_names, then_names };
       
       for(int i=0; i<paramArray.length; ++i) {
           Map<String, RouteRule.MatchPair> param = paramArray[i];
           String[][] names = namesArray[i];
           for(String[] name : names) {
               RouteRule.MatchPair matchPair = param.get(name[0]);
               if(matchPair == null) {
                   continue;
               }
               
               if(!matchPair.getMatches().isEmpty()) {
                   String m = RouteRule.join(matchPair.getMatches());
                   context.put(name[1], m);
               }
               if(!matchPair.getUnmatches().isEmpty()) {
                   String u = RouteRule.join(matchPair.getUnmatches());
                   context.put(name[2], u);
               }
           }
       }
       context.put("route", route);
       context.put("methods", CollectionUtils.sort(new ArrayList<String>(providerService.findMethodsByService(route.getService()))));
  	} catch (ParseException e) {
	e.printStackTrace();
}
  }
 
Example 4
Source File: RouteRuleUtilsTest.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@Test
public void test_isMatchCondition()throws Exception {
    Map<String, RouteRule.MatchPair> condition = new HashMap<String, RouteRule.MatchPair>();
    Map<String, String> valueParams = new HashMap<String, String>();
    Map<String, String> kv = new HashMap<String, String>();
    
    boolean output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    {
        // key1
        Set<String> matches   = new HashSet<String>();
        matches.add("value1_1");
        matches.add("value1_2");
        matches.add("value1_3");
        Set<String> unmatches = new HashSet<String>();
        unmatches.add("v1_1");
        unmatches.add("v1_2");
        unmatches.add("v1_3");
        
        RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key1", p);
        
        // key2 
        matches   = new HashSet<String>();
        matches.add("value2_1");
        matches.add("value2_2");
        matches.add("value2_3");
        unmatches = new HashSet<String>();
        unmatches.add("v2_1");
        unmatches.add("v2_2");
        unmatches.add("v2_3");
        
        p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key2", p);
    }
    
    kv.put("kkk", "vvv");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "vvvXXX");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "value1_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key2", "value2_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    kv.put("key1", "v1_2");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
}
 
Example 5
Source File: Routes.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
/**
   * 显示路由详细信息
   * @param context
   */
  public void show(Map<String, Object> context) {
  	try {
       Route route = routeService.findRoute(Long.parseLong((String)context.get("id")));
       
       if (route == null) {
           throw new IllegalArgumentException("The route is not existed.");
       }
       if(route.getService()!=null && !route.getService().isEmpty()){
           context.put("service", route.getService());
       }
       
       RouteRule routeRule= RouteRule.parse(route);
       
       @SuppressWarnings("unchecked")
       Map<String, RouteRule.MatchPair>[] paramArray = new Map[] {
           routeRule.getWhenCondition(), routeRule.getThenCondition()};
       String[][][] namesArray = new String[][][] {when_names, then_names };
       
       for(int i=0; i<paramArray.length; ++i) {
           Map<String, RouteRule.MatchPair> param = paramArray[i];
           String[][] names = namesArray[i];
           for(String[] name : names) {
               RouteRule.MatchPair matchPair = param.get(name[0]);
               if(matchPair == null) {
                   continue;
               }
               
               if(!matchPair.getMatches().isEmpty()) {
                   String m = RouteRule.join(matchPair.getMatches());
                   context.put(name[1], m);
               }
               if(!matchPair.getUnmatches().isEmpty()) {
                   String u = RouteRule.join(matchPair.getUnmatches());
                   context.put(name[2], u);
               }
           }
       }
       context.put("route", route);
       context.put("methods", CollectionUtils.sort(new ArrayList<String>(providerService.findMethodsByService(route.getService()))));
  	} catch (ParseException e) {
	e.printStackTrace();
}
  }
 
Example 6
Source File: RouteRuleUtilsTest.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
@Test
public void test_isMatchCondition()throws Exception {
    Map<String, RouteRule.MatchPair> condition = new HashMap<String, RouteRule.MatchPair>();
    Map<String, String> valueParams = new HashMap<String, String>();
    Map<String, String> kv = new HashMap<String, String>();
    
    boolean output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    {
        // key1
        Set<String> matches   = new HashSet<String>();
        matches.add("value1_1");
        matches.add("value1_2");
        matches.add("value1_3");
        Set<String> unmatches = new HashSet<String>();
        unmatches.add("v1_1");
        unmatches.add("v1_2");
        unmatches.add("v1_3");
        
        RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key1", p);
        
        // key2 
        matches   = new HashSet<String>();
        matches.add("value2_1");
        matches.add("value2_2");
        matches.add("value2_3");
        unmatches = new HashSet<String>();
        unmatches.add("v2_1");
        unmatches.add("v2_2");
        unmatches.add("v2_3");
        
        p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key2", p);
    }
    
    kv.put("kkk", "vvv");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "vvvXXX");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "value1_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key2", "value2_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    kv.put("key1", "v1_2");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
}
 
Example 7
Source File: Routes.java    From dubbox with Apache License 2.0 4 votes vote down vote up
/**
   * 显示路由详细信息
   * @param context
   */
  public void show(Map<String, Object> context) {
  	try {
       Route route = routeService.findRoute(Long.parseLong((String)context.get("id")));
       
       if (route == null) {
           throw new IllegalArgumentException("The route is not existed.");
       }
       if(route.getService()!=null && !route.getService().isEmpty()){
           context.put("service", route.getService());
       }
       
       RouteRule routeRule= RouteRule.parse(route);
       
       @SuppressWarnings("unchecked")
       Map<String, RouteRule.MatchPair>[] paramArray = new Map[] {
           routeRule.getWhenCondition(), routeRule.getThenCondition()};
       String[][][] namesArray = new String[][][] {when_names, then_names };
       
       for(int i=0; i<paramArray.length; ++i) {
           Map<String, RouteRule.MatchPair> param = paramArray[i];
           String[][] names = namesArray[i];
           for(String[] name : names) {
               RouteRule.MatchPair matchPair = param.get(name[0]);
               if(matchPair == null) {
                   continue;
               }
               
               if(!matchPair.getMatches().isEmpty()) {
                   String m = RouteRule.join(matchPair.getMatches());
                   context.put(name[1], m);
               }
               if(!matchPair.getUnmatches().isEmpty()) {
                   String u = RouteRule.join(matchPair.getUnmatches());
                   context.put(name[2], u);
               }
           }
       }
       context.put("route", route);
       context.put("methods", CollectionUtils.sort(new ArrayList<String>(providerService.findMethodsByService(route.getService()))));
  	} catch (ParseException e) {
	e.printStackTrace();
}
  }
 
Example 8
Source File: RouteRuleUtilsTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void test_isMatchCondition()throws Exception {
    Map<String, RouteRule.MatchPair> condition = new HashMap<String, RouteRule.MatchPair>();
    Map<String, String> valueParams = new HashMap<String, String>();
    Map<String, String> kv = new HashMap<String, String>();
    
    boolean output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    {
        // key1
        Set<String> matches   = new HashSet<String>();
        matches.add("value1_1");
        matches.add("value1_2");
        matches.add("value1_3");
        Set<String> unmatches = new HashSet<String>();
        unmatches.add("v1_1");
        unmatches.add("v1_2");
        unmatches.add("v1_3");
        
        RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key1", p);
        
        // key2 
        matches   = new HashSet<String>();
        matches.add("value2_1");
        matches.add("value2_2");
        matches.add("value2_3");
        unmatches = new HashSet<String>();
        unmatches.add("v2_1");
        unmatches.add("v2_2");
        unmatches.add("v2_3");
        
        p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key2", p);
    }
    
    kv.put("kkk", "vvv");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "vvvXXX");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "value1_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key2", "value2_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    kv.put("key1", "v1_2");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
}
 
Example 9
Source File: Routes.java    From dubbox with Apache License 2.0 4 votes vote down vote up
/**
   * 显示路由详细信息
   * @param context
   */
  public void show(Map<String, Object> context) {
  	try {
       Route route = routeService.findRoute(Long.parseLong((String)context.get("id")));
       
       if (route == null) {
           throw new IllegalArgumentException("The route is not existed.");
       }
       if(route.getService()!=null && !route.getService().isEmpty()){
           context.put("service", route.getService());
       }
       
       RouteRule routeRule= RouteRule.parse(route);
       
       @SuppressWarnings("unchecked")
       Map<String, RouteRule.MatchPair>[] paramArray = new Map[] {
           routeRule.getWhenCondition(), routeRule.getThenCondition()};
       String[][][] namesArray = new String[][][] {when_names, then_names };
       
       for(int i=0; i<paramArray.length; ++i) {
           Map<String, RouteRule.MatchPair> param = paramArray[i];
           String[][] names = namesArray[i];
           for(String[] name : names) {
               RouteRule.MatchPair matchPair = param.get(name[0]);
               if(matchPair == null) {
                   continue;
               }
               
               if(!matchPair.getMatches().isEmpty()) {
                   String m = RouteRule.join(matchPair.getMatches());
                   context.put(name[1], m);
               }
               if(!matchPair.getUnmatches().isEmpty()) {
                   String u = RouteRule.join(matchPair.getUnmatches());
                   context.put(name[2], u);
               }
           }
       }
       context.put("route", route);
       context.put("methods", CollectionUtils.sort(new ArrayList<String>(providerService.findMethodsByService(route.getService()))));
  	} catch (ParseException e) {
	e.printStackTrace();
}
  }
 
Example 10
Source File: RouteRuleUtilsTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void test_isMatchCondition()throws Exception {
    Map<String, RouteRule.MatchPair> condition = new HashMap<String, RouteRule.MatchPair>();
    Map<String, String> valueParams = new HashMap<String, String>();
    Map<String, String> kv = new HashMap<String, String>();
    
    boolean output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    {
        // key1
        Set<String> matches   = new HashSet<String>();
        matches.add("value1_1");
        matches.add("value1_2");
        matches.add("value1_3");
        Set<String> unmatches = new HashSet<String>();
        unmatches.add("v1_1");
        unmatches.add("v1_2");
        unmatches.add("v1_3");
        
        RouteRule.MatchPair p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key1", p);
        
        // key2 
        matches   = new HashSet<String>();
        matches.add("value2_1");
        matches.add("value2_2");
        matches.add("value2_3");
        unmatches = new HashSet<String>();
        unmatches.add("v2_1");
        unmatches.add("v2_2");
        unmatches.add("v2_3");
        
        p = new RouteRule.MatchPair(matches, unmatches);
        condition.put("key2", p);
    }
    
    kv.put("kkk", "vvv");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "vvvXXX");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key1", "value1_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
    
    kv.put("key2", "value2_1");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertTrue(output);
    
    kv.put("key1", "v1_2");
    output = RouteRuleUtils.isMatchCondition(condition, valueParams, kv);
    assertFalse(output);
}