Search in sources :

Example 11 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class InvokeMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Invoke mediator : Mediating from ContinuationState");
    }
    boolean result;
    int subBranch = ((ReliantContinuationState) continuationState).getSubBranch();
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (subBranch == 0) {
        // Default flow
        TemplateMediator templateMediator = (TemplateMediator) synCtx.getSequenceTemplate(targetTemplate);
        if (!continuationState.hasChild()) {
            result = templateMediator.mediate(synCtx, continuationState.getPosition() + 1);
            if (result) {
                templateMediator.popFuncContextFrom(synCtx);
            }
        } else {
            FlowContinuableMediator mediator = (FlowContinuableMediator) templateMediator.getChild(continuationState.getPosition());
            result = mediator.mediate(synCtx, continuationState.getChildContState());
            if (isStatisticsEnabled) {
                ((Mediator) mediator).reportCloseStatistics(synCtx, null);
            }
        }
        if (isStatisticsEnabled) {
            templateMediator.reportCloseStatistics(synCtx, null);
        }
    } else {
        // Pre fetching invoke mediator flow
        String prefetchInvokeKey = key.evaluateValue(synCtx);
        InvokeMediator prefetchInvoke = (InvokeMediator) synCtx.getDefaultConfiguration(prefetchInvokeKey);
        ContinuationState childContinuationState = continuationState.getChildContState();
        result = prefetchInvoke.mediate(synCtx, childContinuationState);
        if (result && !childContinuationState.hasChild()) {
            // Pre fetching invoke mediator flow completed.
            // Remove ContinuationState represent the prefetchInvoke mediator and
            // flip the subbranch to default flow
            continuationState.removeLeafChild();
            ((ReliantContinuationState) continuationState).setSubBranch(0);
            // after prefetch invoke mediator flow, execute default flow
            result = mediate(synCtx, false);
        }
        if (isStatisticsEnabled) {
            prefetchInvoke.reportCloseStatistics(synCtx, null);
        }
    }
    return result;
}
Also used : ReliantContinuationState(org.apache.synapse.continuation.ReliantContinuationState) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) ContinuationState(org.apache.synapse.ContinuationState) ReliantContinuationState(org.apache.synapse.continuation.ReliantContinuationState)

Example 12 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class InvokeMediator method mediate.

private boolean mediate(MessageContext synCtx, boolean executePreFetchingSequence) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Invoking Target EIP Sequence " + targetTemplate + " paramNames : " + pName2ExpressionMap.keySet());
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    // get the target function template and invoke by passing populated
    // parameters
    Mediator mediator = synCtx.getSequenceTemplate(targetTemplate);
    if (mediator == null) {
        handleException("Sequence template " + targetTemplate + " cannot be found", synCtx);
    }
    // setting the log appender when external template executor is called a sequence template inside a car file
    if (mediator instanceof TemplateMediator) {
        CustomLogSetter.getInstance().setLogAppender(((TemplateMediator) mediator).getArtifactContainerName());
    }
    // executing key reference if found defined at configuration.
    if (executePreFetchingSequence && key != null) {
        String defaultConfiguration = key.evaluateValue(synCtx);
        Mediator m = synCtx.getDefaultConfiguration(defaultConfiguration);
        if (m == null) {
            handleException("Sequence named " + key + " cannot be found", synCtx);
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Executing with key " + key);
            }
            ContinuationStackManager.addReliantContinuationState(synCtx, 1, getMediatorPosition());
            boolean result = m.mediate(synCtx);
            if (result) {
                ContinuationStackManager.removeReliantContinuationState(synCtx);
            } else {
                return false;
            }
        }
    }
    if (mediator != null && mediator instanceof TemplateMediator) {
        populateParameters(synCtx, ((TemplateMediator) mediator).getName());
        if (executePreFetchingSequence) {
            ContinuationStackManager.addReliantContinuationState(synCtx, 0, getMediatorPosition());
        }
        boolean result = mediator.mediate(synCtx);
        if (result && executePreFetchingSequence) {
            ContinuationStackManager.removeReliantContinuationState(synCtx);
        }
        return result;
    }
    return false;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator)

