]> source.dussan.org Git - vaadin-framework.git/commitdiff
Redefined UI.runSafely so it throws an exception if session can't be locked (#11219)
authorArtur Signell <artur@vaadin.com>
Thu, 28 Mar 2013 14:04:24 +0000 (16:04 +0200)
committerVaadin Code Review <review@vaadin.com>
Sun, 31 Mar 2013 13:09:15 +0000 (13:09 +0000)
Change-Id: I300bfb25c4040d0df18d3a11594efe2ce6b0679d

server/src/com/vaadin/ui/UI.java
server/src/com/vaadin/ui/UIDetachedException.java [new file with mode: 0644]

index 6a21ba3357b77bba8f56c0975a6bb7aec7d9d052..47cfd471cdacffe16ed07a381c2eff3ef5bc2057 100644 (file)
@@ -1062,36 +1062,37 @@ public abstract class UI extends AbstractSingleComponentContainer implements
      * 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
+     * @throws UIDetachedException
+     *             if the UI is not attached to a session (and locking can
+     *             therefore not be done)
      */
-    public void runSafely(Runnable runnable) {
+    public void runSafely(Runnable runnable) throws UIDetachedException {
         Map<Class<?>, CurrentInstance> old = null;
 
         VaadinSession session = getSession();
 
-        if (session != null) {
-            session.lock();
+        if (session == null) {
+            throw new UIDetachedException();
         }
+
+        session.lock();
         try {
+            if (getSession() == null) {
+                // UI was detached after fetching the session but before we
+                // acquiried the lock.
+                throw new UIDetachedException();
+            }
             old = CurrentInstance.setThreadLocals(this);
             runnable.run();
         } finally {
-            if (session != null) {
-                session.unlock();
-            }
+            session.unlock();
             if (old != null) {
                 CurrentInstance.restoreThreadLocals(old);
             }
         }
 
     }
-
 }
diff --git a/server/src/com/vaadin/ui/UIDetachedException.java b/server/src/com/vaadin/ui/UIDetachedException.java
new file mode 100644 (file)
index 0000000..07207b0
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2000-2013 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.ui;
+
+/**
+ * Exception thrown if the UI has been detached when it should not be.
+ * 
+ * @author Vaadin Ltd
+ * @since 7.1
+ */
+public class UIDetachedException extends RuntimeException {
+
+    public UIDetachedException() {
+        super();
+    }
+
+    public UIDetachedException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public UIDetachedException(String message) {
+        super(message);
+    }
+
+    public UIDetachedException(Throwable cause) {
+        super(cause);
+    }
+
+}