use of io.apiman.gateway.engine.DependsOnComponents 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());
}
}
Aggregations