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