Search in sources :

Example 1 with IComponent

use of io.apiman.gateway.engine.IComponent in project apiman by apiman.

the class ConfigDrivenComponentRegistry method createAndRegisterComponent.

/**
 * Creates the component and registers it in the registry.
 * @param componentType the component type
 * @return the component
 * @throws ComponentNotFoundException when a policy tries to get a component from
 * the context but the component doesn't exist or is otherwise not available.
 */
public <T extends IComponent> T createAndRegisterComponent(Class<T> componentType) throws ComponentNotFoundException {
    try {
        synchronized (components) {
            Class<? extends T> componentClass = engineConfig.getComponentClass(componentType, pluginRegistry);
            Map<String, String> componentConfig = engineConfig.getComponentConfig(componentType);
            T component = create(componentClass, componentConfig);
            components.put(componentType, component);
            // Because components are lazily created, we need to initialize them here
            // if necessary.
            DependsOnComponents annotation = componentClass.getAnnotation(DependsOnComponents.class);
            if (annotation != null) {
                Class<? extends IComponent>[] value = annotation.value();
                for (Class<? extends IComponent> theC : value) {
                    Method setter = ReflectionUtils.findSetter(componentClass, theC);
                    if (setter != null) {
                        IComponent injectedComponent = getComponent(theC);
                        try {
                            setter.invoke(component, new Object[] { injectedComponent });
                        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
            if (component instanceof IRequiresInitialization) {
                ((IRequiresInitialization) component).initialize();
            }
            return component;
        }
    } catch (Exception e) {
        throw new ComponentNotFoundException(componentType.getName());
    }
}
Also used : IComponent(io.apiman.gateway.engine.IComponent) Method(java.lang.reflect.Method) DependsOnComponents(io.apiman.gateway.engine.DependsOnComponents) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRequiresInitialization(io.apiman.gateway.engine.IRequiresInitialization) InvocationTargetException(java.lang.reflect.InvocationTargetException) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException)

Aggregations

DependsOnComponents (io.apiman.gateway.engine.DependsOnComponents)1 IComponent (io.apiman.gateway.engine.IComponent)1 IRequiresInitialization (io.apiman.gateway.engine.IRequiresInitialization)1 ComponentNotFoundException (io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1