Skip to content

Commit d414c7b

Browse files
kezhenxu94beiwei30
authored andcommitted
[DUBBO-3494]: Refactor URL to URLBuilder (#3500)
* refactor URL to URLBuilder. #3494 * remove unrelated changes * replace more with URLBuilder * fix ci failure * remove unnecessary comment
1 parent fe049b8 commit d414c7b

File tree

23 files changed

+657
-142
lines changed

23 files changed

+657
-142
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.dubbo.common.Constants;
2020
import org.apache.dubbo.common.URL;
21+
import org.apache.dubbo.common.URLBuilder;
2122
import org.apache.dubbo.common.utils.IOUtils;
2223
import org.apache.dubbo.rpc.cluster.Router;
2324
import org.apache.dubbo.rpc.cluster.RouterFactory;
@@ -55,9 +56,12 @@ public Router getRouter(URL url) {
5556

5657
// FIXME: this code looks useless
5758
boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false);
58-
URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type)
59+
URL script = URLBuilder.from(url)
60+
.setProtocol(protocol)
61+
.addParameter(Constants.TYPE_KEY, type)
5962
.addParameter(Constants.RUNTIME_KEY, runtime)
60-
.addParameterAndEncoded(Constants.RULE_KEY, rule);
63+
.addParameterAndEncoded(Constants.RULE_KEY, rule)
64+
.build();
6165

6266
return routerFactory.getRouter(script);
6367
} catch (IOException e) {

dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.dubbo.common.Constants;
2020
import org.apache.dubbo.common.URL;
2121

22+
import org.apache.dubbo.common.URLBuilder;
2223
import org.junit.jupiter.api.Assertions;
2324
import org.junit.jupiter.api.Test;
2425

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

34-
providerURL = providerURL.addParameter(Constants.GROUP_KEY, "dubbo")
35+
providerURL = URLBuilder.from(providerURL)
36+
.addParameter(Constants.GROUP_KEY, "dubbo")
3537
.addParameter(Constants.VERSION_KEY, "1.2.3")
3638
.addParameter(Constants.DUBBO_VERSION_KEY, "2.3.7")
3739
.addParameter(Constants.THREADPOOL_KEY, "fixed")
@@ -45,11 +47,13 @@ public void testMergeUrl() throws Exception {
4547
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY, Integer.MAX_VALUE)
4648
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY, Integer.MAX_VALUE)
4749
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY, Integer.MAX_VALUE)
48-
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test");
50+
.addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test")
51+
.build();
4952

50-
URL consumerURL = URL.valueOf("dubbo://localhost:55555");
51-
consumerURL = consumerURL.addParameter(Constants.PID_KEY, "1234");
52-
consumerURL = consumerURL.addParameter(Constants.THREADPOOL_KEY, "foo");
53+
URL consumerURL = new URLBuilder(Constants.DUBBO_PROTOCOL, "localhost", 55555)
54+
.addParameter(Constants.PID_KEY, "1234")
55+
.addParameter(Constants.THREADPOOL_KEY, "foo")
56+
.build();
5357

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

