From d34583895c884767537ad866de942d23c044af52 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 28 May 2013 15:39:02 +0300 Subject: [PATCH] Remove ThreadLocal references from CurrentInstance (#11914) Change-Id: Ic7389a06726026383b96de80b0d038ed11e9d273 --- .../server/ConnectorResourceHandler.java | 6 ++-- .../vaadin/server/GlobalResourceHandler.java | 8 ++--- .../src/com/vaadin/server/VaadinSession.java | 11 +++--- server/src/com/vaadin/ui/UI.java | 4 +-- .../src/com/vaadin/util/CurrentInstance.java | 34 ++++++++++++------- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/server/src/com/vaadin/server/ConnectorResourceHandler.java b/server/src/com/vaadin/server/ConnectorResourceHandler.java index 00d82988d3..3f3f41a179 100644 --- a/server/src/com/vaadin/server/ConnectorResourceHandler.java +++ b/server/src/com/vaadin/server/ConnectorResourceHandler.java @@ -77,8 +77,8 @@ public class ConnectorResourceHandler implements RequestHandler { session.unlock(); } - Map, CurrentInstance> oldThreadLocals = CurrentInstance - .setThreadLocals(ui); + Map, CurrentInstance> oldInstances = CurrentInstance + .setCurrent(ui); try { if (!connector.handleConnectorRequest(request, response, key)) { return error(request, response, connector.getClass() @@ -88,7 +88,7 @@ public class ConnectorResourceHandler implements RequestHandler { + ") did not handle connector request for " + key); } } finally { - CurrentInstance.restoreThreadLocals(oldThreadLocals); + CurrentInstance.restoreInstances(oldInstances); } return true; diff --git a/server/src/com/vaadin/server/GlobalResourceHandler.java b/server/src/com/vaadin/server/GlobalResourceHandler.java index d411b286d0..4235d85024 100644 --- a/server/src/com/vaadin/server/GlobalResourceHandler.java +++ b/server/src/com/vaadin/server/GlobalResourceHandler.java @@ -87,14 +87,14 @@ public class GlobalResourceHandler implements RequestHandler { + " is not a valid global resource path"); } session.lock(); - Map, CurrentInstance> oldThreadLocals = null; + Map, CurrentInstance> oldInstances = null; DownloadStream stream = null; try { UI ui = session.getUIById(Integer.parseInt(uiid)); if (ui == null) { return error(request, response, "No UI found for id " + uiid); } - oldThreadLocals = CurrentInstance.setThreadLocals(ui); + oldInstances = CurrentInstance.setCurrent(ui); ConnectorResource resource; if (LEGACY_TYPE.equals(type)) { resource = legacyResources.get(key); @@ -115,8 +115,8 @@ public class GlobalResourceHandler implements RequestHandler { } } finally { session.unlock(); - if (oldThreadLocals != null) { - CurrentInstance.restoreThreadLocals(oldThreadLocals); + if (oldInstances != null) { + CurrentInstance.restoreInstances(oldInstances); } } diff --git a/server/src/com/vaadin/server/VaadinSession.java b/server/src/com/vaadin/server/VaadinSession.java index eaf611caad..9939ad709e 100644 --- a/server/src/com/vaadin/server/VaadinSession.java +++ b/server/src/com/vaadin/server/VaadinSession.java @@ -1153,15 +1153,15 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { Map, CurrentInstance> old = null; lock(); try { - old = CurrentInstance.setThreadLocals(this); + old = CurrentInstance.setCurrent(this); runnable.run(); } finally { unlock(); if (old != null) { - CurrentInstance.restoreThreadLocals(old); + CurrentInstance.restoreInstances(old); } } - + } /** @@ -1262,14 +1262,13 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { while ((pendingAccess = pendingAccessQueue.poll()) != null) { if (!pendingAccess.isCancelled()) { CurrentInstance.clearAll(); - CurrentInstance - .restoreThreadLocals(pendingAccess.instances); + CurrentInstance.restoreInstances(pendingAccess.instances); accessSynchronously(pendingAccess); } } } finally { CurrentInstance.clearAll(); - CurrentInstance.restoreThreadLocals(oldInstances); + CurrentInstance.restoreInstances(oldInstances); } } diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index 0b21112c97..508564ac0f 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -1157,12 +1157,12 @@ public abstract class UI extends AbstractSingleComponentContainer implements // acquired the lock. throw new UIDetachedException(); } - old = CurrentInstance.setThreadLocals(this); + old = CurrentInstance.setCurrent(this); runnable.run(); } finally { session.unlock(); if (old != null) { - CurrentInstance.restoreThreadLocals(old); + CurrentInstance.restoreInstances(old); } } diff --git a/server/src/com/vaadin/util/CurrentInstance.java b/server/src/com/vaadin/util/CurrentInstance.java index 8e6ef8266e..b97bab3d8a 100644 --- a/server/src/com/vaadin/util/CurrentInstance.java +++ b/server/src/com/vaadin/util/CurrentInstance.java @@ -174,13 +174,15 @@ public class CurrentInstance implements Serializable { } /** - * Restores the given thread locals to the given values. Note that this - * should only be used internally to restore Vaadin classes. + * Restores the given instances to the given values. Note that this should + * only be used internally to restore Vaadin classes. + * + * @since 7.1 * * @param old - * A Class -> Object map to set as thread locals + * A Class -> CurrentInstance map to set as current instances */ - public static void restoreThreadLocals(Map, CurrentInstance> old) { + public static void restoreInstances(Map, CurrentInstance> old) { for (Class c : old.keySet()) { CurrentInstance ci = old.get(c); set(c, ci.instance, ci.inheritable); @@ -189,7 +191,7 @@ public class CurrentInstance implements Serializable { /** * Gets the currently set instances so that they can later be restored using - * {@link #restoreThreadLocals(Map)}. + * {@link #restoreInstances(Map)}. * * @since 7.1 * @@ -216,30 +218,38 @@ public class CurrentInstance implements Serializable { } /** - * Sets thread locals for the UI and all related classes + * Sets current instances for the UI and all related classes. The previously + * defined values can be restored by passing the returned map to + * {@link #restoreInstances(Map)}. + * + * @since 7.1 * * @param ui * The UI - * @return A map containing the old values of the thread locals this method + * @return A map containing the old values of the instances that this method * updated. */ - public static Map, CurrentInstance> setThreadLocals(UI ui) { + public static Map, CurrentInstance> setCurrent(UI ui) { Map, CurrentInstance> old = new HashMap, CurrentInstance>(); old.put(UI.class, new CurrentInstance(UI.getCurrent(), true)); UI.setCurrent(ui); - old.putAll(setThreadLocals(ui.getSession())); + old.putAll(setCurrent(ui.getSession())); return old; } /** - * Sets thread locals for the {@link VaadinSession} and all related classes + * Sets current instances for the {@link VaadinSession} and all related + * classes. The previously defined values can be restored by passing the + * returned map to {@link #restoreInstances(Map)}. + * + * @since 7.1 * * @param session * The VaadinSession - * @return A map containing the old values of the thread locals this method + * @return A map containing the old values of the instances this method * updated. */ - public static Map, CurrentInstance> setThreadLocals( + public static Map, CurrentInstance> setCurrent( VaadinSession session) { Map, CurrentInstance> old = new HashMap, CurrentInstance>(); old.put(VaadinSession.class, -- 2.39.5