Skip to content

Commit

Permalink
[3.0] p2p invoke support wildcard url match. (#8247)
Browse files Browse the repository at this point in the history
* p2p invoke support wildcard url match.

* add license

* add more testcases.

Co-authored-by: Stefan <zjfan@trip.com>
  • Loading branch information
ZakiFan and Stefan authored Jul 13, 2021
1 parent dcdfe5a commit b4d72f9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.constants.CommonConstants;

import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Regex matching of keys is supported.
*/
public class RegexProperties extends Properties {

@Override
public String getProperty(String key) {
String value = super.getProperty(key);
if (value != null) {
return value;
}

// Sort the keys to solve the problem of matching priority.
List<String> sortedKeyList = keySet()
.stream()
.map(k -> (String) k)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());

String keyPattern = sortedKeyList
.stream()
.filter(k -> {
String matchingKey = k;
if(matchingKey.startsWith(CommonConstants.ANY_VALUE)){
matchingKey = CommonConstants.HIDE_KEY_PREFIX + matchingKey;
}
return Pattern.matches(matchingKey, key);
})
.findFirst()
.orElse(null);

return keyPattern == null ? null : super.getProperty(keyPattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.RegexProperties;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.support.Parameter;
Expand Down Expand Up @@ -263,7 +264,7 @@ protected void resolveFile() {
}
}
if (resolveFile != null && resolveFile.length() > 0) {
Properties properties = new Properties();
Properties properties = new RegexProperties();
try (FileInputStream fis = new FileInputStream(new File(resolveFile))) {
properties.load(fis);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RegexPropertiesTest {
@Test
public void testGetProperty(){
RegexProperties regexProperties = new RegexProperties();
regexProperties.setProperty("org.apache.dubbo.provider.*", "http://localhost:20880");
regexProperties.setProperty("org.apache.dubbo.provider.config.*", "http://localhost:30880");
regexProperties.setProperty("org.apache.dubbo.provider.config.demo", "http://localhost:40880");
regexProperties.setProperty("org.apache.dubbo.consumer.*.demo", "http://localhost:50880");
regexProperties.setProperty("*.service", "http://localhost:60880");

Assertions.assertEquals("http://localhost:20880", regexProperties.getProperty("org.apache.dubbo.provider.cluster"));
Assertions.assertEquals("http://localhost:30880", regexProperties.getProperty("org.apache.dubbo.provider.config.cluster"));
Assertions.assertEquals("http://localhost:40880", regexProperties.getProperty("org.apache.dubbo.provider.config.demo"));
Assertions.assertEquals("http://localhost:50880", regexProperties.getProperty("org.apache.dubbo.consumer.service.demo"));
Assertions.assertEquals("http://localhost:60880", regexProperties.getProperty("org.apache.dubbo.service"));
}
}

0 comments on commit b4d72f9

Please sign in to comment.