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 org.springframework.remoting.httpinvoker;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Set;
|
13
|
|
14
|
import org.aopalliance.intercept.MethodInvocation;
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.springframework.remoting.support.RemoteInvocation;
|
17
|
import org.springframework.remoting.support.RemoteInvocationResult;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
21
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
22
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
23
|
import eu.etaxonomy.taxeditor.service.IRemoteInvocationTermCacher;
|
24
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
25
|
|
26
|
/**
|
27
|
* Extension of the <code>HttpInvokerProxyFactoryBean</code> to support caching of general CDM entities and terms.
|
28
|
* <p>
|
29
|
* <b>Performance measurement:</b></br>
|
30
|
* Supports measuring the request processing time at the client side. Setting "{@value #PROP_KEY_MEASURE_DURATION}"
|
31
|
* as system parameter enables the measurement. To make the measurements appear for each request in the log, the logging <code>Level</code>
|
32
|
* for <code>HttpInvokerProxyFactoryBean</code> needs to be set to at least {@link org.apache.log4j.Level#INFO}. Otherwise the duration is
|
33
|
* only reported in case of errors.
|
34
|
*
|
35
|
* @author a.kohlbecker
|
36
|
* @since Feb 4, 2020
|
37
|
*/
|
38
|
public class CachingHttpInvokerProxyFactoryBean extends HttpInvokerProxyFactoryBean {
|
39
|
|
40
|
private static final Logger logger = Logger.getLogger(CachingHttpInvokerProxyFactoryBean.class);
|
41
|
|
42
|
private static final String PROP_KEY_MEASURE_DURATION = "remoting.httpinvoker.measureDuration";
|
43
|
|
44
|
private ICdmEntitySessionManager cdmEntitySessionManager;
|
45
|
|
46
|
private IRemoteInvocationTermCacher remoteInvocationTermCacher;
|
47
|
|
48
|
protected final static Set<String> persistingMethods = new HashSet<>();
|
49
|
|
50
|
protected static boolean measureDuration = false;
|
51
|
|
52
|
static {
|
53
|
persistingMethods.add("merge");
|
54
|
persistingMethods.add("save");
|
55
|
persistingMethods.add("findWithUpdate");
|
56
|
persistingMethods.add("loadWithUpdate");
|
57
|
measureDuration = System.getProperty(PROP_KEY_MEASURE_DURATION) != null;
|
58
|
}
|
59
|
|
60
|
@Override
|
61
|
protected RemoteInvocationResult executeRequest(
|
62
|
RemoteInvocation invocation, MethodInvocation originalInvocation) throws Exception {
|
63
|
|
64
|
// logger.setLevel(Level.INFO);
|
65
|
|
66
|
if (!(invocation.getMethodName() == null) && !(getServiceUrl() == null) && logger.isDebugEnabled()){
|
67
|
logger.debug("Remote invoking : " + getServiceUrl() + "#" + invocation.getMethodName());
|
68
|
}
|
69
|
|
70
|
if(CdmApplicationState.getCurrentAppConfig() == null){
|
71
|
logger.debug("No application context yet, no point caching!");
|
72
|
return doExecuteRequest(invocation, originalInvocation);
|
73
|
}
|
74
|
|
75
|
// A) if this is a term service call for term lists try to get the terms from the cache
|
76
|
if(ITermService.class.isAssignableFrom(originalInvocation.getMethod().getDeclaringClass())){
|
77
|
return handleTermRequest(invocation, originalInvocation);
|
78
|
}
|
79
|
// B) handle other service calls
|
80
|
else {
|
81
|
return handleGeneralRequest(invocation, originalInvocation);
|
82
|
}
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* @param invocation
|
87
|
* @param originalInvocation
|
88
|
* @return
|
89
|
* @throws Exception
|
90
|
*/
|
91
|
private RemoteInvocationResult handleGeneralRequest(RemoteInvocation invocation,
|
92
|
MethodInvocation originalInvocation) throws Exception {
|
93
|
RemoteInvocationResult invocationResult = doExecuteRequest(invocation, originalInvocation);
|
94
|
if(invocationResult.getValue() != null && !invocationResult.hasException()) {
|
95
|
|
96
|
if(persistingMethods.contains(invocation.getMethodName())) {
|
97
|
invocationResult = new RemoteInvocationResult(cdmEntitySessionManager().load(invocationResult.getValue(), true));
|
98
|
logger.debug("Entity cached with updating cached data");
|
99
|
} else if(invocationResult.getValue() instanceof UpdateResult){
|
100
|
UpdateResult result = (UpdateResult)invocationResult.getValue();
|
101
|
if(result.isOk()){
|
102
|
logger.debug("Entity from UpdateResult stored in cache with updating cached data" );
|
103
|
cdmEntitySessionManager().load(result.getCdmEntity(), true);
|
104
|
}
|
105
|
} else {
|
106
|
invocationResult = new RemoteInvocationResult(cdmEntitySessionManager().load(invocationResult.getValue(), false));
|
107
|
logger.debug("Entity cached without updating cached data" );
|
108
|
}
|
109
|
|
110
|
}
|
111
|
return invocationResult;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* @param invocation
|
116
|
* @param originalInvocation
|
117
|
* @return
|
118
|
* @throws Exception
|
119
|
*/
|
120
|
private RemoteInvocationResult handleTermRequest(RemoteInvocation invocation, MethodInvocation originalInvocation)
|
121
|
throws Exception {
|
122
|
RemoteInvocationResult invocationResult = null;
|
123
|
if(remoteInvocationTermCacher != null){
|
124
|
invocationResult = remoteInvocationTermCacher.termsFromCache(invocation);
|
125
|
if(invocationResult == null) {
|
126
|
invocationResult = doExecuteRequest(invocation, originalInvocation);
|
127
|
remoteInvocationTermCacher.cacheTerms(invocation, invocationResult);
|
128
|
logger.debug("Term list loaded and cached");
|
129
|
} else {
|
130
|
logger.debug("Term list found in cache, not loaded");
|
131
|
}
|
132
|
} else {
|
133
|
logger.warn("Term list: No IRemoteInvocationTermCacher configured");
|
134
|
invocationResult = doExecuteRequest(invocation, originalInvocation);
|
135
|
}
|
136
|
return invocationResult;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* @param invocation
|
141
|
* @param originalInvocation
|
142
|
* @return
|
143
|
* @throws Exception
|
144
|
*/
|
145
|
private RemoteInvocationResult doExecuteRequest(RemoteInvocation invocation, MethodInvocation originalInvocation)
|
146
|
throws Exception {
|
147
|
|
148
|
double startTime = 0;
|
149
|
if(measureDuration){
|
150
|
startTime = System.currentTimeMillis();
|
151
|
}
|
152
|
try {
|
153
|
RemoteInvocationResult result = super.executeRequest(invocation, originalInvocation);
|
154
|
if(measureDuration){
|
155
|
double duration = System.currentTimeMillis() - startTime;
|
156
|
logger.info(getServiceUrl() + "#" + invocation.getMethodName() + " [" + duration + " ms]");
|
157
|
}
|
158
|
return result;
|
159
|
} catch(Exception e) {
|
160
|
if(measureDuration){
|
161
|
double duration = System.currentTimeMillis() - startTime;
|
162
|
logger.error("(" + e.getClass().getName() + ": \"" + e.getMessage() + "\") interrupts " + getServiceUrl() + "#" + invocation.getMethodName() + " after [" + duration + " ms]");
|
163
|
}
|
164
|
throw e;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
public IRemoteInvocationTermCacher getRemoteInvocationTermCacher() {
|
169
|
return remoteInvocationTermCacher;
|
170
|
}
|
171
|
public void setRemoteInvocationTermCacher(IRemoteInvocationTermCacher remoteInvocationTermCacher) {
|
172
|
this.remoteInvocationTermCacher = remoteInvocationTermCacher;
|
173
|
}
|
174
|
|
175
|
private ICdmEntitySessionManager cdmEntitySessionManager(){
|
176
|
if(cdmEntitySessionManager == null) {
|
177
|
cdmEntitySessionManager =
|
178
|
((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager();
|
179
|
}
|
180
|
return cdmEntitySessionManager;
|
181
|
}
|
182
|
|
183
|
}
|