1
|
package eu.dnetlib.enabling.node;
|
2
|
|
3
|
import java.util.Date;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import javax.annotation.Resource;
|
8
|
|
9
|
import org.apache.commons.lang.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
13
|
import com.google.common.collect.Maps;
|
14
|
|
15
|
import eu.dnetlib.common.services.AbstractBaseService;
|
16
|
import eu.dnetlib.common.services.ServiceNameResolver;
|
17
|
import eu.dnetlib.common.services.locators.DnetServiceLocator;
|
18
|
import eu.dnetlib.rmi.objects.is.DnetService;
|
19
|
import eu.dnetlib.rmi.objects.is.Operation;
|
20
|
import eu.dnetlib.rmi.objects.is.Subscription;
|
21
|
import eu.dnetlib.rmi.soap.InformationService;
|
22
|
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
|
23
|
|
24
|
public class NodeServicesRegistrator {
|
25
|
|
26
|
@Resource
|
27
|
private List<AbstractBaseService> services;
|
28
|
|
29
|
@Resource
|
30
|
private DnetServiceLocator serviceLocator;
|
31
|
|
32
|
private boolean allDone = false;
|
33
|
private static final Log log = LogFactory.getLog(NodeServicesRegistrator.class);
|
34
|
|
35
|
public void register() throws Exception {
|
36
|
synchronized (this) {
|
37
|
if (!allDone) {
|
38
|
registerServices();
|
39
|
addBlackboardSubscriptions();
|
40
|
log.info("*** Services and subscriptions have been registered ***");
|
41
|
allDone = true;
|
42
|
}
|
43
|
}
|
44
|
}
|
45
|
|
46
|
private void addBlackboardSubscriptions() throws InformationServiceException {
|
47
|
final String managerId = findNodeManagerService().getServiceId();
|
48
|
final InformationService is = findInformationService();
|
49
|
|
50
|
is.deleteAllServiceSubscriptions(managerId);
|
51
|
|
52
|
for (AbstractBaseService service : services) {
|
53
|
if (StringUtils.isNotBlank(service.getServiceId())) {
|
54
|
final Map<String, Object> cond = Maps.newHashMap();
|
55
|
cond.put("service", service.getServiceId());
|
56
|
is.addSubscription(new Subscription(null, managerId, Operation.INSERT, "blackboard", cond));
|
57
|
is.addSubscription(new Subscription(null, managerId, Operation.UPDATE, "blackboard", cond));
|
58
|
is.addSubscription(new Subscription(null, managerId, Operation.DELETE, "blackboard", cond));
|
59
|
}
|
60
|
}
|
61
|
}
|
62
|
|
63
|
private void registerServices() {
|
64
|
try {
|
65
|
final InformationService is = findInformationService();
|
66
|
if (is != null) {
|
67
|
for (AbstractBaseService service : services) {
|
68
|
final String addr = service.getProtocols().get(service.getMainProtocol());
|
69
|
if (StringUtils.isNotBlank(addr)) {
|
70
|
final String id = is.registerService(asDnetService(service));
|
71
|
service.setServiceId(id);
|
72
|
log.info("Service " + id + " registered/updated");
|
73
|
} else {
|
74
|
log.error("MainProtocol (" + service.getMainProtocol() + ") is missing, service: " + service.getClass());
|
75
|
}
|
76
|
}
|
77
|
} else {
|
78
|
log.warn("Service registration failed: infomationService not found");
|
79
|
}
|
80
|
} catch (Throwable e) {
|
81
|
log.warn("Service registration failed: " + e.getMessage());
|
82
|
log.debug(e);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
private DnetService asDnetService(final AbstractBaseService s) {
|
87
|
final DnetService srv = new DnetService();
|
88
|
srv.setName(ServiceNameResolver.resolveServiceName(s.getClass()));
|
89
|
srv.setDate(new Date());
|
90
|
srv.setValid(true);
|
91
|
srv.setProtocols(s.getProtocols());
|
92
|
srv.setProperties(s.getProperties());
|
93
|
return srv;
|
94
|
}
|
95
|
|
96
|
private InformationService findInformationService() {
|
97
|
for (AbstractBaseService s : services) {
|
98
|
if (s instanceof InformationService) { return (InformationService) s; }
|
99
|
}
|
100
|
return serviceLocator.getService(InformationService.class);
|
101
|
}
|
102
|
|
103
|
private NodeManagerServiceImpl findNodeManagerService() {
|
104
|
for (AbstractBaseService s : services) {
|
105
|
if (s instanceof NodeManagerServiceImpl) { return (NodeManagerServiceImpl) s; }
|
106
|
}
|
107
|
throw new RuntimeException("NodeManager service not found");
|
108
|
}
|
109
|
}
|