Example usage for org.springframework.batch.core.listener MethodInvokerMethodInterceptor MethodInvokerMethodInterceptor

List of usage examples for org.springframework.batch.core.listener MethodInvokerMethodInterceptor MethodInvokerMethodInterceptor

Introduction

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

Prototype

public MethodInvokerMethodInterceptor(Map<String, Set<MethodInvoker>> invokerMap, boolean ordered) 

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   www.  j  a v a  2  s.  c o  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();

}