Change-Id: I640244732fb561d9f55da58f8ba94fd02875c191tags/7.1.0
@@ -37,8 +37,7 @@ public interface RequestHandler extends Serializable { | |||
* using VaadinSession or anything inside the VaadinSession you must ensure | |||
* the session is locked. This can be done by extending | |||
* {@link SynchronizedRequestHandler} or by using | |||
* {@link VaadinSession#runSafely(Runnable)} or | |||
* {@link UI#runSafely(Runnable)}. | |||
* {@link VaadinSession#access(Runnable)} or {@link UI#access(Runnable)}. | |||
* </p> | |||
* | |||
* @param session |
@@ -407,12 +407,12 @@ public abstract class VaadinService implements Serializable { | |||
*/ | |||
public void fireSessionDestroy(VaadinSession vaadinSession) { | |||
final VaadinSession session = vaadinSession; | |||
session.runSafely(new Runnable() { | |||
session.access(new Runnable() { | |||
@Override | |||
public void run() { | |||
ArrayList<UI> uis = new ArrayList<UI>(session.getUIs()); | |||
for (final UI ui : uis) { | |||
ui.runSafely(new Runnable() { | |||
ui.access(new Runnable() { | |||
@Override | |||
public void run() { | |||
/* | |||
@@ -1087,7 +1087,7 @@ public abstract class VaadinService implements Serializable { | |||
private void removeClosedUIs(final VaadinSession session) { | |||
ArrayList<UI> uis = new ArrayList<UI>(session.getUIs()); | |||
for (final UI ui : uis) { | |||
ui.runSafely(new Runnable() { | |||
ui.access(new Runnable() { | |||
@Override | |||
public void run() { | |||
if (ui.isClosing()) { | |||
@@ -1245,7 +1245,7 @@ public abstract class VaadinService implements Serializable { | |||
if (session != null) { | |||
final VaadinSession finalSession = session; | |||
session.runSafely(new Runnable() { | |||
session.access(new Runnable() { | |||
@Override | |||
public void run() { | |||
cleanupSession(finalSession); | |||
@@ -1254,7 +1254,7 @@ public abstract class VaadinService implements Serializable { | |||
final long duration = (System.nanoTime() - (Long) request | |||
.getAttribute(REQUEST_START_TIME_ATTRIBUTE)) / 1000000; | |||
session.runSafely(new Runnable() { | |||
session.access(new Runnable() { | |||
@Override | |||
public void run() { | |||
finalSession.setLastRequestDuration(duration); |
@@ -766,13 +766,12 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { | |||
* Locks this session to protect its data from concurrent access. Accessing | |||
* the UI state from outside the normal request handling should always lock | |||
* the session and unlock it when done. The preferred way to ensure locking | |||
* is done correctly is to wrap your code using | |||
* {@link UI#runSafely(Runnable)} (or | |||
* {@link VaadinSession#runSafely(Runnable)} if you are only touching the | |||
* is done correctly is to wrap your code using {@link UI#access(Runnable)} | |||
* (or {@link VaadinSession#access(Runnable)} if you are only touching the | |||
* session and not any UI), e.g.: | |||
* | |||
* <pre> | |||
* myUI.runSafely(new Runnable() { | |||
* myUI.access(new Runnable() { | |||
* @Override | |||
* public void run() { | |||
* // Here it is safe to update the UI. | |||
@@ -1064,23 +1063,27 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { | |||
} | |||
/** | |||
* Performs a safe update of this VaadinSession. | |||
* Provides exclusive access to this session from outside a request handling | |||
* thread. | |||
* <p> | |||
* This method runs the runnable code so that it is safe to update session | |||
* variables. It also ensures that all thread locals are set correctly when | |||
* executing the runnable. | |||
* The given runnable is executed while holding the session lock to ensure | |||
* exclusive access to this session. The session and related thread locals | |||
* are set properly before executing the runnable. | |||
* </p> | |||
* <p> | |||
* Note that using this method for a VaadinSession which has been detached | |||
* from its underlying HTTP session is not necessarily safe. Exclusive | |||
* access is provided through locking which is done using the underlying | |||
* session. | |||
* RPC handlers for components inside this session do not need this method | |||
* as the session is automatically locked by the framework during request | |||
* handling. | |||
* </p> | |||
* | |||
* @param runnable | |||
* The runnable which updates the session | |||
* the runnable which accesses the session | |||
* | |||
* @see #lock() | |||
* @see #getCurrent() | |||
* @see UI#access(Runnable) | |||
*/ | |||
public void runSafely(Runnable runnable) { | |||
public void access(Runnable runnable) { | |||
Map<Class<?>, CurrentInstance> old = null; | |||
lock(); | |||
try { | |||
@@ -1095,6 +1098,15 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { | |||
} | |||
/** | |||
* @deprecated As of 7.1.0.beta1, use {@link #access(Runnable)} instead. | |||
* This method will be removed before the final 7.1.0 release. | |||
*/ | |||
@Deprecated | |||
public void runSafely(Runnable runnable) { | |||
access(runnable); | |||
} | |||
/** | |||
* Gets the CSRF token (aka double submit cookie) that is used to protect | |||
* against Cross Site Request Forgery attacks. |
@@ -632,7 +632,7 @@ public class FileUploadHandler implements RequestHandler { | |||
private void cleanStreamVariable(VaadinSession session, | |||
final ClientConnector owner, final String variableName) { | |||
session.runSafely(new Runnable() { | |||
session.access(new Runnable() { | |||
@Override | |||
public void run() { | |||
owner.getUI() |
@@ -68,7 +68,7 @@ public class LoginForm extends CustomComponent { | |||
} | |||
final StringBuilder responseBuilder = new StringBuilder(); | |||
getUI().runSafely(new Runnable() { | |||
getUI().access(new Runnable() { | |||
@Override | |||
public void run() { | |||
String method = VaadinServletService.getCurrentServletRequest() |
@@ -1092,20 +1092,29 @@ public abstract class UI extends AbstractSingleComponentContainer implements | |||
} | |||
/** | |||
* Performs a safe update of this UI. | |||
* Provides exclusive access to this UI from outside a request handling | |||
* thread. | |||
* <p> | |||
* This method runs the runnable code so that it is safe to update UI and | |||
* session variables. It also ensures that all thread locals are set | |||
* correctly when executing the runnable. | |||
* The given runnable is executed while holding the session lock to ensure | |||
* exclusive access to this UI and its session. The UI and related thread | |||
* locals are set properly before executing the runnable. | |||
* </p> | |||
* <p> | |||
* RPC handlers for components inside this UI do not need this method as the | |||
* session is automatically locked by the framework during request handling. | |||
* </p> | |||
* | |||
* @param runnable | |||
* The runnable which updates the UI | |||
* the runnable which accesses the UI | |||
* @throws UIDetachedException | |||
* if the UI is not attached to a session (and locking can | |||
* therefore not be done) | |||
* | |||
* @see #getCurrent() | |||
* @see VaadinSession#access(Runnable) | |||
* @see VaadinSession#lock() | |||
*/ | |||
public void runSafely(Runnable runnable) throws UIDetachedException { | |||
public void access(Runnable runnable) throws UIDetachedException { | |||
Map<Class<?>, CurrentInstance> old = null; | |||
VaadinSession session = getSession(); | |||
@@ -1118,7 +1127,7 @@ public abstract class UI extends AbstractSingleComponentContainer implements | |||
try { | |||
if (getSession() == null) { | |||
// UI was detached after fetching the session but before we | |||
// acquiried the lock. | |||
// acquired the lock. | |||
throw new UIDetachedException(); | |||
} | |||
old = CurrentInstance.setThreadLocals(this); | |||
@@ -1132,6 +1141,15 @@ public abstract class UI extends AbstractSingleComponentContainer implements | |||
} | |||
/** | |||
* @deprecated As of 7.1.0.beta1, use {@link #access(Runnable)} instead. | |||
* This method will be removed before the final 7.1.0 release. | |||
*/ | |||
@Deprecated | |||
public void runSafely(Runnable runnable) throws UIDetachedException { | |||
access(runnable); | |||
} | |||
/** | |||
* Retrieves the object used for configuring tooltips. | |||
* |
@@ -119,7 +119,7 @@ public class CloseUI extends AbstractTestUI { | |||
@Override | |||
public void run() { | |||
ui.runSafely(new Runnable() { | |||
ui.access(new Runnable() { | |||
@Override | |||
public void run() { |
@@ -11,7 +11,7 @@ public abstract class UIRunSafelyThread extends Thread { | |||
@Override | |||
public void run() { | |||
ui.runSafely(new Runnable() { | |||
ui.access(new Runnable() { | |||
@Override | |||
public void run() { |
@@ -39,7 +39,7 @@ public class UIPolling extends AbstractTestUIWithLog { | |||
} catch (InterruptedException e) { | |||
} | |||
final int iteration = i; | |||
runSafely(new Runnable() { | |||
access(new Runnable() { | |||
@Override | |||
public void run() { | |||
log.log((iteration * SLEEP_TIME) + "ms has passed"); |
@@ -43,7 +43,7 @@ public class BroadcasterUI extends UI implements BroadcastListener { | |||
@Override | |||
public void receiveBroadcast(final String message) { | |||
runSafely(new Runnable() { | |||
access(new Runnable() { | |||
@Override | |||
public void run() { | |||
Notification n = new Notification("Message received", message, |
@@ -27,7 +27,7 @@ public class BasicPush extends AbstractTestUI { | |||
@Override | |||
public void run() { | |||
runSafely(new Runnable() { | |||
access(new Runnable() { | |||
@Override | |||
public void run() { | |||
counter2.setValue(counter2.getValue() + 1); |
@@ -11,7 +11,7 @@ public class PushFromInit extends AbstractTestUIWithLog { | |||
new Thread() { | |||
@Override | |||
public void run() { | |||
runSafely(new Runnable() { | |||
access(new Runnable() { | |||
@Override | |||
public void run() { | |||
log("Logged from background thread started in init"); |
@@ -52,7 +52,7 @@ public class TogglePush extends AbstractTestUI { | |||
new Timer().schedule(new TimerTask() { | |||
@Override | |||
public void run() { | |||
runSafely(new Runnable() { | |||
access(new Runnable() { | |||
@Override | |||
public void run() { | |||
updateCounter(); |