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
    @Override
22
    public IndexService getService() {
23
        if (indexClient == null) {
24
            indexClient = locateIndexService();
25
        }
26

    
27
        return indexClient;
28
    }
29

    
30
    private SolrIndexClient locateIndexService() {
31
       SolrIndexClient client = new SolrIndexClient();
32

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

    
36
            if (urls.size() > 0) {
37
                String url = chooseIndexService(urls);
38

    
39
                client.setSolrServerUrl(url);
40
                client.setInterpretation(this.interpretation);
41
            } else {
42
                throw new IllegalArgumentException("Could not locate an index service with a SOLR interface");
43
            }
44
        } catch (ISLookUpServiceException e) {
45
            logger.error("Error locating service", e);
46
        }
47

    
48
        return client;
49
    }
50

    
51
    private String chooseIndexService(List<String> urls) {
52
        return urls.get(0);
53
    }
54

    
55
    public ServiceLocator<ISLookUpService> getLookUpServiceServiceLocator() {
56
        return lookUpServiceServiceLocator;
57
    }
58

    
59
    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
60
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
61
    }
62

    
63
    public String getInterpretation() {
64
        return interpretation;
65
    }
66

    
67
    public void setInterpretation(String interpretation) {
68
        this.interpretation = interpretation;
69
    }
70
}
(1-1/5)