Search in sources :

Example 1 with MessageGraph

use of org.jaffa.qm.apis.data.MessageGraph in project jaffa-framework by jaffa-projects.

the class JmsQueueAdmin method createMessageGraph.

/**
 * Molds the input JMS Message into a MessageGraph.
 */
private static MessageGraph createMessageGraph(Message message, String queueName, PropertyFilter pf) throws JMSException, IntrospectionException {
    MessageGraph graph = new MessageGraph();
    graph.setQueueMetaData(createQueueMetaData(queueName, pf));
    if (pf.isFieldIncluded("type"))
        graph.setType(queueName);
    if (pf.isFieldIncluded("messageId"))
        graph.setMessageId(message.getJMSMessageID());
    if (pf.isFieldIncluded("priority"))
        graph.setPriority(new Long(message.getJMSPriority()));
    if (pf.isFieldIncluded("createdOn"))
        graph.setCreatedOn(message.getJMSTimestamp() != 0 ? new DateTime(message.getJMSTimestamp()) : null);
    if (pf.isFieldIncluded("createdBy"))
        graph.setCreatedBy(message.getStringProperty(JmsBrowser.HEADER_USER_ID));
    if (pf.isFieldIncluded("errorMessage"))
        graph.setErrorMessage(message.getStringProperty(JmsBrowser.HEADER_ERROR_DETAILS));
    if (pf.isFieldIncluded("payload") && message instanceof TextMessage)
        graph.setPayload(((TextMessage) message).getText());
    if (pf.isFieldIncluded("hasAdminAccess"))
        graph.setHasAdminAccess(JmsBrowser.hasAdminMessageAccess(queueName));
    if (pf.isFieldIncluded("applicationFields") || pf.isFieldIncluded("technicalFields")) {
        // Generate a Map of header elements, keyed by the name of each header element
        // Names beginning with JMS and jaffa_ will be treated as techincal, while the rest will be assumed to be application-specific
        Map<String, MessageField> applicationFields = new LinkedHashMap<String, MessageField>();
        Map<String, MessageField> technicalFields = new TreeMap<String, MessageField>();
        for (Enumeration e = message.getPropertyNames(); e.hasMoreElements(); ) {
            String name = (String) e.nextElement();
            String value = Formatter.format(message.getObjectProperty(name));
            MessageField messageField = new MessageField();
            messageField.setName(name);
            messageField.setValue(value);
            if (name.startsWith("JMS") || name.startsWith("jaffa_"))
                technicalFields.put(name, messageField);
            else
                applicationFields.put(name, messageField);
        }
        // Treat all the JMS* properties of the Message as technical
        if (pf.isFieldIncluded("technicalFields")) {
            BeanInfo beanInfo = Introspector.getBeanInfo(Message.class);
            if (beanInfo != null) {
                PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
                if (pds != null) {
                    for (PropertyDescriptor pd : pds) {
                        if (pd.getReadMethod() != null && !pd.getPropertyType().isArray()) {
                            String name = pd.getName();
                            try {
                                Object value = pd.getReadMethod().invoke(message);
                                MessageField messageField = new MessageField();
                                messageField.setName(name);
                                messageField.setValue(Formatter.format(value));
                                technicalFields.put(name, messageField);
                            } catch (Exception e) {
                                if (log.isDebugEnabled())
                                    log.debug("Exception thrown while reading Message property: " + name, e);
                            }
                        }
                    }
                }
            }
            if (!technicalFields.isEmpty())
                graph.setTechnicalFields(technicalFields.values().toArray(new MessageField[technicalFields.size()]));
        }
        // Add labels to the application-fields from the configuration-file
        if (pf.isFieldIncluded("applicationFields")) {
            QueueInfo queueInfo = ConfigurationService.getInstance().getQueueInfo(queueName);
            if (queueInfo != null && queueInfo.getDisplayParam() != null) {
                for (DisplayParam displayParam : queueInfo.getDisplayParam()) {
                    if (displayParam.getLabel() != null && applicationFields.containsKey(displayParam.getName()))
                        applicationFields.get(displayParam.getName()).setLabel(MessageHelper.replaceTokens(displayParam.getLabel()));
                }
            }
            if (!applicationFields.isEmpty())
                graph.setApplicationFields(applicationFields.values().toArray(new MessageField[applicationFields.size()]));
        }
    }
    return graph;
}
Also used : QueueInfo(org.jaffa.modules.messaging.services.configdomain.QueueInfo) Enumeration(java.util.Enumeration) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) TreeMap(java.util.TreeMap) DateTime(org.jaffa.datatypes.DateTime) MessageField(org.jaffa.qm.apis.data.MessageField) JMSException(javax.jms.JMSException) IntrospectionException(java.beans.IntrospectionException) LinkedHashMap(java.util.LinkedHashMap) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) DisplayParam(org.jaffa.modules.messaging.services.configdomain.DisplayParam) TextMessage(javax.jms.TextMessage)

