Support a regexp based filter of attribute
authorrjung <rjung@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 18 Sep 2011 09:21:24 +0000 (09:21 +0000)
committerrjung <rjung@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 18 Sep 2011 09:21:24 +0000 (09:21 +0000)
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

java/org/apache/catalina/ha/session/ClusterManagerBase.java
java/org/apache/catalina/ha/session/DeltaSession.java

index 0fb0bdc..978ca22 100644 (file)
@@ -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. <code>^(userName|sessionHistory)$</code>.
+     *  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. <code>^(userName|sessionHistory)$</code>.
+     * 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.
+     * <p>
+     * E.g. <code>^(userName|sessionHistory)$</code>
+     * </p>
+     *
+     * @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
+}
index acea432..11b1756 100644 (file)
@@ -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.