@@ -69,12 +69,19 @@ private static class Named extends Configuration {
69
69
70
70
@ Override
71
71
protected String determinePropertyFile () {
72
- final String propFile = System .getProperty ("Configuration." + name + ".rootFile" );
73
- if (propFile = = null ) {
74
- throw new ConfigurationException ( "No property file defined for named configuration " + name ) ;
72
+ String propFile = System .getProperty ("Configuration." + name + ".rootFile" );
73
+ if (propFile ! = null ) {
74
+ return propFile ;
75
75
}
76
76
77
- return propFile ;
77
+ // If Configuration.name.rootFile doesn't exist allow a fallback onto `name.rootFile`
78
+ propFile = System .getProperty (name + ".rootFile" );
79
+ if (propFile != null ) {
80
+ return propFile ;
81
+ }
82
+
83
+ throw new ConfigurationException ("No property file defined for named configuration " + name +
84
+ ": Make sure the property `Configuration." + name + ".rootFile` or `" + name + ".rootFile` is set" );
78
85
}
79
86
}
80
87
@@ -98,6 +105,7 @@ public static Configuration getInstance() {
98
105
* @throws ConfigurationException if the named configuration could not be loaded.
99
106
*/
100
107
public static Configuration getNamed (@ NotNull final String name ) throws ConfigurationException {
108
+ validateConfigName (name );
101
109
return NAMED_CONFIGURATIONS .computeIfAbsent (name , (k ) -> {
102
110
try {
103
111
return new Named (name , DefaultConfigurationContext ::new );
@@ -151,6 +159,7 @@ public static Configuration newStandaloneConfiguration(
151
159
* @return a new Configuration instance, guaranteed to not be cached.
152
160
*/
153
161
public static Configuration newStandaloneConfiguration (@ NotNull final String name ) {
162
+ validateConfigName (name );
154
163
return newStandaloneConfiguration (name , DefaultConfigurationContext ::new );
155
164
}
156
165
@@ -433,4 +442,19 @@ private static void writeLine(StringBuilder out, String sKey, String sLeftValue,
433
442
static boolean isQuiet () {
434
443
return Bootstrap .isQuiet () || System .getProperty (QUIET_PROPERTY ) != null ;
435
444
}
445
+
446
+ /**
447
+ * Check that the passed in config name is not null and is allowed.
448
+ *
449
+ * @param name the Configuration name.
450
+ */
451
+ static void validateConfigName (final String name ) {
452
+ if (name == null ) {
453
+ throw new ConfigurationException ("Configuration name must not be null" );
454
+ }
455
+
456
+ if ("Configuration" .equals (name )) {
457
+ throw new ConfigurationException ("The name `Configuration` may not be used as a Configuration name" );
458
+ }
459
+ }
436
460
}
0 commit comments