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.logging.log4j.LogManager;
|
16
|
import org.apache.logging.log4j.Logger;
|
17
|
import org.springframework.remoting.support.RemoteInvocation;
|
18
|
import org.springframework.remoting.support.RemoteInvocationExecutor;
|
19
|
import org.springframework.util.Assert;
|
20
|
import org.springframework.util.ClassUtils;
|
21
|
|
22
|
/**
|
23
|
* Alternative implementation of the {@link RemoteInvocationExecutor} interface which behaves exactly
|
24
|
* as the {@link org.springframework.remoting.support.DefaultRemoteInvocationExecutor} with the
|
25
|
* additional capability of measuring the execution of the method invocation.
|
26
|
* <p>
|
27
|
* The execution duration in milliseconds will be written to the log when the log level for this
|
28
|
* class is set to <code>DEBUG</code>.
|
29
|
*
|
30
|
* @author a.kohlbecker
|
31
|
* @since Feb 17, 2020
|
32
|
*/
|
33
|
public class DebuggingRemoteInvocationExecutor implements RemoteInvocationExecutor {
|
34
|
|
35
|
private static final Logger logger = LogManager.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
|
}
|