Revision 1c21abc1
Added by Andreas Kohlbecker about 4 years ago
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/service/CdmServiceRequestExecutor.java | ||
---|---|---|
1 | 1 |
package eu.etaxonomy.taxeditor.service; |
2 | 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.http.NoHttpResponseException; |
|
9 |
import org.apache.log4j.Logger; |
|
10 |
import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration; |
|
11 |
import org.springframework.remoting.support.RemoteInvocation; |
|
12 |
import org.springframework.remoting.support.RemoteInvocationResult; |
|
13 |
import org.springframework.stereotype.Component; |
|
14 |
|
|
15 |
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController; |
|
16 |
import eu.etaxonomy.cdm.api.application.CdmApplicationState; |
|
17 |
import eu.etaxonomy.cdm.api.service.UpdateResult; |
|
18 |
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager; |
|
19 |
|
|
20 |
@Component |
|
3 |
// @Component |
|
21 | 4 |
public class CdmServiceRequestExecutor extends CdmAuthenticatedHttpInvokerRequestExecutor { |
22 | 5 |
|
23 |
private static final Logger logger = Logger.getLogger(CdmServiceRequestExecutor.class); |
|
24 |
|
|
25 |
private ICdmEntitySessionManager cdmEntitySessionManager ; |
|
26 |
|
|
27 |
private RemoteInvocation currentRemoteInvocation; |
|
28 |
|
|
29 |
protected final static Set<String> cachableMethods = new HashSet<String>(); |
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
public CdmServiceRequestExecutor() { |
|
34 |
cachableMethods.add("merge"); |
|
35 |
cachableMethods.add("save"); |
|
36 |
cachableMethods.add("findWithUpdate"); |
|
37 |
cachableMethods.add("loadWithUpdate"); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
protected void writeRemoteInvocation(RemoteInvocation invocation, OutputStream os) throws IOException { |
|
42 |
if(cdmEntitySessionManager == null) { |
|
43 |
cdmEntitySessionManager = |
|
44 |
((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager(); |
|
45 |
} |
|
46 |
currentRemoteInvocation = invocation; |
|
47 |
super.writeRemoteInvocation(invocation, os); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, |
|
52 |
java.io.ByteArrayOutputStream baos) |
|
53 |
throws java.io.IOException, |
|
54 |
java.lang.ClassNotFoundException, NoHttpResponseException { |
|
55 |
RemoteInvocationResult rir = fromCache(currentRemoteInvocation); |
|
56 |
|
|
57 |
if(rir == null) { |
|
58 |
|
|
59 |
if (!(currentRemoteInvocation.getMethodName() == null) && !(config.getServiceUrl() == null)){ |
|
60 |
// logger.info("Remote invoking : " + currentRemoteInvocation.getMethodName() + "@" + config.getServiceUrl()); |
|
61 |
} |
|
62 |
try{ |
|
63 |
rir = super.doExecuteRequest(config, baos); |
|
64 |
}catch(Exception e){ |
|
65 |
if (e instanceof NoHttpResponseException){ |
|
66 |
throw e; |
|
67 |
} |
|
68 |
} |
|
69 |
if(rir.getValue() != null && !rir.hasException()) { |
|
70 |
if (currentRemoteInvocation == null){ |
|
71 |
logger.debug("return RemoteInvocationResult without caching" ); |
|
72 |
return rir; |
|
73 |
} |
|
74 |
if(cachableMethods.contains(currentRemoteInvocation.getMethodName())) { |
|
75 |
rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), true)); |
|
76 |
} else if(rir.getValue() instanceof UpdateResult){ |
|
77 |
UpdateResult result = (UpdateResult)rir.getValue(); |
|
78 |
if(result.isOk()){ |
|
79 |
cdmEntitySessionManager.load(result, true); |
|
80 |
} |
|
81 |
} else { |
|
82 |
rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), false)); |
|
83 |
} |
|
84 |
} |
|
85 |
cache(currentRemoteInvocation, rir); |
|
86 |
} |
|
87 |
currentRemoteInvocation = null; |
|
88 |
|
|
89 |
return rir; |
|
90 |
} |
|
91 |
|
|
92 |
public void cache(RemoteInvocation ri, RemoteInvocationResult rir) { |
|
93 |
|
|
94 |
} |
|
95 |
|
|
96 |
public RemoteInvocationResult fromCache(RemoteInvocation ri) { |
|
97 |
return null; |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public void setReadTimeout(int timeout) { |
|
102 |
logger.info("Read time our set to: " + timeout + " ms"); |
|
103 |
super.setReadTimeout(timeout); |
|
104 |
} |
|
105 | 6 |
|
106 | 7 |
} |
Also available in: Unified diff
ref #8842 replacing caching in CdmServiceRequestExecutor by CachingHttpInvokerProxyFactoryBean to avoid having states