Example 2 with MessageGraph

use of org.jaffa.qm.apis.data.MessageGraph in project jaffa-framework by jaffa-projects.

the class JmsQueueAdmin method deleteMessage.

/**
 * NOTE: Each graph is expected to contain type, messageId and status. It may also contain the optional queueMetaData.queueSystemId, which may help optimize the process.
 */
public MessageAdminResponse[] deleteMessage(MessageGraph[] graphs) {
    if (log.isDebugEnabled())
        log.debug("Input to deleteMessage: " + Arrays.toString(graphs));
    Collection<MessageAdminResponse> output = null;
    String[] queueNames = null;
    for (MessageGraph graph : graphs) {
        if (graph.getQueueMetaData() == null || graph.getQueueMetaData().getQueueSystemId() == null || graph.getQueueMetaData().getQueueSystemId().equals(QUEUE_SYSTEM_ID)) {
            // Initialize and sort the queueNames array; if not already done
            if (queueNames == null) {
                queueNames = ConfigurationService.getInstance().getQueueNames();
                Arrays.sort(queueNames);
            }
            if (Arrays.binarySearch(queueNames, graph.getType()) >= 0) {
                try {
                    if (graph.getStatus() == MessageGraph.Status.OPEN) {
                        if (log.isDebugEnabled())
                            log.debug("Deleting OPEN message " + graph.getMessageId());
                        JmsBrowser.deleteMessage(graph.getType(), graph.getMessageId());
                    } else if (graph.getStatus() == MessageGraph.Status.ERROR) {
                        if (log.isDebugEnabled())
                            log.debug("Deleting ERROR message " + graph.getMessageId());
                        JmsBrowser.deleteMessage(graph.getType(), graph.getMessageId());
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("Message cannnot be deleted since unsupported status " + graph.getStatus() + " has been passed");
                    }
                } catch (Exception e) {
                    MessageAdminResponse response = new MessageAdminResponse();
                    response.setSource(graph);
                    ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
                    if (appExps != null) {
                        if (log.isDebugEnabled())
                            log.debug("Error while deleting Message " + graph, appExps);
                        response.setErrors(ServiceError.generate(appExps));
                    } else {
                        log.error("Internal Error while deleting Message " + graph, e);
                        response.setErrors(ServiceError.generate(e));
                    }
                    if (output == null)
                        output = new LinkedList<MessageAdminResponse>();
                    output.add(response);
                }
            } else {
                if (log.isDebugEnabled())
                    log.debug("Message " + graph + " will not be deleted by this implementation, since the queue does not belong to this implementation");
            }
        } else {
            if (log.isDebugEnabled())
                log.debug("Message " + graph + " will not be deleted by this implementation, since the input queueSystemId does not match this system");
        }
    }
    if (log.isDebugEnabled())
        log.debug("Output from deleteMessage: " + output);
    return output != null && output.size() > 0 ? output.toArray(new MessageAdminResponse[output.size()]) : null;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) MessageAdminResponse(org.jaffa.qm.apis.data.MessageAdminResponse) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) JMSException(javax.jms.JMSException) IntrospectionException(java.beans.IntrospectionException)

Example 3 with MessageGraph

use of org.jaffa.qm.apis.data.MessageGraph in project jaffa-framework by jaffa-projects.

the class JmsQueueAdmin method messageQuery.

