Search in sources :

Example 1 with AddressEndpoint

use of org.apache.synapse.endpoints.AddressEndpoint 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 AddressEndpoint

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

the class DynamicResourceTest method testDynamicEndpointLookup.

public void testDynamicEndpointLookup() throws Exception {
    System.out.println("Testing dynamic endpoint lookup...");
    // Phase 1
    System.out.println("Testing basic registry lookup functionality...");
    MessageContext synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep1 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep1);
    assertTrue(ep1.isInitialized());
    assertEquals(1, registry.getHitCount());
    assertEquals("http://test.url", ((AddressEndpoint) ep1).getDefinition().getAddress());
    // Phase 2
    System.out.println("Testing basic endpoint caching...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep2 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep2);
    assertEquals(1, registry.getHitCount());
    assertTrue(ep1 == ep2);
    // Phase 3
    System.out.println("Testing advanced endpoint caching...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    System.out.println("Waiting for the cache to expire...");
    Thread.sleep(8500L);
    Endpoint ep3 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep3);
    assertEquals(1, registry.getHitCount());
    assertTrue(ep1 == ep3);
    // Phase 4
    System.out.println("Testing endpoint reloading...");
    registry.updateResource(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_2));
    System.out.println("Waiting for the cache to expire...");
    Thread.sleep(8500L);
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep4 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep4);
    assertTrue(ep4.isInitialized());
    assertEquals(2, registry.getHitCount());
    assertEquals("http://test2.url", ((AddressEndpoint) ep4).getDefinition().getAddress());
    assertTrue(ep1 != ep4);
    assertTrue(!ep1.isInitialized());
    // Phase 5
    System.out.println("Testing for non-existing endpoints...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep5 = synCtx.getEndpoint("non-existing-endpoint");
    assertNull(ep5);
    System.out.println("Dynamic endpoint lookup tests were successful...");
}
Also used : AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) MessageContext(org.apache.synapse.MessageContext)

Example 3 with AddressEndpoint

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

the class ProxyServiceTest method testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeDisabled.

/**
 * Tests building a proxy service with an unreachable endpoint specified as the published wsdl url with the
 * 'enablePublishWSDLSafeMode' set to true.
 *
 * @throws Exception if an error occurs when converting the URI string to a URI
 */
public void testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeDisabled() throws Exception {
    SynapseConfiguration synCfg = new SynapseConfiguration();
    AxisConfiguration axisCfg = new AxisConfiguration();
    ProxyService proxyService = new ProxyService("faultyPublishWsdlEndpointProxyWithPublishWSDLSafeModeDisabled");
    proxyService.setPublishWSDLEndpoint("wsdlEndPoint");
    AddressEndpoint wsdlEndpoint = new AddressEndpoint();
    EndpointDefinition endpointDefinition = new EndpointDefinition();
    endpointDefinition.setAddress((new URI("http://localhost/SimpleStockService.wsdl")).toString());
    wsdlEndpoint.setDefinition(endpointDefinition);
    proxyService.addParameter("enablePublishWSDLSafeMode", false);
    synCfg.addEndpoint("wsdlEndPoint", wsdlEndpoint);
    try {
        proxyService.buildAxisService(synCfg, axisCfg);
        Assert.fail("Axis service built with an unreachable wsdl endpoint should throw fault");
    } catch (SynapseException e) {
        Assert.assertTrue("Unexpected exception thrown: " + e, e.getMessage().contains("Error reading from wsdl URI"));
    }
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) SynapseException(org.apache.synapse.SynapseException) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URI(java.net.URI)

Example 4 with AddressEndpoint

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

the class ProxyServiceTest method testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeEnabled.

/**
 * Tests building a proxy service with an unreachable endpoint specified as the published wsdl url with the
 * 'enablePublishWSDLSafeMode' set to false.
 *
 * @throws Exception if an error occurs when converting the URI string to a URI
 */
