1
|
package eu.dnetlib.enabling.blackboard;
|
2
|
|
3
|
import java.util.Comparator;
|
4
|
import java.util.Date;
|
5
|
import java.util.Map;
|
6
|
import java.util.UUID;
|
7
|
|
8
|
import javax.annotation.Resource;
|
9
|
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
13
|
import com.google.common.collect.Maps;
|
14
|
import com.google.gson.Gson;
|
15
|
|
16
|
import eu.dnetlib.common.ifaces.BlackboardCallback;
|
17
|
import eu.dnetlib.common.services.locators.DnetServiceLocator;
|
18
|
import eu.dnetlib.common.services.locators.ServiceRunningInstance;
|
19
|
import eu.dnetlib.enabling.annotations.Blackboard;
|
20
|
import eu.dnetlib.enabling.nodeManager.NodeManagerServiceImpl;
|
21
|
import eu.dnetlib.rmi.objects.is.BlackboardActionStatus;
|
22
|
import eu.dnetlib.rmi.objects.is.BlackboardMessageContainer;
|
23
|
import eu.dnetlib.rmi.objects.is.Operation;
|
24
|
import eu.dnetlib.rmi.objects.is.Subscription;
|
25
|
import eu.dnetlib.rmi.soap.InformationService;
|
26
|
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
|
27
|
|
28
|
public class BlackboardDispatcher {
|
29
|
|
30
|
@Resource
|
31
|
private DnetServiceLocator serviceLocator;
|
32
|
|
33
|
@Resource
|
34
|
private NodeManagerServiceImpl nodeManagerService;
|
35
|
|
36
|
private static final Log log = LogFactory.getLog(BlackboardDispatcher.class);
|
37
|
|
38
|
private Map<String, BlackboardCallback<?>> responseActions = Maps.newHashMap();
|
39
|
private Map<String, Class<?>> responseClasses = Maps.newHashMap();
|
40
|
|
41
|
public <T> String createDispatcher(final T message, final BlackboardCallback<T> callback) {
|
42
|
final Blackboard annotation = getMessageAnnotation(message);
|
43
|
final String serviceId = serviceLocator.getServiceId(annotation.serviceClass());
|
44
|
createDispatcher(annotation, serviceId, message, callback);
|
45
|
return serviceId;
|
46
|
}
|
47
|
|
48
|
public <T> String createDispatcher(final T message, final String dsId, final BlackboardCallback<T> callback) {
|
49
|
final Blackboard annotation = getMessageAnnotation(message);
|
50
|
final String serviceId = serviceLocator.getServiceId(annotation.serviceClass(), dsId);
|
51
|
createDispatcher(annotation, serviceId, message, callback);
|
52
|
return serviceId;
|
53
|
}
|
54
|
|
55
|
public <T> String createDispatcher(final T message, final Comparator<ServiceRunningInstance> comparator, final BlackboardCallback<T> callback) {
|
56
|
final Blackboard annotation = getMessageAnnotation(message);
|
57
|
final String serviceId = serviceLocator.getServiceId(annotation.serviceClass(), comparator);
|
58
|
createDispatcher(annotation, serviceId, message, callback);
|
59
|
return serviceId;
|
60
|
}
|
61
|
|
62
|
public <T> void createDispatcher(final String serviceId, final T message, final BlackboardCallback<T> callback) {
|
63
|
final Blackboard annotation = getMessageAnnotation(message);
|
64
|
createDispatcher(annotation, serviceId, message, callback);
|
65
|
}
|
66
|
|
67
|
private <T> void createDispatcher(final Blackboard annotation, final String serviceId, final T message, final BlackboardCallback<T> callback) {
|
68
|
try {
|
69
|
final InformationService is = serviceLocator.getService(InformationService.class);
|
70
|
final BlackboardMessageContainer bbMessage = asSoapMessage(annotation.action(), message);
|
71
|
final Map<String, Object> cond = Maps.newHashMap();
|
72
|
cond.put("id", bbMessage.getId());
|
73
|
final String subscrId = is.addSubscription(new Subscription(null, nodeManagerService.getServiceId(), Operation.UPDATE, "blackboard", cond));
|
74
|
|
75
|
responseActions.put(subscrId, callback);
|
76
|
responseClasses.put(subscrId, message.getClass());
|
77
|
|
78
|
is.addBlackBoardMessage(serviceId, bbMessage);
|
79
|
} catch (InformationServiceException e) {
|
80
|
log.error("Error managing blackboard message", e);
|
81
|
throw new IllegalStateException("Error managing blackboard message", e);
|
82
|
}
|
83
|
|
84
|
}
|
85
|
|
86
|
private <T> Blackboard getMessageAnnotation(final T message) {
|
87
|
if (message != null && message.getClass().isAnnotationPresent(Blackboard.class)) {
|
88
|
return message.getClass().getAnnotation(Blackboard.class);
|
89
|
} else if (message != null) {
|
90
|
log.error("BlackboardMessage annotation is missing, class: " + message.getClass());
|
91
|
throw new IllegalStateException("BlackboardMessage annotation is missing, class: " + message.getClass());
|
92
|
} else {
|
93
|
log.error("Message is null");
|
94
|
throw new IllegalStateException("Message is null");
|
95
|
}
|
96
|
}
|
97
|
|
98
|
private BlackboardMessageContainer asSoapMessage(final String action, final Object message) {
|
99
|
final BlackboardMessageContainer bb = new BlackboardMessageContainer();
|
100
|
bb.setAction(action);
|
101
|
bb.setDate(new Date());
|
102
|
bb.setId("bb-" + UUID.randomUUID());
|
103
|
bb.setStatus(BlackboardActionStatus.ASSIGNED);
|
104
|
bb.setJsonMessage(new Gson().toJson(message));
|
105
|
return bb;
|
106
|
}
|
107
|
|
108
|
public boolean isRegisteredSubscription(final String subscriptionId) {
|
109
|
return responseActions.containsKey(subscriptionId);
|
110
|
}
|
111
|
|
112
|
@SuppressWarnings("unchecked")
|
113
|
public <T> void activateResponseAction(final String subscrId, final BlackboardMessageContainer bbMessage) {
|
114
|
final BlackboardCallback<T> callback = (BlackboardCallback<T>) responseActions.get(subscrId);
|
115
|
|
116
|
if (callback == null) {
|
117
|
throw new RuntimeException("No action has been registered for subscription " + subscrId);
|
118
|
} else {
|
119
|
final BlackboardActionStatus status = bbMessage.getStatus();
|
120
|
final T response = new Gson().fromJson(bbMessage.getJsonMessage(), (Class<T>) responseClasses.get(subscrId));
|
121
|
|
122
|
invokeCallback(status, response, callback);
|
123
|
|
124
|
if (status == BlackboardActionStatus.DONE || status == BlackboardActionStatus.FAILED) {
|
125
|
complete(subscrId, bbMessage);
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
|
130
|
private void complete(final String subscrId, final BlackboardMessageContainer bbMessage) {
|
131
|
responseActions.remove(subscrId);
|
132
|
responseClasses.remove(subscrId);
|
133
|
try {
|
134
|
final InformationService is = serviceLocator.getService(InformationService.class);
|
135
|
is.deleteSubscription(subscrId);
|
136
|
is.deleteBlackBoardMessage(bbMessage);
|
137
|
} catch (InformationServiceException e) {
|
138
|
log.error("Error removing blackboard message", e);
|
139
|
throw new RuntimeException("Error removing blackboard message", e);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
public <T> void invokeCallback(final BlackboardActionStatus status, final T response, final BlackboardCallback<T> callback) {
|
144
|
switch (status) {
|
145
|
case DONE:
|
146
|
callback.onSuccess(response);
|
147
|
break;
|
148
|
case FAILED:
|
149
|
callback.onFail(response);
|
150
|
break;
|
151
|
case ONGOING:
|
152
|
callback.onOngoing(response);
|
153
|
break;
|
154
|
default:
|
155
|
break;
|
156
|
}
|
157
|
}
|
158
|
}
|