public MessageQueryResponse messageQuery(MessageCriteria criteria) {
    if (log.isDebugEnabled())
        log.debug("Input to messageQuery: " + criteria);
    MessageQueryResponse output = new MessageQueryResponse();
    try {
        // Match the queueSystemId as well apply checks for unsupported criteria
        if (FinderTx.match(QUEUE_SYSTEM_ID, criteria.getQueueSystemId()) && FinderTx.match(null, criteria.getSubType()) && FinderTx.match(null, criteria.getDirection()) && FinderTx.match(null, criteria.getLastChangedOn()) && FinderTx.match(null, criteria.getLastChangedBy())) {
            String[] queueNames = JmsBrowser.getAccessibleQueueNames();
            if (queueNames != null && queueNames.length > 0) {
                String jmsFilter = createJmsFilter(criteria);
                PropertyFilter pf = PropertyFilter.getInstance(MessageGraph.class, criteria.getResultGraphRules());
                Collection<MessageGraph> graphs = new LinkedList<MessageGraph>();
                for (String queueName : queueNames) {
                    if (FinderTx.match(queueName, criteria.getType())) {
                        // Retrieve messages for each requested status
                        for (MessageGraph.Status status : SUPPORTED_MESSAGE_STATUS) {
                            if (FinderTx.match(status.toString(), criteria.getStatus())) {
                                Message[] messages = null;
                                if (status == MessageGraph.Status.OPEN)
                                    messages = JmsBrowser.getPendingMessages(queueName, jmsFilter);
                                if (messages != null) {
                                    for (Message message : messages) {
                                        MessageGraph graph = createMessageGraph(message, queueName, pf);
                                        if (pf.isFieldIncluded("status"))
                                            graph.setStatus(status);
                                        graphs.add(graph);
                                    }
                                }
                            }
                        }
                    }
                }
                if (graphs.size() > 0) {
                    output.setGraphs(graphs.toArray(new MessageGraph[graphs.size()]));
                    if (criteria.getOrderByFields() != null && criteria.getOrderByFields().length > 0)
                        Arrays.sort(output.getGraphs(), new GraphComparator(criteria.getOrderByFields()));
                    handlePaging(output, criteria);
                }
            }
        }
    } catch (Exception e) {
        // add errors to the response
        ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
        if (appExps != null) {
            if (log.isDebugEnabled())
                log.debug("Error in messageQuery execution", appExps);
            output.setErrors(ServiceError.generate(appExps));
        } else {
            log.error("Internal Error in messageQuery execution", e);
            output.setErrors(ServiceError.generate(e));
        }
    }
    if (log.isDebugEnabled())
        log.debug("Output from messageQuery: " + output);
    return output;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageQueryResponse(org.jaffa.qm.apis.data.MessageQueryResponse) LinkedList(java.util.LinkedList) JMSException(javax.jms.JMSException) IntrospectionException(java.beans.IntrospectionException) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) GraphComparator(org.jaffa.soa.graph.GraphComparator) PropertyFilter(org.jaffa.qm.util.PropertyFilter)

Example 4 with MessageGraph

use of org.jaffa.qm.apis.data.MessageGraph in project jaffa-framework by jaffa-projects.

the class TransactionAdmin method resubmitMessage.

/**
 * NOTE: Each graph is expected to contain messageId. It may also contain the optional queueMetaData.queueSystemId and type, which may help optimize the process.
 */
