1
|
package eu.dnetlib.common.services;
|
2
|
|
3
|
import java.util.Date;
|
4
|
import java.util.Map;
|
5
|
import java.util.concurrent.Callable;
|
6
|
import java.util.concurrent.ExecutionException;
|
7
|
import java.util.concurrent.ExecutorService;
|
8
|
import java.util.concurrent.Executors;
|
9
|
import java.util.concurrent.Future;
|
10
|
|
11
|
import javax.annotation.PostConstruct;
|
12
|
import javax.annotation.Resource;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.springframework.beans.factory.annotation.Required;
|
18
|
|
19
|
import com.google.common.collect.Maps;
|
20
|
|
21
|
import eu.dnetlib.common.services.locators.UniqueServiceLocator;
|
22
|
import eu.dnetlib.rmi.objects.is.DnetService;
|
23
|
import eu.dnetlib.rmi.soap.BaseService;
|
24
|
import eu.dnetlib.rmi.soap.InformationService;
|
25
|
|
26
|
public class DnetServiceRegistrator {
|
27
|
|
28
|
@Resource
|
29
|
private UniqueServiceLocator serviceLocator;
|
30
|
|
31
|
private static final ExecutorService executor = Executors.newFixedThreadPool(10);
|
32
|
|
33
|
private BaseService service;
|
34
|
private String mainProtocol = "SOAP";
|
35
|
private Map<String, String> properties = Maps.newHashMap();
|
36
|
private Map<String, String> protocols = Maps.newHashMap();
|
37
|
|
38
|
private static final Log log = LogFactory.getLog(DnetServiceRegistrator.class);
|
39
|
|
40
|
@PostConstruct
|
41
|
public void registerService() throws InterruptedException, ExecutionException {
|
42
|
|
43
|
final String addr = protocols.get(mainProtocol);
|
44
|
|
45
|
if (StringUtils.isNotBlank(addr)) {
|
46
|
final DnetService srv = new DnetService();
|
47
|
srv.setName(ServiceNameResolver.resolveServiceName(service.getClass()));
|
48
|
srv.setDate(new Date());
|
49
|
srv.setValid(true);
|
50
|
srv.setProtocols(protocols);
|
51
|
srv.setProperties(properties);
|
52
|
|
53
|
final Future<String> future = executor.submit(new Callable<String>() {
|
54
|
|
55
|
@Override
|
56
|
public String call() throws Exception {
|
57
|
int i = 1;
|
58
|
while (true) {
|
59
|
try {
|
60
|
final InformationService is = getInformationService();
|
61
|
if (is != null) {
|
62
|
return is.registerService(srv);
|
63
|
} else {
|
64
|
log.warn("Service registration delayed (infomationService not found) - try: " + i++);
|
65
|
Thread.sleep(2000);
|
66
|
}
|
67
|
} catch (Throwable e) {
|
68
|
log.warn("Service registration delayed (" + e.getMessage() + ") - try: " + i++);
|
69
|
log.debug("Failed registration", e);
|
70
|
Thread.sleep(2000);
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
});
|
75
|
srv.setId(future.get());
|
76
|
|
77
|
log.info("Service " + srv + " registered/updated");
|
78
|
|
79
|
if (service instanceof AbstractBaseService) {
|
80
|
((AbstractBaseService) service).setDescription(srv);
|
81
|
}
|
82
|
} else {
|
83
|
log.error("MainProtocol " + mainProtocol + " is missing, service: " + service.getClass());
|
84
|
}
|
85
|
}
|
86
|
|
87
|
private InformationService getInformationService() {
|
88
|
return service instanceof InformationService ?
|
89
|
(InformationService) service : serviceLocator.getService(InformationService.class);
|
90
|
}
|
91
|
|
92
|
public BaseService getService() {
|
93
|
return service;
|
94
|
}
|
95
|
|
96
|
@Required
|
97
|
public void setService(final BaseService service) {
|
98
|
this.service = service;
|
99
|
}
|
100
|
|
101
|
public Map<String, String> getProtocols() {
|
102
|
return protocols;
|
103
|
}
|
104
|
|
105
|
@Required
|
106
|
public void setProtocols(final Map<String, String> protocols) {
|
107
|
this.protocols = protocols;
|
108
|
}
|
109
|
|
110
|
public Map<String, String> getProperties() {
|
111
|
return properties;
|
112
|
}
|
113
|
|
114
|
public void setProperties(final Map<String, String> properties) {
|
115
|
this.properties = properties;
|
116
|
}
|
117
|
|
118
|
public String getMainProtocol() {
|
119
|
return mainProtocol;
|
120
|
}
|
121
|
|
122
|
public void setMainProtocol(final String mainProtocol) {
|
123
|
this.mainProtocol = mainProtocol;
|
124
|
}
|
125
|
}
|