use of org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher in project wso2-synapse by wso2.
the class ServiceDynamicLoadbalanceEndpointFactory method createEndpoint.
protected Endpoint createEndpoint(OMElement epConfig, boolean anonymousEndpoint, Properties properties) {
OMElement loadbalanceElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "serviceDynamicLoadbalance"));
if (loadbalanceElement == null) {
return null;
}
String configuration = loadbalanceElement.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "configuration"));
OMElement servicesEle;
if (configuration != null) {
if (configuration.startsWith("$system:")) {
configuration = System.getProperty(configuration.substring("$system:".length()));
}
// Load the file
StAXOMBuilder builder = null;
try {
builder = new StAXOMBuilder(new URL(configuration).openStream());
} catch (Exception e) {
handleException("Could not load ServiceDynamicLoadbalanceEndpoint configuration file " + configuration);
}
servicesEle = builder.getDocumentElement().getFirstChildWithName(SERVICES_QNAME);
} else {
OMElement lbConfigEle = loadbalanceElement.getFirstChildWithName(LB_CONFIG_QNAME);
if (lbConfigEle == null) {
throw new RuntimeException("loadBalancerConfig element not found as a child of " + "serviceDynamicLoadbalance element");
}
servicesEle = lbConfigEle.getFirstChildWithName(SERVICES_QNAME);
}
if (servicesEle == null) {
throw new RuntimeException("services element not found in serviceDynamicLoadbalance configuration");
}
Map<String, String> hostDomainMap = new HashMap<String, String>();
for (Iterator<OMElement> iter = servicesEle.getChildrenWithLocalName("service"); iter.hasNext(); ) {
OMElement serviceEle = iter.next();
OMElement hostsEle = serviceEle.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "hosts"));
if (hostsEle == null) {
throw new RuntimeException("hosts element not found as a child of service element");
}
List<String> hosts = new ArrayList<String>();
for (Iterator<OMElement> hostIter = hostsEle.getChildrenWithLocalName("host"); hostIter.hasNext(); ) {
OMElement hostEle = hostIter.next();
String host = hostEle.getText();
if (host.trim().length() == 0) {
throw new RuntimeException("host cannot be null");
}
hosts.add(host);
}
OMElement domainEle = serviceEle.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "domain"));
if (domainEle == null) {
throw new RuntimeException("domain element not found in as a child of services");
}
String domain = domainEle.getText();
if (domain.trim().length() == 0) {
throw new RuntimeException("domain cannot be null");
}
for (String host : hosts) {
if (hostDomainMap.containsKey(host)) {
throw new RuntimeException("host " + host + " has been already defined for " + "clustering domain " + hostDomainMap.get(host));
}
hostDomainMap.put(host, domain);
}
}
if (hostDomainMap.isEmpty()) {
throw new RuntimeException("No service elements defined under services");
}
LoadbalanceAlgorithm algorithm = LoadbalanceAlgorithmFactory.createLoadbalanceAlgorithm(loadbalanceElement, null);
ServiceDynamicLoadbalanceEndpoint loadbalanceEndpoint = new ServiceDynamicLoadbalanceEndpoint(hostDomainMap, algorithm);
// 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);
}
loadbalanceEndpoint.setFailover(false);
return loadbalanceEndpoint;
}
use of org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher in project wso2-synapse by wso2.
the class SALoadbalanceEndpoint method sendMessage.
public void sendMessage(MessageContext synCtx) {
logSetter();
if (log.isDebugEnabled()) {
log.debug("Start : Session Affinity Load-balance Endpoint " + getName());
}
if (getContext().isState(EndpointContext.ST_OFF)) {
informFailure(synCtx, SynapseConstants.ENDPOINT_LB_NONE_READY, "Loadbalance endpoint : " + getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT + " - is inactive");
return;
}
// first check if this session is associated with a session. if so, get the endpoint
// associated for that session.
SessionInformation sessionInformation = (SessionInformation) synCtx.getProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION);
List<Endpoint> endpoints = (List<Endpoint>) synCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_ENDPOINT_LIST);
if (!(dispatcher instanceof HttpSessionDispatcher)) {
try {
RelayUtils.buildMessage(((Axis2MessageContext) synCtx).getAxis2MessageContext(), false);
} catch (Exception e) {
handleException("Error while building message", e);
}
}
// evaluate the properties
evaluateProperties(synCtx);
if (sessionInformation == null && endpoints == null) {
sessionInformation = dispatcher.getSession(synCtx);
if (sessionInformation != null) {
if (log.isDebugEnabled()) {
log.debug("Current session id : " + sessionInformation.getId());
}
endpoints = dispatcher.getEndpoints(sessionInformation);
if (log.isDebugEnabled()) {
log.debug("Endpoint sequence (path) on current session : " + this + endpoints);
}
synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_ENDPOINT_LIST, endpoints);
// This is for reliably recovery any session information if while response is getting ,
// session information has been removed by cleaner.
// This will not be a cost as session information a not heavy data structure
synCtx.setProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION, sessionInformation);
}
}
if (sessionInformation != null && endpoints != null) {
// send message on current session
sendMessageOnCurrentSession(sessionInformation.getId(), endpoints, synCtx);
} else {
// prepare for a new session
sendMessageOnNewSession(synCtx);
}
}
Aggregations