Search in sources :

Example 71 with Mediator

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

the class DynamicResourceTest method testDynamicSequenceLookup.

public void testDynamicSequenceLookup() throws Exception {
    System.out.println("Testing dynamic sequence lookup...");
    // Phase 1
    System.out.println("Testing basic registry lookup functionality...");
    MessageContext synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
    Mediator seq1 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
    assertNotNull(seq1);
    assertTrue(((SequenceMediator) seq1).isInitialized());
    assertEquals(1, registry.getHitCount());
    seq1.mediate(synCtx);
    assertEquals("bar", synCtx.getProperty("foo"));
    // Phase 2
    System.out.println("Testing basic sequence caching...");
    synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
    Mediator seq2 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
    assertNotNull(seq2);
    assertTrue(((SequenceMediator) seq2).isInitialized());
    assertEquals(1, registry.getHitCount());
    seq2.mediate(synCtx);
    assertEquals("bar", synCtx.getProperty("foo"));
    assertTrue(seq1 == seq2);
    // Phase 3
    System.out.println("Testing advanced sequence caching...");
    synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
    System.out.println("Waiting for the cache to expire...");
    Thread.sleep(8500L);
    Mediator seq3 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
    assertNotNull(seq3);
    assertTrue(((SequenceMediator) seq3).isInitialized());
    assertEquals(1, registry.getHitCount());
    seq3.mediate(synCtx);
    assertEquals("bar", synCtx.getProperty("foo"));
    assertTrue(seq1 == seq3);
    // Phase 4
    System.out.println("Testing sequence reloading...");
    registry.updateResource(KEY_DYNAMIC_SEQUENCE_1, TestUtils.createOMElement(DYNAMIC_SEQUENCE_2));
    System.out.println("Waiting for the cache to expire...");
    Thread.sleep(8500L);
    synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
    Mediator seq4 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
    assertNotNull(seq4);
    assertTrue(((SequenceMediator) seq4).isInitialized());
    assertEquals(2, registry.getHitCount());
    seq4.mediate(synCtx);
    assertEquals("baz", synCtx.getProperty("foo"));
    assertTrue(seq1 != seq4);
    assertTrue(!((SequenceMediator) seq1).isInitialized());
    // Phase 5
    System.out.println("Testing for non-existing sequences...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Mediator seq5 = synCtx.getSequence("non-existing-sequence");
    assertNull(seq5);
    System.out.println("Dynamic sequence lookup tests were successful...");
}
Also used : Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) MessageContext(org.apache.synapse.MessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 72 with Mediator

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

the class ScriptMediatorFactoryTest method testRegPropWithFunctionMediatorFactory.

public void testRegPropWithFunctionMediatorFactory() throws Exception {
    Entry prop = new Entry();
    prop.setValue(MY_MEDIATOR_FOO_FUNC);
    Map<String, Entry> props = new HashMap<String, Entry>();
    props.put("MyFooMediator", prop);
    MessageContext mc = TestUtils.getTestContext("<foo/>", props);
    ScriptMediatorFactory mf = new ScriptMediatorFactory();
    Mediator mediator = mf.createMediator(REG_PROP_FOO_FUNC_MEDIATOR_CONFIG, new Properties());
    assertTrue(mediator.mediate(mc));
}
Also used : Entry(org.apache.synapse.config.Entry) HashMap(java.util.HashMap) Mediator(org.apache.synapse.Mediator) MessageContext(org.apache.synapse.MessageContext) Properties(java.util.Properties)

Example 73 with Mediator

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

the class ScriptMediatorFactoryTest method testInlineScriptMediatorFactory.

public void testInlineScriptMediatorFactory() throws XMLStreamException {
    ScriptMediatorFactory mf = new ScriptMediatorFactory();
    Mediator mediator = mf.createMediator(INLINE_MEDIATOR_CONFIG, new Properties());
    try {
        MessageContext mc = TestUtils.getTestContext("<foo/>", null);
        assertTrue(mediator.mediate(mc));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Mediator(org.apache.synapse.Mediator) MessageContext(org.apache.synapse.MessageContext) Properties(java.util.Properties) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 74 with Mediator

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

the class SynapseXMLConfigurationFactory method defineMediatorTemplate.

public static Mediator defineMediatorTemplate(SynapseConfiguration config, OMElement ele, Properties properties) {
    Mediator mediator = null;
    String name = ele.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name != null) {
        try {
            mediator = MediatorFactoryFinder.getInstance().getMediator(ele, properties);
            if (mediator != null) {
                config.addSequenceTemplate(name, (TemplateMediator) mediator);
            }
        } catch (Exception e) {
            String msg = "Template configuration: " + name + " cannot be built";
            handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_TEMPLATES, msg, e);
        }
        return mediator;
    } else {
        String msg = "Invalid mediation template definition without a name";
        handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_SEQUENCES, msg);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) SynapseException(org.apache.synapse.SynapseException)

Example 75 with Mediator

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

the class SynapseXMLConfigurationFactory method defineSequence.

public static Mediator defineSequence(SynapseConfiguration config, OMElement ele, Properties properties) {
    Mediator mediator = null;
    String name = ele.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name != null) {
        try {
            MediatorFactoryFinder.getInstance().setSynapseImportMap(config.getSynapseImports());
            mediator = MediatorFactoryFinder.getInstance().getMediator(ele, properties);
            if (mediator != null) {
                config.addSequence(name, mediator);
                // this also limits the ability of the mandatory sequence to be dynamic
                if (SynapseConstants.MANDATORY_SEQUENCE_KEY.equals(name)) {
                    config.setMandatorySequence(mediator);
                }
            }
        } catch (Exception e) {
            String msg = "Sequence configuration: " + name + " cannot be built";
            handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_SEQUENCES, msg, e);
        }
        return mediator;
    } else {
        String msg = "Invalid sequence definition without a name";
        handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_SEQUENCES, msg);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) SynapseException(org.apache.synapse.SynapseException)

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