Search in sources :

Example 1 with SwitchCase

use of org.apache.synapse.config.xml.SwitchCase in project wso2-synapse by wso2.

the class SwitchMediator method mediate.

/**
 * Iterate over switch cases and find match and execute selected sequence
 *
 * @param synCtx current context
 * @return as per standard semantics
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Switch mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    int parentsEffectiveTraceState = synCtx.getTracingState();
    // if I have been explicitly asked to enable or disable tracing, set it to the message
    // to pass it on; else, do nothing -> i.e. let the parents state flow
    setEffectiveTraceState(synCtx);
    String sourceText = source.stringValueOf(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("XPath : " + source + " evaluates to : " + sourceText);
    }
    try {
        if ((sourceText == null || cases.isEmpty()) && defaultCase != null) {
            synLog.traceOrDebug("Source XPath evaluated to : null or no switch " + "cases found. Executing the default case");
            ContinuationStackManager.addReliantContinuationState(synCtx, 0, getMediatorPosition());
            boolean result = defaultCase.mediate(synCtx);
            if (result) {
                ContinuationStackManager.removeReliantContinuationState(synCtx);
            }
            return result;
        } else {
            for (int i = 0; i < cases.size(); i++) {
                SwitchCase swCase = cases.get(i);
                if (swCase != null) {
                    if (swCase.matches(sourceText)) {
                        if (synLog.isTraceOrDebugEnabled()) {
                            synLog.traceOrDebug("Matching case found : " + swCase.getRegex());
                        }
                        ContinuationStackManager.addReliantContinuationState(synCtx, i + 1, getMediatorPosition());
                        boolean result = swCase.mediate(synCtx);
                        if (result) {
                            ContinuationStackManager.removeReliantContinuationState(synCtx);
                        }
                        return result;
                    }
                }
            }
            if (defaultCase != null) {
                // if any of the switch cases did not match
                synLog.traceOrDebug("None of the switch cases matched - executing default");
                ContinuationStackManager.addReliantContinuationState(synCtx, 0, getMediatorPosition());
                boolean result = defaultCase.mediate(synCtx);
                if (result) {
                    ContinuationStackManager.removeReliantContinuationState(synCtx);
                }
                return result;
            } else {
                synLog.traceOrDebug("None of the switch cases matched - no default case");
            }
        }
    } finally {
        synCtx.setTracingState(parentsEffectiveTraceState);
    }
    synLog.traceOrDebug("End : Switch mediator");
    return true;
}
Also used : SwitchCase(org.apache.synapse.config.xml.SwitchCase) SynapseLog(org.apache.synapse.SynapseLog)

Example 2 with SwitchCase

use of org.apache.synapse.config.xml.SwitchCase in project wso2-synapse by wso2.

the class SwitchMediatorTest method setUp.

public void setUp() throws Exception {
    ibmMediator = new TestMediator();
    ibmMediator.setHandler(new TestMediateHandler() {

        public void handle(MessageContext synCtx) {
            setExecutedCase("IBM");
        }
    });
    msftMediator = new TestMediator();
    msftMediator.setHandler(new TestMediateHandler() {

        public void handle(MessageContext synCtx) {
            setExecutedCase("MSFT");
        }
    });
    defaultMediator = new TestMediator();
    defaultMediator.setHandler(new TestMediateHandler() {

        public void handle(MessageContext synCtx) {
            setExecutedCase("DEFAULT");
        }
    });
    // create a new switch mediator
    switchMediator = new SwitchMediator();
    // set xpath condition to select symbol
    SynapseXPath xpath = new SynapseXPath("//wsx:symbol");
    xpath.addNamespace("wsx", "http://www.webserviceX.NET/");
    switchMediator.setSource(xpath);
    SwitchCase caseOne = new SwitchCase();
    caseOne.setRegex(Pattern.compile("IBM"));
    AnonymousListMediator mediatorOne = new AnonymousListMediator();
    mediatorOne.addAll(Arrays.asList(new Mediator[] { ibmMediator }));
    caseOne.setCaseMediator(mediatorOne);
    SwitchCase caseTwo = new SwitchCase();
    caseTwo.setRegex(Pattern.compile("MSFT"));
    AnonymousListMediator mediatorTwo = new AnonymousListMediator();
    mediatorTwo.addAll(Arrays.asList(new Mediator[] { msftMediator }));
    caseTwo.setCaseMediator(mediatorTwo);
    SwitchCase caseDefault = new SwitchCase();
    AnonymousListMediator mediatorDefault = new AnonymousListMediator();
    mediatorDefault.addAll(Arrays.asList(new Mediator[] { defaultMediator }));
    caseDefault.setCaseMediator(mediatorDefault);
    // set ibm mediator to be called for IBM, msft for MSFT and default for others..
    switchMediator.addCase(caseOne);
    switchMediator.addCase(caseTwo);
    switchMediator.setDefaultCase(caseDefault);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SwitchCase(org.apache.synapse.config.xml.SwitchCase) TestMediateHandler(org.apache.synapse.mediators.TestMediateHandler) AnonymousListMediator(org.apache.synapse.config.xml.AnonymousListMediator) TestMediator(org.apache.synapse.mediators.TestMediator) Mediator(org.apache.synapse.Mediator) AnonymousListMediator(org.apache.synapse.config.xml.AnonymousListMediator) TestMediator(org.apache.synapse.mediators.TestMediator) MessageContext(org.apache.synapse.MessageContext)

Example 3 with SwitchCase

use of org.apache.synapse.config.xml.SwitchCase in project wso2-synapse by wso2.

the class MediatorTreeTraverseUtil method getMediatorReference.

/**
 * Returns mediator referece associated with position while traversing the mediator tree.
 *
 * @param synCfg      synapse configuration reference
 * @param seqMediator sequence mediator which traverse happens
 * @param position    array of tree nodes specifying position integer with respect to it's parent node
 *                    starting from the root parent.
 * @return Mediator reference
 */
