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();
}
});
}
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...");
}
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"));
}
}
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));
}
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]);
}
}
Aggregations