1
|
package eu.dnetlib.enabling.tools.registration;
|
2
|
|
3
|
import java.io.InputStream;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
|
7
|
import javax.annotation.PostConstruct;
|
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.stereotype.Component;
|
15
|
|
16
|
import eu.dnetlib.clients.ISLookUpClient;
|
17
|
import eu.dnetlib.clients.ISRegistryClient;
|
18
|
import eu.dnetlib.conf.DnetGenericApplicationProperties;
|
19
|
import eu.dnetlib.enabling.annotations.DnetServiceType;
|
20
|
import eu.dnetlib.exceptions.ISLookUpException;
|
21
|
import eu.dnetlib.services.BaseService;
|
22
|
|
23
|
@Component
|
24
|
public class ServiceRegistrationManager {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(ServiceRegistrationManager.class);
|
27
|
|
28
|
@Autowired
|
29
|
private ISLookUpClient isLookup;
|
30
|
|
31
|
@Autowired
|
32
|
private ISRegistryClient isRegistry;
|
33
|
|
34
|
private boolean disabled = false;
|
35
|
|
36
|
@Autowired(required = false)
|
37
|
private List<BaseService> services = new ArrayList<>();
|
38
|
|
39
|
@Autowired
|
40
|
private DnetGenericApplicationProperties containerConfiguration;
|
41
|
|
42
|
@PostConstruct
|
43
|
public void registerAllServices() {
|
44
|
|
45
|
if (disabled) { return; }
|
46
|
|
47
|
disabled = true;
|
48
|
|
49
|
services.stream()
|
50
|
.filter(this::filterService)
|
51
|
.forEach(this::registerService);
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* check if we have already a registered service profile for this service.
|
56
|
*
|
57
|
* @throws
|
58
|
*/
|
59
|
private boolean filterService(final BaseService service) {
|
60
|
|
61
|
try {
|
62
|
|
63
|
final String query = "for $x in //RESOURCE_PROFILE[.//RESOURCE_URI/@value='" + containerConfiguration.getUrl() + "']"
|
64
|
+ " where contains($x//RESOURCE_TYPE/@value/string(), 'Service') return $x//RESOURCE_IDENTIFIER/@value/string()";
|
65
|
|
66
|
final List<String> profIds = isLookup.find(query);
|
67
|
|
68
|
if (profIds.isEmpty()) {
|
69
|
log.debug("there is no service registered for service: " + service.getServiceType());
|
70
|
return true;
|
71
|
} else {
|
72
|
service.setProfileId(profIds.get(0));
|
73
|
return false;
|
74
|
}
|
75
|
} catch (final Throwable e) {
|
76
|
log.error("Error filtering endpoint", e);
|
77
|
disabled = false;
|
78
|
return false;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
private void registerService(final BaseService service) {
|
83
|
try {
|
84
|
final DnetServiceType type = service.getServiceType();
|
85
|
|
86
|
ensureSchemaExists(type);
|
87
|
|
88
|
final String st = IOUtils.toString(getClass().getResourceAsStream("ServiceProfileTemplate.st"));
|
89
|
|
90
|
final StringTemplate templ = new StringTemplate(st);
|
91
|
|
92
|
final String resourceType = type + "ResourceType";
|
93
|
templ.setAttribute("resourceType", resourceType);
|
94
|
templ.setAttribute("serviceName", type);
|
95
|
templ.setAttribute("address", containerConfiguration.getUrl());
|
96
|
templ.setAttribute("protocols", null);
|
97
|
templ.setAttribute("properties", null);
|
98
|
|
99
|
log.debug("template: " + templ.toString());
|
100
|
|
101
|
final String profId = isRegistry.register(templ.toString());
|
102
|
|
103
|
log.info("registered profile for " + resourceType + ": " + profId);
|
104
|
|
105
|
service.setProfileId(profId);
|
106
|
} catch (final Throwable e) {
|
107
|
log.error("Error registering endpoint", e);
|
108
|
disabled = false;
|
109
|
}
|
110
|
}
|
111
|
|
112
|
private void ensureSchemaExists(final DnetServiceType type) throws Exception {
|
113
|
|
114
|
final String xq = "//*[local-name() = 'complexType' and @name = 'RESOURCE_TYPEType']//*[local-name() = 'enumeration' and @value = '" + type
|
115
|
+ "ResourceType']";
|
116
|
try {
|
117
|
isLookup.findOne(xq);
|
118
|
log.info("schema for " + type + " appears to exist");
|
119
|
} catch (final ISLookUpException e) {
|
120
|
final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st");
|
121
|
if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); }
|
122
|
|
123
|
final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream));
|
124
|
final String resourceType = type + "ResourceType";
|
125
|
schema.setAttribute("resourceType", resourceType);
|
126
|
|
127
|
isRegistry.registerSchema(resourceType, schema.toString());
|
128
|
|
129
|
log.info("registered schema for " + type);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
public boolean isDisabled() {
|
134
|
return disabled;
|
135
|
}
|
136
|
|
137
|
}
|