Search in sources :

Example 1 with Default

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