log.error(e, "Uncaught exception in thread %s", t.getName());
}
});
Map<String, String> requiredProperties;
ConfigurationFactory configurationFactory;
if (requiredConfigurationProperties == null) {
// initialize configuration
log.info("Loading configuration");
ConfigurationLoader loader = new ConfigurationLoader();
requiredProperties = Collections.emptyMap();
String configFile = System.getProperty("config");
if (configFile != null) {
requiredProperties = loader.loadPropertiesFrom(configFile);
}
}
else {
requiredProperties = requiredConfigurationProperties;
}
SortedMap<String, String> properties = Maps.newTreeMap();
if (optionalConfigurationProperties != null) {
properties.putAll(optionalConfigurationProperties);
}
properties.putAll(requiredProperties);
properties.putAll(fromProperties(System.getProperties()));
properties = ImmutableSortedMap.copyOf(properties);
configurationFactory = new ConfigurationFactory(properties);
if (logging != null) {
// initialize logging
log.info("Initializing logging");
LoggingConfiguration configuration = configurationFactory.build(LoggingConfiguration.class);
logging.configure(configuration);
}
// create warning logger now that we have logging initialized
final WarningsMonitor warningsMonitor = new WarningsMonitor()
{
@Override
public void onWarning(String message)
{
log.warn(message);
}
};
// initialize configuration factory
for (Module module : modules) {
if (module instanceof ConfigurationAwareModule) {
ConfigurationAwareModule configurationAwareModule = (ConfigurationAwareModule) module;
configurationAwareModule.setConfigurationFactory(configurationFactory);
}
}
// Validate configuration
ConfigurationValidator configurationValidator = new ConfigurationValidator(configurationFactory, warningsMonitor);
List<Message> messages = configurationValidator.validate(modules);
// at this point all config file properties should be used
// so we can calculate the unused properties
final TreeMap<String, String> unusedProperties = Maps.newTreeMap();
unusedProperties.putAll(requiredProperties);
unusedProperties.keySet().removeAll(configurationFactory.getUsedProperties());
// Log effective configuration
if (!quiet) {
logConfiguration(configurationFactory, unusedProperties);
}