1
|
package eu.dnetlib.openaire.exporter.datasource.clients;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.nio.charset.Charset;
|
5
|
import java.util.Queue;
|
6
|
|
7
|
import eu.dnetlib.OpenaireExporterConfig;
|
8
|
import eu.dnetlib.enabling.datasources.common.DatasourceManagerException;
|
9
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
10
|
import eu.dnetlib.openaire.exporter.datasource.clients.utils.IndexDsInfo;
|
11
|
import org.apache.commons.io.IOUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.cache.annotation.CacheEvict;
|
16
|
import org.springframework.cache.annotation.Cacheable;
|
17
|
import org.springframework.core.io.ClassPathResource;
|
18
|
import org.springframework.http.HttpStatus;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
/**
|
22
|
* Created by claudio on 20/10/2016.
|
23
|
*/
|
24
|
@Component
|
25
|
public class ISLookupClient {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(ISLookupClient.class);
|
28
|
|
29
|
@Autowired
|
30
|
private OpenaireExporterConfig config;
|
31
|
|
32
|
@Autowired
|
33
|
private ISLookUpService isLookUpService;
|
34
|
|
35
|
@Cacheable("datasources-is-cache")
|
36
|
public IndexDsInfo calculateCurrentIndexDsInfo(final Queue<Throwable> errors) throws DatasourceManagerException {
|
37
|
log.warn("calculateCurrentIndexDsInfo(): not using cache");
|
38
|
final String[] arr = _isLookUp(_getQuery(config.getFindIndexDsInfo()), errors).split("@@@");
|
39
|
return new IndexDsInfo(
|
40
|
_isLookUp(_getQuery(config.getFindSolrIndexUrl()), errors),
|
41
|
arr[0].trim(), arr[1].trim(), arr[2].trim());
|
42
|
}
|
43
|
|
44
|
@Cacheable("datasources-is-cache")
|
45
|
public String getObjectStoreId(final String dsId, final Queue<Throwable> errors) throws DatasourceManagerException {
|
46
|
log.warn(String.format("getObjectStoreId(%s): not using cache", dsId));
|
47
|
final String xqueryTemplate = _getQuery(config.getFindObjectStore());
|
48
|
return _isLookUp(String.format(xqueryTemplate, dsId), errors);
|
49
|
}
|
50
|
|
51
|
private String _getQuery(final ClassPathResource resource) throws DatasourceManagerException {
|
52
|
try {
|
53
|
return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
|
54
|
} catch (IOException e) {
|
55
|
throw new DatasourceManagerException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Error accessing classpath resource (xquery)", e);
|
56
|
}
|
57
|
}
|
58
|
|
59
|
private String _isLookUp(final String xquery, final Queue<Throwable> errors) {
|
60
|
try {
|
61
|
log.debug(String.format("running xquery: %s", xquery));
|
62
|
final String res = isLookUpService.getResourceProfileByQuery(xquery);
|
63
|
log.debug(String.format("query result: %s", res));
|
64
|
return res;
|
65
|
} catch (Throwable e) {
|
66
|
errors.add(e);
|
67
|
return "";
|
68
|
}
|
69
|
}
|
70
|
|
71
|
@CacheEvict(cacheNames = "datasources-is-cache", allEntries = true)
|
72
|
public void dropCache() {
|
73
|
log.info("dropped dsManager IS cache");
|
74
|
}
|
75
|
|
76
|
}
|