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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations