All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.qatools.camelot.util.ServiceUtil Maven / Gradle / Ivy

There is a newer version: 2.5.4
Show newest version
package ru.yandex.qatools.camelot.util;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.qatools.camelot.common.AnnotatedFieldListener;
import ru.yandex.qatools.camelot.common.AnnotatedMethodListener;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

import static ru.yandex.qatools.camelot.util.ReflectUtil.*;
import static ru.yandex.qatools.camelot.util.TypesUtil.isAssignableFrom;

/**
 * @author Ilya Sadykov (mailto: [email protected])
 */
public abstract class ServiceUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceUtil.class);

    ServiceUtil() {
    }

    /**
     * Inject a lazy value to the annotated field if found
     */
    public static  void injectAnnotatedField(
            Class clazz,
            final Object instance,
            Class annotation,
            final AnnotatedFieldListener value) throws Exception { //NOSONAR
        forEachAnnotatedField(clazz, annotation, new AnnotatedFieldListener() {
            @Override
            public T found(Field field, U annValue) throws Exception {
                T res = value.found(field, annValue);
                if (instance != null && res != null
                        && isAssignableFrom(field.getType(), res.getClass())) {
                    boolean oldAccessible = field.isAccessible();
                    field.setAccessible(true);
                    field.set(instance, res);
                    field.setAccessible(oldAccessible);
                }
                return res;
            }
        });
    }

    /**
     * Sets the value for the field of the instance
     */
    public static  T setFieldValue(Field field, Object instance, T value)
            throws IllegalAccessException {
        if (instance != null) {
            boolean oldAccessible = field.isAccessible();
            field.setAccessible(true);
            field.set(instance, value);
            field.setAccessible(oldAccessible);
            return value;
        }
        return null;
    }

    /**
     * Perform an action on each annotated method
     */
    @SuppressWarnings("unchecked")
    public static  void forEachAnnotatedMethod(
            Class clazz, Class annotation, AnnotatedMethodListener listener)
            throws Exception { //NOSONAR
        for (Method method : getMethodsInClassHierarchy(clazz)) {
            final A annValue = (A) getAnnotation(method, annotation);
            if (annValue != null) {
                listener.found(method, annValue);
            }
        }
    }

    /**
     * Perform an action on each annotated field
     */
    public static  void forEachAnnotatedField(
            Class clazz,
            Class annotationClass,
            AnnotatedFieldListener listener
    ) throws Exception { //NOSONAR
        for (Field field : getFieldsInClassHierarchy(clazz)) {
            //noinspection unchecked
            final U annValue = (U) getAnnotation(field, annotationClass);
            if (annValue != null) {
                listener.found(field, annValue);
            }
        }
    }

    /**
     * Inject a value to the annotated field if found
     */
    public static  void injectAnnotatedField(
            Class clazz,
            Object instance,
            Class annotation,
            final T value
    ) throws Exception { //NOSONAR
        injectAnnotatedField(clazz, instance, annotation, new AnnotatedFieldListener() {
            @Override
            public T found(Field field, U annotation) {
                return value;
            }
        });
    }

    /**
     * Remove all the endpoints associated with uri
     */
    public static void gracefullyRemoveEndpoints(CamelContext camelContext, String uri) throws Exception { //NOSONAR
        try {
            LOGGER.info("Gracefully removing endpoint " + uri);
            final Endpoint endpoint = camelContext.getEndpoint(uri);
            if (endpoint != null) {
                endpoint.stop();
                camelContext.removeEndpoints(endpoint.getEndpointUri());
            }
        } catch (Exception e) {
            LOGGER.debug("Failed to remove endpoint: " + uri, e);
        }
    }

    /**
     * Stop and remove the route by id
     */
    public static void gracefullyRemoveRoute(CamelContext camelContext, String id) throws Exception { //NOSONAR
        if (camelContext.getRoute(id) != null) {
            LOGGER.info("Gracefully removing route " + id);
            try {
                camelContext.stopRoute(id, 10, TimeUnit.SECONDS);
                camelContext.removeRoute(id);
            } catch (Exception e) {
                LOGGER.debug("Failed to remove route: " + id, e);
            }
        }
    }

    /**
     * Start the route by id
     */
    public static void gracefullyStartRoute(CamelContext camelContext, String id) throws Exception { //NOSONAR
        if (camelContext.getRoute(id) != null) {
            LOGGER.info("Gracefully starting route " + id);
            try {
                camelContext.startRoute(id);
            } catch (Exception e) {
                LOGGER.error("Failed to start route: " + id, e);
            }
        }
    }
}