PL-9606 Unable to use several server selectors with different base URLs

This commit is contained in:
Konstantin Krivopustov 2017-08-31 17:45:56 +04:00
parent 498e205c01
commit 78e17a4f66
6 changed files with 45 additions and 21 deletions

View File

@ -25,7 +25,7 @@ import java.util.List;
public interface SessionUrlsHolder {
@Nullable
List<String> getUrls();
List<String> getUrls(String selectorId);
void setUrls(List<String> urls);
void setUrls(String selectorId, List<String> urls);
}

View File

@ -16,6 +16,7 @@
package com.haulmont.cuba.core.sys.remoting.discovery;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -50,13 +51,18 @@ public class StaticServerSelector extends StickySessionServerSelector {
public void init() {
if (baseUrl == null)
throw new IllegalStateException("baseUrl is null");
log.info("Server URL(s): {}", baseUrl);
String[] strings = baseUrl.split("[,;]");
for (String string : strings) {
if (!StringUtils.isBlank(string)) {
urls.add(string + "/" + servletPath);
}
}
log.debug("Server URLs: {}", urls);
log.info("Server URLs: {}", urls);
initSelectorId();
}
protected void initSelectorId() {
id = DigestUtils.md5Hex(baseUrl);
log.trace("Selector id for '" + baseUrl + "' is '" + id + "'");
}
}

View File

@ -45,6 +45,8 @@ public abstract class StickySessionServerSelector implements ServerSelector {
protected SessionUrlsHolder anonymousSessionUrlsHolder;
protected String id;
protected Set<String> failedUrls = new CopyOnWriteArraySet<>();
protected ThreadLocal<List<String>> lastNoSessionUrls = new ThreadLocal<>();
@ -78,6 +80,10 @@ public abstract class StickySessionServerSelector implements ServerSelector {
this.anonymousSessionUrlsHolder = anonymousSessionUrlsHolder;
}
public void setSelectorId(String id) {
this.id = id;
}
/**
* Must be implemented in concrete classes to return a list of available servers.
*/
@ -93,14 +99,14 @@ public abstract class StickySessionServerSelector implements ServerSelector {
lastNoSessionUrls.set(sessionUrls);
} else {
//noinspection unchecked
sessionUrls = sessionUrlsHolder.getUrls();
sessionUrls = sessionUrlsHolder.getUrls(id);
if (sessionUrls == null) {
sessionUrls = lastNoSessionUrls.get();
isNewSession = true;
}
if (sessionUrls == null)
sessionUrls = sortUrls();
sessionUrlsHolder.setUrls(sessionUrls);
sessionUrlsHolder.setUrls(id, sessionUrls);
lastNoSessionUrls.remove();
}

View File

@ -25,7 +25,7 @@ import java.util.List;
*/
public class UserSessionUrlsHolder implements SessionUrlsHolder {
protected static final String SESSION_ATTR = UserSessionUrlsHolder.class.getName() + ".lastSessionUrls";
private static final String SESSION_ATTR = UserSessionUrlsHolder.class.getName() + ".lastSessionUrls";
private UserSession userSession;
@ -34,12 +34,16 @@ public class UserSessionUrlsHolder implements SessionUrlsHolder {
}
@Override
public List<String> getUrls() {
return userSession.getLocalAttribute(SESSION_ATTR);
public List<String> getUrls(String selectorId) {
return userSession.getLocalAttribute(getAttributeName(selectorId));
}
@Override
public void setUrls(List<String> urls) {
userSession.setLocalAttribute(SESSION_ATTR, urls);
public void setUrls(String selectorId, List<String> urls) {
userSession.setLocalAttribute(getAttributeName(selectorId), urls);
}
private String getAttributeName(String selectorId) {
return SESSION_ATTR + (selectorId != null ? "." + selectorId : "");
}
}

View File

@ -36,16 +36,16 @@ public class PortalHttpSessionUrlsHolder implements SessionUrlsHolder {
@SuppressWarnings("unchecked")
@Override
@Nullable
public List<String> getUrls() {
public List<String> getUrls(String selectorId) {
HttpSession httpSession = getHttpSession();
return httpSession != null ? (List<String>) httpSession.getAttribute(SESSION_ATTR) : null;
return httpSession != null ? (List<String>) httpSession.getAttribute(getAttributeName(selectorId)) : null;
}
@Override
public void setUrls(List<String> urls) {
public void setUrls(String selectorId, List<String> urls) {
HttpSession httpSession = getHttpSession();
if (httpSession != null) {
httpSession.setAttribute(SESSION_ATTR, urls);
httpSession.setAttribute(getAttributeName(selectorId), urls);
}
}
@ -58,4 +58,8 @@ public class PortalHttpSessionUrlsHolder implements SessionUrlsHolder {
return null;
}
}
private String getAttributeName(String selectorId) {
return SESSION_ATTR + (selectorId != null ? "." + selectorId : "");
}
}

View File

@ -38,25 +38,25 @@ public class WebHttpSessionUrlsHolder implements SessionUrlsHolder {
@SuppressWarnings("unchecked")
@Override
@Nullable
public List<String> getUrls() {
public List<String> getUrls(String selectorId) {
VaadinRequest vaadinRequest = VaadinService.getCurrentRequest();
if (vaadinRequest != null)
return (List) vaadinRequest.getWrappedSession().getAttribute(SESSION_ATTR);
return (List) vaadinRequest.getWrappedSession().getAttribute(getAttributeName(selectorId));
else {
HttpSession httpSession = getHttpSession();
return httpSession != null ? (List<String>) httpSession.getAttribute(SESSION_ATTR) : null;
return httpSession != null ? (List<String>) httpSession.getAttribute(getAttributeName(selectorId)) : null;
}
}
@Override
public void setUrls(List<String> urls) {
public void setUrls(String selectorId, List<String> urls) {
VaadinRequest vaadinRequest = VaadinService.getCurrentRequest();
if (vaadinRequest != null)
vaadinRequest.getWrappedSession().setAttribute(SESSION_ATTR, urls);
vaadinRequest.getWrappedSession().setAttribute(getAttributeName(selectorId), urls);
else {
HttpSession httpSession = getHttpSession();
if (httpSession != null) {
httpSession.setAttribute(SESSION_ATTR, urls);
httpSession.setAttribute(getAttributeName(selectorId), urls);
}
}
}
@ -70,4 +70,8 @@ public class WebHttpSessionUrlsHolder implements SessionUrlsHolder {
return null;
}
}
protected String getAttributeName(String selectorId) {
return SESSION_ATTR + (selectorId != null ? "." + selectorId : "");
}
}