1 |
45547
|
michele.ar
|
package eu.dnetlib.services;
|
2 |
41601
|
sandro.lab
|
|
3 |
45542
|
michele.ar
|
import java.io.InputStream;
|
4 |
45421
|
michele.ar
|
import java.util.ArrayList;
|
5 |
45556
|
michele.ar
|
import java.util.HashMap;
|
6 |
41742
|
michele.ar
|
import java.util.List;
|
7 |
45556
|
michele.ar
|
import java.util.Set;
|
8 |
45421
|
michele.ar
|
|
9 |
45542
|
michele.ar
|
import org.antlr.stringtemplate.StringTemplate;
|
10 |
|
|
import org.apache.commons.io.IOUtils;
|
11 |
45421
|
michele.ar
|
import org.apache.commons.logging.Log;
|
12 |
|
|
import org.apache.commons.logging.LogFactory;
|
13 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
14 |
45547
|
michele.ar
|
import org.springframework.scheduling.annotation.Scheduled;
|
15 |
45542
|
michele.ar
|
import org.springframework.stereotype.Component;
|
16 |
45421
|
michele.ar
|
|
17 |
45556
|
michele.ar
|
import com.google.common.collect.Sets;
|
18 |
|
|
|
19 |
45465
|
michele.ar
|
import eu.dnetlib.clients.ISLookUpClient;
|
20 |
45542
|
michele.ar
|
import eu.dnetlib.clients.ISRegistryClient;
|
21 |
|
|
import eu.dnetlib.enabling.annotations.DnetServiceType;
|
22 |
|
|
import eu.dnetlib.exceptions.ISLookUpException;
|
23 |
41601
|
sandro.lab
|
|
24 |
45542
|
michele.ar
|
@Component
|
25 |
41742
|
michele.ar
|
public class ServiceRegistrationManager {
|
26 |
41601
|
sandro.lab
|
|
27 |
41742
|
michele.ar
|
private static final Log log = LogFactory.getLog(ServiceRegistrationManager.class);
|
28 |
41601
|
sandro.lab
|
|
29 |
42621
|
sandro.lab
|
@Autowired
|
30 |
45465
|
michele.ar
|
private ISLookUpClient isLookup;
|
31 |
41601
|
sandro.lab
|
|
32 |
45542
|
michele.ar
|
@Autowired
|
33 |
|
|
private ISRegistryClient isRegistry;
|
34 |
41601
|
sandro.lab
|
|
35 |
|
|
private boolean disabled = false;
|
36 |
|
|
|
37 |
45421
|
michele.ar
|
@Autowired(required = false)
|
38 |
45465
|
michele.ar
|
private List<BaseService> services = new ArrayList<>();
|
39 |
41601
|
sandro.lab
|
|
40 |
45547
|
michele.ar
|
@Scheduled(fixedDelay = 20000)
|
41 |
|
|
public void registerAllServices() throws Exception {
|
42 |
41601
|
sandro.lab
|
|
43 |
45421
|
michele.ar
|
if (disabled) { return; }
|
44 |
41601
|
sandro.lab
|
|
45 |
45547
|
michele.ar
|
log.info("registering services...");
|
46 |
|
|
|
47 |
45421
|
michele.ar
|
disabled = true;
|
48 |
41601
|
sandro.lab
|
|
49 |
45556
|
michele.ar
|
final Set<String> schemas = Sets.newHashSet(isLookup.listSchemas());
|
50 |
|
|
|
51 |
45547
|
michele.ar
|
for (final BaseService service : services) {
|
52 |
45556
|
michele.ar
|
|
53 |
|
|
if (!schemas.contains(service.getServiceType())) {
|
54 |
|
|
registerSchema(service.getServiceType());
|
55 |
|
|
}
|
56 |
|
|
|
57 |
45547
|
michele.ar
|
if (!isAlreadyRegistered(service)) {
|
58 |
|
|
registerService(service);
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
log.info("...all services registered");
|
63 |
|
|
|
64 |
41601
|
sandro.lab
|
}
|
65 |
|
|
|
66 |
45547
|
michele.ar
|
private boolean isAlreadyRegistered(final BaseService service) throws ISLookUpException {
|
67 |
41601
|
sandro.lab
|
|
68 |
45556
|
michele.ar
|
final String query = "for $x in collection('/db/DRIVER/DnetService') "
|
69 |
|
|
+ "where $x//baseUrl='" + service.getBaseUrl() + "' "
|
70 |
|
|
+ "and $x//type/@value = '" + service.getServiceType() + "' "
|
71 |
|
|
+ "return $x//id/text()";
|
72 |
41601
|
sandro.lab
|
|
73 |
45547
|
michele.ar
|
final List<String> profIds = isLookup.find(query);
|
74 |
41601
|
sandro.lab
|
|
75 |
45547
|
michele.ar
|
if (profIds.isEmpty()) {
|
76 |
|
|
log.debug("there is no service registered for service: " + service.getServiceType());
|
77 |
41742
|
michele.ar
|
return false;
|
78 |
45547
|
michele.ar
|
} else {
|
79 |
|
|
service.setProfileId(profIds.get(0));
|
80 |
|
|
return true;
|
81 |
41601
|
sandro.lab
|
}
|
82 |
|
|
}
|
83 |
|
|
|
84 |
45547
|
michele.ar
|
private void registerService(final BaseService service) throws Exception {
|
85 |
45542
|
michele.ar
|
|
86 |
45547
|
michele.ar
|
final DnetServiceType type = service.getServiceType();
|
87 |
45542
|
michele.ar
|
|
88 |
45547
|
michele.ar
|
final String st = IOUtils.toString(getClass().getResourceAsStream("ServiceProfileTemplate.st"));
|
89 |
45542
|
michele.ar
|
|
90 |
45547
|
michele.ar
|
final StringTemplate templ = new StringTemplate(st);
|
91 |
45542
|
michele.ar
|
|
92 |
45556
|
michele.ar
|
templ.setAttribute("resourceType", type);
|
93 |
|
|
templ.setAttribute("baseUrl", service.getBaseUrl());
|
94 |
|
|
templ.setAttribute("properties", new HashMap<>());
|
95 |
45542
|
michele.ar
|
|
96 |
45547
|
michele.ar
|
final String profId = isRegistry.register(templ.toString());
|
97 |
45542
|
michele.ar
|
|
98 |
45558
|
michele.ar
|
log.info(" new service: " + profId);
|
99 |
45547
|
michele.ar
|
|
100 |
|
|
service.setProfileId(profId);
|
101 |
|
|
|
102 |
41601
|
sandro.lab
|
}
|
103 |
|
|
|
104 |
45556
|
michele.ar
|
private void registerSchema(final DnetServiceType type) throws Exception {
|
105 |
41601
|
sandro.lab
|
|
106 |
45556
|
michele.ar
|
final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st");
|
107 |
|
|
if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); }
|
108 |
45542
|
michele.ar
|
|
109 |
45556
|
michele.ar
|
final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream));
|
110 |
45542
|
michele.ar
|
|
111 |
45556
|
michele.ar
|
schema.setAttribute("resourceType", type);
|
112 |
45542
|
michele.ar
|
|
113 |
45556
|
michele.ar
|
isRegistry.registerSchema(type.toString(), schema.toString());
|
114 |
|
|
|
115 |
45558
|
michele.ar
|
log.info(" new schema : " + type);
|
116 |
41601
|
sandro.lab
|
}
|
117 |
|
|
|
118 |
41742
|
michele.ar
|
public boolean isDisabled() {
|
119 |
45421
|
michele.ar
|
return disabled;
|
120 |
41601
|
sandro.lab
|
}
|
121 |
|
|
|
122 |
|
|
}
|