Search in sources :

Example 1 with SeqContinuationState

use of org.apache.synapse.continuation.SeqContinuationState in project wso2-synapse by wso2.

the class SequenceMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (sequenceType == SequenceType.NAMED) {
        CustomLogSetter.getInstance().setLogAppender(artifactContainerName);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Mediating using the SeqContinuationState type : " + ((SeqContinuationState) continuationState).getSeqType() + " name : " + ((SeqContinuationState) continuationState).getSeqName());
    }
    Mediator errorHandlerMediator = null;
    // push the errorHandler sequence into the current message as the fault handler
    if (errorHandler != null) {
        errorHandlerMediator = synCtx.getSequence(errorHandler);
        if (errorHandlerMediator != null) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Setting the onError handler : " + errorHandler + " for the sequence : " + name);
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(errorHandlerMediator));
        } else {
            synLog.auditWarn("onError handler : " + errorHandler + " for sequence : " + name + " cannot be found");
        }
    }
    boolean result;
    if (!continuationState.hasChild()) {
        result = super.mediate(synCtx, continuationState.getPosition() + 1);
    } else {
        // if children exists first mediate from them starting from grandchild.
        do {
            FlowContinuableMediator mediator = (FlowContinuableMediator) getChild(continuationState.getPosition());
            result = mediator.mediate(synCtx, continuationState.getChildContState());
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                ((Mediator) mediator).reportCloseStatistics(synCtx, null);
            }
            if (result) {
                // if flow completed remove leaf child
                continuationState.removeLeafChild();
            }
        } while (result && continuationState.hasChild());
        if (result) {
            // after mediating from children, mediate from current SeqContinuationState
            result = super.mediate(synCtx, continuationState.getPosition() + 1);
        }
    }
    if (result) {
        // if flow completed, remove top ContinuationState from stack
        ContinuationStackManager.popContinuationStateStack(synCtx);
    }
    // before we exit normally without an exception
    if (errorHandlerMediator != null) {
        Stack faultStack = synCtx.getFaultStack();
        if (faultStack != null && !faultStack.isEmpty()) {
            Object o = faultStack.peek();
            if (o instanceof MediatorFaultHandler && errorHandlerMediator.equals(((MediatorFaultHandler) o).getFaultMediator())) {
                faultStack.pop();
            }
        }
    }
    return result;
}
Also used : SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) Stack(java.util.Stack)

Example 2 with SeqContinuationState

use of org.apache.synapse.continuation.SeqContinuationState in project wso2-synapse by wso2.

the class MessageHelper method cloneMessageContext.

/**
 * This method will simulate cloning the message context and creating an exact copy of the
 * passed message. One should use this method with care; that is because, inside the new MC,
 * most of the attributes of the MC like opCtx and so on are still kept as references inside
 * the axis2 MessageContext for performance improvements. (Note: U dont have to worrie
 * about the SOAPEnvelope, it is a cloned copy and not a reference from any other MC)
 * @param synCtx - this will be cloned
 * @param cloneSoapEnvelope whether to clone the soap envelope
 * @return cloned Synapse MessageContext
 * @throws AxisFault if there is a failure in creating the new Synapse MC or in a failure in
 *          clonning the underlying axis2 MessageContext
 *
 * @see MessageHelper#cloneAxis2MessageContext
 */
