1
|
package eu.etaxonomy.taxeditor.service;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.OutputStream;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.Set;
|
7
|
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
|
10
|
import org.springframework.remoting.support.RemoteInvocation;
|
11
|
import org.springframework.remoting.support.RemoteInvocationResult;
|
12
|
import org.springframework.stereotype.Component;
|
13
|
|
14
|
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
|
15
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
16
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
17
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
18
|
|
19
|
@Component
|
20
|
public class CdmServiceRequestExecutor extends CdmAuthenticatedHttpInvokerRequestExecutor {
|
21
|
|
22
|
private static final Logger logger = Logger.getLogger(CdmServiceRequestExecutor.class);
|
23
|
|
24
|
private ICdmEntitySessionManager cdmEntitySessionManager ;
|
25
|
|
26
|
private RemoteInvocation currentRemoteInvocation;
|
27
|
|
28
|
protected final static Set<String> cachableMethods = new HashSet<String>();
|
29
|
|
30
|
|
31
|
|
32
|
public CdmServiceRequestExecutor() {
|
33
|
cachableMethods.add("merge");
|
34
|
cachableMethods.add("save");
|
35
|
cachableMethods.add("findWithUpdate");
|
36
|
cachableMethods.add("loadWithUpdate");
|
37
|
}
|
38
|
|
39
|
@Override
|
40
|
protected void writeRemoteInvocation(RemoteInvocation invocation, OutputStream os) throws IOException {
|
41
|
if(cdmEntitySessionManager == null) {
|
42
|
cdmEntitySessionManager =
|
43
|
((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager();
|
44
|
}
|
45
|
currentRemoteInvocation = invocation;
|
46
|
super.writeRemoteInvocation(invocation, os);
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config,
|
51
|
java.io.ByteArrayOutputStream baos)
|
52
|
throws java.io.IOException,
|
53
|
java.lang.ClassNotFoundException {
|
54
|
RemoteInvocationResult rir = fromCache(currentRemoteInvocation);
|
55
|
|
56
|
if(rir == null) {
|
57
|
logger.info("Remote invoking : " + currentRemoteInvocation.getMethodName() + "@" + config.getServiceUrl());
|
58
|
rir = super.doExecuteRequest(config, baos);
|
59
|
if(rir.getValue() != null && !rir.hasException()) {
|
60
|
if(cachableMethods.contains(currentRemoteInvocation.getMethodName())) {
|
61
|
rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), true));
|
62
|
} else if(rir.getValue() instanceof UpdateResult){
|
63
|
UpdateResult result = (UpdateResult)rir.getValue();
|
64
|
if(result.isOk()){
|
65
|
cdmEntitySessionManager.load(result, true);
|
66
|
}
|
67
|
} else {
|
68
|
rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), false));
|
69
|
}
|
70
|
}
|
71
|
cache(currentRemoteInvocation, rir);
|
72
|
}
|
73
|
currentRemoteInvocation = null;
|
74
|
|
75
|
return rir;
|
76
|
}
|
77
|
|
78
|
public void cache(RemoteInvocation ri, RemoteInvocationResult rir) {
|
79
|
|
80
|
}
|
81
|
|
82
|
public RemoteInvocationResult fromCache(RemoteInvocation ri) {
|
83
|
return null;
|
84
|
}
|
85
|
}
|