public MessageAdminResponse[] resubmitMessage(MessageGraph[] graphs) {
    if (log.isDebugEnabled()) {
        log.debug("Input to resubmitMessage: " + Arrays.toString(graphs));
    }
    Collection<MessageAdminResponse> output = null;
    String[] types = TransactionEngine.getInstance().getAccessibleTypeNames();
    Arrays.sort(types);
    for (MessageGraph graph : graphs) {
        if (graph.getQueueMetaData() == null || graph.getQueueMetaData().getQueueSystemId() == null || graph.getQueueMetaData().getQueueSystemId().equals(QUEUE_SYSTEM_ID)) {
            if (graph.getType() == null || (types != null && Arrays.binarySearch(types, graph.getType()) >= 0)) {
                UOW uow = null;
                try {
                    String graphType = graph.getType();
                    String messageId = graph.getMessageId();
                    if (graphType != null && !TransactionEngine.getInstance().hasAdminAccess(graphType)) {
                        throw new ApplicationExceptions(new ApplicationException("error.Jaffa.Transaction.Transaction.noAdminAccess", new Object[] { graphType }));
                    }
                    uow = new UOW();
                    Transaction transaction = transactionDAO.getTransaction(messageId);
                    if (transaction != null) {
                        if (!TransactionEngine.getInstance().hasAdminAccess(transaction.getType())) {
                            throw new ApplicationExceptions(new ApplicationException("error.Jaffa.Transaction.Transaction.noAdminAccess", new Object[] { transaction.getType() }));
                        }
                        if (Transaction.Status.E.toString().equals(transaction.getStatus())) {
                            if (log.isDebugEnabled()) {
                                log.debug("Resubmitting ERROR message " + messageId);
                            }
                            transaction.setStatus(Transaction.Status.O.toString());
                            transaction.setErrorMessage(null);
                            transactionDAO.save(uow, transaction);
                            uow.commit();
                        } else {
                            if (log.isDebugEnabled()) {
                                log.debug("Resubmission cannot be performed since message " + messageId + " is not in ERROR status anymore");
                            }
                        }
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("Resubmission cannot be performed since message " + messageId + " not found");
                        }
                    }
                } catch (Exception e) {
                    MessageAdminResponse response = new MessageAdminResponse();
                    response.setSource(graph);
                    ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
                    if (appExps != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error while resubmitting Message " + graph, appExps);
                        }
                        response.setErrors(ServiceError.generate(appExps));
                    } else {
                        log.error("Internal Error while resubmitting Message " + graph, e);
                        response.setErrors(ServiceError.generate(e));
                    }
                    if (output == null) {
                        output = new LinkedList<MessageAdminResponse>();
                    }
                    output.add(response);
                } finally {
                    if (uow != null) {
                        try {
                            uow.rollback();
                        } catch (Exception ignore) {
                        }
                    }
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Message " + graph + " will not be resubmitted by this implementation, since the queue does not belong to this implementation");
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Message " + graph + " will not be resubmitted by this implementation, since the input queueSystemId does not match this system");
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Output from resubmitMessage: " + output);
    }
    return output != null && output.size() > 0 ? output.toArray(new MessageAdminResponse[output.size()]) : null;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ApplicationException(org.jaffa.exceptions.ApplicationException) Transaction(org.jaffa.transaction.domain.Transaction) MessageAdminResponse(org.jaffa.qm.apis.data.MessageAdminResponse) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) UOW(org.jaffa.persistence.UOW) IllegalPersistentStateRuntimeException(org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) PostLoadFailedException(org.jaffa.persistence.exceptions.PostLoadFailedException) FrameworkException(org.jaffa.exceptions.FrameworkException) QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException) ApplicationException(org.jaffa.exceptions.ApplicationException) LinkedList(java.util.LinkedList)

Example 5 with MessageGraph

use of org.jaffa.qm.apis.data.MessageGraph in project jaffa-framework by jaffa-projects.

the class TransactionAdmin method createMessageGraph.

/**
 * Molds the input Transaction into a MessageGraph.
 */