Example 13 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class AggregateMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState contState) {
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Aggregate mediator : Mediating from ContinuationState");
    }
    boolean result;
    SequenceMediator onCompleteSequence = getOnCompleteSequence();
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (!contState.hasChild()) {
        result = onCompleteSequence.mediate(synCtx, contState.getPosition() + 1);
    } else {
        FlowContinuableMediator mediator = (FlowContinuableMediator) onCompleteSequence.getChild(contState.getPosition());
        result = mediator.mediate(synCtx, contState.getChildContState());
        if (isStatisticsEnabled) {
            ((Mediator) mediator).reportCloseStatistics(synCtx, null);
        }
    }
    if (isStatisticsEnabled) {
        onCompleteSequence.reportCloseStatistics(synCtx, null);
    }
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 14 with Mediator

use of org.apache.synapse.Mediator 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 15 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class SequenceMediator method mediate.

/**
 * If this mediator refers to another named Sequence, execute that. Else
 * execute the list of mediators (children) contained within this. If a referenced
 * named sequence mediator instance cannot be found at runtime, an exception is
 * thrown. This may occur due to invalid configuration of an erroneous runtime
 * change of the synapse configuration. It is the responsibility of the
 * SynapseConfiguration builder to ensure that dead references are not present.
 *
 * @param synCtx the synapse message
 * @return as per standard mediator result
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (sequenceType == SequenceType.NAMED) {
        CustomLogSetter.getInstance().setLogAppender(artifactContainerName);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Sequence " + (name == null ? (key == null ? "<anonymous" : "key=<" + key) : "<" + name) + ">");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (key == null) {
        // The onError sequence for handling errors which may occur during the
        // mediation through this sequence
        Mediator errorHandlerMediator = null;
        Integer statisticReportingIndex = null;
        if (RuntimeStatisticCollector.isStatisticsEnabled()) {
            statisticReportingIndex = reportOpenStatistics(synCtx, false);
        }
        try {
            // 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");
                }
            }
            // Add a new SeqContinuationState as we branched to new Sequence.
            boolean skipAddition = ContinuationStackManager.isSkipSeqContinuationStateAddition(synCtx);
            if (!skipAddition) {
                if (dynamic && registryKey != null) {
                    ContinuationStackManager.addSeqContinuationState(synCtx, registryKey, sequenceType);
                } else {
                    ContinuationStackManager.addSeqContinuationState(synCtx, name, sequenceType);
                }
            }
            boolean result = super.mediate(synCtx);
            if (result && !skipAddition) {
                // if flow completed remove the previously added SeqContinuationState
                ContinuationStackManager.removeSeqContinuationState(synCtx, sequenceType);
            }
            // 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();
                    }
                }
            }
            if (synLog.isTraceOrDebugEnabled()) {
                if (synLog.isTraceTraceEnabled()) {
                    synLog.traceTrace("Message : " + synCtx.getEnvelope());
                }
                synLog.traceOrDebug("End : Sequence <" + (name == null ? "anonymous" : name) + ">");
            }
            return result;
        } finally {
            // End Statistics
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                reportCloseStatistics(synCtx, statisticReportingIndex);
            }
        }
    } else {
        String sequenceKey = key.evaluateValue(synCtx);
        // Mediator m = synCtx.getSequence(key);
        Mediator m = synCtx.getSequence(sequenceKey);
        if (m == null) {
            handleException("Sequence named " + key + " cannot be found", synCtx);
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Executing with key " + key);
            }
            // Update the SeqContinuationState position with the mediator position of
            // sequence mediator in the sequence
            ContinuationStackManager.updateSeqContinuationState(synCtx, getMediatorPosition());
            boolean result = m.mediate(synCtx);
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("End : Sequence key=<" + key + ">");
            }
            return result;
        }
    }
    return false;
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) Stack(java.util.Stack)

Aggregations

Mediator (org.apache.synapse.Mediator)108 Properties (java.util.Properties)30 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)24 OMElement (org.apache.axiom.om.OMElement)22 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)22 MessageContext (org.apache.synapse.MessageContext)16 SynapseLog (org.apache.synapse.SynapseLog)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)12 TestMessageContext (org.apache.synapse.TestMessageContext)12 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)12 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)12 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)12 Test (org.junit.Test)10 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)9 SynapseSequenceType (org.apache.synapse.debug.constructs.SynapseSequenceType)8 SequenceMediationFlowPoint (org.apache.synapse.debug.constructs.SequenceMediationFlowPoint)6 ForEachMediatorFactory (org.apache.synapse.config.xml.ForEachMediatorFactory)5 MediatorFactory (org.apache.synapse.config.xml.MediatorFactory)5 MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)5