Package com.vaadin.util
Class CurrentInstance
- java.lang.Object
-
- com.vaadin.util.CurrentInstance
-
- All Implemented Interfaces:
Serializable
public class CurrentInstance extends Object implements Serializable
Keeps track of various current instances for the current thread. All the instances are automatically cleared after handling a request from the client to avoid leaking memory. The inheritable values are also maintained when execution is moved to another thread, both when a new thread is created and whenVaadinSession.access(Runnable)
orUI.access(Runnable)
is used.Please note that the instances are stored using
WeakReference
. This means that the a current instance value may suddenly disappear if there a no other references to the object.Currently the framework uses the following instances:
Inheritable:
UI
,VaadinService
,VaadinSession
.Non-inheritable:
VaadinRequest
,VaadinResponse
.- Since:
- 7.0.0
- Author:
- Vaadin Ltd
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
clearAll()
Clears all current instances.static <T> void
defineFallbackResolver(Class<T> type, CurrentInstanceFallbackResolver<T> fallbackResolver)
Adds a CurrentInstanceFallbackResolver, that is triggered whenget(Class)
can't find a suitable instance for the given type parameter.static <T> T
get(Class<T> type)
Gets the current instance of a specific type if available.static Map<Class<?>,CurrentInstance>
getInstances(boolean onlyInheritable)
Gets the currently set instances so that they can later be restored usingrestoreInstances(Map)
.static void
restoreInstances(Map<Class<?>,CurrentInstance> old)
Restores the given instances to the given values.static <T> void
set(Class<T> type, T instance)
Sets the current instance of the given type.static Map<Class<?>,CurrentInstance>
setCurrent(VaadinSession session)
Sets current instances for theVaadinSession
and all related classes.static Map<Class<?>,CurrentInstance>
setCurrent(UI ui)
Sets current instances for the UI and all related classes.static <T> void
setInheritable(Class<T> type, T instance)
Sets the current inheritable instance of the given type.
-
-
-
Method Detail
-
get
public static <T> T get(Class<T> type)
Gets the current instance of a specific type if available.When a current instance of the specific type is not found, the
CurrentInstanceFallbackResolver
registered viadefineFallbackResolver(Class, CurrentInstanceFallbackResolver)
(if any) is invoked.- Parameters:
type
- the class to get an instance of- Returns:
- the current instance or the provided type, or
null
if there is no current instance.
-
defineFallbackResolver
public static <T> void defineFallbackResolver(Class<T> type, CurrentInstanceFallbackResolver<T> fallbackResolver)
Adds a CurrentInstanceFallbackResolver, that is triggered whenget(Class)
can't find a suitable instance for the given type parameter.- Parameters:
type
- the class used onget(Class)
invocations to retrieve the current instancefallbackResolver
- the resolver, notnull
- Throws:
IllegalArgumentException
- if there's already a defined fallback resolver for the given type- Since:
- 7.7.14
-
set
public static <T> void set(Class<T> type, T instance)
Sets the current instance of the given type.- Parameters:
type
- the class that should be used when getting the current instance backinstance
- the actual instance- See Also:
setInheritable(Class, Object)
,ThreadLocal
-
setInheritable
public static <T> void setInheritable(Class<T> type, T instance)
Sets the current inheritable instance of the given type. A current instance that is inheritable will be available for child threads and in code run byVaadinSession.access(Runnable)
andUI.access(Runnable)
.- Parameters:
type
- the class that should be used when getting the current instance backinstance
- the actual instance- See Also:
set(Class, Object)
,InheritableThreadLocal
-
clearAll
public static void clearAll()
Clears all current instances.
-
restoreInstances
public static void restoreInstances(Map<Class<?>,CurrentInstance> old)
Restores the given instances to the given values. Note that this should only be used internally to restore Vaadin classes.- Parameters:
old
- A Class -> CurrentInstance map to set as current instances- Since:
- 7.1
-
getInstances
public static Map<Class<?>,CurrentInstance> getInstances(boolean onlyInheritable)
Gets the currently set instances so that they can later be restored usingrestoreInstances(Map)
.- Parameters:
onlyInheritable
-true
if only the inheritable instances should be included;false
to get all instances.- Returns:
- a map containing the current instances
- Since:
- 7.1
-
setCurrent
public static Map<Class<?>,CurrentInstance> setCurrent(UI ui)
Sets current instances for the UI and all related classes. The previously defined values can be restored by passing the returned map torestoreInstances(Map)
.- Parameters:
ui
- The UI- Returns:
- A map containing the old values of the instances that this method updated.
- Since:
- 7.1
-
setCurrent
public static Map<Class<?>,CurrentInstance> setCurrent(VaadinSession session)
Sets current instances for theVaadinSession
and all related classes. The previously defined values can be restored by passing the returned map torestoreInstances(Map)
.- Parameters:
session
- The VaadinSession- Returns:
- A map containing the old values of the instances this method updated.
- Since:
- 7.1
-
-