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
                logger.warn("Falling back to cached value for index : " + cachedUrl);
44
                throw new IllegalArgumentException("Could not locate an index service with a SOLR interface");
45
            }
46

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

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

    
57
        return client;
58
    }
59

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

    
66
    public ServiceLocator<ISLookUpService> getLookUpServiceServiceLocator() {
67
        return lookUpServiceServiceLocator;
68
    }
69

    
70
    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
71
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
72
    }
73

    
74
    public String getInterpretation() {
75
        return interpretation;
76
    }
77

    
78
    public void setInterpretation(String interpretation) {
79
        this.interpretation = interpretation;
80
    }
81

    
82
    public String getCachedUrl() {
83
        return cachedUrl;
84
    }
85

    
86
    public void setCachedUrl(String cachedUrl) {
87
        this.cachedUrl = cachedUrl;
88
    }
89
}
(2-2/5)