Search in sources :

Example 1 with LoadbalanceEndpoint

use of org.apache.synapse.endpoints.LoadbalanceEndpoint in project wso2-synapse by wso2.

the class LoadbalanceEndpointFactory method createEndpoint.

protected Endpoint createEndpoint(OMElement epConfig, boolean anonymousEndpoint, Properties properties) {
    // create the endpoint, manager and the algorithms
    OMElement loadbalanceElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "loadbalance"));
    if (loadbalanceElement != null) {
        LoadbalanceEndpoint loadbalanceEndpoint = new LoadbalanceEndpoint();
        // set endpoint name
        OMAttribute name = epConfig.getAttribute(new QName(org.apache.synapse.config.xml.XMLConfigConstants.NULL_NAMESPACE, "name"));
        if (name != null) {
            loadbalanceEndpoint.setName(name.getAttributeValue());
        }
        LoadbalanceAlgorithm algorithm = null;
        // set endpoints or members
        if (loadbalanceElement.getFirstChildWithName(XMLConfigConstants.ENDPOINT_ELT) != null) {
            if (loadbalanceElement.getChildrenWithName((MEMBER)).hasNext()) {
                String msg = "Invalid Synapse configuration. " + "child elements";
                log.error(msg);
                throw new SynapseException(msg);
            }
            List<Endpoint> endpoints = getEndpoints(loadbalanceElement, loadbalanceEndpoint, properties);
            loadbalanceEndpoint.setChildren(endpoints);
            algorithm = LoadbalanceAlgorithmFactory.createLoadbalanceAlgorithm(loadbalanceElement, endpoints);
            algorithm.setLoadBalanceEndpoint(loadbalanceEndpoint);
        } else if (loadbalanceElement.getFirstChildWithName(MEMBER) != null) {
            if (loadbalanceElement.getChildrenWithName((XMLConfigConstants.ENDPOINT_ELT)).hasNext()) {
                String msg = "Invalid Synapse configuration. " + "loadbalanceEndpoint element cannot have both member & endpoint " + "child elements";
                log.error(msg);
                throw new SynapseException(msg);
            }
            List<Member> members = getMembers(loadbalanceElement);
            loadbalanceEndpoint.setMembers(members);
            algorithm = LoadbalanceAlgorithmFactory.createLoadbalanceAlgorithm2(loadbalanceElement, members);
            loadbalanceEndpoint.startApplicationMembershipTimer();
        }
        if (loadbalanceEndpoint.getChildren() == null && loadbalanceEndpoint.getMembers() == null) {
            String msg = "Invalid Synapse configuration.\n" + "A LoadbalanceEndpoint must have child elements, but the LoadbalanceEndpoint " + "'" + loadbalanceEndpoint.getName() + "' does not have any child elements.";
            log.error(msg);
            throw new SynapseException(msg);
        }
        // set load balance algorithm
        loadbalanceEndpoint.setAlgorithm(algorithm);
        // set if failover is turned off
        String failover = loadbalanceElement.getAttributeValue(new QName("failover"));
        if (failover != null && failover.equalsIgnoreCase("false")) {
            loadbalanceEndpoint.setFailover(false);
        }
        // set buildMessage attribute
        String buildMessageAtt = loadbalanceElement.getAttributeValue(new QName(XMLConfigConstants.BUILD_MESSAGE));
        if (buildMessageAtt != null) {
            loadbalanceEndpoint.setBuildMessageAttAvailable(true);
            if (JavaUtils.isTrueExplicitly(buildMessageAtt)) {
                loadbalanceEndpoint.setBuildMessageAtt(true);
            }
        }
        // process the parameters
        processProperties(loadbalanceEndpoint, epConfig);
        return loadbalanceEndpoint;
    }
    // ToDo
    return null;
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) SynapseException(org.apache.synapse.SynapseException) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) QName(javax.xml.namespace.QName) LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm) OMElement(org.apache.axiom.om.OMElement) ArrayList(java.util.ArrayList) List(java.util.List) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with LoadbalanceEndpoint

use of org.apache.synapse.endpoints.LoadbalanceEndpoint in project wso2-synapse by wso2.

the class LoadbalanceEndpointSerializer method serializeEndpoint.

