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

[DUBBO-3494]: Refactor URL to URLBuilder #3500

Merged
merged 6 commits into from
Mar 7, 2019
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 @@ -18,6 +18,7 @@

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.utils.IOUtils;
import org.apache.dubbo.rpc.cluster.Router;
import org.apache.dubbo.rpc.cluster.RouterFactory;
Expand Down Expand Up @@ -55,9 +56,12 @@ public Router getRouter(URL url) {

// FIXME: this code looks useless
boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false);
URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type)
URL script = URLBuilder.from(url)
.setProtocol(protocol)
.addParameter(Constants.TYPE_KEY, type)
.addParameter(Constants.RUNTIME_KEY, runtime)
.addParameterAndEncoded(Constants.RULE_KEY, rule);
.addParameterAndEncoded(Constants.RULE_KEY, rule)
.build();

return routerFactory.getRouter(script);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;

import org.apache.dubbo.common.URLBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -31,7 +32,8 @@ public void testMergeUrl() throws Exception {
.setUsername("username")
.setPassword("password");

providerURL = providerURL.addParameter(Constants.GROUP_KEY, "dubbo")
providerURL = URLBuilder.from(providerURL)
.addParameter(Constants.GROUP_KEY, "dubbo")
.addParameter(Constants.VERSION_KEY, "1.2.3")
.addParameter(Constants.DUBBO_VERSION_KEY, "2.3.7")
.addParameter(Constants.THREADPOOL_KEY, "fixed")
Expand All @@ -45,11 +47,13 @@ public void testMergeUrl() throws Exception {
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY, Integer.MAX_VALUE)
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY, Integer.MAX_VALUE)
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY, Integer.MAX_VALUE)
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test");
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test")
.build();

URL consumerURL = URL.valueOf("dubbo://localhost:55555");
consumerURL = consumerURL.addParameter(Constants.PID_KEY, "1234");
consumerURL = consumerURL.addParameter(Constants.THREADPOOL_KEY, "foo");
URL consumerURL = new URLBuilder(Constants.DUBBO_PROTOCOL, "localhost", 55555)
.addParameter(Constants.PID_KEY, "1234")
.addParameter(Constants.THREADPOOL_KEY, "foo")
.build();

URL url = ClusterUtils.mergeUrl(providerURL, consumerURL.getParameters());

