Search in sources :

Example 1 with LoadbalanceAlgorithm

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

the class DynamicLoadbalanceEndpointFactory method createEndpoint.

protected Endpoint createEndpoint(OMElement epConfig, boolean anonymousEndpoint, Properties properties) {
    OMElement loadbalanceElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "dynamicLoadbalance"));
    if (loadbalanceElement != null) {
        DynamicLoadbalanceEndpoint loadbalanceEndpoint = new DynamicLoadbalanceEndpoint();
        // set endpoint name
        OMAttribute name = epConfig.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
        if (name != null) {
            loadbalanceEndpoint.setName(name.getAttributeValue());
        }
        // get the session for this endpoint
        OMElement sessionElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "session"));
        if (sessionElement != null) {
            OMElement sessionTimeout = sessionElement.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sessionTimeout"));
            if (sessionTimeout != null) {
                try {
                    loadbalanceEndpoint.setSessionTimeout(Long.parseLong(sessionTimeout.getText().trim()));
                } catch (NumberFormatException nfe) {
                    handleException("Invalid session timeout value : " + sessionTimeout.getText());
                }
            }
            String type = sessionElement.getAttributeValue(new QName("type"));
            if (type.equalsIgnoreCase("soap")) {
                Dispatcher soapDispatcher = new SoapSessionDispatcher();
                loadbalanceEndpoint.setDispatcher(soapDispatcher);
            } else if (type.equalsIgnoreCase("http")) {
                Dispatcher httpDispatcher = new HttpSessionDispatcher();
                loadbalanceEndpoint.setDispatcher(httpDispatcher);
            }
            loadbalanceEndpoint.setSessionAffinity(true);
        }
        // set if failover is turned off
        String failover = loadbalanceElement.getAttributeValue(new QName("failover"));
        if (failover != null && failover.equalsIgnoreCase("false")) {
            loadbalanceEndpoint.setFailover(false);
        } else {
            loadbalanceEndpoint.setFailover(true);
        }
        OMElement eventHandler = loadbalanceElement.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "membershipHandler"));
        if (eventHandler != null) {
            String clazz = eventHandler.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "class")).trim();
            try {
                LoadBalanceMembershipHandler lbMembershipHandler = (LoadBalanceMembershipHandler) Class.forName(clazz).newInstance();
                Properties lbProperties = new Properties();
                for (Iterator props = eventHandler.getChildrenWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "property")); props.hasNext(); ) {
                    OMElement prop = (OMElement) props.next();
                    String propName = prop.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "name")).trim();
                    String propValue = prop.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "value")).trim();
                    lbProperties.put(propName, propValue);
                }
                // Set load balance algorithm
                LoadbalanceAlgorithm algorithm = LoadbalanceAlgorithmFactory.createLoadbalanceAlgorithm(loadbalanceElement, null);
                lbMembershipHandler.init(lbProperties, algorithm);
                loadbalanceEndpoint.setLoadBalanceMembershipHandler(lbMembershipHandler);
            } catch (Exception e) {
                String msg = "Could not instantiate " + "LoadBalanceMembershipHandler implementation " + clazz;
                log.error(msg, e);
                throw new SynapseException(msg, e);
            }
        }
        processProperties(loadbalanceEndpoint, epConfig);
        return loadbalanceEndpoint;
    }
    return null;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher) Dispatcher(org.apache.synapse.endpoints.dispatch.Dispatcher) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) Properties(java.util.Properties) DynamicLoadbalanceEndpoint(org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint) SynapseException(org.apache.synapse.SynapseException) LoadBalanceMembershipHandler(org.apache.synapse.core.LoadBalanceMembershipHandler) Iterator(java.util.Iterator) LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm) OMAttribute(org.apache.axiom.om.OMAttribute) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher)

Example 2 with LoadbalanceAlgorithm

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

the class LoadbalanceAlgorithmFactory method createLoadbalanceAlgorithm2.

public static LoadbalanceAlgorithm createLoadbalanceAlgorithm2(OMElement loadbalanceElement, List<Member> members) {
    LoadbalanceAlgorithm algorithm = createLoadbalanceAlgorithm(loadbalanceElement, null);
    algorithm.setApplicationMembers(members);
    return algorithm;
}
Also used : LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm)

Example 3 with LoadbalanceAlgorithm

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

the class SALoadbalanceEndpointFactory method createEndpoint.

