public AcknowledgeMessage routeMessageToService()

in core/src/main/java/flex/messaging/MessageBroker.java [1290:1380]


    public AcknowledgeMessage routeMessageToService(Message message, Endpoint endpoint) {
        // Make sure message has a messageId
        checkMessageId(message);

        Object serviceResult = null;
        boolean serviced = false;
        Service service = null;
        String destId = message.getDestination();
        try {
            String serviceId = destId != null ? destinationToService.get(destId) : null;

            if ((serviceId == null) && (destId != null) && (!serviceValidationListeners.isEmpty())) {
                for (Enumeration<ServiceValidationListener> iter = serviceValidationListeners.elements(); iter.hasMoreElements(); ) {
                    iter.nextElement().validateDestination(destId);
                }
                serviceId = destinationToService.get(destId);
            }

            if (serviceId != null) {
                service = services.get(serviceId);
                serviced = true;
                Destination destination = service.getDestination(destId);
                inspectOperation(message, destination);
                // Remove the validate endpoint header if it was set.
                if (message.headerExists(Message.VALIDATE_ENDPOINT_HEADER))
                    message.getHeaders().remove(Message.VALIDATE_ENDPOINT_HEADER);

                if (Log.isDebug())
                    Log.getLogger(getLogCategory(message)).debug(
                            "Before invoke service: " + service.getId() + StringUtils.NEWLINE +
                                    "  incomingMessage: " + message + StringUtils.NEWLINE);

                extractRemoteCredentials(service, message);
                serviceResult = service.serviceMessage(message);
            }

            if (!serviced) {
                MessageException lme = new MessageException();
                // The supplied destination id is not registered with any service.
                lme.setMessage(ERR_MSG_NO_SERVICE_FOR_DEST);
                throw lme;
            }

            if (Log.isDebug()) {
                String debugServiceResult = Log.getPrettyPrinter().prettify(serviceResult);
                Log.getLogger(getLogCategory(message)).debug(
                        "After invoke service: " + service.getId() + StringUtils.NEWLINE +
                                "  reply: " + debugServiceResult + StringUtils.NEWLINE);
            }

            AcknowledgeMessage ack;
            if (serviceResult instanceof AcknowledgeMessage) {
                // service will return an ack if they need to transform it in some
                // service-specific way (paging is an example)
                ack = (AcknowledgeMessage) serviceResult;
            } else {
                // most services will return a result of some sort, possibly null,
                // and expect the broker to compose a message to deliver it
                ack = new AcknowledgeMessage();
                ack.setBody(serviceResult);
            }
            ack.setCorrelationId(message.getMessageId());
            ack.setClientId(message.getClientId());
            return ack;
        } catch (MessageException exc) {
            exc.logAtHingePoint(message,
                    null, /* No outbound error message at this point. */
                    "Exception when invoking service '" + (service == null ? "(none)" : service.getId()) + "': ");

            throw exc;
        } catch (RuntimeException exc) {
            Log.getLogger(LogCategories.MESSAGE_GENERAL).error(
                    "Exception when invoking service: " +
                            (service == null ? "(none)" : service.getId()) +
                            StringUtils.NEWLINE +
                            "  with message: " + message + StringUtils.NEWLINE +
                            ExceptionUtil.exceptionFollowedByRootCausesToString(exc) + StringUtils.NEWLINE);

            throw exc;
        } catch (Error exc) {
            Log.getLogger(LogCategories.MESSAGE_GENERAL).error(
                    "Error when invoking service: " +
                            (service == null ? "(none)" : service.getId()) +
                            StringUtils.NEWLINE +
                            "  with message: " + message + StringUtils.NEWLINE +
                            ExceptionUtil.exceptionFollowedByRootCausesToString(exc) + StringUtils.NEWLINE);

            throw exc;
        }

    }