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);
}
}
}
}
}
Aggregations