Skip to content

Commit e76a377

Browse files
authored
allow the properties config to be extend (#3563)
* allow the properties config to be extend * add unit testcase for propertiesProviders * orgnize imports package of ProperteisConfiguration
1 parent adf4700 commit e76a377

File tree

7 files changed

+170
-4
lines changed

7 files changed

+170
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
20+
import org.apache.dubbo.common.extension.SPI;
21+
22+
import java.util.Properties;
23+
24+
@SPI
25+
public interface OrderedPropertiesProvider {
26+
/**
27+
* order
28+
*
29+
* @return
30+
*/
31+
int priority();
32+
33+
/**
34+
* load the properties
35+
*
36+
* @return
37+
*/
38+
Properties initProperties();
39+
}

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,47 @@
1616
*/
1717
package org.apache.dubbo.common.config;
1818

19-
import org.apache.dubbo.common.logger.Logger;
20-
import org.apache.dubbo.common.logger.LoggerFactory;
19+
import org.apache.dubbo.common.extension.ExtensionLoader;
2120
import org.apache.dubbo.common.utils.ConfigUtils;
2221

22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Properties;
25+
import java.util.Set;
26+
2327
/**
2428
* Configuration from system properties and dubbo.properties
2529
*/
2630
public class PropertiesConfiguration extends AbstractPrefixConfiguration {
27-
private static final Logger logger = LoggerFactory.getLogger(PropertiesConfiguration.class);
2831

2932
public PropertiesConfiguration(String prefix, String id) {
3033
super(prefix, id);
34+
35+
ExtensionLoader<OrderedPropertiesProvider> propertiesProviderExtensionLoader = ExtensionLoader.getExtensionLoader(OrderedPropertiesProvider.class);
36+
Set<String> propertiesProviderNames = propertiesProviderExtensionLoader.getSupportedExtensions();
37+
if (propertiesProviderNames == null || propertiesProviderNames.isEmpty()) {
38+
return;
39+
}
40+
List<OrderedPropertiesProvider> orderedPropertiesProviders = new ArrayList<>();
41+
for (String propertiesProviderName : propertiesProviderNames) {
42+
orderedPropertiesProviders.add(propertiesProviderExtensionLoader.getExtension(propertiesProviderName));
43+
}
44+
45+
//order the propertiesProvider according the priority descending
46+
orderedPropertiesProviders.sort((OrderedPropertiesProvider a, OrderedPropertiesProvider b) -> {
47+
return b.priority() - a.priority();
48+
});
49+
50+
//load the default properties
51+
Properties properties = ConfigUtils.getProperties();
52+
53+
//override the properties.
54+
for (OrderedPropertiesProvider orderedPropertiesProvider :
55+
orderedPropertiesProviders) {
56+
properties.putAll(orderedPropertiesProvider.initProperties());
57+
}
58+
59+
ConfigUtils.setProperties(properties);
3160
}
3261

3362
public PropertiesConfiguration() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 java.util.Properties;
20+
21+
public class MockOrderedPropertiesProvider1 implements OrderedPropertiesProvider {
22+
@Override
23+
public int priority() {
24+
return 3;
25+
}
26+
27+
@Override
28+
public Properties initProperties() {
29+
Properties properties = new Properties();
30+
properties.put("testKey", "333");
31+
return properties;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 java.util.Properties;
20+
21+
public class MockOrderedPropertiesProvider2 implements OrderedPropertiesProvider {
22+
@Override
23+
public int priority() {
24+
return 1;
25+
}
26+
27+
@Override
28+
public Properties initProperties() {
29+
Properties properties = new Properties();
30+
properties.put("testKey", "999");
31+
return properties;
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.junit.Assert;
20+
import org.junit.Test;
21+
22+
public class PropertiesConfigurationTest {
23+
24+
@Test
25+
public void testOrderPropertiesProviders() {
26+
PropertiesConfiguration configuration = new PropertiesConfiguration("test", null);
27+
Assert.assertTrue(configuration.getInternalProperty("testKey").equals("999"));
28+
}
29+
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mock1=org.apache.dubbo.common.config.MockOrderedPropertiesProvider1
2+
mock2=org.apache.dubbo.common.config.MockOrderedPropertiesProvider2

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/DynamicConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ default void addListener(String key, ConfigurationListener listener) {
4040
}
4141

4242

43-
/**
43+
/*
4444
* {@link #removeListener(String, String, ConfigurationListener)}
4545
*
4646
* @param key the key to represent a configuration

0 commit comments

Comments
 (0)