Search in sources :

Example 6 with UserContextWrapper

use of org.jaffa.modules.user.services.UserContextWrapper in project jaffa-framework by jaffa-projects.

the class JaffaMessageConsumer method processPayload.

/**
 * Invokes the handler associated with the input payload. If any error occurs
 * while processing the payload, and if the input message is not null, then
 * the message will be redirected to the ErrorQueue. When handling a message
 * published to a Topic, errors if any, will be logged only.
 *
 * @param aMessage
 *          the Message.
 * @param messageInfo
 *          The messageInfo for the payload from the configuration file.
 * @param payload
 *          Any serializable object.
 * @param userId
 *          the user id.
 * @param scheduledTaskId
 *          contains the id of the scheduled task, if invoked by the
 *          scheduler.
 * @param originalMessageId
 *          the original id of the Message being processed.
 * @throws FrameworkException
 *           Indicates some system error.
 * @throws ApplicationExceptions
 *           Indicates application error(s).
 */
private void processPayload(final Message aMessage, final MessageInfo messageInfo, final Object payload, final String userId, final String scheduledTaskId, final String originalMessageId) throws FrameworkException, ApplicationExceptions {
    // Maintain a reference to the currently logged in user. This may typically
    // happen in POST_IMMEDIATE mode
    final String currentUserId = SecurityManager.getPrincipal() != null ? SecurityManager.getPrincipal().getName() : null;
    UserContextWrapper ucw = null;
    boolean createdLoggingContext = false;
    try {
        if (currentUserId != null && currentUserId.equals(userId)) {
            // userId
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Context will not be created for the authenticated user " + userId);
        } else {
            // Sets the context based on the userId
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Creating context for the user " + userId);
            ucw = createUserContextWrapper(userId);
        }
        // Sets Log4J's MDC to enable BusinessEventLogging
        LoggingService.setLoggingContext(payload, messageInfo, userId, scheduledTaskId, originalMessageId);
        createdLoggingContext = true;
        if (LOGGER.isInfoEnabled())
            LOGGER.info(MessageHelper.findMessage("label.Jaffa.Messaging.JaffaMessageBean.start", new Object[] { payload }));
        // Invokes the handler as specified by the 'toClass and toMethod'
        // combination in the MessageConfig
        invokeHandler(aMessage != null ? aMessage.getJMSMessageID() : null, messageInfo, payload);
        if (LOGGER.isInfoEnabled())
            LOGGER.info(MessageHelper.findMessage("label.Jaffa.Messaging.JaffaMessageBean.success", new Object[] { payload }));
    } catch (Exception e) {
        LOGGER.error(MessageHelper.findMessage("error.Jaffa.Messaging.JaffaMessageBean.error", new Object[] { payload }), e);
        // Do nothing when handling a message published to a Topic
        if (messageInfo.getQueueName() != null) {
            // rethrow the exception
            if (aMessage != null)
                handleException(aMessage, payload, e);
            else
                throw ExceptionHelper.throwAFR(e);
        }
    } finally {
        // Unset the Logging context
        if (createdLoggingContext)
            LoggingService.unsetLoggingContext(payload, messageInfo);
        // Clear context for this user
        if (ucw != null) {
            ucw.unsetContext();
            // Restore the context for the original user
            if (currentUserId != null) {
                try {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("Restoring context for the user " + currentUserId);
                    createUserContextWrapper(currentUserId);
                } catch (Exception e) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("Exception thrown while restoring context for the user " + currentUserId, e);
                }
            }
        }
    }
}
Also used : UserContextWrapper(org.jaffa.modules.user.services.UserContextWrapper) FrameworkException(org.jaffa.exceptions.FrameworkException) MessagingException(javax.mail.MessagingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UserSessionSetupException(org.jaffa.presentation.portlet.session.UserSessionSetupException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Aggregations

UserContextWrapper (org.jaffa.modules.user.services.UserContextWrapper)6 ApplicationException (org.jaffa.exceptions.ApplicationException)5 FrameworkException (org.jaffa.exceptions.FrameworkException)4 UOW (org.jaffa.persistence.UOW)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)2 Criteria (org.jaffa.persistence.Criteria)2 IPersistent (org.jaffa.persistence.IPersistent)2 LockedApplicationException (org.jaffa.persistence.engines.jdbcengine.LockedApplicationException)2 UserSessionSetupException (org.jaffa.presentation.portlet.session.UserSessionSetupException)2 Method (java.lang.reflect.Method)1 MessagingException (javax.mail.MessagingException)1 HeaderParam (org.jaffa.modules.messaging.services.HeaderParam)1 ScheduledTask (org.jaffa.modules.scheduler.services.ScheduledTask)1 GraphService (org.jaffa.soa.dataaccess.GraphService)1 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)1 GraphUpdateResponse (org.jaffa.soa.graph.GraphUpdateResponse)1 ServiceError (org.jaffa.soa.graph.ServiceError)1 RaiseEventService (org.jaffa.soa.services.RaiseEventService)1