Project

General

Profile

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.locators.UniqueServiceLocator;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Value;
19
import org.springframework.core.io.ClassPathResource;
20

    
21
public class IndexClientMap {
22

    
23
    private static final Log log = LogFactory.getLog(IndexClientMap.class);
24

    
25
    @Value(value = "${openaire.api.directindex.findSolrIndexUrl.xquery}")
26
    private ClassPathResource findSolrIndexUrl;
27

    
28
    @Value(value = "${openaire.api.directindex.findIndexDsInfo.xquery}")
29
    private ClassPathResource findIndexDsInfo;
30

    
31
    @Resource
32
    private UniqueServiceLocator serviceLocator;
33

    
34
    @Autowired
35
    private IndexDSRetriever indexDSRetriever;
36

    
37
    private Map<IndexDsInfo, CloudIndexClient> clients = Maps.newHashMap();
38

    
39
    public Map<IndexDsInfo, CloudIndexClient> getClients() throws DirecIndexApiException {
40
        final List<IndexDsInfo> idxList = Lists.newArrayList();
41
        try {
42
            idxList.addAll(indexDSRetriever.calculateCurrentIndexDsInfo());
43
        } catch (Throwable e) {
44
            log.warn("Unable to retrieve IndexDS data from the IS", e);
45
        }
46

    
47
        if (idxList == null || idxList.isEmpty()) {
48
            throw new DirecIndexApiException("cannot create index: no public Search Service found");
49
        }
50
        if (idxList.size() > 1) {
51
            log.warn("found more than 1 public search service");
52
        }
53

    
54
        for(IndexDsInfo i : idxList) {
55
            if (!clients.containsKey(i)) {
56
                try {
57
                    clients.put(i, CloudIndexClientFactory.newIndexClient(i.getIndexBaseUrl(), i.getColl(), false));
58
                } catch (Throwable e) {
59
                    log.warn("Unable to create index client", e);
60
                }
61
            }
62
        }
63
        return clients;
64
    }
65

    
66
}
(2-2/8)