protected Endpoint createEndpoint(OMElement epConfig, boolean anonymousEndpoint, Properties properties) {
    // create the endpoint, manager and the algorithms
    SALoadbalanceEndpoint loadbalanceEndpoint = new SALoadbalanceEndpoint();
    // get the session for this endpoint
    OMElement sessionElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "session"));
    if (sessionElement != null) {
        OMElement sessionTimeout = sessionElement.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sessionTimeout"));
        if (sessionTimeout != null) {
            try {
                loadbalanceEndpoint.setSessionTimeout(Long.parseLong(sessionTimeout.getText().trim()));
            } catch (NumberFormatException nfe) {
                handleException("Invalid session timeout value : " + sessionTimeout.getText());
            }
        }
        String type = sessionElement.getAttributeValue(new QName("type"));
        if (type.equalsIgnoreCase("soap")) {
            Dispatcher soapDispatcher = new SoapSessionDispatcher();
            loadbalanceEndpoint.setDispatcher(soapDispatcher);
        } else if (type.equalsIgnoreCase("http")) {
            Dispatcher httpDispatcher = new HttpSessionDispatcher();
            loadbalanceEndpoint.setDispatcher(httpDispatcher);
        } else if (type.equalsIgnoreCase("simpleClientSession")) {
            Dispatcher csDispatcher = new SimpleClientSessionDispatcher();
            loadbalanceEndpoint.setDispatcher(csDispatcher);
        }
    } else {
        handleException("Session affinity endpoints should " + "have a session element in the configuration.");
    }
    // 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());
    }
    OMElement loadbalanceElement;
    loadbalanceElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "loadbalance"));
    if (loadbalanceElement != null) {
        // set endpoints
        List<Endpoint> endpoints = getEndpoints(loadbalanceElement, loadbalanceEndpoint, properties);
        loadbalanceEndpoint.setChildren(endpoints);
        // set load balance algorithm
        LoadbalanceAlgorithm algorithm = LoadbalanceAlgorithmFactory.createLoadbalanceAlgorithm(loadbalanceElement, endpoints);
        loadbalanceEndpoint.setAlgorithm(algorithm);
        // 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;
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher) Dispatcher(org.apache.synapse.endpoints.dispatch.Dispatcher) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) SimpleClientSessionDispatcher(org.apache.synapse.endpoints.dispatch.SimpleClientSessionDispatcher) SimpleClientSessionDispatcher(org.apache.synapse.endpoints.dispatch.SimpleClientSessionDispatcher) SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint) LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) OMAttribute(org.apache.axiom.om.OMAttribute) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher)

Example 4 with LoadbalanceAlgorithm

use of org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm 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 5 with LoadbalanceAlgorithm

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

the class LoadbalanceAlgorithmFactory method createLoadbalanceAlgorithm.

public static LoadbalanceAlgorithm createLoadbalanceAlgorithm(OMElement loadbalanceElement, List endpoints) {
    // default algorithm is round robin
    LoadbalanceAlgorithm algorithm = new RoundRobin(endpoints);
    OMAttribute policyAttribute = loadbalanceElement.getAttribute(new QName(null, XMLConfigConstants.LOADBALANCE_POLICY));
    OMAttribute algoAttribute = loadbalanceElement.getAttribute(new QName(null, XMLConfigConstants.LOADBALANCE_ALGORITHM));
    if (policyAttribute != null && algoAttribute != null) {
        String msg = "You cannot specify both the 'policy' & 'algorithm' in the configuration. " + "It is sufficient to provide only the 'algorithm'.";
        // We cannot continue execution. Hence it is logged at fatal level
        log.fatal(msg);
        throw new SynapseException(msg);
    }
    if (algoAttribute != null) {
        String algorithmStr = algoAttribute.getAttributeValue().trim();
        try {
            algorithm = (LoadbalanceAlgorithm) Class.forName(algorithmStr).newInstance();
            algorithm.setEndpoints(endpoints);
        } catch (Exception e) {
            String msg = "Cannot instantiate LoadbalanceAlgorithm implementation class " + algorithmStr;
            // We cannot continue execution. Hence it is logged at fatal level
            log.fatal(msg, e);
            throw new SynapseException(msg, e);
        }
    } else if (policyAttribute != null) {
        // currently only the roundRobin policy is supported
        if (!policyAttribute.getAttributeValue().trim().equals("roundRobin")) {
            String msg = "Unsupported algorithm " + policyAttribute.getAttributeValue().trim() + " specified. Please use the 'algorithm' attribute to specify the " + "correct loadbalance algorithm implementation.";
            // We cannot continue execution. Hence it is logged at fatal level
            log.fatal(msg);
            throw new SynapseException(msg);
        }
    }
    return algorithm;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm) RoundRobin(org.apache.synapse.endpoints.algorithms.RoundRobin) OMAttribute(org.apache.axiom.om.OMAttribute) SynapseException(org.apache.synapse.SynapseException)

Aggregations

LoadbalanceAlgorithm (org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm)7 QName (javax.xml.namespace.QName)5 OMAttribute (org.apache.axiom.om.OMAttribute)5 OMElement (org.apache.axiom.om.OMElement)4 SynapseException (org.apache.synapse.SynapseException)4 Dispatcher (org.apache.synapse.endpoints.dispatch.Dispatcher)3 HttpSessionDispatcher (org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher)3 SoapSessionDispatcher (org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher)3 Endpoint (org.apache.synapse.endpoints.Endpoint)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Properties (java.util.Properties)1 StAXOMBuilder (org.apache.axiom.om.impl.builder.StAXOMBuilder)1 GroupManagementAgent (org.apache.axis2.clustering.management.GroupManagementAgent)1 LoadBalanceMembershipHandler (org.apache.synapse.core.LoadBalanceMembershipHandler)1 DynamicLoadbalanceEndpoint (org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint)1 LoadbalanceEndpoint (org.apache.synapse.endpoints.LoadbalanceEndpoint)1 SALoadbalanceEndpoint (org.apache.synapse.endpoints.SALoadbalanceEndpoint)1