dubbo-common/src/main/java/org/apache/dubbo/common/URL.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
* @see java.net.URL
7272
* @see java.net.URI
7373
*/
74-
public /**final**/
74+
public /*final**/
7575
class URL implements Serializable {
7676

7777
private static final long serialVersionUID = -1985165475234910535L;
@@ -166,9 +166,9 @@ public URL(String protocol, String username, String password, String host, int p
166166
}
167167
this.path = path;
168168
if (parameters == null) {
169-
parameters = new HashMap<String, String>();
169+
parameters = new HashMap<>();
170170
} else {
171-
parameters = new HashMap<String, String>(parameters);
171+
parameters = new HashMap<>(parameters);
172172
}
173173
this.parameters = Collections.unmodifiableMap(parameters);
174174
}
@@ -191,10 +191,10 @@ public static URL valueOf(String url) {
191191
int port = 0;
192192
String path = null;
193193
Map<String, String> parameters = null;
194-
int i = url.indexOf("?"); // seperator between body and parameters
194+
int i = url.indexOf("?"); // separator between body and parameters
195195
if (i >= 0) {
196-
String[] parts = url.substring(i + 1).split("\\&");
197-
parameters = new HashMap<String, String>();
196+
String[] parts = url.substring(i + 1).split("&");
197+
parameters = new HashMap<>();
198198
for (String part : parts) {
199199
part = part.trim();
200200
if (part.length() > 0) {
@@ -265,7 +265,7 @@ public static URL valueOf(String url, String... reserveParams) {
265265
if (reserveParams == null || reserveParams.length == 0) {
266266
return result;
267267
}
268-
Map<String, String> newMap = new HashMap<String, String>(reserveParams.length);
268+
Map<String, String> newMap = new HashMap<>(reserveParams.length);
269269
Map<String, String> oldMap = result.getParameters();
270270
for (String reserveParam : reserveParams) {
271271
String tmp = oldMap.get(reserveParam);
@@ -277,7 +277,7 @@ public static URL valueOf(String url, String... reserveParams) {
277277
}
278278

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

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

511511
private Map<String, Number> getNumbers() {
512512
if (numbers == null) { // concurrent initialization is tolerant
513-
numbers = new ConcurrentHashMap<String, Number>();
513+
numbers = new ConcurrentHashMap<>();
514514
}
515515
return numbers;
516516
}
517517

518518
private Map<String, URL> getUrls() {
519519
if (urls == null) { // concurrent initialization is tolerant
520-
urls = new ConcurrentHashMap<String, URL>();
520+
urls = new ConcurrentHashMap<>();
521521
}
522522
return urls;
523523
}
@@ -1004,7 +1004,7 @@ public URL addParameter(String key, String value) {
10041004
return this;
10051005
}
10061006

1007-
Map<String, String> map = new HashMap<String, String>(getParameters());
1007+
Map<String, String> map = new HashMap<>(getParameters());
10081008
map.put(key, value);
10091009
return new URL(protocol, username, password, host, port, path, map);
10101010
}
@@ -1017,7 +1017,7 @@ public URL addParameterIfAbsent(String key, String value) {
10171017
if (hasParameter(key)) {
10181018
return this;
10191019
}
1020-
Map<String, String> map = new HashMap<String, String>(getParameters());
1020+
Map<String, String> map = new HashMap<>(getParameters());
10211021
map.put(key, value);
10221022
return new URL(protocol, username, password, host, port, path, map);
10231023
}
@@ -1053,7 +1053,7 @@ public URL addParameters(Map<String, String> parameters) {
10531053
return this;
10541054
}
10551055

1056-
Map<String, String> map = new HashMap<String, String>(getParameters());
1056+
Map<String, String> map = new HashMap<>(getParameters());
10571057
map.putAll(parameters);
10581058
return new URL(protocol, username, password, host, port, path, map);
10591059
}
@@ -1062,7 +1062,7 @@ public URL addParametersIfAbsent(Map<String, String> parameters) {
10621062
if (CollectionUtils.isEmptyMap(parameters)) {
10631063
return this;
10641064
}
1065-
Map<String, String> map = new HashMap<String, String>(parameters);
1065+
Map<String, String> map = new HashMap<>(parameters);
10661066
map.putAll(getParameters());
10671067
return new URL(protocol, username, password, host, port, path, map);
10681068
}
@@ -1074,7 +1074,7 @@ public URL addParameters(String... pairs) {
10741074
if (pairs.length % 2 != 0) {
10751075
throw new IllegalArgumentException("Map pairs can not be odd number.");
10761076
}
1077-
Map<String, String> map = new HashMap<String, String>();
1077+
Map<String, String> map = new HashMap<>();
10781078
int len = pairs.length / 2;
10791079
for (int i = 0; i < len; i++) {
10801080
map.put(pairs[2 * i], pairs[2 * i + 1]);
@@ -1107,7 +1107,7 @@ public URL removeParameters(String... keys) {
11071107
if (keys == null || keys.length == 0) {
11081108
return this;
11091109
}
1110-
Map<String, String> map = new HashMap<String, String>(getParameters());
1110+
Map<String, String> map = new HashMap<>(getParameters());
11111111
for (String key : keys) {
11121112
map.remove(key);
11131113
}
@@ -1118,7 +1118,7 @@ public URL removeParameters(String... keys) {
11181118
}
11191119

11201120
public URL clearParameters() {
1121-
return new URL(protocol, username, password, host, port, path, new HashMap<String, String>());
1121+
return new URL(protocol, username, password, host, port, path, new HashMap<>());
11221122
}
11231123

11241124
public String getRawParameter(String key) {
@@ -1144,7 +1144,7 @@ public String getRawParameter(String key) {
11441144
}
11451145

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

0 commit comments

Comments
 (0)