Skip to content

Commit 61a93f9

Browse files
Debug info
1 parent 4412290 commit 61a93f9

File tree

1 file changed

+62
-23
lines changed

1 file changed

+62
-23
lines changed

src/main/java/com/github/bordertech/config/DefaultConfiguration.java

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Set;
2929
import java.util.Stack;
3030
import java.util.StringTokenizer;
31+
import java.util.TreeSet;
3132
import org.apache.commons.configuration.Configuration;
3233
import org.apache.commons.configuration.ConversionException;
3334
import org.apache.commons.configuration.MapConfiguration;
@@ -123,6 +124,11 @@ public class DefaultConfiguration implements Configuration {
123124
@Deprecated
124125
private static final String LEGACY_SYSTEM_PARAMETERS_PREFIX = "bordertech.wcomponents.parameters.system.";
125126

127+
/**
128+
* The prefix output before log messages.
129+
*/
130+
private static final String LOG_PREFIX = "PARAM_DEBUG: ";
131+
126132
// -----------------------------------------------------------------------------------------------------------------
127133
// State used during loading of parameters
128134
/**
@@ -146,10 +152,11 @@ public class DefaultConfiguration implements Configuration {
146152
* Holds the current environment suffix (if set).
147153
*/
148154
private String currentEnvironment = null;
155+
149156
/**
150157
* Our backing store is a Map object.
151158
*/
152-
private Map<String, Object> backing;
159+
private Map<String, String> backing;
153160

154161
/**
155162
* Explicitly cache booleans for flag look-up speed.
@@ -282,9 +289,10 @@ private void load() {
282289
// Do nothing while loop
283290
} while (substitute());
284291

292+
log(getDumpHeader());
285293
if (isDumpProperties()) {
286-
log(getDebuggingInfo());
287-
log(getMessages());
294+
log(getDumpMessages());
295+
log(getDumpPropertyDetails());
288296
}
289297

290298
// We don't want the StringBuilder hanging around after 'DUMP'.
@@ -316,9 +324,9 @@ private boolean isDumpProperties() {
316324
}
317325

318326
/**
319-
* @return debugging information for logging on application start-up.
327+
* @return debugging information for logging
320328
*/
321-
private String getDebuggingInfo() {
329+
private String getDumpHeader() {
322330
File cwd = new File(".");
323331
String workingDir;
324332

@@ -335,25 +343,65 @@ private String getDebuggingInfo() {
335343
ProtectionDomain domain = getClass().getProtectionDomain();
336344
if (domain != null) {
337345
CodeSource codesource = domain.getCodeSource();
338-
codesourceStr = codesource == null ? "" : " code location of ConfigImpl: " + codesource.getLocation();
346+
codesourceStr = codesource == null ? "" : " code location of Config implementation: " + codesource.getLocation();
339347
}
340348
} catch (Exception failed) {
341-
codesourceStr = "Could not determine location of ConfigImpl [" + failed.getMessage() + "].";
349+
codesourceStr = "Could not determine location of Config implementation [" + failed.getMessage() + "].";
342350
}
343351

344352
StringBuilder info = new StringBuilder();
345353

346-
info.append("----Parameters start----");
354+
info.append("----Config: Info start----");
347355
info.append(codesourceStr);
348356
info.append("\nWorking directory is ");
349357
info.append(workingDir);
350-
info.append("\nTo dump all params to stdout set ");
358+
info.append("\nTo dump all params set ");
351359
info.append(DUMP);
352360
info.append(" to true; currently value is ");
353361
info.append(isDumpProperties());
354362
info.append("\nLOGGING can be controlled by configuring org.apache.commons.logging.impl.SimpleLog.");
355363
info.append("\nSimpleLog writes to System.err by default.");
356-
info.append("\n----Parameters end------");
364+
info.append("\n----Config: Info end------");
365+
366+
return info.toString();
367+
}
368+
369+
/**
370+
* @return dump of all properties loaded with their location history
371+
*/
372+
private String getDumpPropertyDetails() {
373+
374+
StringBuilder info = new StringBuilder();
375+
376+
info.append("----Config: Properties loaded start----\n");
377+
378+
for (String key : new TreeSet<>(backing.keySet())) {
379+
String value = backing.get(key);
380+
String history = locations.get(key);
381+
info.append(LOG_PREFIX);
382+
info.append(key);
383+
info.append(" = ");
384+
info.append(value);
385+
info.append(" (");
386+
info.append(history);
387+
info.append(")\n");
388+
}
389+
390+
info.append("----Config: Properties loaded end----\n");
391+
392+
return info.toString();
393+
}
394+
395+
/**
396+
* @return debugging load messages
397+
*/
398+
private String getDumpMessages() {
399+
400+
StringBuilder info = new StringBuilder();
401+
402+
info.append("----Config: Load messages start----\n");
403+
info.append(messages.toString());
404+
info.append("\n----Config: Load messages end----\n");
357405

358406
return info.toString();
359407
}
@@ -585,8 +633,7 @@ private ClassLoader getParamsClassLoader() {
585633
return loader;
586634
} else {
587635
// Rats - this should not happen with a sane application server
588-
recordMessage(
589-
"Whoa - is visible to context class loader, but it gives a different class");
636+
recordMessage("Whoa - is visible to context class loader, but it gives a different class");
590637
// If this happens we need to investigate further, but for the time being we'll use the context class
591638
// loader
592639
return loader;
@@ -715,13 +762,6 @@ private void recordMessage(final String msg) {
715762
messages.append(msg).append('\n');
716763
}
717764

718-
/**
719-
* @return the set of logged messages.
720-
*/
721-
private String getMessages() {
722-
return messages.toString();
723-
}
724-
725765
/**
726766
* Clears the logged message buffer.
727767
*/
@@ -1303,7 +1343,7 @@ public Properties getSubProperties(final String prefix, final boolean truncate)
13031343

13041344
int length = prefix.length();
13051345

1306-
for (Map.Entry<String, Object> entry : backing.entrySet()) {
1346+
for (Map.Entry<String, String> entry : backing.entrySet()) {
13071347

13081348
String key = entry.getKey();
13091349

@@ -1315,7 +1355,7 @@ public Properties getSubProperties(final String prefix, final boolean truncate)
13151355
newKey = key.substring(length);
13161356
}
13171357

1318-
sub.setProperty(newKey, (String) entry.getValue());
1358+
sub.setProperty(newKey, entry.getValue());
13191359
}
13201360
}
13211361

@@ -1396,8 +1436,7 @@ public void addOrModifyProperty(final String name, final String value) {
13961436
throw new IllegalArgumentException("value parameter can not be null.");
13971437
}
13981438

1399-
recordMessage(
1400-
"modifyProperties() - Adding property '" + name + "' with the value '" + value + "'.");
1439+
recordMessage("modifyProperties() - Adding property '" + name + "' with the value '" + value + "'.");
14011440

14021441
runtimeProperties.setProperty(name, value);
14031442

0 commit comments

Comments
 (0)