protected OMElement serializeEndpoint(Endpoint endpoint) {
    if (!(endpoint instanceof LoadbalanceEndpoint)) {
        handleException("Invalid endpoint type.");
    }
    fac = OMAbstractFactory.getOMFactory();
    OMElement endpointElement = fac.createOMElement("endpoint", SynapseConstants.SYNAPSE_OMNAMESPACE);
    LoadbalanceEndpoint loadbalanceEndpoint = (LoadbalanceEndpoint) endpoint;
    serializeCommonAttributes(endpoint, endpointElement);
    OMElement loadbalanceElement = fac.createOMElement("loadbalance", SynapseConstants.SYNAPSE_OMNAMESPACE);
    endpointElement.addChild(loadbalanceElement);
    loadbalanceElement.addAttribute(XMLConfigConstants.LOADBALANCE_ALGORITHM, loadbalanceEndpoint.getAlgorithm().getClass().getName(), null);
    // set if failover is turned off in the endpoint
    if (!loadbalanceEndpoint.isFailover()) {
        loadbalanceElement.addAttribute("failover", "false", null);
    }
    if (loadbalanceEndpoint.isBuildMessageAtt()) {
        loadbalanceElement.addAttribute(XMLConfigConstants.BUILD_MESSAGE, Boolean.toString(loadbalanceEndpoint.isBuildMessageAtt()), null);
    }
    // Serialize endpoint elements which are children of the loadbalance element
    if (loadbalanceEndpoint.getChildren() != null) {
        for (Endpoint childEndpoint : loadbalanceEndpoint.getChildren()) {
            loadbalanceElement.addChild(EndpointSerializer.getElementFromEndpoint(childEndpoint));
        }
    } else {
        for (Member member : loadbalanceEndpoint.getMembers()) {
            OMElement memberEle = fac.createOMElement("member", SynapseConstants.SYNAPSE_OMNAMESPACE, loadbalanceElement);
            memberEle.addAttribute(fac.createOMAttribute("hostName", null, member.getHostName()));
            memberEle.addAttribute(fac.createOMAttribute("httpPort", null, String.valueOf(member.getHttpPort())));
            memberEle.addAttribute(fac.createOMAttribute("httpsPort", null, String.valueOf(member.getHttpsPort())));
            loadbalanceElement.addChild(memberEle);
        }
    }
    // serialize the parameters
    serializeProperties(loadbalanceEndpoint, endpointElement);
    return endpointElement;
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) OMElement(org.apache.axiom.om.OMElement) Member(org.apache.axis2.clustering.Member)

Example 3 with LoadbalanceEndpoint

use of org.apache.synapse.endpoints.LoadbalanceEndpoint in project wso2-synapse by wso2.

the class SendMediatorSerializationTest method testSimpleLoadbalanceSendSerialization.

public void testSimpleLoadbalanceSendSerialization() {
    String sendConfig = "<send xmlns=\"http://ws.apache.org/ns/synapse\">" + "<endpoint>" + "<loadbalance>" + "<endpoint>" + "<address uri=\"http://localhost:9001/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "<endpoint>" + "<address uri=\"http://localhost:9002/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "<endpoint>" + "<address uri=\"http://localhost:9003/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "</loadbalance>" + "</endpoint>" + "</send>";
    OMElement config1 = createOMElement(sendConfig);
    SendMediator send1 = (SendMediator) factory.createMediator(config1, new Properties());
    OMElement config2 = serializer.serializeMediator(null, send1);
    SendMediator send2 = (SendMediator) factory.createMediator(config2, new Properties());
    assertTrue("Top level endpoint should be a load balance endpoint.", send2.getEndpoint() instanceof LoadbalanceEndpoint);
    LoadbalanceEndpoint endpoint = (LoadbalanceEndpoint) send2.getEndpoint();
    List addresses = endpoint.getChildren();
    assertEquals("There should be 3 leaf level address endpoints", addresses.size(), 3);
    assertTrue("Leaf level endpoints should be address endpoints", addresses.get(0) instanceof AddressEndpoint);
    assertTrue("Leaf level endpoints should be address endpoints", addresses.get(1) instanceof AddressEndpoint);
    assertTrue("Leaf level endpoints should be address endpoints", addresses.get(2) instanceof AddressEndpoint);
    AddressEndpoint addressEndpoint = (AddressEndpoint) addresses.get(0);
    assertTrue("URI of address endpoint is not serialized properly", "http://localhost:9001/services/Service1".equals(addressEndpoint.getDefinition().getAddress()));
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) OMElement(org.apache.axiom.om.OMElement) List(java.util.List) SendMediator(org.apache.synapse.mediators.builtin.SendMediator) Properties(java.util.Properties)

Example 4 with LoadbalanceEndpoint

use of org.apache.synapse.endpoints.LoadbalanceEndpoint in project wso2-synapse by wso2.

the class CallMediatorSerializationTest method testSimpleLoadbalanceCallSerialization.

