public void callback(final Message message) {
if (!message.isFlagSet(RoutingFlag.FromRemote))
return;
try {
final LocalContext localContext = LocalContext.get(message);
final String typeName = message.get(String.class, CDIProtocol.BeanType);
final Set<String> annotationTypes = message.get(Set.class, CDIProtocol.Qualifiers);
switch (CDICommands.valueOf(message.getCommandType())) {
case RemoteSubscribe:
final Class<?> type = Class.forName(typeName);
final ClientObserverMetadata clientObserver = new ClientObserverMetadata(type, annotationTypes);
if (!clientObservers.contains(clientObserver)) {
if (type == null || !EnvUtil.isPortableType(type)) {
log.warn("client tried to register a non-portable type: " + type);
return;
}
clientObservers.add(clientObserver);
}
eventRoutingTable.activateRoute(typeName, annotationTypes, message.getResource(QueueSession.class, "Session"));
break;
case RemoteUnsubscribe:
eventRoutingTable.deactivateRoute(typeName, annotationTypes, message.getResource(QueueSession.class, "Session"));
break;
case CDIEvent:
if (!isRoutable(localContext, message)) {
return;
}
final Object o = message.get(Object.class, CDIProtocol.BeanReference);
EventConversationContext.activate(o, CDIServerUtil.getSession(message));
try {
final Set<String> qualifierNames = message.get(Set.class, CDIProtocol.Qualifiers);
List<Annotation> qualifiers = new ArrayList<Annotation>();
if (qualifierNames != null) {
for (final String qualifierName : qualifierNames) {
final Annotation qualifier = allQualifiers.get(qualifierName);
if (qualifier != null) {
qualifiers.add(qualifier);
}
}
}
// Fire event to all local observers
Annotation[] qualArray = qualifiers.toArray(new Annotation[qualifiers.size()]);
Set<ObserverMethod<? super Object>> observerMethods = beanManager.resolveObserverMethods(o, qualArray);
for (ObserverMethod<? super Object> observer : observerMethods) {
// Don't mirror the event back to the clients
if (!(AnyEventObserver.class.equals(observer.getBeanClass()))) {
observer.notify(o);
}
}
} finally {
EventConversationContext.deactivate();
}
break;
case AttachRemote:
if (observedEvents.size() > 0) {
MessageBuilder.createConversation(message).toSubject(CDI.CLIENT_DISPATCHER_SUBJECT)
.command(CDICommands.AttachRemote).with(MessageParts.RemoteServices, getEventTypes()).done().reply();
}
else {
MessageBuilder.createConversation(message).toSubject(CDI.CLIENT_DISPATCHER_SUBJECT)
.command(CDICommands.AttachRemote).with(MessageParts.RemoteServices, "").done().reply();
}
localContext.setAttribute(CDI_EVENT_CHANNEL_OPEN, "1");
break;
default:
throw new IllegalArgumentException("Unknown command type " + message.getCommandType());
}