Project

General

Profile

1 35681 michele.ar
package eu.dnetlib.enabling.node;
2
3
import java.util.Date;
4
import java.util.List;
5 35692 michele.ar
import java.util.Map;
6 35681 michele.ar
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 35692 michele.ar
import com.google.common.collect.Maps;
14
15 35681 michele.ar
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 35692 michele.ar
import eu.dnetlib.rmi.objects.is.Operation;
20
import eu.dnetlib.rmi.objects.is.Subscription;
21 35681 michele.ar
import eu.dnetlib.rmi.soap.InformationService;
22 35692 michele.ar
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
23 35681 michele.ar
24 35692 michele.ar
public class NodeServicesRegistrator {
25 35681 michele.ar
26
	@Resource
27
	private List<AbstractBaseService> services;
28
29
	@Resource
30
	private DnetServiceLocator serviceLocator;
31
32 35696 michele.ar
	private boolean allDone = false;
33 35692 michele.ar
	private static final Log log = LogFactory.getLog(NodeServicesRegistrator.class);
34 35681 michele.ar
35 35692 michele.ar
	public void register() throws Exception {
36 35696 michele.ar
		synchronized (this) {
37
			if (!allDone) {
38
				registerServices();
39
				addBlackboardSubscriptions();
40
				log.info("*** Services and subscriptions have been registered ***");
41
				allDone = true;
42
			}
43
		}
44 35692 michele.ar
	}
45 35681 michele.ar
46 35696 michele.ar
	private void addBlackboardSubscriptions() throws InformationServiceException {
47 35692 michele.ar
		final String managerId = findNodeManagerService().getServiceId();
48
		final InformationService is = findInformationService();
49
50
		is.deleteAllServiceSubscriptions(managerId);
51
52 35681 michele.ar
		for (AbstractBaseService service : services) {
53 35692 michele.ar
			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 35681 michele.ar
			}
60
		}
61
	}
62
63 35696 michele.ar
	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 35681 michele.ar
					}
76
				}
77 35696 michele.ar
			} else {
78
				log.warn("Service registration failed: infomationService not found");
79 35681 michele.ar
			}
80 35696 michele.ar
		} catch (Throwable e) {
81
			log.warn("Service registration failed: " + e.getMessage());
82
			log.debug(e);
83 35692 michele.ar
		}
84
	}
85 35681 michele.ar
86 35692 michele.ar
	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 35681 michele.ar
	}
95 35696 michele.ar
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 35681 michele.ar
}