]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use APPLICATION_SCOPE for the session lock (#11804)
authorTatu Lund <tatu@vaadin.com>
Wed, 13 Nov 2019 16:39:36 +0000 (18:39 +0200)
committerAnna Koskinen <Ansku@users.noreply.github.com>
Wed, 13 Nov 2019 16:39:36 +0000 (18:39 +0200)
* Use APPLICATION_SCOPE for the session lock

To be able to do this, relevant methods in VaadinService are made protected so
that VaadinPortletService can override them.

The Vaadin session itself is also stored in APPLICATION_SCOPE. The default
scope is PORTLET_SCOPE, so lock would otherwise not be in sync with
the session.

server/src/main/java/com/vaadin/server/VaadinPortletService.java
server/src/main/java/com/vaadin/server/VaadinService.java
server/src/test/java/com/vaadin/server/VaadinPortletServiceTest.java

index 43441810f2c9d09eb05261ede1defe74bfe4bf70..fd80b25261dfae2711c8ca56c2d08ff3f870e6d4 100644 (file)
@@ -22,6 +22,8 @@ import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.List;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -371,4 +373,40 @@ public class VaadinPortletService extends VaadinService {
         getWrappedPortletSession(wrappedSession).removeAttribute(
                 getSessionAttributeName(), PortletSession.APPLICATION_SCOPE);
     }
+
+    @Override
+    protected void setSessionLock(WrappedSession wrappedSession, Lock lock) {
+        if (wrappedSession == null) {
+            throw new IllegalArgumentException(
+                    "Can't set a lock for a null session");
+        }
+        Object currentSessionLock = getWrappedPortletSession(wrappedSession)
+                .getAttribute(getLockAttributeName(),
+                        PortletSession.APPLICATION_SCOPE);
+        assert (currentSessionLock == null
+                || currentSessionLock == lock) : "Changing the lock for a session is not allowed";
+
+        getWrappedPortletSession(wrappedSession).setAttribute(
+                getLockAttributeName(), lock,
+                PortletSession.APPLICATION_SCOPE);
+    }
+
+    @Override
+    protected Lock getSessionLock(WrappedSession wrappedSession) {
+        Object lock = getWrappedPortletSession(wrappedSession)
+                .getAttribute(getLockAttributeName(),
+                        PortletSession.APPLICATION_SCOPE);
+
+        if (lock instanceof ReentrantLock) {
+            return (ReentrantLock) lock;
+        }
+
+        if (lock == null) {
+            return null;
+        }
+
+        throw new RuntimeException(
+                "Something else than a ReentrantLock was stored in the "
+                        + getLockAttributeName() + " in the session");
+    }
 }
index 5107ff15016ecbc90965e0fdf17f02fcb5f58f9d..bed040f9f1235f1f1919ebf6a6330e2d5ee28aa5 100644 (file)
@@ -549,7 +549,7 @@ public abstract class VaadinService implements Serializable {
      * @param lock
      *            The lock object
      */
-    private void setSessionLock(WrappedSession wrappedSession, Lock lock) {
+    protected void setSessionLock(WrappedSession wrappedSession, Lock lock) {
         if (wrappedSession == null) {
             throw new IllegalArgumentException(
                     "Can't set a lock for a null session");
@@ -567,7 +567,7 @@ public abstract class VaadinService implements Serializable {
      *
      * @return The attribute name for the lock
      */
-    private String getLockAttributeName() {
+    protected String getLockAttributeName() {
         return getServiceName() + ".lock";
     }
 
index 124ce76688a333b55ffd5452465cf15d69c9a1a9..a0cb49dd23bd3385c6940ee3b33b2f3583408894 100644 (file)
@@ -7,6 +7,8 @@ import static org.mockito.Mockito.when;
 
 import java.util.concurrent.locks.ReentrantLock;
 
+import javax.portlet.PortletSession;
+
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -184,9 +186,9 @@ public class VaadinPortletServiceTest {
             ReentrantLock mockLock = Mockito.mock(ReentrantLock.class);
             when(mockLock.isHeldByCurrentThread()).thenReturn(true);
 
-            WrappedSession emptyWrappedSession = Mockito
+            WrappedPortletSession emptyWrappedSession = Mockito
                     .mock(WrappedPortletSession.class);
-            when(emptyWrappedSession.getAttribute("null.lock"))
+            when(emptyWrappedSession.getAttribute("null.lock",PortletSession.APPLICATION_SCOPE))
                     .thenReturn(mockLock);
             VaadinRequest requestWithUIIDSet = Mockito
                     .mock(VaadinRequest.class);