The Vaadin session itself is also stored in APPLICATION_SCOPE. The default scope is PORTLET_SCOPE, so lock would be otherwise not be in sync with the session. To be able to do this, relevant methods in VaadinService are made protected so that VaadinPortletService can override them. Fixes #11611tags/8.10.0.alpha1
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.net.URL; | import java.net.URL; | ||||
import java.util.List; | 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.Level; | ||||
import java.util.logging.Logger; | import java.util.logging.Logger; | ||||
getWrappedPortletSession(wrappedSession).removeAttribute( | getWrappedPortletSession(wrappedSession).removeAttribute( | ||||
getSessionAttributeName(), PortletSession.APPLICATION_SCOPE); | 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"); | |||||
} | |||||
} | } |
* @param lock | * @param lock | ||||
* The lock object | * The lock object | ||||
*/ | */ | ||||
private void setSessionLock(WrappedSession wrappedSession, Lock lock) { | |||||
protected void setSessionLock(WrappedSession wrappedSession, Lock lock) { | |||||
if (wrappedSession == null) { | if (wrappedSession == null) { | ||||
throw new IllegalArgumentException( | throw new IllegalArgumentException( | ||||
"Can't set a lock for a null session"); | "Can't set a lock for a null session"); | ||||
* | * | ||||
* @return The attribute name for the lock | * @return The attribute name for the lock | ||||
*/ | */ | ||||
private String getLockAttributeName() { | |||||
protected String getLockAttributeName() { | |||||
return getServiceName() + ".lock"; | return getServiceName() + ".lock"; | ||||
} | } | ||||
import java.util.concurrent.locks.ReentrantLock; | import java.util.concurrent.locks.ReentrantLock; | ||||
import javax.portlet.PortletSession; | |||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.mockito.Mockito; | import org.mockito.Mockito; | ||||
ReentrantLock mockLock = Mockito.mock(ReentrantLock.class); | ReentrantLock mockLock = Mockito.mock(ReentrantLock.class); | ||||
when(mockLock.isHeldByCurrentThread()).thenReturn(true); | when(mockLock.isHeldByCurrentThread()).thenReturn(true); | ||||
WrappedSession emptyWrappedSession = Mockito | |||||
WrappedPortletSession emptyWrappedSession = Mockito | |||||
.mock(WrappedPortletSession.class); | .mock(WrappedPortletSession.class); | ||||
when(emptyWrappedSession.getAttribute("null.lock")) | |||||
when(emptyWrappedSession.getAttribute("null.lock",PortletSession.APPLICATION_SCOPE)) | |||||
.thenReturn(mockLock); | .thenReturn(mockLock); | ||||
VaadinRequest requestWithUIIDSet = Mockito | VaadinRequest requestWithUIIDSet = Mockito | ||||
.mock(VaadinRequest.class); | .mock(VaadinRequest.class); |