44import lombok .NonNull ;
55import lombok .RequiredArgsConstructor ;
66import lombok .SneakyThrows ;
7- import org .apache .commons .configuration2 .PropertiesConfiguration ;
8- import org .apache .commons .configuration2 .builder .fluent .Configurations ;
97
8+ import java .io .InputStream ;
109import java .net .URL ;
1110import java .util .ArrayList ;
12- import java .util .Iterator ;
1311import java .util .List ;
12+ import java .util .Properties ;
13+ import java .util .Set ;
1414
1515@ RequiredArgsConstructor
1616@ Data
1717public class Configuration {
1818
1919 private final @ NonNull String prefix ;
20- private final PropertiesConfiguration properties ;
20+ private final Properties properties ;
2121
2222 private String envKey (@ NonNull String key ) {
2323 return (prefix + "_" + key ).toUpperCase ().replace ('.' , '_' );
@@ -33,29 +33,41 @@ private String resolve(@NonNull String key) {
3333 if (env != null ) {
3434 return env ;
3535 }
36- return properties .getString (prefix + "." + key , null );
36+ return properties .getProperty (prefix + "." + key );
3737 }
3838
3939 @ SneakyThrows
4040 public Configuration (@ NonNull String prefix , @ NonNull URL fileUrl ) {
4141 this .prefix = prefix ;
42- this .properties = new Configurations ().properties (fileUrl );
42+ this .properties = new Properties ();
43+ try (InputStream in = fileUrl .openStream ()) {
44+ if (in != null ) {
45+ this .properties .load (in );
46+ }
47+ }
4348 }
4449
4550 @ SneakyThrows
4651 public Configuration (@ NonNull String prefix ) {
4752 this .prefix = prefix ;
48- this .properties = new Configurations ().properties (getClass ().getResource ("/app.properties" ));
53+ this .properties = new Properties ();
54+ URL resource = getClass ().getResource ("/app.properties" );
55+ if (resource != null ) {
56+ try (InputStream in = resource .openStream ()) {
57+ if (in != null ) {
58+ this .properties .load (in );
59+ }
60+ }
61+ }
4962 }
5063
5164 public List <String > keys () {
5265 List <String > result = new ArrayList <>();
53- Iterator <String > keysIterator = properties .getKeys (prefix );
54- while (keysIterator .hasNext ()) {
55- String key = keysIterator .next ();
56- if (key .startsWith (prefix + "." )) {
57- String strippedKey = key .substring (prefix .length () + 1 ); // +1 for the dot
58- result .add (strippedKey );
66+ Set <String > names = properties .stringPropertyNames ();
67+ String prefixDot = prefix + "." ;
68+ for (String key : names ) {
69+ if (key .startsWith (prefixDot )) {
70+ result .add (key .substring (prefixDot .length ()));
5971 }
6072 }
6173 return result ;
@@ -109,4 +121,4 @@ public boolean getBoolean(@NonNull String key, boolean defaultValue) {
109121 String value = resolve (key );
110122 return value != null ? Boolean .parseBoolean (value ) : defaultValue ;
111123 }
112- }
124+ }
0 commit comments