Class Providers
- java.lang.Object
-
- org.glassfish.jersey.internal.inject.Providers
-
public final class Providers extends java.lang.Object
Utility class providing a set of utility methods for easier and more type-safe interaction with an injection layer.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Providers.ProviderRuntime
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime>
EXTERNAL_PROVIDER_INTERFACE_WHITELIST
Map of all supported external (i.e.private static java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime>
JAX_RS_PROVIDER_INTERFACE_WHITELIST
Map of all standard JAX-RS providers and their run-time affinity.private static java.util.logging.Logger
LOGGER
-
Constructor Summary
Constructors Modifier Constructor Description private
Providers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
checkProviderRuntime(java.lang.Class<?> component, ContractProvider model, javax.ws.rs.RuntimeType runtimeConstraint, boolean scanned, boolean isResource)
Check thecomponent
whether it is appropriate correctly configured for client or serverruntime
.private static void
computeProviderContracts(java.lang.Class<?> clazz, java.util.Set<java.lang.Class<?>> contracts)
static void
ensureContract(java.lang.Class<?> contract, java.lang.Class<?>... implementations)
Ensure the supplied implementation classes implement the expected contract.private static boolean
findFirstProviderContract(java.lang.Class<?> clazz)
static <T> java.lang.Iterable<T>
getAllProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the iterable of all providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container.static <T> java.lang.Iterable<T>
getAllProviders(InjectionManager injectionManager, java.lang.Class<T> contract, java.util.Comparator<T> comparator)
Get the iterable of all providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container ordered based on the givencomparator
.static <T> java.lang.Iterable<T>
getAllProviders(InjectionManager injectionManager, java.lang.Class<T> contract, RankedComparator<T> comparator)
Get the sorted iterable of allproviders
(custom and default) registered for the given service provider contract in the underlyinginjection manager
container.static <T> java.lang.Iterable<RankedProvider<T>>
getAllRankedProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the iterable of allproviders
(custom and default) registered for the given service provider contract in the underlyinginjection manager
container.static <T> java.lang.Iterable<T>
getAllRankedSortedProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the iterable of all providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container and automatically sorted usingranked comparator
.static <T> java.util.Collection<ServiceHolder<T>>
getAllServiceHolders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get collection of allServiceHolder
s bound for providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container.private static javax.ws.rs.RuntimeType
getContractConstraint(java.lang.Class<?> clazz, javax.ws.rs.RuntimeType defaultConstraint)
static <T> java.util.Set<T>
getCustomProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the set of all custom providers registered for the given service provider contract in the underlyinginjection manager
container.private static java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime>
getExternalProviderInterfaces()
private static java.lang.Iterable<java.lang.Class<?>>
getImplementedContracts(java.lang.Class<?> clazz)
private static java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime>
getJaxRsProviderInterfaces()
private static int
getPriority(java.lang.Class<?> serviceClass)
private static <T> java.util.Set<T>
getProviderClasses(java.util.Collection<ServiceHolder<T>> providers)
static java.util.Set<java.lang.Class<?>>
getProviderContracts(java.lang.Class<?> clazz)
Returns provider contracts recognized by Jersey that are implemented by theclazz
.static <T> java.util.Set<T>
getProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the set of default providers registered for the given service provider contract in the underlyinginjection manager
container.private static <T> java.util.List<ServiceHolder<T>>
getServiceHolders(InjectionManager bm, java.lang.Class<T> contract, java.lang.annotation.Annotation... qualifiers)
private static <T> java.util.List<ServiceHolder<T>>
getServiceHolders(InjectionManager injectionManager, java.lang.Class<T> contract, java.util.Comparator<java.lang.Class<?>> objectComparator, java.lang.annotation.Annotation... qualifiers)
private static <T> T
holder2service(ServiceHolder<T> holder)
static boolean
isJaxRsProvider(java.lang.Class<?> clazz)
Returnstrue
if given component class is a JAX-RS provider.static boolean
isProvider(java.lang.Class<?> clazz)
Returnstrue
if the given component class is a provider (implements specific interfaces).static boolean
isSupportedContract(java.lang.Class<?> type)
Check if the given Java type is a Jersey-supported contract.private static void
logProviderSkipped(java.lang.StringBuilder sb, java.lang.Class<?> provider, boolean alsoResourceClass)
static <T> java.lang.Iterable<T>
mergeAndSortRankedProviders(RankedComparator<T> comparator, java.lang.Iterable<java.lang.Iterable<RankedProvider<T>>> providerIterables)
Merge and sort given providers withranked comparator
.static <T> java.lang.Iterable<T>
sortRankedProviders(RankedComparator<T> comparator, java.lang.Iterable<RankedProvider<T>> providers)
Sort given providers withranked comparator
.
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
JAX_RS_PROVIDER_INTERFACE_WHITELIST
private static final java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime> JAX_RS_PROVIDER_INTERFACE_WHITELIST
Map of all standard JAX-RS providers and their run-time affinity.
-
EXTERNAL_PROVIDER_INTERFACE_WHITELIST
private static final java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime> EXTERNAL_PROVIDER_INTERFACE_WHITELIST
Map of all supported external (i.e. non-Jersey) contracts and their run-time affinity.
-
-
Method Detail
-
getJaxRsProviderInterfaces
private static java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime> getJaxRsProviderInterfaces()
-
getExternalProviderInterfaces
private static java.util.Map<java.lang.Class<?>,Providers.ProviderRuntime> getExternalProviderInterfaces()
-
getProviders
public static <T> java.util.Set<T> getProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the set of default providers registered for the given service provider contract in the underlyinginjection manager
container.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.- Returns:
- set of all available default service provider instances for the contract.
-
getCustomProviders
public static <T> java.util.Set<T> getCustomProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the set of all custom providers registered for the given service provider contract in the underlyinginjection manager
container.Returned providers are sorted based on
Priority
(lowerPriority
value is higher priority, seePriorities
.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.- Returns:
- set of all available service provider instances for the contract.
-
getAllProviders
public static <T> java.lang.Iterable<T> getAllProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the iterable of all providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.- Returns:
- iterable of all available service provider instances for the contract. Return value is never null.
-
getAllRankedProviders
public static <T> java.lang.Iterable<RankedProvider<T>> getAllRankedProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the iterable of allproviders
(custom and default) registered for the given service provider contract in the underlyinginjection manager
container.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.- Returns:
- iterable of all available ranked service providers for the contract. Return value is never
null
.
-
sortRankedProviders
public static <T> java.lang.Iterable<T> sortRankedProviders(RankedComparator<T> comparator, java.lang.Iterable<RankedProvider<T>> providers)
Sort given providers withranked comparator
.- Type Parameters:
T
- service provider contract Java type.- Parameters:
comparator
- comparator to sort the providers with.providers
- providers to be sorted.- Returns:
- sorted
iterable
instance containing given providers. The returned value is nevernull
.
-
getAllRankedSortedProviders
public static <T> java.lang.Iterable<T> getAllRankedSortedProviders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get the iterable of all providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container and automatically sorted usingranked comparator
.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.- Returns:
- iterable of all available service providers for the contract. Return value is never
null
.
-
mergeAndSortRankedProviders
public static <T> java.lang.Iterable<T> mergeAndSortRankedProviders(RankedComparator<T> comparator, java.lang.Iterable<java.lang.Iterable<RankedProvider<T>>> providerIterables)
Merge and sort given providers withranked comparator
.- Type Parameters:
T
- service provider contract Java type.- Parameters:
comparator
- comparator to sort the providers with.providerIterables
- providers to be sorted.- Returns:
- merged and sorted
iterable
instance containing given providers. The returned value is nevernull
.
-
getAllProviders
public static <T> java.lang.Iterable<T> getAllProviders(InjectionManager injectionManager, java.lang.Class<T> contract, RankedComparator<T> comparator)
Get the sorted iterable of allproviders
(custom and default) registered for the given service provider contract in the underlyinginjection manager
container.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.comparator
- comparator to sort the providers with.- Returns:
- set of all available ranked service providers for the contract. Return value is never null.
-
getAllServiceHolders
public static <T> java.util.Collection<ServiceHolder<T>> getAllServiceHolders(InjectionManager injectionManager, java.lang.Class<T> contract)
Get collection of allServiceHolder
s bound for providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.- Returns:
- set of all available service provider instances for the contract
-
getServiceHolders
private static <T> java.util.List<ServiceHolder<T>> getServiceHolders(InjectionManager bm, java.lang.Class<T> contract, java.lang.annotation.Annotation... qualifiers)
-
getServiceHolders
private static <T> java.util.List<ServiceHolder<T>> getServiceHolders(InjectionManager injectionManager, java.lang.Class<T> contract, java.util.Comparator<java.lang.Class<?>> objectComparator, java.lang.annotation.Annotation... qualifiers)
-
isJaxRsProvider
public static boolean isJaxRsProvider(java.lang.Class<?> clazz)
Returnstrue
if given component class is a JAX-RS provider.- Parameters:
clazz
- class to check.- Returns:
true
if the class is a JAX-RS provider,false
otherwise.
-
getAllProviders
public static <T> java.lang.Iterable<T> getAllProviders(InjectionManager injectionManager, java.lang.Class<T> contract, java.util.Comparator<T> comparator)
Get the iterable of all providers (custom and default) registered for the given service provider contract in the underlyinginjection manager
container ordered based on the givencomparator
.- Type Parameters:
T
- service provider contract Java type.- Parameters:
injectionManager
- underlying injection manager.contract
- service provider contract.comparator
- comparator to be used for sorting the returned providers.- Returns:
- set of all available service provider instances for the contract ordered using the given
comparator
.
-
getProviderClasses
private static <T> java.util.Set<T> getProviderClasses(java.util.Collection<ServiceHolder<T>> providers)
-
holder2service
private static <T> T holder2service(ServiceHolder<T> holder)
-
getPriority
private static int getPriority(java.lang.Class<?> serviceClass)
-
getProviderContracts
public static java.util.Set<java.lang.Class<?>> getProviderContracts(java.lang.Class<?> clazz)
Returns provider contracts recognized by Jersey that are implemented by theclazz
. Recognized provider contracts include all JAX-RS providers as well as all Jersey SPI components annotated with@Contract
annotation.- Parameters:
clazz
- class to extract the provider interfaces from.- Returns:
- set of provider contracts implemented by the given class.
-
computeProviderContracts
private static void computeProviderContracts(java.lang.Class<?> clazz, java.util.Set<java.lang.Class<?>> contracts)
-
checkProviderRuntime
public static boolean checkProviderRuntime(java.lang.Class<?> component, ContractProvider model, javax.ws.rs.RuntimeType runtimeConstraint, boolean scanned, boolean isResource)
Check thecomponent
whether it is appropriate correctly configured for client or serverruntime
.If a problem occurs a warning is logged and if the component is not usable at all in the current runtime
false
is returned. For classes found during component scanning (scanned=true) certain warnings are completely ignored (e.g. componentsconstrained to
the client runtime and found by server-side class path scanning will be silently ignored and no warning will be logged).- Parameters:
component
- the class of the component being checked.model
- model of the component.runtimeConstraint
- current runtime (client or server).scanned
-false
if the component type has been registered explicitly;true
if the class has been discovered during any form of component scanning.isResource
-true
if the component is also a resource class.- Returns:
true
if component is acceptable for use in the given runtime type,false
otherwise.
-
logProviderSkipped
private static void logProviderSkipped(java.lang.StringBuilder sb, java.lang.Class<?> provider, boolean alsoResourceClass)
-
isSupportedContract
public static boolean isSupportedContract(java.lang.Class<?> type)
Check if the given Java type is a Jersey-supported contract.- Parameters:
type
- contract type.- Returns:
true
if given type is a Jersey-supported contract,false
otherwise.
-
getContractConstraint
private static javax.ws.rs.RuntimeType getContractConstraint(java.lang.Class<?> clazz, javax.ws.rs.RuntimeType defaultConstraint)
-
getImplementedContracts
private static java.lang.Iterable<java.lang.Class<?>> getImplementedContracts(java.lang.Class<?> clazz)
-
isProvider
public static boolean isProvider(java.lang.Class<?> clazz)
Returnstrue
if the given component class is a provider (implements specific interfaces). SeegetProviderContracts(java.lang.Class<?>)
.- Parameters:
clazz
- class to test.- Returns:
true
if the class is provider,false
otherwise.
-
ensureContract
public static void ensureContract(java.lang.Class<?> contract, java.lang.Class<?>... implementations)
Ensure the supplied implementation classes implement the expected contract.- Parameters:
contract
- contract that is expected to be implemented by the implementation classes.implementations
- contract implementations.- Throws:
java.lang.IllegalArgumentException
- in case any of the implementation classes does not implement the expected contract.
-
findFirstProviderContract
private static boolean findFirstProviderContract(java.lang.Class<?> clazz)
-
-