public static Mediator getMediatorReference(SynapseConfiguration synCfg, Mediator seqMediator, int[] position) {
    Mediator current_mediator = null;
    for (int counter = 0; counter < position.length; counter++) {
        if (counter == 0) {
            int mediatorCount = ((AbstractListMediator) seqMediator).getList().size();
            int correctedPosition = getCorrectedPossition((AbstractListMediator) seqMediator, position[counter]);
            if (mediatorCount > correctedPosition) {
                current_mediator = ((AbstractListMediator) seqMediator).getChild(correctedPosition);
            } else {
                log.warn("Mediator position requested is larger than last index : " + position[counter]);
            }
        }
        if (current_mediator != null && counter != 0) {
            if (current_mediator instanceof InvokeMediator) {
                current_mediator = synCfg.getSequenceTemplate(((InvokeMediator) current_mediator).getTargetTemplate());
            } else if (current_mediator instanceof FilterMediator) {
                if (position[counter] == 0) {
                    if (((FilterMediator) current_mediator).getElseMediator() != null) {
                        current_mediator = ((FilterMediator) current_mediator).getElseMediator();
                    } else if (((FilterMediator) current_mediator).getElseKey() != null) {
                        current_mediator = synCfg.getSequence(((FilterMediator) current_mediator).getElseKey());
                    }
                    continue;
                } else if (position[counter] == 1) {
                    if (((FilterMediator) current_mediator).getThenKey() != null) {
                        current_mediator = synCfg.getSequence(((FilterMediator) current_mediator).getThenKey());
                    } else {
                        counter = counter + 1;
                        if (counter < position.length) {
                            int mediatorCount = ((AbstractListMediator) current_mediator).getList().size();
                            int correctedPosition = getCorrectedPossition((AbstractListMediator) current_mediator, position[counter]);
                            if (mediatorCount > correctedPosition) {
                                current_mediator = ((AbstractListMediator) current_mediator).getChild(correctedPosition);
                            } else {
                                log.warn("Mediator position requested is larger than last index : " + position[counter]);
                            }
                        }
                    }
                    continue;
                }
            } else if (current_mediator instanceof SwitchMediator) {
                if (position[counter] == 0) {
                    SwitchCase switchCase = ((SwitchMediator) current_mediator).getDefaultCase();
                    if (switchCase != null) {
                        current_mediator = switchCase.getCaseMediator();
                    } else {
                        current_mediator = null;
                    }
                } else {
                    SwitchCase switchCase = ((SwitchMediator) current_mediator).getCases().get(position[counter] - 1);
                    if (switchCase != null) {
                        current_mediator = switchCase.getCaseMediator();
                    } else {
                        current_mediator = null;
                    }
                }
                continue;
            } else if (current_mediator instanceof AggregateMediator) {
                if (((AggregateMediator) current_mediator).getOnCompleteSequence() != null) {
                    current_mediator = ((AggregateMediator) current_mediator).getOnCompleteSequence();
                } else if (((AggregateMediator) current_mediator).getOnCompleteSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((AggregateMediator) current_mediator).getOnCompleteSequenceRef());
                }
            } else if (current_mediator instanceof ForEachMediator) {
                if (((ForEachMediator) current_mediator).getSequence() != null) {
                    current_mediator = ((ForEachMediator) current_mediator).getSequence();
                } else if (((ForEachMediator) current_mediator).getSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((ForEachMediator) current_mediator).getSequenceRef());
                }
            } else if (current_mediator instanceof IterateMediator) {
                if (((IterateMediator) current_mediator).getTarget().getSequence() != null) {
                    current_mediator = ((IterateMediator) current_mediator).getTarget().getSequence();
                } else if (((IterateMediator) current_mediator).getTarget().getSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((IterateMediator) current_mediator).getTarget().getSequenceRef());
                }
            } else if (current_mediator instanceof CloneMediator) {
                if (((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequence() != null) {
                    current_mediator = ((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequence();
                } else if (((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequenceRef() != null) {
                    current_mediator = synCfg.getSequence(((CloneMediator) current_mediator).getTargets().get(position[counter]).getSequenceRef());
                }
                continue;
            } else if (current_mediator.getType().equals("ThrottleMediator")) {
                current_mediator = ((EnclosedInlinedSequence) current_mediator).getInlineSequence(synCfg, position[counter]);
                continue;
            } else if (current_mediator.getType().equals("EntitlementMediator")) {
                current_mediator = ((EnclosedInlinedSequence) current_mediator).getInlineSequence(synCfg, position[counter]);
                continue;
            } else if (current_mediator.getType().equals("CacheMediator")) {
                current_mediator = ((EnclosedInlinedSequence) current_mediator).getInlineSequence(synCfg, 0);
            }
            if (current_mediator != null && (current_mediator instanceof AbstractListMediator)) {
                int mediatorCount = ((AbstractListMediator) current_mediator).getList().size();
                int correctedPosition = getCorrectedPossition((AbstractListMediator) current_mediator, position[counter]);
                if (mediatorCount > correctedPosition) {
                    current_mediator = ((AbstractListMediator) current_mediator).getChild(correctedPosition);
                } else {
                    log.warn("Mediator position requested is larger than last index : " + position[counter]);
                }
            } else {
                current_mediator = null;
                break;
            }
        }
    }
    return current_mediator;
}
Also used : AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) SwitchCase(org.apache.synapse.config.xml.SwitchCase) AggregateMediator(org.apache.synapse.mediators.eip.aggregator.AggregateMediator) ForEachMediator(org.apache.synapse.mediators.builtin.ForEachMediator) EnclosedInlinedSequence(org.apache.synapse.debug.constructs.EnclosedInlinedSequence) InvokeMediator(org.apache.synapse.mediators.template.InvokeMediator) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) SwitchMediator(org.apache.synapse.mediators.filters.SwitchMediator) InvokeMediator(org.apache.synapse.mediators.template.InvokeMediator) ForEachMediator(org.apache.synapse.mediators.builtin.ForEachMediator) IterateMediator(org.apache.synapse.mediators.eip.splitter.IterateMediator) CommentMediator(org.apache.synapse.mediators.builtin.CommentMediator) FilterMediator(org.apache.synapse.mediators.filters.FilterMediator) CloneMediator(org.apache.synapse.mediators.eip.splitter.CloneMediator) Mediator(org.apache.synapse.Mediator) AggregateMediator(org.apache.synapse.mediators.eip.aggregator.AggregateMediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) SwitchMediator(org.apache.synapse.mediators.filters.SwitchMediator) IterateMediator(org.apache.synapse.mediators.eip.splitter.IterateMediator) CloneMediator(org.apache.synapse.mediators.eip.splitter.CloneMediator)

Example 4 with SwitchCase

use of org.apache.synapse.config.xml.SwitchCase in project wso2-synapse by wso2.

the class SwitchMediator method setComponentStatisticsId.

@Override
public void setComponentStatisticsId(ArtifactHolder holder) {
    if (getAspectConfiguration() == null) {
        configure(new AspectConfiguration(getMediatorName()));
    }
    String mediatorId = StatisticIdentityGenerator.getIdForFlowContinuableMediator(getMediatorName(), ComponentType.MEDIATOR, holder);
    getAspectConfiguration().setUniqueId(mediatorId);
    for (SwitchCase switchCase : cases) {
        switchCase.setStatisticIdForMediators(holder);
    }
    StatisticIdentityGenerator.reportingFlowContinuableEndEvent(mediatorId, ComponentType.MEDIATOR, holder);
}
Also used : SwitchCase(org.apache.synapse.config.xml.SwitchCase) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration)

