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...");
}
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));
}
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();
}
}
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;
}
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;
}
Aggregations