Skip to content

Commit 3b0fa6c

Browse files
committed
decide between strict / less strict property reading
this way we can force not-null / not-empty values for required properties, but also relax the conditions for optional properties
1 parent 72a3722 commit 3b0fa6c

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/main/java/de/rwth/idsg/steve/SteveConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ private SteveConfiguration() {
5050
.httpPort(p.getInt("http.port"))
5151
.httpsEnabled(p.getBoolean("https.enabled"))
5252
.httpsPort(p.getInt("https.port"))
53-
.keyStorePath(p.getString("keystore.path"))
54-
.keyStorePassword(p.getString("keystore.password"))
53+
.keyStorePath(p.getOptionalString("keystore.path"))
54+
.keyStorePassword(p.getOptionalString("keystore.password"))
5555
.build();
5656

5757
db = DB.builder()

src/main/java/de/rwth/idsg/steve/utils/PropertiesFileLoader.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.rwth.idsg.steve.utils;
22

3+
import com.google.common.base.Strings;
4+
35
import java.io.FileNotFoundException;
46
import java.io.InputStream;
57
import java.util.Properties;
@@ -26,15 +28,59 @@ public PropertiesFileLoader(String fileName) {
2628
}
2729
}
2830

31+
// -------------------------------------------------------------------------
32+
// Strict
33+
// -------------------------------------------------------------------------
34+
2935
public String getString(String key) {
30-
return prop.getProperty(key);
36+
String s = prop.getProperty(key);
37+
38+
if (s == null) {
39+
throw new IllegalArgumentException("The property '" + key + "' is not found");
40+
}
41+
42+
if (s.isEmpty()) {
43+
throw new IllegalArgumentException("The property '" + key + "' has no value set");
44+
}
45+
46+
return s;
3147
}
3248

3349
public boolean getBoolean(String key) {
34-
return Boolean.parseBoolean(prop.getProperty(key));
50+
return Boolean.parseBoolean(getString(key));
3551
}
3652

3753
public int getInt(String key) {
38-
return Integer.parseInt(prop.getProperty(key));
54+
return Integer.parseInt(getString(key));
55+
}
56+
57+
// -------------------------------------------------------------------------
58+
// Return null if not set
59+
// -------------------------------------------------------------------------
60+
61+
public String getOptionalString(String key) {
62+
String s = prop.getProperty(key);
63+
if (Strings.isNullOrEmpty(s)) {
64+
return null;
65+
}
66+
return s;
67+
}
68+
69+
public Boolean getOptionalBoolean(String key) {
70+
String s = getOptionalString(key);
71+
if (s == null) {
72+
return null;
73+
} else {
74+
return Boolean.parseBoolean(s);
75+
}
76+
}
77+
78+
public Integer getOptionalInt(String key) {
79+
String s = getOptionalString(key);
80+
if (s == null) {
81+
return null;
82+
} else {
83+
return Integer.parseInt(s);
84+
}
3985
}
4086
}

0 commit comments

Comments
 (0)