Project

General

Profile

1 25725 antonis.le
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 25736 antonis.le
    private String interpretation = null;
20 25725 antonis.le
21 45684 katerina.i
    private String cachedUrl = null;
22
23 25725 antonis.le
    @Override
24
    public IndexService getService() {
25
        if (indexClient == null) {
26
            indexClient = locateIndexService();
27
        }
28
29
        return indexClient;
30
    }
31
32
    private SolrIndexClient locateIndexService() {
33 47316 katerina.i
        SolrIndexClient client = new SolrIndexClient();
34 25725 antonis.le
35
        try {
36 30806 katerina.i
            List<String> urls = lookUpServiceServiceLocator.getService().quickSearchProfile("for $x in //RESOURCE_PROFILE//PROTOCOL[./@name='solr'] return data($x/@address)");
37 25725 antonis.le
38
            if (urls.size() > 0) {
39 58157 katerina.i
                logger.debug("cached url " + cachedUrl);
40 45684 katerina.i
                cachedUrl = chooseIndexService(urls);
41 25725 antonis.le
42
            } else {
43 45684 katerina.i
                logger.warn("Falling back to cached value for index : " + cachedUrl);
44 25725 antonis.le
                throw new IllegalArgumentException("Could not locate an index service with a SOLR interface");
45
            }
46 45684 katerina.i
47 47316 katerina.i
        } catch (ISLookUpServiceException ise) {
48
            logger.warn("Error locating service", ise);
49 45684 katerina.i
50
        } finally {
51 47316 katerina.i
            logger.warn("Falling back to cached value for index : " + cachedUrl);
52 45684 katerina.i
            client.setSolrServerUrl(cachedUrl);
53 58157 katerina.i
54 45684 katerina.i
            client.setInterpretation(this.interpretation);
55 25725 antonis.le
        }
56
57
        return client;
58
    }
59
60
    private String chooseIndexService(List<String> urls) {
61 58157 katerina.i
        if (cachedUrl.equals("${services.index.default.url}"))
62
            return urls.get(0);
63
        return cachedUrl;
64 25725 antonis.le
    }
65
66
    public ServiceLocator<ISLookUpService> getLookUpServiceServiceLocator() {
67
        return lookUpServiceServiceLocator;
68
    }
69
70
    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
71
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
72
    }
73 25736 antonis.le
74
    public String getInterpretation() {
75
        return interpretation;
76
    }
77
78
    public void setInterpretation(String interpretation) {
79
        this.interpretation = interpretation;
80
    }
81 45684 katerina.i
82
    public String getCachedUrl() {
83
        return cachedUrl;
84
    }
85
86
    public void setCachedUrl(String cachedUrl) {
87
        this.cachedUrl = cachedUrl;
88
    }
89 25725 antonis.le
}