From: rjung Date: Sun, 18 Sep 2011 09:21:24 +0000 (+0000) Subject: Support a regexp based filter of attribute X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=d2f74a62d08df534701b21679ca938f9c0baab45;p=tomcat7.0 Support a regexp based filter of attribute names in ClusterManagerBase and DeltaSession. Only attributes whose names match will be distributed. An empty filter means all attributes will be distributed (unchanged default behaviour). git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1172234 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/ha/session/ClusterManagerBase.java b/java/org/apache/catalina/ha/session/ClusterManagerBase.java index 0fb0bdc2b..978ca229b 100644 --- a/java/org/apache/catalina/ha/session/ClusterManagerBase.java +++ b/java/org/apache/catalina/ha/session/ClusterManagerBase.java @@ -19,6 +19,7 @@ package org.apache.catalina.ha.session; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.regex.Pattern; import org.apache.catalina.Container; import org.apache.catalina.Loader; @@ -35,6 +36,64 @@ import org.apache.catalina.tribes.io.ReplicationStream; public abstract class ClusterManagerBase extends ManagerBase implements ClusterManager { + /** + * The pattern used for including session attributes to + * replication, e.g. ^(userName|sessionHistory)$. + * If not set, all session attributes will be eligible for replication. + */ + private String sessionAttributeFilter = null; + + /** + * The compiled pattern used for including session attributes to + * replication, e.g. ^(userName|sessionHistory)$. + * If not set, all session attributes will be eligible for replication. + */ + private Pattern sessionAttributePattern = null; + + + /** + * Return the string pattern used for including session attributes + * to replication. + * + * @return the sessionAttributeFilter + */ + public String getSessionAttributeFilter() { + return sessionAttributeFilter; + } + + /** + * Set the pattern used for including session attributes to replication. + * If not set, all session attributes will be eligible for replication. + *

+ * E.g. ^(userName|sessionHistory)$ + *

+ * + * @param sessionAttributeFilter + * the filter name pattern to set + */ + public void setSessionAttributeFilter(String sessionAttributeFilter) { + if (sessionAttributeFilter == null + || sessionAttributeFilter.trim().equals("")) { + this.sessionAttributeFilter = null; + sessionAttributePattern = null; + } else { + this.sessionAttributeFilter = sessionAttributeFilter; + sessionAttributePattern = Pattern.compile(sessionAttributeFilter); + } + } + + /** + * Check whether the given session attribute should be distributed + * + * @return true if the attribute should be distributed + */ + public boolean willAttributeDistribute(String name) { + if (sessionAttributePattern == null) { + return true; + } + return sessionAttributePattern.matcher(name).matches(); + } + public static ClassLoader[] getClassLoaders(Container container) { Loader loader = null; ClassLoader classLoader = null; @@ -88,4 +147,4 @@ public abstract class ClusterManagerBase extends ManagerBase public void unload() { // NOOP } -} \ No newline at end of file +} diff --git a/java/org/apache/catalina/ha/session/DeltaSession.java b/java/org/apache/catalina/ha/session/DeltaSession.java index acea43272..11b175682 100644 --- a/java/org/apache/catalina/ha/session/DeltaSession.java +++ b/java/org/apache/catalina/ha/session/DeltaSession.java @@ -559,6 +559,35 @@ public class DeltaSession extends StandardSession implements Externalizable,Clus /** + * Check whether the Object can be distributed. + * The object is always distributable, if the cluster manager + * decides to never distribute it. + * @param name The name of the attribute to check + * @param value The value of the attribute to check + * @return true if the attribute is distributable, false otherwise + */ + protected boolean isAttributeDistributable(String name, Object value) { + if (manager instanceof ClusterManagerBase && + !((ClusterManagerBase)manager).willAttributeDistribute(name)) + return true; + return super.isAttributeDistributable(name, value); + } + + /** + * Exclude attributes from replication. + * @param name the attribute's name + * @return true is attribute should not be replicated + */ + protected boolean exclude(String name) { + + if (super.exclude(name)) + return true; + if (manager instanceof ClusterManagerBase) + return !((ClusterManagerBase)manager).willAttributeDistribute(name); + return false; + } + + /** * Remove the object bound with the specified name from this session. If the * session does not have an object bound with this name, this method does * nothing. @@ -635,6 +664,7 @@ public class DeltaSession extends StandardSession implements Externalizable,Clus // -------------------------------------------- HttpSession Private Methods + /** * Read a serialized version of this session object from the specified * object input stream.