Revision 45465
Added by Michele Artini over 7 years ago
ServiceRegistrator.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.enabling.tools.registration; |
2 | 2 |
|
3 | 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 | 4 |
|
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 | 5 |
import org.antlr.stringtemplate.StringTemplate; |
23 | 6 |
import org.apache.commons.io.IOUtils; |
24 | 7 |
import org.apache.commons.logging.Log; |
25 | 8 |
import org.apache.commons.logging.LogFactory; |
26 |
import org.springframework.beans.factory.annotation.Required;
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
27 | 10 |
|
28 |
/** |
|
29 |
* This class takes care of registering a service. |
|
30 |
* |
|
31 |
* TODO: merge the implementation |
|
32 |
* |
|
33 |
* @author marko |
|
34 |
* |
|
35 |
*/ |
|
11 |
import eu.dnetlib.clients.ISLookUpClient; |
|
12 |
import eu.dnetlib.clients.ISRegistryClient; |
|
13 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
|
14 |
import eu.dnetlib.enabling.tools.BaseService; |
|
15 |
import eu.dnetlib.exceptions.ISLookUpException; |
|
16 |
|
|
36 | 17 |
public class ServiceRegistrator { |
37 | 18 |
|
38 |
/** |
|
39 |
* logger. |
|
40 |
*/ |
|
41 | 19 |
private static final Log log = LogFactory.getLog(ServiceRegistrator.class); |
42 | 20 |
|
43 |
/**
|
|
44 |
* locator.
|
|
45 |
*/
|
|
46 |
private UniqueServiceLocator serviceLocator;
|
|
21 |
@Autowired
|
|
22 |
private ISLookUpClient isLookup;
|
|
23 |
@Autowired
|
|
24 |
private ISRegistryClient isRegistry;
|
|
47 | 25 |
|
48 | 26 |
/** |
49 |
* epr builder. |
|
50 |
*/ |
|
51 |
private EndpointReferenceBuilder<Endpoint> eprBuilder; |
|
52 |
|
|
53 |
/** |
|
54 | 27 |
* component which finds an hnm profile. |
28 |
* |
|
29 |
* @param url |
|
55 | 30 |
*/ |
56 |
private HNMLocator hnmLocator = new NullHNMLocator(); |
|
57 | 31 |
|
32 |
public String registerService(final BaseService service, final String url) throws Exception { |
|
58 | 33 |
|
34 |
final DnetServiceType type = service.getServiceType(); |
|
59 | 35 |
|
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 |
} |
|
36 |
ensureSchemaExists(type); |
|
69 | 37 |
|
70 |
private String registerService(final String serviceName, final W3CEndpointReference epr, final Map<String, String> serviceProperties, |
|
71 |
final Map<String, String> extraProtocols) throws Exception { |
|
38 |
final String st = IOUtils.toString(getClass().getResourceAsStream("ServiceProfileTemplate.st")); |
|
72 | 39 |
|
73 |
ensureSchemaExists(serviceName);
|
|
40 |
final StringTemplate templ = new StringTemplate(st);
|
|
74 | 41 |
|
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 |
|
|
42 |
final String resourceType = type + "ResourceType"; |
|
94 | 43 |
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); |
|
44 |
templ.setAttribute("serviceName", type); |
|
45 |
templ.setAttribute("address", url); |
|
46 |
templ.setAttribute("protocols", null); |
|
47 |
templ.setAttribute("properties", null); |
|
100 | 48 |
|
101 | 49 |
log.debug("template: " + templ.toString()); |
102 | 50 |
|
103 |
final String profId = this.serviceLocator.getService(ISRegistryService.class, true).registerProfile(templ.toString());
|
|
51 |
final String profId = isRegistry.register(templ.toString());
|
|
104 | 52 |
|
105 |
final String topic = "UPDATE/" + resourceType + "/" + profId + "/RESOURCE_PROFILE/BODY/BLACKBOARD/LAST_REQUEST";
|
|
53 |
log.info("registered profile for " + resourceType + ": " + profId);
|
|
106 | 54 |
|
107 |
getServiceLocator().getService(ISSNService.class, true).subscribe(epr, topic, 0); |
|
108 |
|
|
109 |
log.info("registered profile for " + serviceName + ": " + profId); |
|
110 |
|
|
111 | 55 |
return profId; |
112 | 56 |
} |
113 | 57 |
|
114 | 58 |
/** |
115 | 59 |
* Check that the service schema for this service already exists, and create it if it doesn't. |
116 | 60 |
* |
117 |
* @param serviceName
|
|
61 |
* @param type
|
|
118 | 62 |
* service name |
119 | 63 |
*/ |
120 |
protected void ensureSchemaExists(final String serviceName) throws Exception { |
|
64 |
protected void ensureSchemaExists(final DnetServiceType type) throws Exception { |
|
65 |
|
|
66 |
final String xq = "//*[local-name() = 'complexType' and @name = 'RESOURCE_TYPEType']//*[local-name() = 'enumeration' and @value = '" + type |
|
67 |
+ "ResourceType']"; |
|
121 | 68 |
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); |
|
69 |
isLookup.findOne(xq); |
|
70 |
log.info("schema for " + type + " appears to exist"); |
|
71 |
} catch (final ISLookUpException e) { |
|
72 |
registerServiceSchema(type); |
|
128 | 73 |
} |
129 | 74 |
} |
130 | 75 |
|
131 |
private void registerServiceSchema(final String serviceName) throws Exception {
|
|
76 |
private void registerServiceSchema(final DnetServiceType type) throws Exception {
|
|
132 | 77 |
|
133 | 78 |
final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st"); |
134 | 79 |
if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); } |
135 | 80 |
|
136 | 81 |
final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream)); |
137 |
|
|
138 |
final String resourceType = serviceName + "ResourceType"; |
|
82 |
final String resourceType = type + "ResourceType"; |
|
139 | 83 |
schema.setAttribute("resourceType", resourceType); |
140 | 84 |
|
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 |
} |
|
85 |
isRegistry.registerSchema(resourceType, schema.toString()); |
|
150 | 86 |
|
151 |
registry.addResourceType(resourceType, schema.toString());
|
|
87 |
log.info("registered schema for " + type);
|
|
152 | 88 |
|
153 |
log.info("registered schema for " + serviceName); |
|
154 |
|
|
155 | 89 |
} |
156 | 90 |
|
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 | 91 |
} |
Also available in: Unified diff