1
|
package eu.dnetlib.enabling.tools.registration;
|
2
|
|
3
|
import java.io.InputStream;
|
4
|
import java.io.StringWriter;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.Map;
|
7
|
import javax.xml.transform.dom.DOMResult;
|
8
|
import javax.xml.ws.Endpoint;
|
9
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
10
|
|
11
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
12
|
import eu.dnetlib.enabling.tools.AbstractBaseService;
|
13
|
import eu.dnetlib.enabling.tools.BaseServiceUtils;
|
14
|
import eu.dnetlib.enabling.tools.HNMLocator;
|
15
|
import eu.dnetlib.enabling.tools.NullHNMLocator;
|
16
|
import eu.dnetlib.rmi.common.BaseService;
|
17
|
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
|
18
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
19
|
import eu.dnetlib.rmi.enabling.ISRegistryService;
|
20
|
import eu.dnetlib.rmi.enabling.ISSNService;
|
21
|
import eu.dnetlib.soap.EndpointReferenceBuilder;
|
22
|
import org.antlr.stringtemplate.StringTemplate;
|
23
|
import org.apache.commons.io.IOUtils;
|
24
|
import org.apache.commons.logging.Log;
|
25
|
import org.apache.commons.logging.LogFactory;
|
26
|
import org.springframework.beans.factory.annotation.Required;
|
27
|
|
28
|
/**
|
29
|
* This class takes care of registering a service.
|
30
|
*
|
31
|
* TODO: merge the implementation
|
32
|
*
|
33
|
* @author marko
|
34
|
*
|
35
|
*/
|
36
|
public class ServiceRegistrator {
|
37
|
|
38
|
/**
|
39
|
* logger.
|
40
|
*/
|
41
|
private static final Log log = LogFactory.getLog(ServiceRegistrator.class);
|
42
|
|
43
|
/**
|
44
|
* locator.
|
45
|
*/
|
46
|
private UniqueServiceLocator serviceLocator;
|
47
|
|
48
|
/**
|
49
|
* epr builder.
|
50
|
*/
|
51
|
private EndpointReferenceBuilder<Endpoint> eprBuilder;
|
52
|
|
53
|
/**
|
54
|
* component which finds an hnm profile.
|
55
|
*/
|
56
|
private HNMLocator hnmLocator = new NullHNMLocator();
|
57
|
|
58
|
|
59
|
|
60
|
public String registerService(final BaseService service, final Endpoint endpoint) throws Exception {
|
61
|
if (service instanceof AbstractBaseService) {
|
62
|
AbstractBaseService abs = (AbstractBaseService) service;
|
63
|
return registerService(BaseServiceUtils.getServiceName(service.getClass()), this.eprBuilder.getEndpointReference(endpoint),
|
64
|
abs.getServiceProperties(), abs.getExtraProtocols());
|
65
|
}
|
66
|
return registerService(BaseServiceUtils.getServiceName(service.getClass()), this.eprBuilder.getEndpointReference(endpoint), new HashMap<>(),
|
67
|
new HashMap<>());
|
68
|
}
|
69
|
|
70
|
private String registerService(final String serviceName, final W3CEndpointReference epr, final Map<String, String> serviceProperties,
|
71
|
final Map<String, String> extraProtocols) throws Exception {
|
72
|
|
73
|
ensureSchemaExists(serviceName);
|
74
|
|
75
|
final DOMResult result = new DOMResult();
|
76
|
epr.writeTo(result);
|
77
|
|
78
|
final InputStream templateStream = getClass().getResourceAsStream("ServiceProfileTemplate.st");
|
79
|
if (templateStream == null) { throw new IllegalStateException("cannot find service profile template"); }
|
80
|
|
81
|
final StringWriter buffer = new StringWriter();
|
82
|
IOUtils.copy(templateStream, buffer);
|
83
|
|
84
|
final StringTemplate templ = new StringTemplate(buffer.toString());
|
85
|
|
86
|
final String resourceType = serviceName + "ResourceType";
|
87
|
|
88
|
final String address = result.getNode().getChildNodes().item(0).getChildNodes().item(0).getTextContent();
|
89
|
final String hnmId = this.hnmLocator.getHNMForUrl(address);
|
90
|
|
91
|
// skip registration if there is no HNM yet.
|
92
|
if (hnmId == null) { return null; }
|
93
|
|
94
|
templ.setAttribute("resourceType", resourceType);
|
95
|
templ.setAttribute("serviceName", serviceName);
|
96
|
templ.setAttribute("address", address);
|
97
|
templ.setAttribute("protocols", extraProtocols);
|
98
|
templ.setAttribute("parentId", hnmId);
|
99
|
templ.setAttribute("properties", serviceProperties);
|
100
|
|
101
|
log.debug("template: " + templ.toString());
|
102
|
|
103
|
final String profId = this.serviceLocator.getService(ISRegistryService.class, true).registerProfile(templ.toString());
|
104
|
|
105
|
final String topic = "UPDATE/" + resourceType + "/" + profId + "/RESOURCE_PROFILE/BODY/BLACKBOARD/LAST_REQUEST";
|
106
|
|
107
|
getServiceLocator().getService(ISSNService.class, true).subscribe(epr, topic, 0);
|
108
|
|
109
|
log.info("registered profile for " + serviceName + ": " + profId);
|
110
|
|
111
|
return profId;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Check that the service schema for this service already exists, and create it if it doesn't.
|
116
|
*
|
117
|
* @param serviceName
|
118
|
* service name
|
119
|
*/
|
120
|
protected void ensureSchemaExists(final String serviceName) throws Exception {
|
121
|
try {
|
122
|
this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
123
|
"//*[local-name() = 'complexType' and @name = 'RESOURCE_TYPEType']//*[local-name() = 'enumeration' and @value = '" + serviceName
|
124
|
+ "ResourceType']");
|
125
|
log.info("schema for " + serviceName + " appears to exist");
|
126
|
} catch (final ISLookUpDocumentNotFoundException e) {
|
127
|
registerServiceSchema(serviceName);
|
128
|
}
|
129
|
}
|
130
|
|
131
|
private void registerServiceSchema(final String serviceName) throws Exception {
|
132
|
|
133
|
final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st");
|
134
|
if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); }
|
135
|
|
136
|
final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream));
|
137
|
|
138
|
final String resourceType = serviceName + "ResourceType";
|
139
|
schema.setAttribute("resourceType", resourceType);
|
140
|
|
141
|
if (this.serviceLocator == null) {
|
142
|
log.error("************* SERVICE LOCATOR IS NULL:" + serviceName);
|
143
|
return;
|
144
|
}
|
145
|
final ISRegistryService registry = this.serviceLocator.getService(ISRegistryService.class, true);
|
146
|
if (registry == null) {
|
147
|
log.error("************* REGISTRY SERVICE IS NULL");
|
148
|
return;
|
149
|
}
|
150
|
|
151
|
registry.addResourceType(resourceType, schema.toString());
|
152
|
|
153
|
log.info("registered schema for " + serviceName);
|
154
|
|
155
|
}
|
156
|
|
157
|
public EndpointReferenceBuilder<Endpoint> getEprBuilder() {
|
158
|
return this.eprBuilder;
|
159
|
}
|
160
|
|
161
|
@Required
|
162
|
public void setEprBuilder(final EndpointReferenceBuilder<Endpoint> eprBuilder) {
|
163
|
this.eprBuilder = eprBuilder;
|
164
|
}
|
165
|
|
166
|
public HNMLocator getHnmLocator() {
|
167
|
return this.hnmLocator;
|
168
|
}
|
169
|
|
170
|
public void setHnmLocator(final HNMLocator hnmLocator) {
|
171
|
this.hnmLocator = hnmLocator;
|
172
|
}
|
173
|
|
174
|
public UniqueServiceLocator getServiceLocator() {
|
175
|
return this.serviceLocator;
|
176
|
}
|
177
|
|
178
|
@Required
|
179
|
public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
|
180
|
this.serviceLocator = serviceLocator;
|
181
|
}
|
182
|
}
|