Skip to content

Commit 542ea09

Browse files
lhotarimerlimat
authored andcommitted
Migrate deprecated commons-configuration 1.x to commons-configuration 2.x (#4604)
* Migration to commons-configuration2 and commons-lang3 * Change test that breaks with commons configuration 2 * Add commons-beanutils dependency * Revert "Change test that breaks with commons configuration 2" This reverts commit 073c267. * Fix ConfigKeyTest * Configure list handling to match commons-configuration 1.x default behavior * Fix the issue in using the API to configure defaults * Configure list delimiter handler also for distributedlog's ConfigurationSubscription * Address issue with system properties being null * Use list delimiter handler in DistributedLogConfiguration too * Fix spotbugs check * Fix ConfigurationTest - system properties are read when ServerConfiguration gets initialized * Fix checkstyle
1 parent dd8eecc commit 542ea09

File tree

142 files changed

+594
-410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+594
-410
lines changed

bookkeeper-benchmark/src/main/java/org/apache/bookkeeper/benchmark/BenchBookie.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import org.apache.commons.cli.Options;
5050
import org.apache.commons.cli.ParseException;
5151
import org.apache.commons.cli.PosixParser;
52-
import org.apache.commons.lang.SystemUtils;
52+
import org.apache.commons.lang3.SystemUtils;
5353
import org.apache.zookeeper.KeeperException;
5454
import org.slf4j.Logger;
5555
import org.slf4j.LoggerFactory;

bookkeeper-common/pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
<groupId>org.jctools</groupId>
6060
<artifactId>jctools-core</artifactId>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.apache.commons</groupId>
64+
<artifactId>commons-lang3</artifactId>
65+
</dependency>
6266
<dependency>
6367
<groupId>io.netty.incubator</groupId>
6468
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
@@ -90,11 +94,6 @@
9094
<version>${project.parent.version}</version>
9195
<scope>test</scope>
9296
</dependency>
93-
<dependency>
94-
<groupId>org.apache.commons</groupId>
95-
<artifactId>commons-lang3</artifactId>
96-
<scope>test</scope>
97-
</dependency>
9897
<dependency>
9998
<groupId>org.awaitility</groupId>
10099
<artifactId>awaitility</artifactId>

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/conf/ComponentConfiguration.java

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@
2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
2323
import java.net.URL;
24+
import java.time.Duration;
25+
import java.util.Collection;
2426
import java.util.HashMap;
2527
import java.util.Iterator;
2628
import java.util.List;
2729
import java.util.Map;
2830
import java.util.Properties;
2931
import org.apache.bookkeeper.common.util.JsonUtil;
3032
import org.apache.bookkeeper.common.util.JsonUtil.ParseJsonException;
31-
import org.apache.commons.configuration.CompositeConfiguration;
32-
import org.apache.commons.configuration.Configuration;
33-
import org.apache.commons.configuration.ConfigurationException;
34-
import org.apache.commons.configuration.PropertiesConfiguration;
33+
import org.apache.commons.configuration2.CompositeConfiguration;
34+
import org.apache.commons.configuration2.Configuration;
35+
import org.apache.commons.configuration2.ConfigurationDecoder;
36+
import org.apache.commons.configuration2.ImmutableConfiguration;
37+
import org.apache.commons.configuration2.ex.ConfigurationException;
38+
import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
39+
import org.apache.commons.configuration2.interpol.Lookup;
40+
import org.apache.commons.configuration2.sync.LockMode;
41+
import org.apache.commons.configuration2.sync.Synchronizer;
3542

3643
/**
3744
* Component Configuration.
@@ -70,7 +77,7 @@ public CompositeConfiguration getUnderlyingConf() {
7077
* @throws ConfigurationException when failed to load configuration.
7178
*/
7279
public void loadConf(URL confURL) throws ConfigurationException {
73-
Configuration loadedConf = new PropertiesConfiguration(confURL);
80+
Configuration loadedConf = ConfigurationUtil.newConfiguration(conf -> conf.propertiesBuilder(confURL));
7481
loadConf(loadedConf);
7582
}
7683

@@ -292,6 +299,128 @@ public List<Object> getList(String key, List<?> defaultValue) {
292299
return conf.getList(getKeyName(key), defaultValue);
293300
}
294301

302+
@Override
303+
public ConfigurationInterpolator getInterpolator() {
304+
return conf.getInterpolator();
305+
}
306+
307+
@Override
308+
public void installInterpolator(Map<String, ? extends Lookup> prefixLookups,
309+
Collection<? extends Lookup> defLookups) {
310+
conf.installInterpolator(prefixLookups, defLookups);
311+
}
312+
313+
@Override
314+
public void setInterpolator(ConfigurationInterpolator ci) {
315+
conf.setInterpolator(ci);
316+
}
317+
318+
@Override
319+
public <T> T get(Class<T> cls, String key) {
320+
return conf.get(cls, key);
321+
}
322+
323+
@Override
324+
public <T> T get(Class<T> cls, String key, T defaultValue) {
325+
return conf.get(cls, key, defaultValue);
326+
}
327+
328+
@Override
329+
public Object getArray(Class<?> cls, String key) {
330+
return conf.getArray(cls, key);
331+
}
332+
333+
@Override
334+
@Deprecated
335+
public Object getArray(Class<?> cls, String key, Object defaultValue) {
336+
return conf.getArray(cls, key, defaultValue);
337+
}
338+
339+
@Override
340+
public <T> Collection<T> getCollection(Class<T> cls, String key, Collection<T> target) {
341+
return conf.getCollection(cls, key, target);
342+
}
343+
344+
@Override
345+
public <T> Collection<T> getCollection(Class<T> cls, String key, Collection<T> target, Collection<T> defaultValue) {
346+
return conf.getCollection(cls, key, target, defaultValue);
347+
}
348+
349+
@Override
350+
public Duration getDuration(String key) {
351+
return conf.getDuration(key);
352+
}
353+
354+
@Override
355+
public Duration getDuration(String key, Duration defaultValue) {
356+
return conf.getDuration(key, defaultValue);
357+
}
358+
359+
@Override
360+
public String getEncodedString(String key) {
361+
return conf.getEncodedString(key);
362+
}
363+
364+
@Override
365+
public String getEncodedString(String key, ConfigurationDecoder decoder) {
366+
return conf.getEncodedString(key, decoder);
367+
}
368+
369+
@Override
370+
public <T extends Enum<T>> T getEnum(String key, Class<T> enumType) {
371+
return conf.getEnum(key, enumType);
372+
}
373+
374+
@Override
375+
public <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultValue) {
376+
return conf.getEnum(key, enumType, defaultValue);
377+
}
378+
379+
@Override
380+
public Iterator<String> getKeys(String prefix, String delimiter) {
381+
return conf.getKeys(prefix, delimiter);
382+
}
383+
384+
@Override
385+
public <T> List<T> getList(Class<T> cls, String key) {
386+
return conf.getList(cls, key);
387+
}
388+
389+
@Override
390+
public <T> List<T> getList(Class<T> cls, String key, List<T> defaultValue) {
391+
return conf.getList(cls, key, defaultValue);
392+
}
393+
394+
@Override
395+
public ImmutableConfiguration immutableSubset(String prefix) {
396+
return conf.immutableSubset(prefix);
397+
}
398+
399+
@Override
400+
public int size() {
401+
return conf.size();
402+
}
403+
404+
@Override
405+
public Synchronizer getSynchronizer() {
406+
return conf.getSynchronizer();
407+
}
408+
409+
@Override
410+
public void lock(LockMode mode) {
411+
conf.lock(mode);
412+
}
413+
414+
@Override
415+
public void setSynchronizer(Synchronizer sync) {
416+
conf.setSynchronizer(sync);
417+
}
418+
419+
@Override
420+
public void unlock(LockMode mode) {
421+
conf.unlock(mode);
422+
}
423+
295424
/**
296425
* returns the string representation of json format of this config.
297426
*

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/conf/ConcurrentConfiguration.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import java.util.Iterator;
2424
import java.util.concurrent.ConcurrentHashMap;
2525
import java.util.concurrent.ConcurrentMap;
26-
import org.apache.commons.configuration.AbstractConfiguration;
26+
import org.apache.commons.configuration2.AbstractConfiguration;
27+
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
2728

2829
/**
2930
* Configuration view built on concurrent hash map for fast thread-safe access.
@@ -38,6 +39,7 @@ public class ConcurrentConfiguration extends AbstractConfiguration {
3839
private final ConcurrentMap<String, Object> map;
3940

4041
public ConcurrentConfiguration() {
42+
setListDelimiterHandler(new DefaultListDelimiterHandler(','));
4143
this.map = new ConcurrentHashMap<>();
4244
}
4345

@@ -48,22 +50,27 @@ protected void addPropertyDirect(String key, Object value) {
4850
}
4951

5052
@Override
51-
public Object getProperty(String key) {
53+
public Object getPropertyInternal(String key) {
5254
return map.get(key);
5355
}
5456

5557
@Override
56-
public Iterator getKeys() {
58+
public Iterator getKeysInternal() {
5759
return map.keySet().iterator();
5860
}
5961

6062
@Override
61-
public boolean containsKey(String key) {
63+
public boolean containsKeyInternal(String key) {
6264
return map.containsKey(key);
6365
}
6466

6567
@Override
66-
public boolean isEmpty() {
68+
protected boolean containsValueInternal(Object o) {
69+
return map.containsValue(o);
70+
}
71+
72+
@Override
73+
public boolean isEmptyInternal() {
6774
return map.isEmpty();
6875
}
6976

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/conf/ConfigDef.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import java.util.stream.IntStream;
4141
import lombok.Getter;
4242
import lombok.extern.slf4j.Slf4j;
43-
import org.apache.commons.configuration.Configuration;
44-
import org.apache.commons.lang.StringUtils;
43+
import org.apache.commons.configuration2.Configuration;
44+
import org.apache.commons.lang3.StringUtils;
4545

4646
/**
4747
* A definition of a configuration instance.

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/conf/ConfigKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import org.apache.bookkeeper.common.annotation.InterfaceAudience.Public;
3434
import org.apache.bookkeeper.common.conf.validators.NullValidator;
3535
import org.apache.bookkeeper.common.util.ReflectionUtils;
36-
import org.apache.commons.configuration.Configuration;
37-
import org.apache.commons.configuration.ConfigurationException;
36+
import org.apache.commons.configuration2.Configuration;
37+
import org.apache.commons.configuration2.ex.ConfigurationException;
3838

3939
/**
4040
* A configuration key in a configuration.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.bookkeeper.common.conf;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.Properties;
24+
import java.util.function.Function;
25+
import org.apache.commons.configuration2.Configuration;
26+
import org.apache.commons.configuration2.MapConfiguration;
27+
import org.apache.commons.configuration2.PropertiesConfiguration;
28+
import org.apache.commons.configuration2.builder.BasicBuilderParameters;
29+
import org.apache.commons.configuration2.builder.DefaultParametersManager;
30+
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
31+
import org.apache.commons.configuration2.builder.fluent.Configurations;
32+
import org.apache.commons.configuration2.builder.fluent.Parameters;
33+
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
34+
import org.apache.commons.configuration2.ex.ConfigurationException;
35+
36+
public class ConfigurationUtil {
37+
/**
38+
* Create a new PropertiesConfiguration using the given builder function.
39+
* The purpose of this method is to configure the list handling to behave in the same way as in
40+
* commons-configuration 1.x. without duplicating the code in multiple places.
41+
*
42+
* @param builderFunction a function that takes a Configurations object and returns a FileBasedConfigurationBuilder
43+
* @return a new PropertiesConfiguration
44+
* @throws ConfigurationException if there is an error creating the configuration
45+
*/
46+
public static PropertiesConfiguration newConfiguration(
47+
Function<Configurations, FileBasedConfigurationBuilder<PropertiesConfiguration>> builderFunction)
48+
throws ConfigurationException {
49+
// configure defaults
50+
DefaultParametersManager parametersManager = new DefaultParametersManager();
51+
parametersManager.registerDefaultsHandler(BasicBuilderParameters.class, basicBuilderParameters -> {
52+
// configure list handling to behave in the same way as in commons-configuration 1.x
53+
basicBuilderParameters.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
54+
});
55+
Parameters params = new Parameters(parametersManager);
56+
Configurations configurations = new Configurations(params);
57+
// create a new builder using the provided function
58+
FileBasedConfigurationBuilder<PropertiesConfiguration> builder = builderFunction.apply(configurations);
59+
// build the configuration
60+
return builder.getConfiguration();
61+
}
62+
63+
/**
64+
* Read system properties as a Configuration. Where a copy of the system properties is made
65+
* so that mutations to the Configuration do not affect the system properties.
66+
*
67+
* @return a Configuration object containing the system properties
68+
*/
69+
public static Configuration readSystemPropertiesAsConfiguration() {
70+
Properties properties = System.getProperties();
71+
Map<String, Object> propertiesMap = new HashMap<String, Object>();
72+
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
73+
propertiesMap.put(entry.getKey().toString(), entry.getValue());
74+
}
75+
return new MapConfiguration(propertiesMap);
76+
}
77+
}

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/net/ServiceURI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import lombok.RequiredArgsConstructor;
3535
import org.apache.bookkeeper.common.annotation.InterfaceAudience.Public;
3636
import org.apache.bookkeeper.common.annotation.InterfaceStability.Evolving;
37-
import org.apache.commons.lang.StringUtils;
37+
import org.apache.commons.lang3.StringUtils;
3838

3939
/**
4040
* ServiceURI represents service uri within bookkeeper cluster.

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/util/OrderedExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.apache.bookkeeper.stats.OpStatsLogger;
4646
import org.apache.bookkeeper.stats.StatsLogger;
4747
import org.apache.bookkeeper.stats.ThreadRegistry;
48-
import org.apache.commons.lang.StringUtils;
48+
import org.apache.commons.lang3.StringUtils;
4949
import org.slf4j.MDC;
5050

5151
/**

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/util/ReflectionUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.lang.reflect.Constructor;
2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
24-
import org.apache.commons.configuration.Configuration;
25-
import org.apache.commons.configuration.ConfigurationException;
24+
import org.apache.commons.configuration2.Configuration;
25+
import org.apache.commons.configuration2.ex.ConfigurationException;
2626

2727
/**
2828
* General Class Reflection Utils.

0 commit comments

Comments
 (0)