Project

General

Profile

Download (2.89 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.remoting;
10

    
11
import java.lang.reflect.InvocationTargetException;
12
import java.util.Arrays;
13
import java.util.stream.Collectors;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.remoting.support.RemoteInvocation;
17
import org.springframework.remoting.support.RemoteInvocationExecutor;
18
import org.springframework.util.Assert;
19
import org.springframework.util.ClassUtils;
20

    
21
/**
22
 * Alternative implementation of the {@link RemoteInvocationExecutor} interface which behaves exactly
23
 * as the {@link org.springframework.remoting.support.DefaultRemoteInvocationExecutor} with the
24
 * additional capability of measuring the execution of the method invocation.
25
 * <p>
26
 * The execution duration in milliseconds will be written to the log when the log level for this
27
 * class is set to <code>DEBUG</code>.
28
 *
29
 * @author a.kohlbecker
30
 * @since Feb 17, 2020
31
 *
32
 */
33
public class DebuggingRemoteInvocationExecutor implements RemoteInvocationExecutor {
34

    
35
    private static final Logger logger = Logger.getLogger(DebuggingRemoteInvocationExecutor.class);
36

    
37
    @Override
38
    public Object invoke(RemoteInvocation invocation, Object targetObject)
39
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
40

    
41
        Assert.notNull(invocation, "RemoteInvocation must not be null");
42
        Assert.notNull(targetObject, "Target object must not be null");
43
        boolean domeasure = logger.isDebugEnabled();
44
        String targetInvocationStr = null;
45
        long start = 0;
46
        if(domeasure) {
47
        targetInvocationStr = targetCdmServiceInterfaces(targetObject) + "#" + invocation.getMethodName() + "(" +
48
                ClassUtils.classNamesToString(invocation.getParameterTypes()) + ")";
49
        logger.debug("invoking: " + targetInvocationStr);
50
        start = System.currentTimeMillis();
51
        }
52
        Object invocationResult = invocation.invoke(targetObject);
53
        if (domeasure) {
54
            logger.debug("invocation: " + targetInvocationStr + " completed [" + (System.currentTimeMillis() - start) + " ms]");
55
        }
56

    
57
        return invocationResult;
58
    }
59

    
60
    /**
61
     * @param targetObject
62
     * @return
63
     */
64
    private String targetCdmServiceInterfaces(Object targetObject) {
65
        String interfacesStr = targetObject.getClass().getName();
66
        if(interfacesStr.contains("$Proxy")){
67
            Class<?>[] intfs = targetObject.getClass().getInterfaces();
68
            interfacesStr = Arrays.stream(intfs).map(c -> c.getName())
69
                    .filter(n -> n.startsWith("eu.etaxonomy.cdm.api.service."))
70
                    .collect( Collectors.joining( "," ) );
71
        }
72
        return interfacesStr;
73
    }
74

    
75

    
76

    
77

    
78
}
    (1-1/1)