summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/ui/UI.java
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/com/vaadin/ui/UI.java')
-rw-r--r--server/src/com/vaadin/ui/UI.java40
1 files changed, 40 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java
index 796d1f08ea..6a21ba3357 100644
--- a/server/src/com/vaadin/ui/UI.java
+++ b/server/src/com/vaadin/ui/UI.java
@@ -1054,4 +1054,44 @@ public abstract class UI extends AbstractSingleComponentContainer implements
public int getTabIndex() {
return getState(false).tabIndex;
}
+
+ /**
+ * Performs a safe update of this UI.
+ * <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.
+ * </p>
+ * <p>
+ * Note that exclusive access is only guaranteed as long as the UI is
+ * attached to a VaadinSession. If the UI is not attached to a session, this
+ * method makes no guarantees. If the UI is detached then the current
+ * session will also be null.
+ * </p>
+ *
+ * @param runnable
+ * The runnable which updates the UI
+ */
+ public void runSafely(Runnable runnable) {
+ Map<Class<?>, CurrentInstance> old = null;
+
+ VaadinSession session = getSession();
+
+ if (session != null) {
+ session.lock();
+ }
+ try {
+ old = CurrentInstance.setThreadLocals(this);
+ runnable.run();
+ } finally {
+ if (session != null) {
+ session.unlock();
+ }
+ if (old != null) {
+ CurrentInstance.restoreThreadLocals(old);
+ }
+ }
+
+ }
+
}