Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / service / CdmServiceRequestExecutor.java
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.beans.factory.annotation.Autowired;
8 import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
9 import org.springframework.remoting.support.RemoteInvocation;
10 import org.springframework.remoting.support.RemoteInvocationResult;
11 import org.springframework.stereotype.Component;
12
13 import eu.etaxonomy.cdm.api.service.UpdateResult;
14 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
15
16 @Component
17 public class CdmServiceRequestExecutor extends CdmAuthenticatedHttpInvokerRequestExecutor {
18
19 private static final Logger logger = Logger.getLogger(CdmServiceRequestExecutor.class);
20
21 @Autowired
22 private ICdmEntitySessionManager cdmEntitySessionManager;
23
24 private RemoteInvocation currentRemoteInvocation;
25
26 @Override
27 protected void writeRemoteInvocation(RemoteInvocation invocation, OutputStream os) throws IOException {
28 currentRemoteInvocation = invocation;
29 super.writeRemoteInvocation(invocation, os);
30 }
31
32 @Override
33 protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config,
34 java.io.ByteArrayOutputStream baos)
35 throws java.io.IOException,
36 java.lang.ClassNotFoundException {
37 RemoteInvocationResult rir = fromCache(currentRemoteInvocation);
38
39 // if rir is not null at this point we assume that the
40 // the object has already been loaded in the cache and
41 // does not need to be reloaded
42
43 if(rir == null) {
44 logger.info("Remote invoking : " + currentRemoteInvocation.getMethodName() + "@" + config.getServiceUrl());
45 rir = super.doExecuteRequest(config, baos);
46 if(rir.getValue() != null && !rir.hasException()) {
47 if(rir.getValue() instanceof UpdateResult){
48 UpdateResult result = (UpdateResult)rir.getValue();
49 if(result.isOk()){
50 rir = new RemoteInvocationResult(cdmEntitySessionManager.load(result, true));
51 }
52 } else {
53 rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), true));
54 }
55 }
56 cache(currentRemoteInvocation, rir);
57 }
58 currentRemoteInvocation = null;
59
60 return rir;
61 }
62
63 public void cache(RemoteInvocation ri, RemoteInvocationResult rir) {
64
65 }
66
67 public RemoteInvocationResult fromCache(RemoteInvocation ri) {
68 return null;
69 }
70 }