use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.
the class ThrottleMediator method init.
public void init(SynapseEnvironment se) {
if (onAcceptMediator instanceof ManagedLifecycle) {
((ManagedLifecycle) onAcceptMediator).init(se);
} else if (onAcceptSeqKey != null) {
SequenceMediator onAcceptSeq = (SequenceMediator) se.getSynapseConfiguration().getSequence(onAcceptSeqKey);
if (onAcceptSeq == null || onAcceptSeq.isDynamic()) {
se.addUnavailableArtifactRef(onAcceptSeqKey);
}
}
if (onRejectMediator instanceof ManagedLifecycle) {
((ManagedLifecycle) onRejectMediator).init(se);
} else if (onRejectSeqKey != null) {
SequenceMediator onRejectSeq = (SequenceMediator) se.getSynapseConfiguration().getSequence(onRejectSeqKey);
if (onRejectSeq == null || onRejectSeq.isDynamic()) {
se.addUnavailableArtifactRef(onRejectSeqKey);
}
}
// reference to axis2 configuration context
configContext = ((Axis2SynapseEnvironment) se).getAxis2ConfigurationContext();
// throttling data holder initialization of
// runtime throttle data eg :- throttle contexts
dataHolder = (ThrottleDataHolder) configContext.getProperty(ThrottleConstants.THROTTLE_INFO_KEY);
if (dataHolder == null) {
log.debug("Data holder not present in current Configuration Context");
synchronized (configContext) {
dataHolder = (ThrottleDataHolder) configContext.getProperty(ThrottleConstants.THROTTLE_INFO_KEY);
if (dataHolder == null) {
dataHolder = new ThrottleDataHolder();
configContext.setNonReplicableProperty(ThrottleConstants.THROTTLE_INFO_KEY, dataHolder);
}
}
}
// initializes whether clustering is enabled an Env. level
ClusteringAgent clusteringAgent = configContext.getAxisConfiguration().getClusteringAgent();
if (clusteringAgent != null) {
isClusteringEnable = true;
}
// static policy initialization
if (inLinePolicy != null) {
log.debug("Initializing using static throttling policy : " + inLinePolicy);
try {
throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(inLinePolicy));
if (throttle != null && concurrentAccessController == null) {
concurrentAccessController = throttle.getConcurrentAccessController();
if (concurrentAccessController != null) {
dataHolder.setConcurrentAccessController(key, concurrentAccessController);
}
}
} catch (ThrottleException e) {
handleException("Error processing the throttling policy", e, null);
}
}
// access rate controller initialization
accessControler = new AccessRateController();
// replicator for global concurrent state maintenance
if (isClusteringEnable) {
concurrentAccessReplicator = new ConcurrentAccessReplicator(configContext);
}
}
use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.
the class ThrottleMediator method mediate.
public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
SynapseLog synLog = getLog(synCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Throttle mediator : Mediating from ContinuationState");
}
boolean result;
boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
int subBranch = ((ReliantContinuationState) continuationState).getSubBranch();
if (subBranch == 0) {
if (!continuationState.hasChild()) {
result = ((SequenceMediator) onAcceptMediator).mediate(synCtx, continuationState.getPosition() + 1);
} else {
FlowContinuableMediator mediator = (FlowContinuableMediator) ((SequenceMediator) onAcceptMediator).getChild(continuationState.getPosition());
result = mediator.mediate(synCtx, continuationState.getChildContState());
if (isStatisticsEnabled) {
((Mediator) mediator).reportCloseStatistics(synCtx, null);
}
}
if (isStatisticsEnabled) {
onAcceptMediator.reportCloseStatistics(synCtx, null);
}
} else {
if (!continuationState.hasChild()) {
result = ((SequenceMediator) onRejectMediator).mediate(synCtx, continuationState.getPosition() + 1);
} else {
FlowContinuableMediator mediator = (FlowContinuableMediator) ((SequenceMediator) onRejectMediator).getChild(continuationState.getPosition());
result = mediator.mediate(synCtx, continuationState.getChildContState());
if (isStatisticsEnabled) {
((Mediator) mediator).reportCloseStatistics(synCtx, null);
}
}
if (isStatisticsEnabled) {
onRejectMediator.reportCloseStatistics(synCtx, null);
}
}
return result;
}
use of org.apache.synapse.mediators.base.SequenceMediator in project wso2-synapse by wso2.
the class ThrottleMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof ThrottleMediator)) {
handleException("Invalid Mediator has passed to serializer");
}
ThrottleMediator throttleMediator = (ThrottleMediator) m;
OMElement throttle = fac.createOMElement("throttle", synNS);
OMElement policy = fac.createOMElement("policy", synNS);
String key = throttleMediator.getPolicyKey();
if (key != null) {
policy.addAttribute(fac.createOMAttribute("key", nullNS, key));
throttle.addChild(policy);
} else {
OMNode inlinePolicy = throttleMediator.getInLinePolicy();
if (inlinePolicy != null) {
policy.addChild(inlinePolicy);
throttle.addChild(policy);
}
}
saveTracingState(throttle, throttleMediator);
String id = throttleMediator.getId();
if (id != null) {
throttle.addAttribute(fac.createOMAttribute("id", nullNS, id));
}
String onReject = throttleMediator.getOnRejectSeqKey();
if (onReject != null) {
throttle.addAttribute(fac.createOMAttribute(XMLConfigConstants.ONREJECT, nullNS, onReject));
} else {
Mediator mediator = throttleMediator.getOnRejectMediator();
SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
if (mediator != null && mediator instanceof SequenceMediator) {
OMElement element = serializer.serializeAnonymousSequence(null, (SequenceMediator) mediator);
element.setLocalName(XMLConfigConstants.ONREJECT);
throttle.addChild(element);
}
}
String onAccept = throttleMediator.getOnAcceptSeqKey();
if (onAccept != null) {
throttle.addAttribute(fac.createOMAttribute(XMLConfigConstants.ONACCEPT, nullNS, onAccept));
} else {
Mediator mediator = throttleMediator.getOnAcceptMediator();
SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
if (mediator != null && mediator instanceof SequenceMediator) {
OMElement element = serializer.serializeAnonymousSequence(null, (SequenceMediator) mediator);
element.setLocalName(XMLConfigConstants.ONACCEPT);
throttle.addChild(element);
}
}
return throttle;
}
use of org.apache.synapse.mediators.base.SequenceMediator 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.mediators.base.SequenceMediator in project wso2-synapse by wso2.
the class ResourceTest method testFaultSequence.
public void testFaultSequence() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
SequenceMediator inSequence = getTestSequence("seq.in", "seq.in.value");
((PropertyMediator) inSequence.getChild(0)).setScope("axis2");
XSLTMediator xsltMediator = new XSLTMediator();
xsltMediator.setXsltKey(new Value("/bogus/key"));
inSequence.addChild(xsltMediator);
resource.setInSequence(inSequence);
SequenceMediator faultSequence = getTestSequence("seq.fault", "seq.fault.value");
((PropertyMediator) faultSequence.getChild(0)).setScope("axis2");
resource.setFaultSequence(faultSequence);
api.addResource(resource);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
synapseConfig.addSequence("main", getTestSequence("main.in", "main.value"));
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
MessageContextCreatorForAxis2.setSynConfig(synapseConfig);
MessageContextCreatorForAxis2.setSynEnv(synCtx.getEnvironment());
org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
mc.setConfigurationContext(((Axis2SynapseEnvironment) synCtx.getEnvironment()).getAxis2ConfigurationContext());
new SynapseMessageReceiver().receive(mc);
assertEquals("seq.in.value", mc.getProperty("seq.in"));
assertEquals("seq.fault.value", mc.getProperty("seq.fault"));
}
Aggregations