Class ComponentBag
- java.lang.Object
-
- org.glassfish.jersey.model.internal.ComponentBag
-
- Direct Known Subclasses:
ComponentBag.ImmutableComponentBag
public class ComponentBag extends java.lang.Object
An internal Jersey container for custom component classes and instances. The component bag can automatically compute acontract provider
model for the registered component type and stores it with the component registration.The rules for managing components inside a component bag are derived from the rules of JAX-RS
Configurable
API. In short:- The iteration order of registered components mirrors the registration order of these components.
- There can be only one registration for any given component type.
- Existing registrations cannot be overridden (any attempt to override an existing registration will be rejected).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ComponentBag.ImmutableComponentBag
Immutable version ofComponentBag
.
-
Field Summary
Fields Modifier and Type Field Description (package private) static Inflector<ContractProvider.Builder,ContractProvider>
AS_IS
Contract provider model enhancer that builds a model as is, without any modifications.static java.util.function.Predicate<ContractProvider>
BINDERS_ONLY
A filtering strategy that includes only models that containBinder
provider contract.private static java.util.function.Function<java.lang.Object,Binder>
CAST_TO_BINDER
private java.util.Set<java.lang.Class<?>>
classes
Registered component classes collection and it's immutable view.private java.util.Set<java.lang.Class<?>>
classesView
static java.util.function.Predicate<ContractProvider>
EXCLUDE_EMPTY
A filtering strategy that excludes models with no recognized contracts.private static java.util.function.Predicate<ContractProvider>
EXCLUDE_META_PROVIDERS
A filtering strategy that excludes all pure meta-provider models (i.e.static java.util.function.Predicate<ContractProvider>
EXECUTOR_SERVICE_PROVIDER_ONLY
A filtering strategy that includes only models that containExecutorServiceProvider
provider contract.static java.util.function.BiPredicate<ContractProvider,InjectionManager>
EXTERNAL_ONLY
A filtering strategy that includes only models that contain contract registrable byInjectionManager
.static java.util.function.Predicate<ContractProvider>
INCLUDE_ALL
A filtering strategy that accepts any contract provider model.private java.util.Set<java.lang.Object>
instances
Registered component instances collection and it's immutable view.private java.util.Set<java.lang.Object>
instancesView
private java.util.Set<java.lang.Class<?>>
modelKeysView
private java.util.Map<java.lang.Class<?>,ContractProvider>
models
Map of contract provider models for the registered component classes and instances it's immutable view.private java.util.function.Predicate<ContractProvider>
registrationStrategy
Contract provider model registration strategy.static java.util.function.Predicate<ContractProvider>
SCHEDULED_EXECUTOR_SERVICE_PROVIDER_ONLY
A filtering strategy that includes only models that containScheduledExecutorServiceProvider
provider contract.
-
Constructor Summary
Constructors Modifier Constructor Description private
ComponentBag(java.util.function.Predicate<ContractProvider> registrationStrategy)
private
ComponentBag(java.util.function.Predicate<ContractProvider> registrationStrategy, java.util.Set<java.lang.Class<?>> classes, java.util.Set<java.lang.Object> instances, java.util.Map<java.lang.Class<?>,ContractProvider> models)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.util.Map<java.lang.Class<?>,java.lang.Integer>
asMap(java.util.Set<java.lang.Class<?>> contractSet)
void
clear()
Removes all the component registrations and resets the component bag instance to a state as if it was create anew.ComponentBag
copy()
Get a copy of this component bag.static java.util.function.Predicate<ContractProvider>
excludeMetaProviders(InjectionManager injectionManager)
A method creates thePredicate
which is able to filter all Jersey meta-providers along with the components which is able to register the current usedInjectionManager
.java.util.Set<java.lang.Class<?>>
getClasses()
Get all registered component classes, includingfeatures
andbinders
meta-providers.java.util.Set<java.lang.Class<?>>
getClasses(java.util.function.Predicate<ContractProvider> filter)
Get a subset of all registered component classes using thefilter
predicate to determine for each component class based on it's contract provider class model whether it should be kept or filtered out.static <T> java.util.List<T>
getFromBinders(InjectionManager injectionManager, ComponentBag componentBag, java.util.function.Function<java.lang.Object,T> cast, java.util.function.Predicate<Binding> filter)
IfT
object is registered inComponentBag
using theBinder
,T
is not visible using the methods for getting classes and instancesgetClasses(Predicate)
andgetInstances(Predicate)
.java.util.Set<java.lang.Object>
getInstances()
Get all registered component instances, includingfeatures
andbinders
meta-providers.java.util.Set<java.lang.Object>
getInstances(java.util.function.Predicate<ContractProvider> filter)
Get a subset of all registered component instances using thefilter
predicate to determine for each component instance based on it's contract provider class model whether it should be kept or filtered out.ContractProvider
getModel(java.lang.Class<?> componentClass)
Get a model for a given component class, ornull
if no such component is registered in the component bag.java.util.Set<java.lang.Class<?>>
getRegistrations()
Get an unmodifiable view of all component classes, for which a registration exists (either class or instance based) in the component bag.ComponentBag
immutableCopy()
Get immutable copy of a component bag.(package private) void
loadFrom(ComponentBag bag)
Clear and initialize the component registrations from given bag instance.static ContractProvider
modelFor(java.lang.Class<?> componentClass)
Create a contract provider model by introspecting a component class.private static ContractProvider
modelFor(java.lang.Class<?> componentClass, int defaultPriority, java.util.Map<java.lang.Class<?>,java.lang.Integer> contractMap, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Create a contract provider for a given component class.static ComponentBag
newInstance(java.util.function.Predicate<ContractProvider> registrationStrategy)
Create new empty component bag.boolean
register(java.lang.Class<?> componentClass, int priority, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class as a contract provider with an explicitly specified binding priority.boolean
register(java.lang.Class<?> componentClass, java.util.Map<java.lang.Class<?>,java.lang.Integer> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class as a contract provider for the specified contracts.boolean
register(java.lang.Class<?> componentClass, java.util.Set<java.lang.Class<?>> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class as a contract provider for the specified contracts.boolean
register(java.lang.Class<?> componentClass, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class using a given registration strategy.boolean
register(java.lang.Object component, int priority, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component as a contract provider with an explicitly specified binding priority.boolean
register(java.lang.Object component, java.util.Map<java.lang.Class<?>,java.lang.Integer> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component as a contract provider for the specified contracts.boolean
register(java.lang.Object component, java.util.Set<java.lang.Class<?>> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component as a contract provider for the specified contracts.boolean
register(java.lang.Object component, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component using a given registration strategy.private boolean
registerModel(java.lang.Class<?> componentClass, int defaultPriority, java.util.Map<java.lang.Class<?>,java.lang.Integer> contractMap, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register acontract provider model
for a given class.
-
-
-
Field Detail
-
EXCLUDE_META_PROVIDERS
private static final java.util.function.Predicate<ContractProvider> EXCLUDE_META_PROVIDERS
A filtering strategy that excludes all pure meta-provider models (i.e. models that only contain recognized meta-provider contracts -Feature
and/orBinder
and/or external meta-provider fromInjectionManager.isRegistrable(Class)
).This filter predicate returns
false
for allcontract provider models
that represent a model containing only recognized meta-provider contracts.
-
CAST_TO_BINDER
private static final java.util.function.Function<java.lang.Object,Binder> CAST_TO_BINDER
-
EXTERNAL_ONLY
public static final java.util.function.BiPredicate<ContractProvider,InjectionManager> EXTERNAL_ONLY
A filtering strategy that includes only models that contain contract registrable byInjectionManager
.This filter predicate returns
true
for allcontract provider models
that represent an object which can be registered using specificInjectionManager
contract.
-
BINDERS_ONLY
public static final java.util.function.Predicate<ContractProvider> BINDERS_ONLY
A filtering strategy that includes only models that containBinder
provider contract.This filter predicate returns
true
for allcontract provider models
that represent a provider registered to provideBinder
contract.
-
EXECUTOR_SERVICE_PROVIDER_ONLY
public static final java.util.function.Predicate<ContractProvider> EXECUTOR_SERVICE_PROVIDER_ONLY
A filtering strategy that includes only models that containExecutorServiceProvider
provider contract.This filter predicate returns
true
for allcontract provider models
that represent a provider registered to provideExecutorServiceProvider
contract.
-
SCHEDULED_EXECUTOR_SERVICE_PROVIDER_ONLY
public static final java.util.function.Predicate<ContractProvider> SCHEDULED_EXECUTOR_SERVICE_PROVIDER_ONLY
A filtering strategy that includes only models that containScheduledExecutorServiceProvider
provider contract.This filter predicate returns
true
for allcontract provider models
that represent a provider registered to provideScheduledExecutorServiceProvider
contract.
-
EXCLUDE_EMPTY
public static final java.util.function.Predicate<ContractProvider> EXCLUDE_EMPTY
A filtering strategy that excludes models with no recognized contracts.This filter predicate returns
false
for allcontract provider models
that are empty, i.e. do not contain any recognized contracts.
-
INCLUDE_ALL
public static final java.util.function.Predicate<ContractProvider> INCLUDE_ALL
A filtering strategy that accepts any contract provider model.This filter predicate returns
true
for any contract provider model.
-
AS_IS
static final Inflector<ContractProvider.Builder,ContractProvider> AS_IS
Contract provider model enhancer that builds a model as is, without any modifications.
-
registrationStrategy
private final java.util.function.Predicate<ContractProvider> registrationStrategy
Contract provider model registration strategy.
-
classes
private final java.util.Set<java.lang.Class<?>> classes
Registered component classes collection and it's immutable view.
-
classesView
private final java.util.Set<java.lang.Class<?>> classesView
-
instances
private final java.util.Set<java.lang.Object> instances
Registered component instances collection and it's immutable view.
-
instancesView
private final java.util.Set<java.lang.Object> instancesView
-
models
private final java.util.Map<java.lang.Class<?>,ContractProvider> models
Map of contract provider models for the registered component classes and instances it's immutable view.
-
modelKeysView
private final java.util.Set<java.lang.Class<?>> modelKeysView
-
-
Constructor Detail
-
ComponentBag
private ComponentBag(java.util.function.Predicate<ContractProvider> registrationStrategy)
-
ComponentBag
private ComponentBag(java.util.function.Predicate<ContractProvider> registrationStrategy, java.util.Set<java.lang.Class<?>> classes, java.util.Set<java.lang.Object> instances, java.util.Map<java.lang.Class<?>,ContractProvider> models)
-
-
Method Detail
-
excludeMetaProviders
public static java.util.function.Predicate<ContractProvider> excludeMetaProviders(InjectionManager injectionManager)
A method creates thePredicate
which is able to filter all Jersey meta-providers along with the components which is able to register the current usedInjectionManager
.- Parameters:
injectionManager
- current injection manager.- Returns:
Predicate
excluding Jersey meta-providers and the specific ones for a currentInjectionManager
.
-
newInstance
public static ComponentBag newInstance(java.util.function.Predicate<ContractProvider> registrationStrategy)
Create new empty component bag.- Parameters:
registrationStrategy
- function driving the decision (based on the introspectedcontract provider model
) whether or not should the component class registration continue towards a successful completion.- Returns:
- a new empty component bag.
-
getFromBinders
public static <T> java.util.List<T> getFromBinders(InjectionManager injectionManager, ComponentBag componentBag, java.util.function.Function<java.lang.Object,T> cast, java.util.function.Predicate<Binding> filter)
IfT
object is registered inComponentBag
using theBinder
,T
is not visible using the methods for getting classes and instancesgetClasses(Predicate)
andgetInstances(Predicate)
.Method selects all
bindings
and picks up the instances or creates the instances fromClassBinding
(injection does not work at this moment).- Parameters:
injectionManager
- injection manager to create an object fromT
class.componentBag
- component bag which provides registered binders.- Returns:
- all instances/classes registered using binders.
-
register
public boolean register(java.lang.Class<?> componentClass, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class using a given registration strategy.- Parameters:
componentClass
- class to be introspected as a contract provider and registered, based on the registration strategy decision.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Class<?> componentClass, int priority, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class as a contract provider with an explicitly specified binding priority.- Parameters:
componentClass
- class to be introspected as a contract provider and registered.priority
- explicitly specified binding priority for the provider contracts implemented by the component.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Class<?> componentClass, java.util.Set<java.lang.Class<?>> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class as a contract provider for the specified contracts.- Parameters:
componentClass
- class to be introspected as a contract provider and registered.contracts
- contracts to bind the component class to.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Class<?> componentClass, java.util.Map<java.lang.Class<?>,java.lang.Integer> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component class as a contract provider for the specified contracts.- Parameters:
componentClass
- class to be introspected as a contract provider and registered.contracts
- contracts with their priorities to bind the component class to.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Object component, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component using a given registration strategy.- Parameters:
component
- instance to be introspected as a contract provider and registered, based on the registration strategy decision.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Object component, int priority, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component as a contract provider with an explicitly specified binding priority.- Parameters:
component
- instance to be introspected as a contract provider and registered, based on the registration strategy decision.priority
- explicitly specified binding priority for the provider contracts implemented by the component.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Object component, java.util.Set<java.lang.Class<?>> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component as a contract provider for the specified contracts.- Parameters:
component
- instance to be introspected as a contract provider and registered, based on the registration strategy decision.contracts
- contracts to bind the component to.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
register
public boolean register(java.lang.Object component, java.util.Map<java.lang.Class<?>,java.lang.Integer> contracts, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register a component as a contract provider for the specified contracts.- Parameters:
component
- instance to be introspected as a contract provider and registered, based on the registration strategy decision.contracts
- contracts with their priorities to bind the component to.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
if the component registration was successful.
-
registerModel
private boolean registerModel(java.lang.Class<?> componentClass, int defaultPriority, java.util.Map<java.lang.Class<?>,java.lang.Integer> contractMap, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Register acontract provider model
for a given class.- Parameters:
componentClass
- registered component class.defaultPriority
- default component priority. If -1, the value from the component classPriority
annotation will be used (if any).contractMap
- map of contracts and their binding priorities. Ifnull
, the contracts will gathered by introspecting the component class. Content of the contract map may be modified during the registration processing.modelEnhancer
- custom contract provider model enhancer.- Returns:
true
upon successful registration of a contract provider model for a given component class,false
otherwise.
-
modelFor
public static ContractProvider modelFor(java.lang.Class<?> componentClass)
Create a contract provider model by introspecting a component class.- Parameters:
componentClass
- component class to create contract provider model for.- Returns:
- contract provider model for the class.
-
modelFor
private static ContractProvider modelFor(java.lang.Class<?> componentClass, int defaultPriority, java.util.Map<java.lang.Class<?>,java.lang.Integer> contractMap, Inflector<ContractProvider.Builder,ContractProvider> modelEnhancer)
Create a contract provider for a given component class.- Parameters:
componentClass
- component class to create contract provider model for.defaultPriority
- default component priority. If -1, the value from the component classPriority
annotation will be used (if any).contractMap
- map of contracts and their binding priorities. Ifnull
, the contracts will gathered by introspecting the component class. Content of the contract map may be modified during the registration processing.modelEnhancer
- custom contract provider model enhancer.- Returns:
- contract provider model for the class.
-
asMap
private static java.util.Map<java.lang.Class<?>,java.lang.Integer> asMap(java.util.Set<java.lang.Class<?>> contractSet)
-
getClasses
public java.util.Set<java.lang.Class<?>> getClasses()
Get all registered component classes, includingfeatures
andbinders
meta-providers.- Returns:
- all registered component classes.
-
getInstances
public java.util.Set<java.lang.Object> getInstances()
Get all registered component instances, includingfeatures
andbinders
meta-providers.- Returns:
- all registered component instances.
-
getClasses
public java.util.Set<java.lang.Class<?>> getClasses(java.util.function.Predicate<ContractProvider> filter)
Get a subset of all registered component classes using thefilter
predicate to determine for each component class based on it's contract provider class model whether it should be kept or filtered out.- Parameters:
filter
- function that decides whether a particular class should be returned or not.- Returns:
- filtered subset of registered component classes.
-
getInstances
public java.util.Set<java.lang.Object> getInstances(java.util.function.Predicate<ContractProvider> filter)
Get a subset of all registered component instances using thefilter
predicate to determine for each component instance based on it's contract provider class model whether it should be kept or filtered out.- Parameters:
filter
- function that decides whether a particular class should be returned or not.- Returns:
- filtered subset of registered component instances.
-
getRegistrations
public java.util.Set<java.lang.Class<?>> getRegistrations()
Get an unmodifiable view of all component classes, for which a registration exists (either class or instance based) in the component bag.- Returns:
- set of classes of all component classes and instances registered in this component bag.
-
getModel
public ContractProvider getModel(java.lang.Class<?> componentClass)
Get a model for a given component class, ornull
if no such component is registered in the component bag.- Parameters:
componentClass
- class of the registered component to retrieve the contract provider model for.- Returns:
- model for a given component class, or
null
if no such component is registered.
-
copy
public ComponentBag copy()
Get a copy of this component bag.- Returns:
- component bag copy.
-
immutableCopy
public ComponentBag immutableCopy()
Get immutable copy of a component bag.- Returns:
- immutable view of a component bag.
-
clear
public void clear()
Removes all the component registrations and resets the component bag instance to a state as if it was create anew.
-
loadFrom
void loadFrom(ComponentBag bag)
Clear and initialize the component registrations from given bag instance.- Parameters:
bag
- component bag to initialize this one with.
-
-