Search in sources :

Example 1 with ConfigurationAdapter

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

the class ConfigurationAdapterFactory method loadAdapters.

private static void loadAdapters() {
    Map<Class<?>, Class<? extends ConfigurationAdapter>> all = new HashMap<>();
    ServiceLoader<ConfigurationAdapterDescriptor> loader = ServiceLoader.load(ConfigurationAdapterDescriptor.class);
    if (loader != null) {
        Iterator<ConfigurationAdapterDescriptor> i = loader.iterator();
        if (i != null) {
            while (i.hasNext()) {
                ConfigurationAdapterDescriptor descriptor = i.next();
                Map<Class<?>, Class<? extends ConfigurationAdapter>> add = descriptor.providedConfigurationAdapters();
                if (add != null) {
                    all.putAll(add);
                }
            }
        }
    }
    ADAPTERS = Collections.unmodifiableMap(all);
}
Also used : ConfigurationAdapter(org.apache.knox.gateway.config.ConfigurationAdapter) HashMap(java.util.HashMap) ConfigurationAdapterDescriptor(org.apache.knox.gateway.config.spi.ConfigurationAdapterDescriptor)

Example 2 with ConfigurationAdapter

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

the class BeanConfigurationAdapterDescriptorTest method testDescriptor.

@Test
public void testDescriptor() {
    ConfigurationAdapterDescriptor descriptor = new BeanConfigurationAdapterDescriptor();
    Map<Class<?>, Class<? extends ConfigurationAdapter>> map = descriptor.providedConfigurationAdapters();
    assertThat(map, hasKey((Class) Object.class));
    Class<? extends ConfigurationAdapter> type = map.get(Object.class);
    assertThat("Descriptor didn't return " + BeanConfigurationAdapter.class.getName(), type == BeanConfigurationAdapter.class);
}
Also used : ConfigurationAdapter(org.apache.knox.gateway.config.ConfigurationAdapter) ConfigurationAdapterDescriptor(org.apache.knox.gateway.config.spi.ConfigurationAdapterDescriptor) Test(org.junit.Test)

Example 3 with ConfigurationAdapter

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

the class ConfigurationAdapterFactory method get.

public static ConfigurationAdapter get(Object config) throws ConfigurationException {
    if (config == null) {
        throw new NullPointerException("Configuration adapter instantiation impossible for null config object.");
    }
    try {
        Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters = getAdapters();
        Class configType = config.getClass();
        Class adapterType = findAdapterTypeForConfigTypeOrParent(adapters, configType);
        if (adapterType == null) {
            throw new ConfigurationException("No configuration adapter found for config type " + configType.getName());
        }
        Constructor c = findConstructorForConfigType(adapterType, configType);
        if (!c.isAccessible()) {
            c.setAccessible(true);
        }
        Object adapter = c.newInstance(config);
        return ConfigurationAdapter.class.cast(adapter);
    } catch (ConfigurationException e) {
        throw e;
    } catch (Exception e) {
        throw new ConfigurationException("Configuration adapter instantiation failed.", e);
    }
}
Also used : ConfigurationAdapter(org.apache.knox.gateway.config.ConfigurationAdapter) ConfigurationException(org.apache.knox.gateway.config.ConfigurationException) Constructor(java.lang.reflect.Constructor) ConfigurationException(org.apache.knox.gateway.config.ConfigurationException)

Aggregations

ConfigurationAdapter (org.apache.knox.gateway.config.ConfigurationAdapter)3 ConfigurationAdapterDescriptor (org.apache.knox.gateway.config.spi.ConfigurationAdapterDescriptor)2 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 ConfigurationException (org.apache.knox.gateway.config.ConfigurationException)1 Test (org.junit.Test)1