Search in sources :

Example 1 with Configure

use of org.apache.knox.gateway.config.Configure 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)

Example 2 with Configure

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

the class DefaultConfigurationInjector method injectMethodValue.

private void injectMethodValue(Method method, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding) throws ConfigurationException {
    Configure methodTag = method.getAnnotation(Configure.class);
    if (methodTag != null) {
        Alias aliasTag = method.getAnnotation(Alias.class);
        String methodName = getConfigName(method, aliasTag);
        Class[] argTypes = method.getParameterTypes();
        Object[] args = new Object[argTypes.length];
        Annotation[][] argTags = method.getParameterAnnotations();
        for (int i = 0; i < argTypes.length; i++) {
            String argName = getConfigName(methodName, argTags[i]);
            String bndName = getBindName(target, argName, binding);
            Object argValue = retrieveValue(target, bndName, argName, argTypes[i], adapter, binding);
            if (argValue == null) {
                Default defTag = findAnnotation(argTags[i], Default.class);
                if (defTag != null) {
                    String strValue = defTag.value();
                    argValue = convertValue(target, argName, strValue, argTypes[i]);
                } else {
                    throw new ConfigurationException(String.format("Failed to find configuration for %s as %s of %s via %s", bndName, argName, target.getClass().getName(), adapter.getClass().getName()));
                }
            }
            args[i] = argValue;
        }
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        try {
            method.invoke(target, args);
        } catch (Exception e) {
            throw new ConfigurationException(String.format("Failed to inject method configuration via %s of %s", methodName, target.getClass().getName()), e);
        }
    }
}
Also used : ConfigurationException(org.apache.knox.gateway.config.ConfigurationException) Alias(org.apache.knox.gateway.config.Alias) Configure(org.apache.knox.gateway.config.Configure) Default(org.apache.knox.gateway.config.Default) ConfigurationException(org.apache.knox.gateway.config.ConfigurationException)

Aggregations

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