Search in sources :

Example 1 with PropertyInclude

use of org.apache.synapse.PropertyInclude in project wso2-synapse by wso2.

the class WeightedRRLCAlgorithm method intialize.

/**
 * Initialize the algorithm reading the configurations from the endpoints.
 */
private void intialize() {
    // get the global properties
    if (loadBalanceEndpoint != null && loadBalanceEndpoint instanceof PropertyInclude) {
        PropertyInclude include = (PropertyInclude) loadBalanceEndpoint;
        MediatorProperty val = include.getProperty(LB_WEIGHTED_RRLC_ROUNDS_PER_RECAL);
        if (val != null) {
            roundsPerRecalculation = Integer.parseInt(val.getValue());
        }
    }
    // initialize the states list, this runs only once
    list = new WeightedState[endpoints.size()];
    int totalWeight = 0;
    for (Endpoint endpoint : endpoints) {
        if (endpoint instanceof PropertyInclude) {
            PropertyInclude include = (PropertyInclude) endpoint;
            MediatorProperty val = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT);
            if (val == null) {
                String msg = "Parameter " + "loadbalance.weighted.weight should be specified for every " + "endpoint in the load balance group";
                log.error(msg);
                throw new SynapseException(msg);
            }
            totalWeight += Integer.parseInt(val.getValue());
        }
    }
    this.totalWeight = totalWeight;
    for (int i = 0; i < endpoints.size(); i++) {
        Endpoint e = endpoints.get(i);
        if (e instanceof PropertyInclude) {
            PropertyInclude include = (PropertyInclude) e;
            MediatorProperty weight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT);
            String key;
            URL url;
            if (e instanceof AddressEndpoint) {
                AddressEndpoint addressEndpoint = (AddressEndpoint) e;
                try {
                    url = new URL(addressEndpoint.getDefinition().getAddress());
                } catch (MalformedURLException e1) {
                    String msg = "Mulformed URL in address endpoint";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            } else if (e instanceof WSDLEndpoint) {
                WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) e;
                try {
                    url = new URL(wsdlEndpoint.getDefinition().getAddress());
                } catch (MalformedURLException e1) {
                    String msg = "Mulformed URL in address endpoint";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            } else {
                String msg = "Only AddressEndpoint and WSDLEndpoint can be used " + "with WeightedRRLCAlgorithm";
                log.error(msg);
                throw new SynapseException(msg);
            }
            // construct the key
            key = url.getHost() + ":" + url.getPort();
            WeightedState state = new WeightedState(Integer.parseInt(weight.getValue()), i, key);
            MediatorProperty minimumWeight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT_MIN);
            if (minimumWeight != null) {
                state.setMinWeight(Integer.parseInt(minimumWeight.getValue()));
            }
            MediatorProperty maxWeight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT_MAX);
            if (maxWeight != null) {
                state.setMaxWeight(Integer.parseInt(maxWeight.getValue()));
            }
            list[i] = state;
        }
    }
    // sort the states according to the initial fixed weights
    Arrays.sort(list, new Comparator<WeightedState>() {

        public int compare(WeightedState o1, WeightedState o2) {
            return o2.getFixedWeight() - o1.getFixedWeight();
        }
    });
}
Also used : WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) MalformedURLException(java.net.MalformedURLException) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) SynapseException(org.apache.synapse.SynapseException) PropertyInclude(org.apache.synapse.PropertyInclude) WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) URL(java.net.URL)

Example 2 with PropertyInclude

use of org.apache.synapse.PropertyInclude in project wso2-synapse by wso2.

the class WeightedRoundRobin method init.

public void init(SynapseEnvironment se) {
    if (endpoints == null) {
        String msg = "Endpoints are not set, cannot initialize the algorithm";
        log.error(msg);
        throw new SynapseException(msg);
    }
    endpointStates = new EndpointState[endpoints.size()];
    for (int i = 0; i < endpoints.size(); i++) {
        Endpoint endpoint = endpoints.get(i);
        if (!(endpoint instanceof PropertyInclude)) {
            EndpointState state = new EndpointState(i, DEFAULT_WEIGHT);
            endpointStates[i] = state;
        } else {
            MediatorProperty property = ((PropertyInclude) endpoint).getProperty(LOADBALANCE_WEIGHT);
            EndpointState state;
            if (property != null) {
                int weight = Integer.parseInt(property.getValue());
                if (weight <= 0) {
                    String msg = "Weight must be greater than zero";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
                state = new EndpointState(i, weight);
            } else {
                state = new EndpointState(i, DEFAULT_WEIGHT);
            }
            endpointStates[i] = state;
        }
    }
    if (loadBalanceEndpoint instanceof PropertyInclude) {
        MediatorProperty threadLocalProperty = ((PropertyInclude) loadBalanceEndpoint).getProperty(LOADBALANCE_ThEADLOCAL);
        if (threadLocalProperty != null && threadLocalProperty.getValue().equals("true")) {
            isThreadLocal = true;
        }
    }
    view = new WeightedRoundRobinView(this);
    MBeanRegistrar.getInstance().registerMBean(view, "LBAlgorithms", loadBalanceEndpoint.getName() != null ? loadBalanceEndpoint.getName() : "LBEpr");
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) Endpoint(org.apache.synapse.endpoints.Endpoint) PropertyInclude(org.apache.synapse.PropertyInclude) Endpoint(org.apache.synapse.endpoints.Endpoint)

Aggregations

PropertyInclude (org.apache.synapse.PropertyInclude)2 SynapseException (org.apache.synapse.SynapseException)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)1 WSDLEndpoint (org.apache.synapse.endpoints.WSDLEndpoint)1