* @return a Guice module configured with setting support.
* @throws ConfigurationException on configuration error
*/
public static Module bindSettings(String propertiesFileKey, Class<?>... settingsArg)
throws ConfigurationException {
final CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new SystemConfiguration());
String propertyFile = config.getString(propertiesFileKey);
if (propertyFile != null) {
config.addConfiguration(new PropertiesConfiguration(propertyFile));
}
List<Field> fields = new ArrayList<Field>();
for (Class<?> settings : settingsArg) {
fields.addAll(Arrays.asList(settings.getDeclaredFields()));
}
// Reflect on settings class and absorb settings
final Map<Setting, Field> settings = new LinkedHashMap<Setting, Field>();
for (Field field : fields) {
if (!field.isAnnotationPresent(Setting.class)) {
continue;
}
// Validate target type
SettingTypeValidator typeHelper = supportedSettingTypes.get(field.getType());
if (typeHelper == null || !typeHelper.check(field.getGenericType())) {
throw new IllegalArgumentException(field.getType()
+ " is not one of the supported setting types");
}
Setting setting = field.getAnnotation(Setting.class);
settings.put(setting, field);
}
// Now validate them
List<String> missingProperties = new ArrayList<String>();
for (Setting setting : settings.keySet()) {
if (setting.defaultValue().isEmpty()) {
if (!config.containsKey(setting.name())) {
missingProperties.add(setting.name());
}
}
}
if (missingProperties.size() > 0) {
StringBuilder error = new StringBuilder();
error.append("The following required properties are missing from the server configuration: ");
error.append(Joiner.on(", ").join(missingProperties));
throw new ConfigurationException(error.toString());
}
// bundle everything up in an injectable guice module
return new AbstractModule() {
@Override
protected void configure() {
// We must iterate the settings a third time when binding.
// Note: do not collapse these loops as that will damage
// early error detection. The runtime is still O(n) in setting count.
for (Map.Entry<Setting, Field> entry : settings.entrySet()) {
Class<?> type = entry.getValue().getType();
Setting setting = entry.getKey();
if (int.class.equals(type)) {
Integer defaultValue = null;
if (!setting.defaultValue().isEmpty()) {
defaultValue = Integer.parseInt(setting.defaultValue());
}
bindConstant().annotatedWith(Names.named(setting.name()))
.to(config.getInteger(setting.name(), defaultValue));
} else if (boolean.class.equals(type)) {
Boolean defaultValue = null;
if (!setting.defaultValue().isEmpty()) {
defaultValue = Boolean.parseBoolean(setting.defaultValue());
}
bindConstant().annotatedWith(Names.named(setting.name()))
.to(config.getBoolean(setting.name(), defaultValue));
} else if (String.class.equals(type)) {
bindConstant().annotatedWith(Names.named(setting.name()))
.to(config.getString(setting.name(), setting.defaultValue()));
} else {
String[] value = config.getStringArray(setting.name());
if (value.length == 0 && !setting.defaultValue().isEmpty()) {
value = setting.defaultValue().split(",");
}
bind(new TypeLiteral<List<String>>() {}).annotatedWith(Names.named(setting.name()))
.toInstance(ImmutableList.copyOf(value));