Project

General

Profile

Download (3.28 KB) Statistics
| Branch: | Tag: | Revision:
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

    
58
		    if (!(currentRemoteInvocation.getMethodName() == null) && !(config.getServiceUrl() == null)){
59
		       // logger.info("Remote invoking : " + currentRemoteInvocation.getMethodName() + "@" + config.getServiceUrl());
60
		    }
61
		    rir = super.doExecuteRequest(config, baos);
62
			if(rir.getValue() != null && !rir.hasException()) {
63
			    if (currentRemoteInvocation == null){
64
			        logger.debug("return RemoteInvocationResult without caching" );
65
			        return rir;
66
			    }
67
                if(cachableMethods.contains(currentRemoteInvocation.getMethodName())) {
68
                    rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), true));
69
                } else if(rir.getValue() instanceof UpdateResult){
70
                    UpdateResult result = (UpdateResult)rir.getValue();
71
                    if(result.isOk()){
72
                        cdmEntitySessionManager.load(result, true);
73
                    }
74
                } else {
75
                    rir = new RemoteInvocationResult(cdmEntitySessionManager.load(rir.getValue(), false));
76
                }
77
			}
78
			cache(currentRemoteInvocation, rir);
79
		}
80
		currentRemoteInvocation = null;
81

    
82
		return rir;
83
	}
84

    
85
	public void cache(RemoteInvocation ri, RemoteInvocationResult rir) {
86

    
87
	}
88

    
89
	public RemoteInvocationResult fromCache(RemoteInvocation ri) {
90
	    return null;
91
	}
92
}
(4-4/6)