Project

General

Profile

1
package eu.dnetlib.data.search.solr;
2

    
3
import eu.dnetlib.api.data.IndexService;
4
import eu.dnetlib.api.enabling.ISLookUpService;
5
import eu.dnetlib.api.enabling.ISLookUpServiceException;
6
import gr.uoa.di.driver.util.ServiceLocator;
7
import org.apache.log4j.Logger;
8

    
9
import java.util.List;
10

    
11
/**
12
 * Created by antleb on 2/4/14.
13
 */
14
public class SolrClientLocator implements ServiceLocator<IndexService> {
15
    private Logger logger = Logger.getLogger(getClass());
16

    
17
    private SolrIndexClient indexClient = null;
18
    private ServiceLocator<ISLookUpService> lookUpServiceServiceLocator = null;
19
    private String interpretation = null;
20

    
21
    private String cachedUrl = null;
22

    
23
    @Override
24
    public IndexService getService() {
25
        if (indexClient == null) {
26
            indexClient = locateIndexService();
27
        }
28

    
29
        return indexClient;
30
    }
31

    
32
    private SolrIndexClient locateIndexService() {
33
        SolrIndexClient client = new SolrIndexClient();
34

    
35
        try {
36
            List<String> urls = lookUpServiceServiceLocator.getService().quickSearchProfile("for $x in //RESOURCE_PROFILE//PROTOCOL[./@name='solr'] return data($x/@address)");
37

    
38
            if (urls.size() > 0) {
39
                logger.debug("cached url " + cachedUrl);
40
                cachedUrl = chooseIndexService(urls);
41

    
42
            } else {
43
                cachedUrl = getCachedIndexService(urls);
44
                logger.warn("Falling back to cached value for index : " + cachedUrl);
45
                throw new IllegalArgumentException("Could not locate an index service with a SOLR interface");
46
            }
47

    
48
        } catch (ISLookUpServiceException ise) {
49
            logger.warn("Error locating service", ise);
50

    
51
        } finally {
52
            logger.warn("Falling back to cached value for index : " + cachedUrl);
53
            client.setSolrServerUrl(cachedUrl);
54
            
55
            client.setInterpretation(this.interpretation);
56
        }
57

    
58
        return client;
59
    }
60

    
61
    private String getCachedIndexService(List<String> urls) {
62
        if (cachedUrl.equals("${services.index.default.url}"))
63
            return urls.get(0);
64
        return cachedUrl;
65
    }
66

    
67
    private String chooseIndexService(List<String> urls) {
68
        return urls.get(0);
69
    }
70

    
71

    
72
    public ServiceLocator<ISLookUpService> getLookUpServiceServiceLocator() {
73
        return lookUpServiceServiceLocator;
74
    }
75

    
76
    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
77
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
78
    }
79

    
80
    public String getInterpretation() {
81
        return interpretation;
82
    }
83

    
84
    public void setInterpretation(String interpretation) {
85
        this.interpretation = interpretation;
86
    }
87

    
88
    public String getCachedUrl() {
89
        return cachedUrl;
90
    }
91

    
92
    public void setCachedUrl(String cachedUrl) {
93
        this.cachedUrl = cachedUrl;
94
    }
95
}
(2-2/5)