Example usage for org.springframework.batch.core.listener ListenerMetaData getAnnotation

List of usage examples for org.springframework.batch.core.listener ListenerMetaData getAnnotation

Introduction

In this page you can find the example usage for org.springframework.batch.core.listener ListenerMetaData getAnnotation.

Prototype

public Class<? extends Annotation> getAnnotation();

Source Link

Usage

From source file:org.springframework.batch.core.listener.AbstractListenerFactoryBean.java

@Override
public Object getObject() {
    if (metaDataMap == null) {
        metaDataMap = new HashMap<String, String>();
    }//from  w w  w.j av a 2  s  .  co  m
    // Because all annotations and interfaces should be checked for, make
    // sure that each meta data
    // entry is represented.
    for (ListenerMetaData metaData : this.getMetaDataValues()) {
        if (!metaDataMap.containsKey(metaData.getPropertyName())) {
            // put null so that the annotation and interface is checked
            metaDataMap.put(metaData.getPropertyName(), null);
        }
    }

    Set<Class<?>> listenerInterfaces = new HashSet<Class<?>>();

    // For every entry in the map, try and find a method by interface, name,
    // or annotation. If the same
    Map<String, Set<MethodInvoker>> invokerMap = new HashMap<String, Set<MethodInvoker>>();
    boolean synthetic = false;
    for (Entry<String, String> entry : metaDataMap.entrySet()) {
        final ListenerMetaData metaData = this.getMetaDataFromPropertyName(entry.getKey());
        Set<MethodInvoker> invokers = new HashSet<MethodInvoker>();

        MethodInvoker invoker;
        invoker = getMethodInvokerForInterface(metaData.getListenerInterface(), metaData.getMethodName(),
                delegate, metaData.getParamTypes());
        if (invoker != null) {
            invokers.add(invoker);
        }

        invoker = getMethodInvokerByName(entry.getValue(), delegate, metaData.getParamTypes());
        if (invoker != null) {
            invokers.add(invoker);
            synthetic = true;
        }

        if (metaData.getAnnotation() != null) {
            invoker = getMethodInvokerByAnnotation(metaData.getAnnotation(), delegate,
                    metaData.getParamTypes());
            if (invoker != null) {
                invokers.add(invoker);
                synthetic = true;
            }
        }

        if (!invokers.isEmpty()) {
            invokerMap.put(metaData.getMethodName(), invokers);
            listenerInterfaces.add(metaData.getListenerInterface());
        }

    }

    if (listenerInterfaces.isEmpty()) {
        listenerInterfaces.add(this.getDefaultListenerClass());
    }

    if (!synthetic) {
        int count = 0;
        for (Class<?> listenerInterface : listenerInterfaces) {
            if (listenerInterface.isInstance(delegate)) {
                count++;
            }
        }
        // All listeners can be supplied by the delegate itself
        if (count == listenerInterfaces.size()) {
            return delegate;
        }
    }

    boolean ordered = false;
    if (delegate instanceof Ordered) {
        ordered = true;
        listenerInterfaces.add(Ordered.class);
    }

    // create a proxy listener for only the interfaces that have methods to
    // be called
    ProxyFactory proxyFactory = new ProxyFactory();
    if (delegate instanceof Advised) {
        proxyFactory.setTargetSource(((Advised) delegate).getTargetSource());
    } else {
        proxyFactory.setTarget(delegate);
    }
    proxyFactory.setInterfaces(listenerInterfaces.toArray(new Class[0]));
    proxyFactory
            .addAdvisor(new DefaultPointcutAdvisor(new MethodInvokerMethodInterceptor(invokerMap, ordered)));
    return proxyFactory.getProxy();

}

From source file:org.springframework.batch.core.listener.AbstractListenerFactoryBean.java

/**
 * Convenience method to check whether the given object is or can be made
 * into a listener./*from   w  w  w  .j a v  a 2 s  .  c  o  m*/
 *
 * @param target the object to check
 * @return true if the delegate is an instance of any of the listener
 * interface, or contains the marker annotations
 */
public static boolean isListener(Object target, Class<?> listenerType, ListenerMetaData[] metaDataValues) {
    if (target == null) {
        return false;
    }
    if (listenerType.isInstance(target)) {
        return true;
    }
    if (target instanceof Advised) {
        TargetSource targetSource = ((Advised) target).getTargetSource();
        if (targetSource != null && targetSource.getTargetClass() != null
                && listenerType.isAssignableFrom(targetSource.getTargetClass())) {
            return true;
        }

        if (targetSource != null && targetSource.getTargetClass() != null
                && targetSource.getTargetClass().isInterface()) {
            logger.warn(String.format(
                    "%s is an interface.  The implementing class will not be queried for annotation based listener configurations.  If using @StepScope on a @Bean method, be sure to return the implementing class so listener annotations can be used.",
                    targetSource.getTargetClass().getName()));
        }
    }
    for (ListenerMetaData metaData : metaDataValues) {
        if (MethodInvokerUtils.getMethodInvokerByAnnotation(metaData.getAnnotation(), target) != null) {
            return true;
        }
    }
    return false;
}