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