Skip to content

Commit b8827f9

Browse files
chickenljralf0131
authored andcommitted
[Dubbo-3367] Fail to parse config text with white space (#3590)
1 parent a8b28cf commit b8827f9

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ConfigurationUtils {
3838
public static int getServerShutdownTimeout() {
3939
int timeout = Constants.DEFAULT_SERVER_SHUTDOWN_TIMEOUT;
4040
Configuration configuration = Environment.getInstance().getConfiguration();
41-
String value = configuration.getString(Constants.SHUTDOWN_WAIT_KEY);
41+
String value = StringUtils.trim(configuration.getString(Constants.SHUTDOWN_WAIT_KEY));
4242

4343
if (value != null && value.length() > 0) {
4444
try {
@@ -47,7 +47,7 @@ public static int getServerShutdownTimeout() {
4747
// ignore
4848
}
4949
} else {
50-
value = configuration.getString(Constants.SHUTDOWN_WAIT_SECONDS_KEY);
50+
value = StringUtils.trim(configuration.getString(Constants.SHUTDOWN_WAIT_SECONDS_KEY));
5151
if (value != null && value.length() > 0) {
5252
try {
5353
timeout = Integer.parseInt(value) * 1000;
@@ -64,7 +64,7 @@ public static String getProperty(String property) {
6464
}
6565

6666
public static String getProperty(String property, String defaultValue) {
67-
return Environment.getInstance().getConfiguration().getString(property, defaultValue);
67+
return StringUtils.trim(Environment.getInstance().getConfiguration().getString(property, defaultValue));
6868
}
6969

7070
public static Map<String, String> parseProperties(String content) throws IOException {

dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,4 +789,8 @@ public static String toArgumentString(Object[] args) {
789789
}
790790
return buf.toString();
791791
}
792+
793+
public static String trim(String str) {
794+
return str == null ? null : str.trim();
795+
}
792796
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.common.config;
18+
19+
import org.apache.dubbo.common.Constants;
20+
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
*
26+
*/
27+
public class ConfigurationUtilsTest {
28+
29+
@Test
30+
public void testGetServerShutdownTimeout () {
31+
System.setProperty(Constants.SHUTDOWN_WAIT_KEY, " 10000");
32+
Assertions.assertEquals(10000, ConfigurationUtils.getServerShutdownTimeout());
33+
System.clearProperty(Constants.SHUTDOWN_WAIT_KEY);
34+
}
35+
36+
@Test
37+
public void testGetProperty () {
38+
System.setProperty(Constants.SHUTDOWN_WAIT_KEY, " 10000");
39+
Assertions.assertEquals("10000", ConfigurationUtils.getProperty(Constants.SHUTDOWN_WAIT_KEY));
40+
System.clearProperty(Constants.SHUTDOWN_WAIT_KEY);
41+
}
42+
}

dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,12 @@ public void testToArgumentString() throws Exception {
287287
assertThat(s, containsString("0,"));
288288
assertThat(s, containsString("{\"enabled\":true}"));
289289
}
290+
291+
@Test
292+
public void testTrim() {
293+
assertEquals("left blank", StringUtils.trim(" left blank"));
294+
assertEquals("right blank", StringUtils.trim("right blank "));
295+
assertEquals("bi-side blank", StringUtils.trim(" bi-side blank "));
296+
297+
}
290298
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public void refresh() {
557557
for (Method method : methods) {
558558
if (ClassHelper.isSetter(method)) {
559559
try {
560-
String value = compositeConfiguration.getString(extractPropertyName(getClass(), method));
560+
String value = StringUtils.trim(compositeConfiguration.getString(extractPropertyName(getClass(), method)));
561561
// isTypeMatch() is called to avoid duplicate and incorrect update, for example, we have two 'setGeneric' methods in ReferenceConfig.
562562
if (StringUtils.isNotEmpty(value) && ClassHelper.isTypeMatch(method.getParameterTypes()[0], value)) {
563563
method.invoke(this, ClassHelper.convertPrimitive(method.getParameterTypes()[0], value));

0 commit comments

Comments
 (0)