Search in sources :

Example 46 with SequenceMediator

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);
    }
}
Also used : ThrottleDataHolder(org.apache.synapse.commons.throttle.core.ThrottleDataHolder) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AccessRateController(org.apache.synapse.commons.throttle.core.AccessRateController) ClusteringAgent(org.apache.axis2.clustering.ClusteringAgent) ConcurrentAccessReplicator(org.apache.synapse.commons.throttle.core.ConcurrentAccessReplicator)

Example 47 with SequenceMediator

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;
}
Also used : ReliantContinuationState(org.apache.synapse.continuation.ReliantContinuationState) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 48 with SequenceMediator

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;
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediatorSerializer(org.apache.synapse.config.xml.SequenceMediatorSerializer) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 49 with SequenceMediator

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...");
}
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 50 with SequenceMediator

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"));
}
Also used : SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URITemplateHelper(org.apache.synapse.rest.dispatch.URITemplateHelper) XSLTMediator(org.apache.synapse.mediators.transform.XSLTMediator) PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) Value(org.apache.synapse.mediators.Value) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseMessageReceiver(org.apache.synapse.core.axis2.SynapseMessageReceiver) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)70 TestMediator (org.apache.synapse.mediators.TestMediator)18 OMElement (org.apache.axiom.om.OMElement)12 ProxyService (org.apache.synapse.core.axis2.ProxyService)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 Mediator (org.apache.synapse.Mediator)11 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)11 MessageContext (org.apache.synapse.MessageContext)9 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)8 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)7 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)7 OMAttribute (org.apache.axiom.om.OMAttribute)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)6 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)6 File (java.io.File)4 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)4 SynapseException (org.apache.synapse.SynapseException)4 SynapseLog (org.apache.synapse.SynapseLog)4 QName (javax.xml.namespace.QName)3 OMNode (org.apache.axiom.om.OMNode)3