public void testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeEnabled() throws Exception {
    MessageContext synCtx = new TestMessageContext();
    SynapseConfiguration synCfg = new SynapseConfiguration();
    synCtx.setConfiguration(synCfg);
    AxisConfiguration axisCfg = new AxisConfiguration();
    ProxyService proxyService = new ProxyService("faultyPublishWsdlEndpointProxyWithPublishWSDLSafeModeEnabled");
    proxyService.setPublishWSDLEndpoint("wsdlEndPoint");
    AddressEndpoint wsdlEndpoint = new AddressEndpoint();
    EndpointDefinition endpointDefinition = new EndpointDefinition();
    endpointDefinition.setAddress((new URI("http://localhost/SimpleStockService.wsdl")).toString());
    wsdlEndpoint.setDefinition(endpointDefinition);
    proxyService.addParameter("enablePublishWSDLSafeMode", true);
    synCfg.addEndpoint("wsdlEndPoint", wsdlEndpoint);
    Assert.assertNull("Axis service built with an unreachable wsdl endpoint be null", proxyService.buildAxisService(synCfg, axisCfg));
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) TestMessageContext(org.apache.synapse.TestMessageContext) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) MessageContext(org.apache.synapse.MessageContext) TestMessageContext(org.apache.synapse.TestMessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URI(java.net.URI)

Example 5 with AddressEndpoint

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

the class WeightedRRLCAlgorithmTest method testNextEndpoint.

public void testNextEndpoint() {
    MessageContext messageContext = createMessageContext();
    LoadbalanceEndpoint endpoint = createLoadBalanceEndpoint();
    String[] firstTwoRoundsExpected = { "6", "6", "6", "6", "6", "6", "5", "5", "5", "5", "5", "3", "3", "3", "2", "2", "1", "6", "6", "6", "6", "6", "6", "5", "5", "5", "5", "5", "3", "3", "3", "2", "2", "1" };
    LoadbalanceAlgorithm algo = endpoint.getAlgorithm();
    String[] firstTwoRoundsResults = new String[34];
    for (int i = 0; i < 34; i++) {
        Endpoint epr = algo.getNextEndpoint(messageContext, null);
        if (epr instanceof AddressEndpoint) {
            firstTwoRoundsResults[i] = ((AddressEndpoint) epr).getProperty(WeightedRRLCAlgorithm.LB_WEIGHTED_RRLC_WEIGHT).getValue();
        }
    }
    for (int i = 0; i < 34; i++) {
        assertEquals(firstTwoRoundsExpected[i], firstTwoRoundsResults[i]);
    }
    String[] secondTwoRoundsExpected = { "6", "6", "6", "6", "5", "5", "5", "5", "5", "5", "5", "3", "3", "3", "3", "2", "2", "2", "2", "1", "1", "6", "6", "6", "6", "5", "5", "5", "5", "5", "5", "5", "3", "3" };
    String[] secondTwoRoundsResults = new String[34];
    for (int i = 0; i < 34; i++) {
        Endpoint epr = algo.getNextEndpoint(messageContext, null);
        if (epr instanceof AddressEndpoint) {
            secondTwoRoundsResults[i] = ((AddressEndpoint) epr).getProperty(WeightedRRLCAlgorithm.LB_WEIGHTED_RRLC_WEIGHT).getValue();
        }
    }
    for (int i = 0; i < 34; i++) {
        assertEquals(secondTwoRoundsExpected[i], secondTwoRoundsResults[i]);
    }
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint)

Aggregations

AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)34 OMElement (org.apache.axiom.om.OMElement)17 Endpoint (org.apache.synapse.endpoints.Endpoint)12 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)10 MessageContext (org.apache.synapse.MessageContext)10 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)10 EndpointDefinition (org.apache.synapse.endpoints.EndpointDefinition)10 Properties (java.util.Properties)8 ArrayList (java.util.ArrayList)7 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)7 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)7 Test (org.junit.Test)7 List (java.util.List)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)6 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)6 LoadbalanceEndpoint (org.apache.synapse.endpoints.LoadbalanceEndpoint)6 SynapseException (org.apache.synapse.SynapseException)5 FailoverEndpoint (org.apache.synapse.endpoints.FailoverEndpoint)4 CallMediator (org.apache.synapse.mediators.builtin.CallMediator)4 SendMediator (org.apache.synapse.mediators.builtin.SendMediator)4