Expand Down
40 changes: 20 additions & 20 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
* @see java.net.URL
* @see java.net.URI
*/
public /**final**/
public /*final**/
class URL implements Serializable {

private static final long serialVersionUID = -1985165475234910535L;
Expand Down Expand Up @@ -166,9 +166,9 @@ public URL(String protocol, String username, String password, String host, int p
}
this.path = path;
if (parameters == null) {
parameters = new HashMap<String, String>();
parameters = new HashMap<>();
} else {
parameters = new HashMap<String, String>(parameters);
parameters = new HashMap<>(parameters);
}
this.parameters = Collections.unmodifiableMap(parameters);
}
Expand All @@ -191,10 +191,10 @@ public static URL valueOf(String url) {
int port = 0;
String path = null;
Map<String, String> parameters = null;
int i = url.indexOf("?"); // seperator between body and parameters
int i = url.indexOf("?"); // separator between body and parameters
if (i >= 0) {
String[] parts = url.substring(i + 1).split("\\&");
parameters = new HashMap<String, String>();
String[] parts = url.substring(i + 1).split("&");
parameters = new HashMap<>();
for (String part : parts) {
part = part.trim();
if (part.length() > 0) {
Expand Down Expand Up @@ -265,7 +265,7 @@ public static URL valueOf(String url, String... reserveParams) {
if (reserveParams == null || reserveParams.length == 0) {
return result;
}
Map<String, String> newMap = new HashMap<String, String>(reserveParams.length);
Map<String, String> newMap = new HashMap<>(reserveParams.length);
Map<String, String> oldMap = result.getParameters();
for (String reserveParam : reserveParams) {
String tmp = oldMap.get(reserveParam);
Expand All @@ -277,7 +277,7 @@ public static URL valueOf(String url, String... reserveParams) {
}

public static URL valueOf(URL url, String[] reserveParams, String[] reserveParamPrefixs) {
Map<String, String> newMap = new HashMap<String, String>();
Map<String, String> newMap = new HashMap<>();
Map<String, String> oldMap = url.getParameters();
if (reserveParamPrefixs != null && reserveParamPrefixs.length != 0) {
for (Map.Entry<String, String> entry : oldMap.entrySet()) {
Expand Down Expand Up @@ -425,7 +425,7 @@ public String getBackupAddress(int defaultPort) {
}

public List<URL> getBackupUrls() {
List<URL> urls = new ArrayList<URL>();
List<URL> urls = new ArrayList<>();
urls.add(this);
String[] backups = getParameter(Constants.BACKUP_KEY, new String[0]);
if (backups != null && backups.length > 0) {
Expand Down Expand Up @@ -510,14 +510,14 @@ public List<String> getParameter(String key, List<String> defaultValue) {

private Map<String, Number> getNumbers() {
if (numbers == null) { // concurrent initialization is tolerant
numbers = new ConcurrentHashMap<String, Number>();
numbers = new ConcurrentHashMap<>();
}
return numbers;
}

private Map<String, URL> getUrls() {
if (urls == null) { // concurrent initialization is tolerant
urls = new ConcurrentHashMap<String, URL>();
urls = new ConcurrentHashMap<>();
}
return urls;
}
Expand Down Expand Up @@ -1004,7 +1004,7 @@ public URL addParameter(String key, String value) {
return this;
}

Map<String, String> map = new HashMap<String, String>(getParameters());
Map<String, String> map = new HashMap<>(getParameters());
map.put(key, value);
return new URL(protocol, username, password, host, port, path, map);
}
Expand All @@ -1017,7 +1017,7 @@ public URL addParameterIfAbsent(String key, String value) {
if (hasParameter(key)) {
return this;
}
Map<String, String> map = new HashMap<String, String>(getParameters());
Map<String, String> map = new HashMap<>(getParameters());
map.put(key, value);
return new URL(protocol, username, password, host, port, path, map);
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public URL addParameters(Map<String, String> parameters) {
return this;
}

Map<String, String> map = new HashMap<String, String>(getParameters());
Map<String, String> map = new HashMap<>(getParameters());
map.putAll(parameters);
return new URL(protocol, username, password, host, port, path, map);
}
Expand All @@ -1062,7 +1062,7 @@ public URL addParametersIfAbsent(Map<String, String> parameters) {
if (CollectionUtils.isEmptyMap(parameters)) {
return this;
}
Map<String, String> map = new HashMap<String, String>(parameters);
Map<String, String> map = new HashMap<>(parameters);
map.putAll(getParameters());
return new URL(protocol, username, password, host, port, path, map);
}
Expand All @@ -1074,7 +1074,7 @@ public URL addParameters(String... pairs) {
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("Map pairs can not be odd number.");
}
Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new HashMap<>();
int len = pairs.length / 2;
for (int i = 0; i < len; i++) {
map.put(pairs[2 * i], pairs[2 * i + 1]);
Expand Down Expand Up @@ -1107,7 +1107,7 @@ public URL removeParameters(String... keys) {
if (keys == null || keys.length == 0) {
return this;
}
Map<String, String> map = new HashMap<String, String>(getParameters());
Map<String, String> map = new HashMap<>(getParameters());
for (String key : keys) {
map.remove(key);
}
Expand All @@ -1118,7 +1118,7 @@ public URL removeParameters(String... keys) {
}

public URL clearParameters() {
return new URL(protocol, username, password, host, port, path, new HashMap<String, String>());
return new URL(protocol, username, password, host, port, path, new HashMap<>());
}

public String getRawParameter(String key) {
Expand All @@ -1144,7 +1144,7 @@ public String getRawParameter(String key) {
}

public Map<String, String> toMap() {
Map<String, String> map = new HashMap<String, String>(parameters);
Map<String, String> map = new HashMap<>(parameters);
if (protocol != null) {
map.put(Constants.PROTOCOL_KEY, protocol);
}
Expand Down Expand Up @@ -1217,7 +1217,7 @@ private void buildParameters(StringBuilder buf, boolean concat, String[] paramet
if (CollectionUtils.isNotEmptyMap(getParameters())) {
List<String> includes = (ArrayUtils.isEmpty(parameters) ? null : Arrays.asList(parameters));
boolean first = true;
for (Map.Entry<String, String> entry : new TreeMap<String, String>(getParameters()).entrySet()) {
for (Map.Entry<String, String> entry : new TreeMap<>(getParameters()).entrySet()) {
if (entry.getKey() != null && entry.getKey().length() > 0
&& (includes == null || includes.contains(entry.getKey()))) {
if (first) {
Expand Down
Loading