1 | package eu.clarin.sru.fcs.aggregator.scan; |
---|
2 | |
---|
3 | import com.sun.jersey.api.client.Client; |
---|
4 | import eu.clarin.weblicht.bindings.cmd.StringBinding; |
---|
5 | import eu.clarin.weblicht.bindings.cmd.cp.CenterExtendedInformation; |
---|
6 | import eu.clarin.weblicht.bindings.cmd.cp.CenterProfile; |
---|
7 | import eu.clarin.weblicht.bindings.cmd.cp.WebReference; |
---|
8 | import eu.clarin.weblicht.bindings.cr.Center; |
---|
9 | import eu.clarin.weblicht.connectors.ConnectorException; |
---|
10 | import eu.clarin.weblicht.connectors.cr.CenterRegistryConnector; |
---|
11 | import java.net.URI; |
---|
12 | import java.util.ArrayList; |
---|
13 | import java.util.List; |
---|
14 | import java.util.logging.Level; |
---|
15 | import java.util.logging.Logger; |
---|
16 | |
---|
17 | /** |
---|
18 | * Center registry node. Its children are centers (institutions). |
---|
19 | * |
---|
20 | * @author Yana Panchenko |
---|
21 | * @author edima |
---|
22 | */ |
---|
23 | public class CenterRegistryLive implements CenterRegistry { |
---|
24 | |
---|
25 | private static final Logger LOGGER = Logger.getLogger(CenterRegistryLive.class.getName()); |
---|
26 | |
---|
27 | private static final Integer CONNECT_TIMEOUT = 3000; |
---|
28 | private static final Integer READ_TIMEOUT = 10000; |
---|
29 | |
---|
30 | private String centerRegistryUrl; |
---|
31 | private boolean hasInstitutionsLoaded = false; |
---|
32 | private List<Institution> centers = new ArrayList<Institution>(); |
---|
33 | private final EndpointFilter filter; |
---|
34 | private final Client client; |
---|
35 | |
---|
36 | public CenterRegistryLive(String centerRegistryUrl, EndpointFilter filter) { |
---|
37 | super(); |
---|
38 | this.centerRegistryUrl = centerRegistryUrl; |
---|
39 | this.filter = filter; |
---|
40 | this.client = ClientFactory.create(CONNECT_TIMEOUT, READ_TIMEOUT); |
---|
41 | } |
---|
42 | |
---|
43 | @Override |
---|
44 | public boolean hasCQLInstitutionsLoaded() { |
---|
45 | return hasInstitutionsLoaded; |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public void loadCQLInstitutions() { |
---|
50 | if (hasInstitutionsLoaded) { |
---|
51 | return; |
---|
52 | } |
---|
53 | hasInstitutionsLoaded = true; |
---|
54 | URI url = URI.create(centerRegistryUrl); |
---|
55 | try (CenterRegistryConnector connector = new CenterRegistryConnector(client, url)) { |
---|
56 | List<Center> regCenters = connector.retrieveCenters(); |
---|
57 | for (Center regCenter : regCenters) { |
---|
58 | String institutionUrl = regCenter.getId(); |
---|
59 | String institutionName = regCenter.getCenterName(); |
---|
60 | Institution institution = new Institution(institutionName, institutionUrl); |
---|
61 | // display in the tree only those institutions that have CQL endpoints: |
---|
62 | CenterProfile profile = connector.retrieveCenterProfile(regCenter); |
---|
63 | CenterExtendedInformation info = profile.getCenterExtendedInformation(); |
---|
64 | List<WebReference> webRefs = info.getWebReference(); |
---|
65 | if (webRefs != null) { |
---|
66 | for (WebReference webRef : webRefs) { |
---|
67 | List<StringBinding> sbs = webRef.getDescription(); |
---|
68 | for (StringBinding sb : sbs) { |
---|
69 | if ("CQL".equals(sb.getValue())) { |
---|
70 | String endpoint = webRef.getWebsite(); |
---|
71 | if (filter == null || filter.filter(endpoint)) { |
---|
72 | institution.addEndpoint(endpoint); |
---|
73 | } |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | if (!institution.getEndpoints().isEmpty()) { |
---|
80 | centers.add(institution); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | } catch (ConnectorException ex) { |
---|
85 | Logger.getLogger(CenterRegistryLive.class.getName()).log(Level.SEVERE, null, ex); |
---|
86 | } |
---|
87 | LOGGER.log(Level.FINE, "Number of Centers: {0}", centers.size()); |
---|
88 | } |
---|
89 | |
---|
90 | @Override |
---|
91 | public List<Institution> getCQLInstitutions() { |
---|
92 | loadCQLInstitutions(); |
---|
93 | return centers; |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | public Institution getCQLInstitution(int index) { |
---|
98 | loadCQLInstitutions(); |
---|
99 | if (index >= centers.size()) { |
---|
100 | return null; |
---|
101 | } |
---|
102 | return centers.get(index); |
---|
103 | } |
---|
104 | } |
---|