Revision 45556
Added by Michele Artini over 7 years ago
ServiceRegistrationManager.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.InputStream; |
4 | 4 |
import java.util.ArrayList; |
5 |
import java.util.HashMap; |
|
5 | 6 |
import java.util.List; |
7 |
import java.util.Set; |
|
6 | 8 |
|
7 | 9 |
import org.antlr.stringtemplate.StringTemplate; |
8 | 10 |
import org.apache.commons.io.IOUtils; |
... | ... | |
12 | 14 |
import org.springframework.scheduling.annotation.Scheduled; |
13 | 15 |
import org.springframework.stereotype.Component; |
14 | 16 |
|
17 |
import com.google.common.collect.Sets; |
|
18 |
|
|
15 | 19 |
import eu.dnetlib.clients.ISLookUpClient; |
16 | 20 |
import eu.dnetlib.clients.ISRegistryClient; |
17 |
import eu.dnetlib.conf.DnetGenericApplicationProperties; |
|
18 | 21 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
19 | 22 |
import eu.dnetlib.exceptions.ISLookUpException; |
20 | 23 |
|
... | ... | |
34 | 37 |
@Autowired(required = false) |
35 | 38 |
private List<BaseService> services = new ArrayList<>(); |
36 | 39 |
|
37 |
@Autowired |
|
38 |
private DnetGenericApplicationProperties containerConfiguration; |
|
39 |
|
|
40 | 40 |
@Scheduled(fixedDelay = 20000) |
41 | 41 |
public void registerAllServices() throws Exception { |
42 | 42 |
|
... | ... | |
46 | 46 |
|
47 | 47 |
disabled = true; |
48 | 48 |
|
49 |
final Set<String> schemas = Sets.newHashSet(isLookup.listSchemas()); |
|
50 |
|
|
49 | 51 |
for (final BaseService service : services) { |
52 |
|
|
53 |
if (!schemas.contains(service.getServiceType())) { |
|
54 |
registerSchema(service.getServiceType()); |
|
55 |
} |
|
56 |
|
|
50 | 57 |
if (!isAlreadyRegistered(service)) { |
51 | 58 |
registerService(service); |
52 | 59 |
} |
... | ... | |
58 | 65 |
|
59 | 66 |
private boolean isAlreadyRegistered(final BaseService service) throws ISLookUpException { |
60 | 67 |
|
61 |
final String query = "for $x in //RESOURCE_PROFILE[.//RESOURCE_URI/@value='" + containerConfiguration.getUrl() + "']" |
|
62 |
+ " where contains($x//RESOURCE_TYPE/@value/string(), 'Service') return $x//RESOURCE_IDENTIFIER/@value/string()"; |
|
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()"; |
|
63 | 72 |
|
64 | 73 |
final List<String> profIds = isLookup.find(query); |
65 | 74 |
|
... | ... | |
76 | 85 |
|
77 | 86 |
final DnetServiceType type = service.getServiceType(); |
78 | 87 |
|
79 |
ensureSchemaExists(type); |
|
80 |
|
|
81 | 88 |
final String st = IOUtils.toString(getClass().getResourceAsStream("ServiceProfileTemplate.st")); |
82 | 89 |
|
83 | 90 |
final StringTemplate templ = new StringTemplate(st); |
84 | 91 |
|
85 |
final String resourceType = type + "ResourceType"; |
|
86 |
templ.setAttribute("resourceType", resourceType); |
|
87 |
templ.setAttribute("serviceName", type); |
|
88 |
templ.setAttribute("address", containerConfiguration.getUrl()); |
|
89 |
templ.setAttribute("protocols", null); |
|
90 |
templ.setAttribute("properties", null); |
|
92 |
templ.setAttribute("resourceType", type); |
|
93 |
templ.setAttribute("baseUrl", service.getBaseUrl()); |
|
94 |
templ.setAttribute("properties", new HashMap<>()); |
|
91 | 95 |
|
92 | 96 |
log.debug("template: " + templ.toString()); |
93 | 97 |
|
94 | 98 |
final String profId = isRegistry.register(templ.toString()); |
95 | 99 |
|
96 |
log.info(" registered profile for " + resourceType + ": " + profId);
|
|
100 |
log.info(" registered profile for " + type + ": " + profId);
|
|
97 | 101 |
|
98 | 102 |
service.setProfileId(profId); |
99 | 103 |
|
100 | 104 |
} |
101 | 105 |
|
102 |
private void ensureSchemaExists(final DnetServiceType type) throws Exception {
|
|
106 |
private void registerSchema(final DnetServiceType type) throws Exception {
|
|
103 | 107 |
|
104 |
final String xq = "//*[local-name() = 'complexType' and @name = 'RESOURCE_TYPEType']//*[local-name() = 'enumeration' and @value = '" + type |
|
105 |
+ "ResourceType']"; |
|
106 |
try { |
|
107 |
isLookup.findOne(xq); |
|
108 |
log.info(" schema for " + type + " appears to exist"); |
|
109 |
} catch (final ISLookUpException e) { |
|
110 |
final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st"); |
|
111 |
if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); } |
|
108 |
final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st"); |
|
109 |
if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); } |
|
112 | 110 |
|
113 |
final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream)); |
|
114 |
final String resourceType = type + "ResourceType"; |
|
115 |
schema.setAttribute("resourceType", resourceType); |
|
111 |
final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream)); |
|
116 | 112 |
|
117 |
isRegistry.registerSchema(resourceType, schema.toString());
|
|
113 |
schema.setAttribute("resourceType", type);
|
|
118 | 114 |
|
119 |
log.info(" registered schema for " + type); |
|
120 |
} |
|
115 |
isRegistry.registerSchema(type.toString(), schema.toString()); |
|
116 |
|
|
117 |
log.info(" registered schema for " + type); |
|
121 | 118 |
} |
122 | 119 |
|
123 | 120 |
public boolean isDisabled() { |
Also available in: Unified diff