private static MessageGraph createMessageGraph(Transaction transaction, PropertyFilter pf) throws FrameworkException {
    MessageGraph graph = new MessageGraph();
    graph.setQueueMetaData(createQueueMetaData(transaction.getSubType(), pf));
    if (pf.isFieldIncluded("type")) {
        graph.setType(transaction.getType());
    }
    if (pf.isFieldIncluded("subType")) {
        graph.setSubType(transaction.getSubType());
    }
    if (pf.isFieldIncluded("messageId")) {
        graph.setMessageId(transaction.getId());
    }
    if (pf.isFieldIncluded("direction")) {
        graph.setDirection(transaction.getDirection());
    }
    if (pf.isFieldIncluded("status")) {
        graph.setStatus(transactionToMessageStatus(transaction.getStatus()));
    }
    if (pf.isFieldIncluded("createdOn")) {
        graph.setCreatedOn(transaction.getCreatedOn());
    }
    if (pf.isFieldIncluded("createdBy")) {
        graph.setCreatedBy(transaction.getCreatedBy());
    }
    if (pf.isFieldIncluded("lastChangedOn")) {
        graph.setLastChangedOn(transaction.getLastChangedOn());
    }
    if (pf.isFieldIncluded("lastChangedBy")) {
        graph.setLastChangedBy(transaction.getLastChangedBy());
    }
    if (pf.isFieldIncluded("errorMessage")) {
        graph.setErrorMessage(transaction.getErrorMessage());
    }
    if (pf.isFieldIncluded("payload")) {
        graph.setPayload(transaction.getTransactionPayloadObject() != null ? transaction.getTransactionPayloadObject().returnInternalPayload() : null);
    }
    if (pf.isFieldIncluded("hasAdminAccess")) {
        graph.setHasAdminAccess(TransactionEngine.getInstance().hasAdminAccess(transaction.getType()));
    }
    if (pf.isFieldIncluded("applicationFields")) {
        Map<String, MessageField> applicationFields = new LinkedHashMap<String, MessageField>();
        TransactionField[] transactionFields = transaction.getTransactionFieldArray();
        if (transactionFields != null && transactionFields.length > 0) {
            for (int i = 0; i < transactionFields.length; i++) {
                TransactionField transactionField = transactionFields[i];
                MessageField applicationField = new MessageField();
                applicationField.setName(transactionField.getFieldName());
                applicationField.setValue(transactionField.getValue());
                applicationFields.put(applicationField.getName(), applicationField);
            }
        }
        // Add labels to the application-fields from the configuration-file
        TypeInfo typeInfo = ConfigurationService.getInstance().getTypeInfo(transaction.getType());
        if (typeInfo != null && typeInfo.getDisplayParam() != null) {
            for (DisplayParam displayParam : typeInfo.getDisplayParam()) {
                if (displayParam.getLabel() != null && applicationFields.containsKey(displayParam.getName())) {
                    applicationFields.get(displayParam.getName()).setLabel(MessageHelper.replaceTokens(displayParam.getLabel()));
                }
            }
        }
        if (!applicationFields.isEmpty()) {
            graph.setApplicationFields(applicationFields.values().toArray(new MessageField[applicationFields.size()]));
        }
    }
    if (pf.isFieldIncluded("messageDependencies")) {
        TransactionDependency[] transactionDependencies = transaction.getTransactionDependencyArray();
        if (transactionDependencies != null && transactionDependencies.length > 0) {
            MessageDependency[] messageDependencies = new MessageDependency[transactionDependencies.length];
            for (int i = 0; i < transactionDependencies.length; i++) {
                TransactionDependency transactionDependency = transactionDependencies[i];
                MessageDependency messageDependency = new MessageDependency();
                messageDependency.setDependsOnId(transactionDependency.getDependsOnId());
                if (transactionDependency.getStatus() != null) {
                    MessageDependency.Status messageDependencyStatus = null;
                    TransactionDependency.Status transactionDependencyStatus = TransactionDependency.Status.valueOf(transactionDependency.getStatus());
                    switch(transactionDependencyStatus) {
                        case O:
                            messageDependencyStatus = MessageDependency.Status.OPEN;
                            break;
                        case S:
                            messageDependencyStatus = MessageDependency.Status.SUCCESS;
                            break;
                    }
                    messageDependency.setStatus(messageDependencyStatus);
                }
                messageDependencies[i] = messageDependency;
            }
            graph.setMessageDependencies(messageDependencies);
        }
    }
    return graph;
}
Also used : MessageDependency(org.jaffa.qm.apis.data.MessageDependency) TransactionField(org.jaffa.transaction.domain.TransactionField) TypeInfo(org.jaffa.transaction.services.configdomain.TypeInfo) MessageField(org.jaffa.qm.apis.data.MessageField) LinkedHashMap(java.util.LinkedHashMap) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) DisplayParam(org.jaffa.transaction.services.configdomain.DisplayParam) TransactionDependency(org.jaffa.transaction.domain.TransactionDependency)

Aggregations

MessageGraph (org.jaffa.qm.apis.data.MessageGraph)9 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)7 IntrospectionException (java.beans.IntrospectionException)4 LinkedList (java.util.LinkedList)4 JMSException (javax.jms.JMSException)4 MessageAdminResponse (org.jaffa.qm.apis.data.MessageAdminResponse)4 LinkedHashMap (java.util.LinkedHashMap)3 ApplicationException (org.jaffa.exceptions.ApplicationException)3 FrameworkException (org.jaffa.exceptions.FrameworkException)3 JaffaMessagingFrameworkException (org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException)3 IllegalPersistentStateRuntimeException (org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException)3 PostLoadFailedException (org.jaffa.persistence.exceptions.PostLoadFailedException)3 QueryFailedException (org.jaffa.persistence.exceptions.QueryFailedException)3 MessageQueryResponse (org.jaffa.qm.apis.data.MessageQueryResponse)3 Transaction (org.jaffa.transaction.domain.Transaction)3 TextMessage (javax.jms.TextMessage)2 UOW (org.jaffa.persistence.UOW)2 MessageField (org.jaffa.qm.apis.data.MessageField)2 PropertyFilter (org.jaffa.qm.util.PropertyFilter)2 BeanInfo (java.beans.BeanInfo)1