Search in sources :

Example 1 with Optional

use of org.apache.knox.gateway.config.Optional in project knox by apache.

the class DefaultConfigurationInjector method injectFieldValue.

private void injectFieldValue(Field field, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding) throws ConfigurationException {
    Configure annotation = field.getAnnotation(Configure.class);
    if (annotation != null) {
        Alias alias = field.getAnnotation(Alias.class);
        String name = getConfigName(field, alias);
        String bind = getBindName(target, name, binding);
        Object value = retrieveValue(target, bind, name, field.getType(), adapter, binding);
        if (value == null) {
            Optional optional = field.getAnnotation(Optional.class);
            if (optional == null) {
                throw new ConfigurationException(String.format("Failed to find configuration for %s bound to %s of %s via %s", bind, name, target.getClass().getName(), adapter.getClass().getName()));
            }
        } else {
            try {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                field.set(target, value);
            } catch (Exception e) {
                throw new ConfigurationException(String.format("Failed to inject field configuration property %s of %s", name, target.getClass().getName()), e);
            }
        }
    }
}
Also used : Optional(org.apache.knox.gateway.config.Optional) ConfigurationException(org.apache.knox.gateway.config.ConfigurationException) Alias(org.apache.knox.gateway.config.Alias) Configure(org.apache.knox.gateway.config.Configure) ConfigurationException(org.apache.knox.gateway.config.ConfigurationException)

Aggregations

Alias (org.apache.knox.gateway.config.Alias)1 ConfigurationException (org.apache.knox.gateway.config.ConfigurationException)1 Configure (org.apache.knox.gateway.config.Configure)1 Optional (org.apache.knox.gateway.config.Optional)1