Skip to content

Commit

Permalink
DUBBO-410 动态配置优化,支持~前缀
Browse files Browse the repository at this point in the history
git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@1842 1a56cb94-b969-4eaa-88fa-be21384802f2
  • Loading branch information
william.liangf committed May 23, 2012
1 parent d2a89fe commit 84644d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package com.alibaba.dubbo.rpc.cluster.configurator;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.cluster.Configurator;
Expand All @@ -40,8 +44,33 @@ public URL getUrl() {
}

public URL configure(URL url) {
if (isMatch(getUrl(), url)) {
return doConfigure(url);
if (configuratorUrl == null || configuratorUrl.getHost() == null
|| url == null || url.getHost() == null) {
return url;
}
if (Constants.ANYHOST_VALUE.equals(configuratorUrl.getHost())
|| url.getHost().equals(configuratorUrl.getHost())) {
String configApplication = configuratorUrl.getParameter(Constants.APPLICATION_KEY, configuratorUrl.getUsername());
String currentApplication = url.getParameter(Constants.APPLICATION_KEY, url.getUsername());
if (configApplication == null || Constants.ANY_VALUE.equals(configApplication)
|| configApplication.equals(currentApplication)) {
if (configuratorUrl.getPort() == 0 || url.getPort() == configuratorUrl.getPort()) {
Set<String> condtionKeys = new HashSet<String>();
for (Map.Entry<String, String> entry : configuratorUrl.getParameters().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.startsWith("~") || Constants.APPLICATION_KEY.equals(key)
|| Constants.SIDE_KEY.equals(key)) {
condtionKeys.add(key);
if (value != null && ! Constants.ANY_VALUE.equals(value)
&& ! value.equals(url.getParameter(key.startsWith("~") ? key.substring(1) : key))) {
return url;
}
}
}
return doConfigure(url, configuratorUrl.removeParameters(condtionKeys));
}
}
}
return url;
}
Expand All @@ -52,31 +81,11 @@ public int compareTo(Configurator o) {
}
return getUrl().getHost().compareTo(o.getUrl().getHost());
}

private boolean isMatch(URL configuratorUrl, URL providerUrl) {
if (configuratorUrl == null || configuratorUrl.getHost() == null
|| providerUrl == null || providerUrl.getHost() == null) {
return false;
}
/*if (! providerUrl.getServiceKey().equals(configuratorUrl.getServiceKey())) {
return false;
}*/
if (Constants.ANYHOST_VALUE.equals(configuratorUrl.getHost())
|| providerUrl.getHost().equals(configuratorUrl.getHost())) {
String configApplication = configuratorUrl.getParameter(Constants.APPLICATION_KEY, configuratorUrl.getUsername());
String providerApplication = providerUrl.getParameter(Constants.APPLICATION_KEY, providerUrl.getUsername());
if (configApplication == null || Constants.ANY_VALUE.equals(configApplication)
|| configApplication.equals(providerApplication)) {
if (configuratorUrl.getPort() > 0) {
return providerUrl.getPort() == configuratorUrl.getPort();
} else {
return true;
}
}
}
return false;
}

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

public static void main(String[] args) {
System.out.println(URL.encode("timeout=100"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public AbsentConfigurator(URL url) {
super(url);
}

public URL doConfigure(URL url) {
return url.addParametersIfAbsent(getUrl().getParameters());
public URL doConfigure(URL currentUrl, URL configUrl) {
return currentUrl.addParametersIfAbsent(configUrl.getParameters());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public OverrideConfigurator(URL url) {
super(url);
}

public URL doConfigure(URL providerUrl) {
return providerUrl.addParameters(getUrl().getParameters());
public URL doConfigure(URL currentUrl, URL configUrl) {
return currentUrl.addParameters(configUrl.getParameters());
}

}

0 comments on commit 84644d0

Please sign in to comment.