Revision 1c21abc1
Added by Andreas Kohlbecker about 4 years ago
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteConfiguration.java | ||
---|---|---|
19 | 19 |
import org.springframework.beans.BeansException; |
20 | 20 |
import org.springframework.context.ApplicationContext; |
21 | 21 |
import org.springframework.context.ApplicationContextAware; |
22 |
import org.springframework.remoting.httpinvoker.CachingHttpInvokerProxyFactoryBean; |
|
22 | 23 |
import org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor; |
23 |
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; |
|
24 | 24 |
import org.springframework.security.access.AccessDecisionVoter; |
25 | 25 |
import org.springframework.security.authentication.AuthenticationProvider; |
26 | 26 |
import org.springframework.security.authentication.ProviderManager; |
... | ... | |
92 | 92 |
import eu.etaxonomy.taxeditor.service.CdmAuthenticatedHttpInvokerRequestExecutor; |
93 | 93 |
import eu.etaxonomy.taxeditor.service.CdmServiceRequestExecutor; |
94 | 94 |
import eu.etaxonomy.taxeditor.service.ICachedCommonService; |
95 |
import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
|
|
95 |
import eu.etaxonomy.taxeditor.service.RemoteInvocationTermCacher;
|
|
96 | 96 |
import eu.etaxonomy.taxeditor.session.CdmEntitySessionManager; |
97 | 97 |
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager; |
98 | 98 |
|
... | ... | |
103 | 103 |
* |
104 | 104 |
*/ |
105 | 105 |
@Component |
106 |
// TODO split configurations into proper @Configuration class
|
|
106 |
// TODO split into CdmRepository and proper @Configuration class
|
|
107 | 107 |
public class CdmApplicationRemoteConfiguration implements ICdmRepository, ApplicationContextAware { |
108 | 108 |
|
109 |
private static final int HTTP_READ_TIMEOUT = 0; |
|
110 |
|
|
109 | 111 |
@SuppressWarnings("unused") |
110 | 112 |
private static final Logger logger = Logger.getLogger(CdmApplicationRemoteConfiguration.class); |
111 | 113 |
|
... | ... | |
155 | 157 |
} else { |
156 | 158 |
baseUrl = "http://" + remoteSource.getServer() + ":" + String.valueOf(remoteSource.getPort()) + "/" + remoteSource.getContextPath(); |
157 | 159 |
} |
158 |
HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
|
|
159 |
proxy.setServiceInterface(clazz); |
|
160 |
proxy.setServiceUrl(baseUrl + serviceSuffix); |
|
160 |
CachingHttpInvokerProxyFactoryBean proxyFactory = new CachingHttpInvokerProxyFactoryBean();
|
|
161 |
proxyFactory.setServiceInterface(clazz);
|
|
162 |
proxyFactory.setServiceUrl(baseUrl + serviceSuffix);
|
|
161 | 163 |
if(executor != null) { |
162 |
executor.setReadTimeout(0); |
|
163 |
proxy.setHttpInvokerRequestExecutor(executor); |
|
164 |
executor.setReadTimeout(HTTP_READ_TIMEOUT); |
|
165 |
proxyFactory.setHttpInvokerRequestExecutor(executor); |
|
166 |
} |
|
167 |
if(ITermService.class.isAssignableFrom(clazz)){ |
|
168 |
proxyFactory.setRemoteInvocationTermCacher(new RemoteInvocationTermCacher()); |
|
164 | 169 |
} |
165 |
proxy.afterPropertiesSet(); |
|
166 |
return proxy.getObject(); |
|
170 |
proxyFactory.afterPropertiesSet();
|
|
171 |
return proxyFactory.getObject();
|
|
167 | 172 |
} |
168 | 173 |
|
169 | 174 |
// ****************************** APPLICATION CONTEXT *************************************************/ |
... | ... | |
297 | 302 |
|
298 | 303 |
@Override |
299 | 304 |
public ITermService getTermService(){ |
300 |
return (ITermService) getService(ITermService.class, "/remoting/term.service", new TermServiceRequestExecutor());
|
|
305 |
return (ITermService) getService(ITermService.class, "/remoting/term.service", new CdmServiceRequestExecutor());
|
|
301 | 306 |
} |
302 | 307 |
|
303 | 308 |
@Override |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/cache/CdmServiceCacher.java | ||
---|---|---|
15 | 15 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
16 | 16 |
import eu.etaxonomy.cdm.model.term.DefinedTermBase; |
17 | 17 |
import eu.etaxonomy.cdm.model.term.TermBase; |
18 |
import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
|
|
18 |
import eu.etaxonomy.taxeditor.service.RemoteInvocationTermCacher;
|
|
19 | 19 |
import eu.etaxonomy.taxeditor.session.CdmEntitySession; |
20 | 20 |
import eu.etaxonomy.taxeditor.session.ICdmEntitySession; |
21 | 21 |
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager; |
... | ... | |
53 | 53 |
|
54 | 54 |
DefinedTermBase.setCacher(this); |
55 | 55 |
CdmTransientEntityCacher.setPermanentCacher(this); |
56 |
TermServiceRequestExecutor.setDefaultCacher(this); |
|
56 |
//TermServiceRequestExecutor.setDefaultCacher(this); |
|
57 |
RemoteInvocationTermCacher.setDefaultCacher(this); |
|
57 | 58 |
|
58 | 59 |
cacheLoader = new CacheLoader(this); |
59 | 60 |
} |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/source/CdmServerInfo.java | ||
---|---|---|
284 | 284 |
return true; |
285 | 285 |
} |
286 | 286 |
} catch (CdmSourceException e) { |
287 |
logger.error(e.getMessage(), e); |
|
287 | 288 |
throw new CDMServerException(e); |
288 | 289 |
} |
289 | 290 |
|
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 |
} |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/service/IRemoteInvocationTermCacher.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2020 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.taxeditor.service; |
|
10 |
|
|
11 |
import org.springframework.remoting.support.RemoteInvocation; |
|
12 |
import org.springframework.remoting.support.RemoteInvocationResult; |
|
13 |
|
|
14 |
/** |
|
15 |
* @author a.kohlbecker |
|
16 |
* @since Feb 4, 2020 |
|
17 |
* |
|
18 |
*/ |
|
19 |
public interface IRemoteInvocationTermCacher { |
|
20 |
|
|
21 |
/** |
|
22 |
* @param ri |
|
23 |
* @param rir |
|
24 |
*/ |
|
25 |
void cacheTerms(RemoteInvocation ri, RemoteInvocationResult rir); |
|
26 |
|
|
27 |
/** |
|
28 |
* @param ri |
|
29 |
* @return |
|
30 |
*/ |
|
31 |
RemoteInvocationResult termsFromCache(RemoteInvocation ri); |
|
32 |
|
|
33 |
} |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/service/RemoteInvocationTermCacher.java | ||
---|---|---|
1 |
package eu.etaxonomy.taxeditor.service; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.HashSet; |
|
5 |
import java.util.List; |
|
6 |
import java.util.Map; |
|
7 |
import java.util.Set; |
|
8 |
|
|
9 |
import org.apache.log4j.Logger; |
|
10 |
import org.springframework.remoting.support.RemoteInvocation; |
|
11 |
import org.springframework.remoting.support.RemoteInvocationResult; |
|
12 |
|
|
13 |
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher; |
|
14 |
import eu.etaxonomy.cdm.model.term.DefinedTermBase; |
|
15 |
import eu.etaxonomy.cdm.model.term.TermType; |
|
16 |
|
|
17 |
|
|
18 |
public class RemoteInvocationTermCacher implements IRemoteInvocationTermCacher { |
|
19 |
private static final Logger logger = Logger.getLogger(RemoteInvocationTermCacher.class); |
|
20 |
|
|
21 |
private static Map<TermType, RemoteInvocationResult> termTypeMap = new HashMap<>(); |
|
22 |
|
|
23 |
private static CdmServiceCacher cdmServiceCacher; |
|
24 |
|
|
25 |
public static void setDefaultCacher(CdmServiceCacher csc) { |
|
26 |
cdmServiceCacher = csc; |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public void cacheTerms(RemoteInvocation ri, RemoteInvocationResult rir) { |
|
31 |
if(cdmServiceCacher != null) { |
|
32 |
if(ri.getMethodName().equals("listByTermType")) { |
|
33 |
if(ri.getArguments()[1] == null) { |
|
34 |
Set<DefinedTermBase<?>> terms = new HashSet<>(); |
|
35 |
if(rir.getValue() != null) { |
|
36 |
terms.addAll((List<DefinedTermBase<?>>)rir.getValue()); |
|
37 |
|
|
38 |
for(DefinedTermBase<?> term : terms) { |
|
39 |
cdmServiceCacher.load(term); |
|
40 |
} |
|
41 |
termTypeMap.put((TermType)ri.getArguments()[0], rir); |
|
42 |
} |
|
43 |
|
|
44 |
} |
|
45 |
} |
|
46 |
} else { |
|
47 |
logger.info("Default CdmServiceCacher is null. Cannot cache terms"); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public RemoteInvocationResult termsFromCache(RemoteInvocation ri) { |
|
53 |
return termTypeMap.get(ri.getArguments()[0]); |
|
54 |
} |
|
55 |
|
|
56 |
} |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/service/TermServiceRequestExecutor.java | ||
---|---|---|
1 | 1 |
package eu.etaxonomy.taxeditor.service; |
2 | 2 |
|
3 | 3 |
import java.util.HashMap; |
4 |
import java.util.HashSet; |
|
5 |
import java.util.List; |
|
6 | 4 |
import java.util.Map; |
7 |
import java.util.Set; |
|
8 | 5 |
|
9 | 6 |
import org.apache.log4j.Logger; |
10 |
import org.springframework.remoting.support.RemoteInvocation; |
|
11 | 7 |
import org.springframework.remoting.support.RemoteInvocationResult; |
12 | 8 |
|
13 | 9 |
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher; |
14 |
import eu.etaxonomy.cdm.model.term.DefinedTermBase; |
|
15 | 10 |
import eu.etaxonomy.cdm.model.term.TermType; |
16 | 11 |
|
17 | 12 |
|
... | ... | |
26 | 21 |
cdmServiceCacher = csc; |
27 | 22 |
} |
28 | 23 |
|
29 |
@Override |
|
30 |
public void cache(RemoteInvocation ri, RemoteInvocationResult rir) { |
|
31 |
if(cdmServiceCacher != null) { |
|
32 |
if(ri.getMethodName().equals("listByTermType")) { |
|
33 |
if(ri.getArguments()[1] == null) { |
|
34 |
Set<DefinedTermBase<?>> terms = new HashSet<>(); |
|
35 |
if(rir.getValue() != null) { |
|
36 |
terms.addAll((List<DefinedTermBase<?>>)rir.getValue()); |
|
37 |
|
|
38 |
for(DefinedTermBase<?> term : terms) { |
|
39 |
cdmServiceCacher.load(term); |
|
40 |
} |
|
41 |
termTypeMap.put((TermType)ri.getArguments()[0], rir); |
|
42 |
} |
|
43 |
|
|
44 |
} |
|
45 |
} |
|
46 |
} else { |
|
47 |
logger.info("Default CdmServiceCacher is null. Cannot cache terms"); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public RemoteInvocationResult fromCache(RemoteInvocation ri) { |
|
53 |
return termTypeMap.get(ri.getArguments()[0]); |
|
54 |
} |
|
55 |
|
|
56 | 24 |
} |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionManager.java | ||
---|---|---|
86 | 86 |
notifyObservers(); |
87 | 87 |
} |
88 | 88 |
|
89 |
|
|
90 |
/* (non-Javadoc) |
|
91 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#getSessions() |
|
92 |
*/ |
|
93 | 89 |
@Override |
94 | 90 |
public Collection<ICdmEntitySession> getSessions() { |
95 | 91 |
return ownerSessionMap.values(); |
96 | 92 |
} |
97 | 93 |
|
98 |
|
|
99 |
/* (non-Javadoc) |
|
100 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#bind(eu.etaxonomy.taxeditor.session.ISessionEventListener) |
|
101 |
*/ |
|
102 | 94 |
@Override |
103 | 95 |
public void bind(ICdmEntitySessionEnabled sessionOwner) { |
104 | 96 |
if(sessionOwner == null) { |
... | ... | |
119 | 111 |
return ownerSessionMap.containsKey(sessionOwner); |
120 | 112 |
} |
121 | 113 |
|
122 |
|
|
123 |
/* (non-Javadoc) |
|
124 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(T) |
|
125 |
*/ |
|
126 | 114 |
@Override |
127 | 115 |
public <T extends Object> T load(T obj, boolean update) { |
128 | 116 |
if(tlActiveSession.get() == null) { |
... | ... | |
132 | 120 |
} |
133 | 121 |
} |
134 | 122 |
|
135 |
|
|
136 | 123 |
@Override |
137 | 124 |
public <T extends CdmBase> void update() { |
138 | 125 |
if(tlActiveSession.get() != null) { |
... | ... | |
140 | 127 |
} |
141 | 128 |
} |
142 | 129 |
|
143 |
/* (non-Javadoc) |
|
144 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(T) |
|
145 |
*/ |
|
146 | 130 |
@Override |
147 | 131 |
public <T extends CdmBase> T load(T cdmBase, boolean update) { |
148 | 132 |
if(tlActiveSession.get() == null) { |
... | ... | |
151 | 135 |
return tlActiveSession.get().load(cdmBase, update); |
152 | 136 |
} |
153 | 137 |
|
154 |
|
|
155 |
/* (non-Javadoc) |
|
156 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(eu.etaxonomy.cdm.api.service.UpdateResult, boolean) |
|
157 |
*/ |
|
158 | 138 |
@Override |
159 | 139 |
public UpdateResult load(UpdateResult updateResult, boolean update) { |
160 | 140 |
if(tlActiveSession.get() == null) { |
... | ... | |
163 | 143 |
return tlActiveSession.get().load(updateResult, update); |
164 | 144 |
} |
165 | 145 |
|
166 |
|
|
167 |
/* (non-Javadoc) |
|
168 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(eu.etaxonomy.cdm.persistence.dto.MergeResult, boolean) |
|
169 |
*/ |
|
170 | 146 |
@Override |
171 | 147 |
public MergeResult load(MergeResult mergeResult, boolean update) { |
172 | 148 |
if(tlActiveSession.get() == null) { |
... | ... | |
175 | 151 |
return tlActiveSession.get().load(mergeResult, update); |
176 | 152 |
} |
177 | 153 |
|
178 |
|
|
179 |
/* (non-Javadoc) |
|
180 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(java.util.Collection) |
|
181 |
*/ |
|
182 | 154 |
@Override |
183 | 155 |
public <T extends CdmBase> Collection<T> load(Collection<T> cdmBaseList, boolean update) { |
184 | 156 |
if(tlActiveSession.get() == null) { |
... | ... | |
236 | 208 |
notifyObservers(); |
237 | 209 |
} |
238 | 210 |
|
239 |
/* (non-Javadoc) |
|
240 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#addSessionListener() |
|
241 |
*/ |
|
242 | 211 |
@Override |
243 | 212 |
public void addSessionObserver(ICdmEntitySessionManagerObserver sessionObserver) { |
244 | 213 |
sessionObservers.add(sessionObserver); |
... | ... | |
250 | 219 |
} |
251 | 220 |
} |
252 | 221 |
|
253 |
/* (non-Javadoc) |
|
254 |
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#isRemoting() |
|
255 |
*/ |
|
256 | 222 |
@Override |
257 | 223 |
public boolean isRemoting() { |
258 | 224 |
// FIXME:Remoting stupid method to check whether we are in remoting |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/ICdmEntitySession.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
public <O extends Object> O load(O obj, boolean update); |
21 | 21 |
|
22 |
/** |
|
23 |
* @see eu.etaxonomy.cdm.cache.CdmTransientEntityCacher#load(Collection, boolean) |
|
24 |
*/ |
|
22 | 25 |
public <T extends CdmBase> T load(T cdmBase, boolean update); |
23 | 26 |
|
24 | 27 |
public UpdateResult load(UpdateResult updateResult, boolean update); |
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/session/ICdmEntitySessionManager.java | ||
---|---|---|
20 | 20 |
public abstract void bind(ICdmEntitySessionEnabled sessionOwner); |
21 | 21 |
|
22 | 22 |
|
23 |
/** |
|
24 |
* @see eu.etaxonomy.cdm.cache.CdmTransientEntityCacher#load(Collection, boolean) |
|
25 |
*/ |
|
23 | 26 |
public abstract <T extends Object> T load(T obj, boolean update); |
24 | 27 |
|
25 |
|
|
28 |
/** |
|
29 |
* @see eu.etaxonomy.cdm.cache.CdmTransientEntityCacher#load(Collection, boolean) |
|
30 |
*/ |
|
26 | 31 |
public abstract <T extends CdmBase> T load(T cdmBase, boolean update); |
27 | 32 |
|
28 | 33 |
|
eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/springframework/remoting/httpinvoker/CachingHttpInvokerProxyFactoryBean.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2020 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package org.springframework.remoting.httpinvoker; |
|
10 |
|
|
11 |
import java.util.HashSet; |
|
12 |
import java.util.Set; |
|
13 |
|
|
14 |
import org.aopalliance.intercept.MethodInvocation; |
|
15 |
import org.apache.log4j.Logger; |
|
16 |
import org.springframework.remoting.support.RemoteInvocation; |
|
17 |
import org.springframework.remoting.support.RemoteInvocationResult; |
|
18 |
|
|
19 |
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController; |
|
20 |
import eu.etaxonomy.cdm.api.application.CdmApplicationState; |
|
21 |
import eu.etaxonomy.cdm.api.service.ITermService; |
|
22 |
import eu.etaxonomy.cdm.api.service.UpdateResult; |
|
23 |
import eu.etaxonomy.taxeditor.service.IRemoteInvocationTermCacher; |
|
24 |
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager; |
|
25 |
|
|
26 |
/** |
|
27 |
* Extension of the <code>HttpInvokerProxyFactoryBean</code> to support caching of general CDM entities and terms. |
|
28 |
* <p> |
|
29 |
* <b>Performance measurement:</b></br> |
|
30 |
* Supports measuring the request processing time at the client side. Setting "{@value #PROP_KEY_MEASURE_DURATION}" |
|
31 |
* as system parameter enables the measurement. To make the measurements appear in the log, the log <code>Level</code> |
|
32 |
* for this class needs to be set to at least {@link org.apache.log4j.Level#INFO} |
|
33 |
* |
|
34 |
* |
|
35 |
* @author a.kohlbecker |
|
36 |
* @since Feb 4, 2020 |
|
37 |
* |
|
38 |
*/ |
|
39 |
public class CachingHttpInvokerProxyFactoryBean extends HttpInvokerProxyFactoryBean { |
|
40 |
|
|
41 |
private static final String PROP_KEY_MEASURE_DURATION = "remoting.httpinvoker.measureDuration"; |
|
42 |
|
|
43 |
private static final Logger logger = Logger.getLogger(CachingHttpInvokerProxyFactoryBean.class); |
|
44 |
|
|
45 |
private ICdmEntitySessionManager cdmEntitySessionManager; |
|
46 |
|
|
47 |
private IRemoteInvocationTermCacher remoteInvocationTermCacher; |
|
48 |
|
|
49 |
protected final static Set<String> persistingMethods = new HashSet<String>(); |
|
50 |
|
|
51 |
protected static boolean measureDuration = false; |
|
52 |
|
|
53 |
static { |
|
54 |
persistingMethods.add("merge"); |
|
55 |
persistingMethods.add("save"); |
|
56 |
persistingMethods.add("findWithUpdate"); |
|
57 |
persistingMethods.add("loadWithUpdate"); |
|
58 |
measureDuration = System.getProperty(PROP_KEY_MEASURE_DURATION) != null; |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
protected RemoteInvocationResult executeRequest( |
|
63 |
RemoteInvocation invocation, MethodInvocation originalInvocation) throws Exception { |
|
64 |
|
|
65 |
// logger.setLevel(Level.INFO); |
|
66 |
|
|
67 |
if (!(invocation.getMethodName() == null) && !(getServiceUrl() == null) && logger.isDebugEnabled()){ |
|
68 |
logger.debug("Remote invoking : " + getServiceUrl() + "#" + invocation.getMethodName()); |
|
69 |
} |
|
70 |
|
|
71 |
if(CdmApplicationState.getCurrentAppConfig() == null){ |
|
72 |
logger.debug("No application context yet, no point caching!"); |
|
73 |
return doExecuteRequest(invocation, originalInvocation); |
|
74 |
} |
|
75 |
|
|
76 |
// A) if this is a term service call for term lists try to get the terms from the cache |
|
77 |
if(ITermService.class.isAssignableFrom(originalInvocation.getMethod().getDeclaringClass())){ |
|
78 |
return handleTermRequest(invocation, originalInvocation); |
|
79 |
} |
|
80 |
// B) handle other service calls |
|
81 |
else { |
|
82 |
return handleGeneralRequest(invocation, originalInvocation); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* @param invocation |
|
88 |
* @param originalInvocation |
|
89 |
* @return |
|
90 |
* @throws Exception |
|
91 |
*/ |
|
92 |
private RemoteInvocationResult handleGeneralRequest(RemoteInvocation invocation, |
|
93 |
MethodInvocation originalInvocation) throws Exception { |
|
94 |
RemoteInvocationResult invocationResult = doExecuteRequest(invocation, originalInvocation); |
|
95 |
if(invocationResult.getValue() != null && !invocationResult.hasException()) { |
|
96 |
|
|
97 |
if(persistingMethods.contains(invocation.getMethodName())) { |
|
98 |
invocationResult = new RemoteInvocationResult(cdmEntitySessionManager().load(invocationResult.getValue(), true)); |
|
99 |
logger.debug("Entity cached with updating cached data"); |
|
100 |
} else if(invocationResult.getValue() instanceof UpdateResult){ |
|
101 |
UpdateResult result = (UpdateResult)invocationResult.getValue(); |
|
102 |
if(result.isOk()){ |
|
103 |
logger.debug("Entity from UpdateResult stored in cache with updating cached data" ); |
|
104 |
cdmEntitySessionManager().load(result.getCdmEntity(), true); |
|
105 |
} |
|
106 |
} else { |
|
107 |
invocationResult = new RemoteInvocationResult(cdmEntitySessionManager().load(invocationResult.getValue(), false)); |
|
108 |
logger.debug("Entity cached without updating cached data" ); |
|
109 |
} |
|
110 |
|
|
111 |
} |
|
112 |
return invocationResult; |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* @param invocation |
|
117 |
* @param originalInvocation |
|
118 |
* @return |
|
119 |
* @throws Exception |
|
120 |
*/ |
|
121 |
private RemoteInvocationResult handleTermRequest(RemoteInvocation invocation, MethodInvocation originalInvocation) |
|
122 |
throws Exception { |
|
123 |
RemoteInvocationResult invocationResult = null; |
|
124 |
if(remoteInvocationTermCacher != null){ |
|
125 |
invocationResult = remoteInvocationTermCacher.termsFromCache(invocation); |
|
126 |
if(invocationResult == null) { |
|
127 |
invocationResult = doExecuteRequest(invocation, originalInvocation); |
|
128 |
remoteInvocationTermCacher.cacheTerms(invocation, invocationResult); |
|
129 |
logger.debug("Term list loaded and cached"); |
|
130 |
} else { |
|
131 |
logger.debug("Term list found in cache, not loaded"); |
|
132 |
} |
|
133 |
} else { |
|
134 |
logger.warn("Term list: No IRemoteInvocationTermCacher configured"); |
|
135 |
invocationResult = doExecuteRequest(invocation, originalInvocation); |
|
136 |
} |
|
137 |
return invocationResult; |
|
138 |
} |
|
139 |
|
|
140 |
/** |
|
141 |
* @param invocation |
|
142 |
* @param originalInvocation |
|
143 |
* @return |
|
144 |
* @throws Exception |
|
145 |
*/ |
|
146 |
private RemoteInvocationResult doExecuteRequest(RemoteInvocation invocation, MethodInvocation originalInvocation) |
|
147 |
throws Exception { |
|
148 |
|
|
149 |
double startTime = 0; |
|
150 |
if(measureDuration){ |
|
151 |
startTime = System.currentTimeMillis(); |
|
152 |
} |
|
153 |
RemoteInvocationResult result = super.executeRequest(invocation, originalInvocation); |
|
154 |
if(measureDuration){ |
|
155 |
double duration = System.currentTimeMillis() - startTime; |
|
156 |
logger.info(getServiceUrl() + "#" + invocation.getMethodName() + " [" + duration + " ms]"); |
|
157 |
} |
|
158 |
return result; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* @return the remoteInvocationTermCacher |
|
163 |
*/ |
|
164 |
public IRemoteInvocationTermCacher getRemoteInvocationTermCacher() { |
|
165 |
return remoteInvocationTermCacher; |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* @param remoteInvocationTermCacher the remoteInvocationTermCacher to set |
|
170 |
*/ |
|
171 |
public void setRemoteInvocationTermCacher(IRemoteInvocationTermCacher remoteInvocationTermCacher) { |
|
172 |
this.remoteInvocationTermCacher = remoteInvocationTermCacher; |
|
173 |
} |
|
174 |
|
|
175 |
private ICdmEntitySessionManager cdmEntitySessionManager(){ |
|
176 |
if(cdmEntitySessionManager == null) { |
|
177 |
cdmEntitySessionManager = |
|
178 |
((CdmApplicationRemoteController)CdmApplicationState.getCurrentAppConfig()).getCdmEntitySessionManager(); |
|
179 |
} |
|
180 |
return cdmEntitySessionManager; |
|
181 |
} |
|
182 |
|
|
183 |
} |
Also available in: Unified diff
ref #8842 replacing caching in CdmServiceRequestExecutor by CachingHttpInvokerProxyFactoryBean to avoid having states