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