ru.yandex.qatools.camelot.common.PluginContextInjectorImpl Maven / Gradle / Ivy
package ru.yandex.qatools.camelot.common;
import org.apache.camel.Exchange;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.qatools.camelot.api.*;
import ru.yandex.qatools.camelot.api.annotations.*;
import ru.yandex.qatools.camelot.config.PluginContext;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static ru.yandex.qatools.camelot.util.ReflectUtil.getAnnotationValue;
import static ru.yandex.qatools.camelot.util.ReflectUtil.getFieldsInClassHierarchy;
import static ru.yandex.qatools.camelot.util.ServiceUtil.forEachAnnotatedField;
import static ru.yandex.qatools.camelot.util.TypesUtil.*;
/**
* @author Ilya Sadykov (mailto: [email protected])
* @author Innokenty Shuvalov (mailto: [email protected])
*/
public class PluginContextInjectorImpl implements PluginContextInjector
{
protected final static Logger LOGGER = LoggerFactory.getLogger(PluginContextInjectorImpl.class);
private static final Map,
Collection>>> cache = new ConcurrentHashMap<>();
@Override
public void inject(P pluginObj, PluginContext pluginConfig) {
inject(pluginObj, pluginConfig, null);
}
@Override
public void inject(final P object, final PluginContext context, final Exchange exchange) {
if (!cache.containsKey(object.getClass())) {
try {
cacheClassInfo(object.getClass());
} catch (Exception e) {
LOGGER.error("Failed to cache the plugin context injection context!", e);
}
}
performInjection(object, context, exchange);
}
private void performInjection(final Object object, final PluginContext context, final Exchange exchange) { //NOSONAR
try {
injectField(object.getClass(), MainInput.class, object, context.getMainInput());
injectField(object.getClass(), Config.class, object, context.getAppConfig());
injectPluginRelatedField(object, PluginStorage.class, new InjectHandler() {
@Override
public Storage onByClass(Class pluginClass) {
return context.getInterop().storage(pluginClass);
}
@Override
public Storage onById(String pluginId) {
return context.getInterop().storage(pluginId);
}
@Override
public Storage onDefault() {
return context.getStorage();
}
});
injectPluginRelatedField(object, Input.class, new InjectHandler() {
@Override
public EventProducer onByClass(Class pluginClass) {
return context.getInterop().input(pluginClass);
}
@Override
public EventProducer onById(String pluginId) {
return context.getInterop().input(pluginId);
}
@Override
public EventProducer onDefault() {
return context.getInput();
}
});
injectPluginRelatedField(object, Output.class, new InjectHandler() {
@Override
public EventProducer onByClass(Class pluginClass) {
return context.getInterop().output(pluginClass);
}
@Override
public EventProducer onById(String pluginId) {
return context.getInterop().output(pluginId);
}
@Override
public EventProducer onDefault() {
return context.getOutput();
}
});
injectPluginRelatedField(object, Repository.class, new InjectHandler() {
@Override
public AggregatorRepository onByClass(Class pluginClass) {
return context.getInterop().repo(pluginClass);
}
@Override
public AggregatorRepository onById(String pluginId) {
return context.getInterop().repo(pluginId);
}
@Override
public AggregatorRepository onDefault() {
return context.getRepository();
}
});
injectField(object.getClass(), ClientSender.class, object, new FieldListener() {
@Override
public ClientMessageSender found(Field field, AnnotationInfo info) throws Exception { //NOSONAR
return context.getClientSendersProvider().getSender(info.topic);
}
});
injectField(object.getClass(), Plugins.class, object, new FieldListener() {
@Override
public PluginsInterop found(Field field, AnnotationInfo info) throws Exception { //NOSONAR
return context.getInterop();
}
});
injectPluginRelatedField(object, Plugin.class, new InjectHandler() {
@Override
public PluginInterop onByClass(Class pluginClass) {
return context.getInterop().forPlugin(pluginClass);
}
@Override
public PluginInterop onById(String pluginId) {
return context.getInterop().forPlugin(pluginId);
}
@Override
public PluginInterop onDefault() {
return null;
}
});
injectField(object.getClass(), ConfigValue.class, object, new FieldListener