diff --git a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java index 9d361f8cdd6..1a1459a45a0 100644 --- a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java +++ b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/NoSqlSession.java @@ -77,7 +77,11 @@ public class NoSqlSession extends AbstractSession @Override public void setAttribute(String name, Object value) { - if ( updateAttribute(name,value) ) + Object old = changeAttribute(name,value); + if (value == null && old == null) + return; //not dirty, no change + + if (value==null || !value.equals(old)) { if (_dirty==null) { @@ -96,30 +100,7 @@ public class NoSqlSession extends AbstractSession super.timeout(); } - /* - * a boolean version of the setAttribute method that lets us manage the _dirty set - */ - protected boolean updateAttribute (String name, Object value) - { - Object old=null; - synchronized (this) - { - checkValid(); - old=doPutOrRemove(name,value); - } - if (value==null || !value.equals(old)) - { - if (old!=null) - unbindValue(name,old); - if (value!=null) - bindValue(name,value); - - _manager.doSessionAttributeListeners(this,name,old,value); - return true; - } - return false; - } /* ------------------------------------------------------------ */ @Override diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java index 03cfd80d6e7..69ecf5306c3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java @@ -443,7 +443,7 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI @Override public void putValue(java.lang.String name, java.lang.Object value) throws IllegalStateException { - setAttribute(name,value); + changeAttribute(name,value); } /* ------------------------------------------------------------ */ @@ -481,10 +481,16 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI @Override public void setAttribute(String name, Object value) { - updateAttribute(name,value); + changeAttribute(name,value); } /* ------------------------------------------------------------ */ + /** + * @param name + * @param value + * @deprecated use changeAttribute(String,Object) instead + * @return + */ protected boolean updateAttribute (String name, Object value) { Object old=null; @@ -506,6 +512,53 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI } return false; } + + + /* ------------------------------------------------------------ */ + /** + * Either set (perhaps replace) or remove the value of the attribute + * in the session. The appropriate session attribute listeners are + * also called. + * + * @param name + * @param value + * @return + */ + protected Object changeAttribute (String name, Object value) + { + Object old=null; + synchronized (this) + { + checkValid(); + old=doPutOrRemove(name,value); + } + + callSessionAttributeListeners(name, value, old); + + return old; + } + + /* ------------------------------------------------------------ */ + /** + * Call binding and attribute listeners based on the new and old + * values of the attribute. + * + * @param name name of the attribute + * @param newValue new value of the attribute + * @param oldValue previous value of the attribute + */ + protected void callSessionAttributeListeners (String name, Object newValue, Object oldValue) + { + if (newValue==null || !newValue.equals(oldValue)) + { + if (oldValue!=null) + unbindValue(name,oldValue); + if (newValue!=null) + bindValue(name,newValue); + + _manager.doSessionAttributeListeners(this,name,oldValue,newValue); + } + } /* ------------------------------------------------------------ */ protected void addAttributes(Map map) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java index e48a3b61303..4b6804669b8 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java @@ -246,15 +246,19 @@ public class JDBCSessionManager extends AbstractSessionManager @Override public void setAttribute (String name, Object value) { - updateAttribute(name, value); + Object old = changeAttribute(name, value); + if (value == null && old == null) + return; //if same as remove attribute but attribute was already removed, no change + _dirty = true; } @Override public void removeAttribute (String name) { - super.removeAttribute(name); - _dirty=true; + Object old = changeAttribute(name, null); + if (old != null) //only dirty if there was a previous value + _dirty=true; } @Override