public static MessageContext cloneMessageContext(MessageContext synCtx, boolean cloneSoapEnvelope) throws AxisFault {
    // creates the new MessageContext and clone the internal axis2 MessageContext
    // inside the synapse message context and place that in the new one
    MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
    Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
    axis2MC.setAxis2MessageContext(cloneAxis2MessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext(), cloneSoapEnvelope));
    newCtx.setConfiguration(synCtx.getConfiguration());
    newCtx.setEnvironment(synCtx.getEnvironment());
    newCtx.setContextEntries(synCtx.getContextEntries());
    // set the parent correlation details to the cloned MC -
    // for the use of aggregation like tasks
    newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
    // copying the core parameters of the synapse MC
    newCtx.setTo(synCtx.getTo());
    newCtx.setReplyTo(synCtx.getReplyTo());
    newCtx.setSoapAction(synCtx.getSoapAction());
    newCtx.setWSAAction(synCtx.getWSAAction());
    newCtx.setResponse(synCtx.isResponse());
    // copy all the synapse level properties to the newCtx
    for (Object o : synCtx.getPropertyKeySet()) {
        // If there are non String keyed properties neglect them rather than trow exception
        if (o instanceof String) {
            String strkey = (String) o;
            Object obj = synCtx.getProperty(strkey);
            if (obj instanceof String) {
            // No need to do anything since Strings are immutable
            } else if (obj instanceof ArrayList) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone Started for  ArrayList property: " + strkey + ".");
                }
                // Call this method to deep clone ArrayList
                obj = cloneArrayList((ArrayList) obj);
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone Ended for  ArrayList property: " + strkey + ".");
                }
            } else if (obj instanceof Stack && strkey.equals(SynapseConstants.SYNAPSE__FUNCTION__STACK)) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone for Template function stack");
                }
                obj = getClonedTemplateStack((Stack<TemplateContext>) obj);
            } else if (obj instanceof OMElement) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone for OMElement");
                }
                obj = (OMElement) ((OMElement) obj).cloneOMElement();
            } else if (obj instanceof ResponseState) {
            // do nothing and let the same reference to go to the cloned context
            } else {
                /**
                 * Need to add conditions according to type if found in
                 * future
                 */
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
                }
            }
            newCtx.setProperty(strkey, obj);
        }
    }
    // Make deep copy of fault stack so that parent will not be lost it's fault stack
    Stack<FaultHandler> faultStack = synCtx.getFaultStack();
    if (!faultStack.isEmpty()) {
        List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
        newFaultStack.addAll(faultStack);
        for (FaultHandler faultHandler : newFaultStack) {
            if (faultHandler != null) {
                newCtx.pushFaultHandler(faultHandler);
            }
        }
    }
    Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
    if (functionStack != null) {
        newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
    }
    if (log.isDebugEnabled()) {
        log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
    }
    // Copy ContinuationStateStack from original MC to the new MC
    if (synCtx.isContinuationEnabled()) {
        Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
        newCtx.setContinuationEnabled(true);
        for (ContinuationState continuationState : continuationStates) {
            if (continuationState != null) {
                newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
            }
        }
    }
    newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
    return newCtx;
}
Also used : ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) Stack(java.util.Stack) ContinuationState(org.apache.synapse.ContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) ResponseState(org.apache.synapse.core.axis2.ResponseState) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) FaultHandler(org.apache.synapse.FaultHandler) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 3 with SeqContinuationState

use of org.apache.synapse.continuation.SeqContinuationState in project wso2-synapse by wso2.

the class MessageHelper method cloneMessageContextForAggregateMediator.

