1
|
package eu.dnetlib.openaire.directindex.api;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import javax.annotation.Resource;
|
7
|
|
8
|
import com.google.common.collect.Lists;
|
9
|
import com.google.common.collect.Maps;
|
10
|
import eu.dnetlib.data.index.CloudIndexClient;
|
11
|
import eu.dnetlib.data.index.CloudIndexClientException;
|
12
|
import eu.dnetlib.data.index.CloudIndexClientFactory;
|
13
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
14
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
15
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
16
|
import org.apache.commons.io.IOUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.springframework.beans.factory.annotation.Value;
|
20
|
import org.springframework.cache.annotation.CacheEvict;
|
21
|
import org.springframework.cache.annotation.Cacheable;
|
22
|
import org.springframework.core.io.ClassPathResource;
|
23
|
|
24
|
public class IndexClientMap {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(IndexClientMap.class);
|
27
|
|
28
|
@Value(value = "${openaire.api.directindex.findSolrIndexUrl.xquery}")
|
29
|
private ClassPathResource findSolrIndexUrl;
|
30
|
|
31
|
@Value(value = "${openaire.api.directindex.findIndexDsInfo.xquery}")
|
32
|
private ClassPathResource findIndexDsInfo;
|
33
|
|
34
|
@Resource
|
35
|
private UniqueServiceLocator serviceLocator;
|
36
|
|
37
|
private Map<IndexDsInfo, CloudIndexClient> clients = Maps.newHashMap();
|
38
|
|
39
|
public Map<IndexDsInfo, CloudIndexClient> getClients() throws DirecIndexApiException {
|
40
|
try {
|
41
|
final List<IndexDsInfo> idxList = calculateCurrentIndexDsInfo();
|
42
|
|
43
|
if (idxList == null || idxList.isEmpty()) {
|
44
|
throw new DirecIndexApiException("cannot create index: no public Search Service found");
|
45
|
}
|
46
|
if (idxList.size() > 1) {
|
47
|
log.warn("found more than 1 public search service");
|
48
|
}
|
49
|
|
50
|
for(IndexDsInfo i : idxList) {
|
51
|
if (!clients.containsKey(i)) {
|
52
|
clients.put(i, CloudIndexClientFactory.newIndexClient(i.getIndexBaseUrl(), i.getColl(), false));
|
53
|
}
|
54
|
}
|
55
|
return clients;
|
56
|
|
57
|
} catch (IOException | ISLookUpException | CloudIndexClientException e) {
|
58
|
throw new DirecIndexApiException(e);
|
59
|
}
|
60
|
}
|
61
|
|
62
|
@Cacheable("indexDsInfo")
|
63
|
public List<IndexDsInfo> calculateCurrentIndexDsInfo() throws IOException, ISLookUpException {
|
64
|
final List<IndexDsInfo> list = Lists.newArrayList();
|
65
|
|
66
|
final String queryUrl = IOUtils.toString(findSolrIndexUrl.getInputStream());
|
67
|
final String queryDs = IOUtils.toString(findIndexDsInfo.getInputStream());
|
68
|
|
69
|
final ISLookUpService lu = serviceLocator.getService(ISLookUpService.class);
|
70
|
final String indexBaseUrl = lu.getResourceProfileByQuery(queryUrl);
|
71
|
|
72
|
final List<String> idxDs = lu.quickSearchProfile(queryDs);
|
73
|
for (final String idx : idxDs) {
|
74
|
final String[] arr = idx.split("@@@");
|
75
|
list.add(new IndexDsInfo(indexBaseUrl, arr[0].trim(), arr[1].trim(), arr[2].trim()));
|
76
|
}
|
77
|
return list;
|
78
|
}
|
79
|
|
80
|
@CacheEvict("indexDsInfo")
|
81
|
public void evictCache(){
|
82
|
log.info("Evicting indexDsInfo cache");
|
83
|
}
|
84
|
}
|