public void testSimpleLoadbalanceCallSerialization() {
    String callConfig = "<call xmlns=\"http://ws.apache.org/ns/synapse\">" + "<endpoint>" + "<loadbalance>" + "<endpoint>" + "<address uri=\"http://localhost:9001/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "<endpoint>" + "<address uri=\"http://localhost:9002/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "<endpoint>" + "<address uri=\"http://localhost:9003/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "</loadbalance>" + "</endpoint>" + "</call>";
    OMElement config1 = createOMElement(callConfig);
    CallMediator call1 = (CallMediator) factory.createMediator(config1, new Properties());
    OMElement config2 = serializer.serializeMediator(null, call1);
    CallMediator call2 = (CallMediator) factory.createMediator(config2, new Properties());
    assertTrue("Top level endpoint should be a load balance endpoint.", call2.getEndpoint() instanceof LoadbalanceEndpoint);
    LoadbalanceEndpoint endpoint = (LoadbalanceEndpoint) call2.getEndpoint();
    List addresses = endpoint.getChildren();
    assertEquals("There should be 3 leaf level address endpoints", addresses.size(), 3);
    assertTrue("Leaf level endpoints should be address endpoints", addresses.get(0) instanceof AddressEndpoint);
    assertTrue("Leaf level endpoints should be address endpoints", addresses.get(1) instanceof AddressEndpoint);
    assertTrue("Leaf level endpoints should be address endpoints", addresses.get(2) instanceof AddressEndpoint);
    AddressEndpoint addressEndpoint = (AddressEndpoint) addresses.get(0);
    assertTrue("URI of address endpoint is not serialized properly", "http://localhost:9001/services/Service1".equals(addressEndpoint.getDefinition().getAddress()));
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) OMElement(org.apache.axiom.om.OMElement) List(java.util.List) CallMediator(org.apache.synapse.mediators.builtin.CallMediator) Properties(java.util.Properties)

Example 5 with LoadbalanceEndpoint

use of org.apache.synapse.endpoints.LoadbalanceEndpoint in project wso2-synapse by wso2.

the class CallMediatorSerializationTest method testNestedLoadbalanceFailoverCallSerialization.

public void testNestedLoadbalanceFailoverCallSerialization() {
    String callConfig = "<call xmlns=\"http://ws.apache.org/ns/synapse\">" + "<endpoint>" + "<loadbalance>" + "<endpoint>" + "<address uri=\"http://localhost:9001/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "<endpoint>" + "<failover>" + "<endpoint>" + "<address uri=\"http://localhost:9002/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "<endpoint>" + "<address uri=\"http://localhost:9003/services/Service1\">" + "<enableAddressing/>" + "</address>" + "</endpoint>" + "</failover>" + "</endpoint>" + "</loadbalance>" + "</endpoint>" + "</call>";
    OMElement config1 = createOMElement(callConfig);
    CallMediator call1 = (CallMediator) factory.createMediator(config1, new Properties());
    OMElement config2 = serializer.serializeMediator(null, call1);
    CallMediator call2 = (CallMediator) factory.createMediator(config2, new Properties());
    assertTrue("Top level endpoint should be a load balance endpoint.", call2.getEndpoint() instanceof LoadbalanceEndpoint);
    LoadbalanceEndpoint loadbalanceEndpoint = (LoadbalanceEndpoint) call2.getEndpoint();
    List children = loadbalanceEndpoint.getChildren();
    assertEquals("Top level endpoint should have 2 child endpoints.", children.size(), 2);
    assertTrue("First child should be a address endpoint", children.get(0) instanceof AddressEndpoint);
    assertTrue("Second child should be a fail over endpoint", children.get(1) instanceof FailoverEndpoint);
    FailoverEndpoint failoverEndpoint = (FailoverEndpoint) children.get(1);
    List children2 = failoverEndpoint.getChildren();
    assertEquals("Fail over endpoint should have 2 children.", children2.size(), 2);
    assertTrue("Children of the fail over endpoint should be address endpoints.", children2.get(0) instanceof AddressEndpoint);
    assertTrue("Children of the fail over endpoint should be address endpoints.", children2.get(1) instanceof AddressEndpoint);
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) OMElement(org.apache.axiom.om.OMElement) List(java.util.List) CallMediator(org.apache.synapse.mediators.builtin.CallMediator) Properties(java.util.Properties) FailoverEndpoint(org.apache.synapse.endpoints.FailoverEndpoint)

Aggregations

LoadbalanceEndpoint (org.apache.synapse.endpoints.LoadbalanceEndpoint)9 OMElement (org.apache.axiom.om.OMElement)6 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)6 List (java.util.List)5 Properties (java.util.Properties)4 Endpoint (org.apache.synapse.endpoints.Endpoint)4 FailoverEndpoint (org.apache.synapse.endpoints.FailoverEndpoint)2 CallMediator (org.apache.synapse.mediators.builtin.CallMediator)2 SendMediator (org.apache.synapse.mediators.builtin.SendMediator)2 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 Member (org.apache.axis2.clustering.Member)1 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)1 MessageContext (org.apache.synapse.MessageContext)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1