public static MessageContext cloneMessageContextForAggregateMediator(MessageContext synCtx) throws AxisFault {
    // creates the new MessageContext and clone the internal axis2 MessageContext
    // inside the synapse message context and place that in the new one
    MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
    Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
    axis2MC.setAxis2MessageContext(cloneAxis2MessageContextForAggregate(((Axis2MessageContext) synCtx).getAxis2MessageContext()));
    newCtx.setConfiguration(synCtx.getConfiguration());
    newCtx.setEnvironment(synCtx.getEnvironment());
    newCtx.setContextEntries(synCtx.getContextEntries());
    // set the parent correlation details to the cloned MC -
    // for the use of aggregation like tasks
    newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
    // copying the core parameters of the synapse MC
    newCtx.setTo(synCtx.getTo());
    newCtx.setReplyTo(synCtx.getReplyTo());
    newCtx.setSoapAction(synCtx.getSoapAction());
    newCtx.setWSAAction(synCtx.getWSAAction());
    newCtx.setResponse(synCtx.isResponse());
    // copy all the synapse level properties to the newCtx
    for (Object o : synCtx.getPropertyKeySet()) {
        // throw exception
        if (o instanceof String) {
            /**
             * Clone the properties and add to new context
             * If not cloned can give errors in target configuration
             */
            String strkey = (String) o;
            Object obj = synCtx.getProperty(strkey);
            if (obj instanceof String) {
            // No need to do anything since Strings are immutable
            } else if (obj instanceof ArrayList) {
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone Started for  ArrayList property: " + strkey + ".");
                }
                // Call this method to deep clone ArrayList
                obj = cloneArrayList((ArrayList) obj);
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone Ended for  ArrayList property: " + strkey + ".");
                }
            } else {
                /**
                 * Need to add conditions according to type if found in
                 * future
                 */
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
                }
            }
            newCtx.setProperty(strkey, obj);
        }
    }
    // Make deep copy of fault stack so that parent will not be lost it's fault stack
    Stack<FaultHandler> faultStack = synCtx.getFaultStack();
    if (!faultStack.isEmpty()) {
        List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
        newFaultStack.addAll(faultStack);
        for (FaultHandler faultHandler : newFaultStack) {
            if (faultHandler != null) {
                newCtx.pushFaultHandler(faultHandler);
            }
        }
    }
    Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
    if (functionStack != null) {
        newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
    }
    if (log.isDebugEnabled()) {
        log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
    }
    // Copy ContinuationStateStack from original MC to the new MC
    if (synCtx.isContinuationEnabled()) {
        Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
        newCtx.setContinuationEnabled(true);
        for (ContinuationState continuationState : continuationStates) {
            if (continuationState != null) {
                newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
            }
        }
    }
    newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
    return newCtx;
}
Also used : ArrayList(java.util.ArrayList) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) Stack(java.util.Stack) ContinuationState(org.apache.synapse.ContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) FaultHandler(org.apache.synapse.FaultHandler) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 4 with SeqContinuationState

use of org.apache.synapse.continuation.SeqContinuationState in project wso2-synapse by wso2.

the class Axis2SynapseEnvironment method mediateFromContinuationStateStack.

/**
 * When request is sent using a Call Mediator, mediate the response message using the
 * ContinuationState Stack
 * @param synCtx MessageContext
 * @return whether mediation is completed
 */
private boolean mediateFromContinuationStateStack(MessageContext synCtx) {
    if (log.isDebugEnabled()) {
        log.debug("Mediating response using the ContinuationStateStack");
    }
    if (synCtx.getContinuationStateStack().isEmpty()) {
        // ideally this should never happens
        log.warn("ContinuationStateStack empty. No ContinuationState to mediate the response ");
        return false;
    }
    if (RuntimeStatisticCollector.isStatisticsEnabled()) {
        OpenEventCollector.openContinuationEvents(synCtx);
    }
    // First push fault handlers for first continuation state.
    SeqContinuationState seqContinuationState = (SeqContinuationState) ContinuationStackManager.peakContinuationStateStack(synCtx);
    if (seqContinuationState != null) {
        ContinuationStackManager.pushFaultHandler(synCtx, seqContinuationState);
    } else {
        return false;
    }
    boolean result = false;
    do {
        seqContinuationState = (SeqContinuationState) ContinuationStackManager.peakContinuationStateStack(synCtx);
        if (seqContinuationState != null) {
            SequenceMediator sequenceMediator = ContinuationStackManager.retrieveSequence(synCtx, seqContinuationState);
            // Report Statistics for this continuation call
            result = sequenceMediator.mediate(synCtx, seqContinuationState);
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                sequenceMediator.reportCloseStatistics(synCtx, null);
            }
        } else {
            break;
        }
    // for any result close the sequence as it will be handled by the callback method in statistics
    } while (result && !synCtx.getContinuationStateStack().isEmpty());
    return result;
}
Also used : SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Aggregations

SeqContinuationState (org.apache.synapse.continuation.SeqContinuationState)4 Stack (java.util.Stack)3 ArrayList (java.util.ArrayList)2 ContinuationState (org.apache.synapse.ContinuationState)2 FaultHandler (org.apache.synapse.FaultHandler)2 MessageContext (org.apache.synapse.MessageContext)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 TemplateContext (org.apache.synapse.mediators.template.TemplateContext)2 OMElement (org.apache.axiom.om.OMElement)1 Mediator (org.apache.synapse.Mediator)1 SynapseLog (org.apache.synapse.SynapseLog)1 ResponseState (org.apache.synapse.core.axis2.ResponseState)1 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)1 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)1 MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)1 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)1