Aggregations

SwitchCase (org.apache.synapse.config.xml.SwitchCase)4 Mediator (org.apache.synapse.Mediator)2 MessageContext (org.apache.synapse.MessageContext)1 SynapseLog (org.apache.synapse.SynapseLog)1 AspectConfiguration (org.apache.synapse.aspects.AspectConfiguration)1 AnonymousListMediator (org.apache.synapse.config.xml.AnonymousListMediator)1 EnclosedInlinedSequence (org.apache.synapse.debug.constructs.EnclosedInlinedSequence)1 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)1 TestMediateHandler (org.apache.synapse.mediators.TestMediateHandler)1 TestMediator (org.apache.synapse.mediators.TestMediator)1 CommentMediator (org.apache.synapse.mediators.builtin.CommentMediator)1 ForEachMediator (org.apache.synapse.mediators.builtin.ForEachMediator)1 AggregateMediator (org.apache.synapse.mediators.eip.aggregator.AggregateMediator)1 CloneMediator (org.apache.synapse.mediators.eip.splitter.CloneMediator)1 IterateMediator (org.apache.synapse.mediators.eip.splitter.IterateMediator)1 FilterMediator (org.apache.synapse.mediators.filters.FilterMediator)1 SwitchMediator (org.apache.synapse.mediators.filters.SwitchMediator)1 InvokeMediator (org.apache.synapse.mediators.template.InvokeMediator)1 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)1