public class SharedConfigurationState
extends java.lang.Object
ThreadLocal field in bundle). But other cases could arise too. Intended to be used for very rare cases:
use it only if you can't avoid shared state in configuration time (for example, like guicey gsp bundle,
which requires global configuration, accessible by multiple bundles).
Internally, guicey use it to store created Injector and make it available statically
(see InjectorLookup).
Universal sharing place should simplify testing in the future: if new global state will appear, there would be no need to reset anything in tests (to clear state, for example, on testing errors). One "dirty" place to replace all separate hacks.
Shared state could be accessed statically with get(Application), or within
GuiceyBundle. Guicey hooks could use
GuiceBundle.Builder.withSharedState(java.util.function.Consumer) to
access application state. Alternatively, dropwizard Environment may be used for resolution:
get(Environment).
Classes are used as state keys to simplify usage (in most cases, bundle class will be used as key). Shared value could be set only once (to prevent complex situations with state substitutions). It is advised to initialize shared value only in initialization phase (to avoid potential static access errors).
Objects available in shared state by default: Bootstrap,
Configuration, Environment,
ConfigurationTree, Injector
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CONTEXT_APPLICATION_PROPERTY
Attribute name used to store application instance in application context attributes.
|
| Constructor and Description |
|---|
SharedConfigurationState() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
assignTo(io.dropwizard.Application application)
Called on initialization phase to assign registry instance to application (to be able to statically
reference registry).
|
static void |
clear()
Clears stored injectors references.
|
static java.util.Optional<SharedConfigurationState> |
get(io.dropwizard.Application application)
Static lookup for entire application registry.
|
<V> V |
get(java.lang.Class<?> key)
Note: in spite of fact that class is used for key, actual value is stored with full class name string.
|
<V> V |
get(java.lang.Class<?> key,
java.util.function.Supplier<V> defaultValue)
Get value or declare default if value not set yet.
|
static java.util.Optional<SharedConfigurationState> |
get(io.dropwizard.setup.Environment environment)
Static lookup for entire application registry by environment instance.
|
java.util.Set<java.lang.String> |
getKeys()
Note: each key is a full name of registration class.
|
static SharedConfigurationState |
getOrFail(io.dropwizard.Application application,
java.lang.String message,
java.lang.Object... args)
Shortcut for
get(Application) to immediately fail if registry is not available. |
<V> V |
getOrFail(java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
Shortcut for
get(Class) to immediately fail if value not set. |
static SharedConfigurationState |
getOrFail(io.dropwizard.setup.Environment environment,
java.lang.String message,
java.lang.Object... args)
Shortcut for
get(Environment) to immediately fail if registry is not available. |
protected void |
listen(io.dropwizard.setup.Environment environment)
Called on run phase to assign to application lifecycle and listen for shutdown.
|
static <V> java.util.Optional<V> |
lookup(io.dropwizard.Application application,
java.lang.Class<?> key)
Static lookup for registry value.
|
static <V> java.util.Optional<V> |
lookup(io.dropwizard.setup.Environment environment,
java.lang.Class<?> key)
Static lookup for registry value by environment instance.
|
static <V> V |
lookupOrFail(io.dropwizard.Application application,
java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
Shortcut for
lookup(Application, Class) to immediately fail if value is not available. |
static <V> V |
lookupOrFail(io.dropwizard.setup.Environment environment,
java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
Shortcut for
lookup(Environment, Class) to immediately fail if value is not available. |
void |
put(java.lang.Class<?> key,
java.lang.Object value)
Assumed to be used to store some configuration during startup.
|
static int |
statesCount()
Could be useful only in tests in order to validate possibly stale applications.
|
java.lang.String |
toString() |
public static final java.lang.String CONTEXT_APPLICATION_PROPERTY
public <V> V get(java.lang.Class<?> key)
V - shared object typekey - shared object keypublic <V> V get(java.lang.Class<?> key,
java.util.function.Supplier<V> defaultValue)
V - shared object typekey - shared object keydefaultValue - default shared object providerpublic <V> V getOrFail(java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
get(Class) to immediately fail if value not set. Supposed to be used by shared state
consumers (to validate situations when value must exists for sure).V - shared object typekey - shared object keymessage - exception message (could use String.format(String, Object...) placeholders)args - placeholder arguments for error messagejava.lang.IllegalStateException - if value not setpublic void put(java.lang.Class<?> key,
java.lang.Object value)
Class is used for key to avoid dummy mistakes with strings (in most cases bundle class would be key). But internally string class name is used in order to unify classes from different class loaders.
Note: warning will be printed if existing state value is overridden because it is assumed that some global configuration object would be shared once and later all participants will work with the same object.
key - shared object keyvalue - shared value (usually configuration object)public java.util.Set<java.lang.String> getKeys()
public java.lang.String toString()
toString in class java.lang.Objectprotected void assignTo(io.dropwizard.Application application)
application - application instanceprotected void listen(io.dropwizard.setup.Environment environment)
environment - environment objectpublic static <V> java.util.Optional<V> lookup(io.dropwizard.Application application,
java.lang.Class<?> key)
V - shared object keyapplication - application instancekey - shared object keypublic static <V> java.util.Optional<V> lookup(io.dropwizard.setup.Environment environment,
java.lang.Class<?> key)
V - shared object keyenvironment - dropwizard environment objectkey - shared object keypublic static <V> V lookupOrFail(io.dropwizard.Application application,
java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
lookup(Application, Class) to immediately fail if value is not available.V - shared object typeapplication - application instancekey - shared object keymessage - exception message (could use String.format(String, Object...) placeholders)args - placeholder arguments for error messagejava.lang.IllegalStateException - if value not availablepublic static <V> V lookupOrFail(io.dropwizard.setup.Environment environment,
java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
lookup(Environment, Class) to immediately fail if value is not available.V - shared object typeenvironment - environment instancekey - shared object keymessage - exception message (could use String.format(String, Object...) placeholders)args - placeholder arguments for error messagejava.lang.IllegalStateException - if value not availablepublic static java.util.Optional<SharedConfigurationState> get(io.dropwizard.Application application)
application - application instancepublic static java.util.Optional<SharedConfigurationState> get(io.dropwizard.setup.Environment environment)
environment - environment instancepublic static SharedConfigurationState getOrFail(io.dropwizard.Application application, java.lang.String message, java.lang.Object... args)
get(Application) to immediately fail if registry is not available.application - application instancemessage - exception message (could use String.format(String, Object...) placeholders)args - placeholder arguments for error messagejava.lang.IllegalStateException - if no state is associated with application yetpublic static SharedConfigurationState getOrFail(io.dropwizard.setup.Environment environment, java.lang.String message, java.lang.Object... args)
get(Environment) to immediately fail if registry is not available.environment - environment instancemessage - exception message (could use String.format(String, Object...) placeholders)args - placeholder arguments for error messagejava.lang.IllegalStateException - if no state is associated with application yetpublic static void clear()
May be useful in tests, when application was not shut down properly (but just to clear memory).
WARNING: calling this method while application is working may cause incorrect behaviour.
public static int statesCount()