Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code review #3094

Merged
merged 4 commits into from
Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,36 @@
public interface Configurator extends Comparable<Configurator> {

/**
* get the configurator url.
* Get the configurator url.
*
* @return configurator url.
*/
URL getUrl();

/**
* Configure the provider url.
* O
*
* @param url - old rovider url.
* @param url - old provider url.
* @return new provider url.
*/
URL configure(URL url);


/**
* Convert override urls to map for use when re-refer.
* Send all rules every time, the urls will be reassembled and calculated
* Convert override urls to map for use when re-refer. Send all rules every time, the urls will be reassembled and
* calculated
*
* @param urls Contract:
* </br>1.override://0.0.0.0/...( or override://ip:port...?anyhost=true)&para1=value1... means global rules (all of the providers take effect)
* </br>2.override://ip:port...?anyhost=false Special rules (only for a certain provider)
* </br>3.override:// rule is not supported... ,needs to be calculated by registry itself.
* </br>4.override://0.0.0.0/ without parameters means clearing the override
* @return
* URL contract:
* <ol>
* <li>override://0.0.0.0/...( or override://ip:port...?anyhost=true)&para1=value1... means global rules
* (all of the providers take effect)</li>
* <li>override://ip:port...?anyhost=false Special rules (only for a certain provider)</li>
* <li>override:// rule is not supported... ,needs to be calculated by registry itself</li>
* <li>override://0.0.0.0/ without parameters means clearing the override</li>
* </ol>
*
* @param urls URL list to convert
* @return converted configurator list
*/
static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
if (CollectionUtils.isEmpty(urls)) {
Expand All @@ -70,13 +74,13 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
ConfiguratorFactory configuratorFactory = ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
.getAdaptiveExtension();

List<Configurator> configurators = new ArrayList<Configurator>(urls.size());
List<Configurator> configurators = new ArrayList<>(urls.size());
for (URL url : urls) {
if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
configurators.clear();
break;
}
Map<String, String> override = new HashMap<String, String>(url.getParameters());
Map<String, String> override = new HashMap<>(url.getParameters());
//The anyhost parameter of override may be added automatically, it can't change the judgement of changing url
override.remove(Constants.ANYHOST_KEY);
if (override.size() == 0) {
Expand All @@ -88,4 +92,25 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
Collections.sort(configurators);
return Optional.of(configurators);
}

/**
* Sort by host, then by priority
* 1. the url with a specific host ip should have higher priority than 0.0.0.0
* 2. if two url has the same host, compare by priority value;
*/
default int compareTo(Configurator o) {
if (o == null) {
return -1;
}

int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
// host is the same, sort by priority
if (ipCompare == 0) {
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0);
int j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
return Integer.compare(i, j);
} else {
return ipCompare;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,6 @@ private URL configureIfMatch(String host, URL url) {
return url;
}

/**
* Sort by host, priority
* 1. the url with a specific host ip should have higher priority than 0.0.0.0
* 2. if two url has the same host, compare by priority value;
*
* @param o
* @return
*/
@Override
public int compareTo(Configurator o) {
if (o == null) {
return -1;
}

int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
if (ipCompare == 0) {//host is the same, sort by priority
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0),
j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
return Integer.compare(i, j);
} else {
return ipCompare;
}


}

protected abstract URL doConfigure(URL currentUrl, URL configUrl);

}
Loading