Interface StrategySelector
-
- All Superinterfaces:
java.io.Serializable,Service
public interface StrategySelector extends Service
Service which acts as a registry for named strategy implementations. Strategies are more open ended than services, though a strategy managed here might very well also be a service. The strategy is any interface that has multiple, (possibly short) named implementations. StrategySelector manages resolution of particular implementation by (possibly short) name via theselectStrategyImplementor(java.lang.Class<T>, java.lang.String)method, which is the main contract here. As indicated in the docs of that method the given name might be either a short registered name or the implementation FQN. As an example, consider resolving theTransactionCoordinatorBuilderimplementation to use. To use the JDBC-based TransactionCoordinatorBuilder the passed name might be either"jdbc"or"org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl"(which is the FQN). Strategy implementations can be managed byregisterStrategyImplementor(java.lang.Class<T>, java.lang.String, java.lang.Class<? extends T>)andunRegisterStrategyImplementor(java.lang.Class<T>, java.lang.Class<? extends T>). Originally designed to help the OSGi use case, though no longer used there. The service also exposes a general typing API viaresolveStrategy(java.lang.Class<T>, java.lang.Object)andresolveDefaultableStrategy(java.lang.Class<T>, java.lang.Object, T)which accept implementation references rather than implementation names, allowing for a multitude of interpretations of said "implementation reference". See the docs forresolveDefaultableStrategy(java.lang.Class<T>, java.lang.Object, T)for details.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> java.util.Collection<java.lang.Class<? extends T>>getRegisteredStrategyImplementors(java.lang.Class<T> strategy)Retrieve all of the registered implementors of the given strategy.<T> voidregisterStrategyImplementor(java.lang.Class<T> strategy, java.lang.String name, java.lang.Class<? extends T> implementation)Registers a named implementor of a particular strategy contract.<T> TresolveDefaultableStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, java.util.concurrent.Callable<T> defaultResolver)Resolve strategy instances.<T> TresolveDefaultableStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, T defaultValue)Resolve strategy instances.<T> TresolveStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference)Resolve strategy instances.<T> TresolveStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, java.util.concurrent.Callable<T> defaultResolver, StrategyCreator<T> creator)<T> TresolveStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, T defaultValue, StrategyCreator<T> creator)<T> java.lang.Class<? extends T>selectStrategyImplementor(java.lang.Class<T> strategy, java.lang.String name)Locate the named strategy implementation.<T> voidunRegisterStrategyImplementor(java.lang.Class<T> strategy, java.lang.Class<? extends T> implementation)Un-registers a named implementor of a particular strategy contract.
-
-
-
Method Detail
-
registerStrategyImplementor
<T> void registerStrategyImplementor(java.lang.Class<T> strategy, java.lang.String name, java.lang.Class<? extends T> implementation)Registers a named implementor of a particular strategy contract.- Type Parameters:
T- The type of the strategy. Used to make sure that the strategy and implementation are type compatible.- Parameters:
strategy- The strategy contract.name- The registration nameimplementation- The implementation Class
-
unRegisterStrategyImplementor
<T> void unRegisterStrategyImplementor(java.lang.Class<T> strategy, java.lang.Class<? extends T> implementation)Un-registers a named implementor of a particular strategy contract. Un-registers all named registrations for the given strategy contract naming the given class.- Type Parameters:
T- The type of the strategy. Used to make sure that the strategy and implementation are type compatible.- Parameters:
strategy- The strategy contract.implementation- The implementation Class
-
selectStrategyImplementor
<T> java.lang.Class<? extends T> selectStrategyImplementor(java.lang.Class<T> strategy, java.lang.String name)Locate the named strategy implementation.- Type Parameters:
T- The type of the strategy. Used to make sure that the strategy and implementation are type compatible.- Parameters:
strategy- The type of strategy to be resolved.name- The name of the strategy to locate; might be either a registered name or the implementation FQN.- Returns:
- The named strategy implementation class.
-
resolveStrategy
<T> T resolveStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference)Resolve strategy instances. See discussion onresolveDefaultableStrategy(java.lang.Class<T>, java.lang.Object, T). Only difference is that here, the implied default value isnull.- Type Parameters:
T- The type of the strategy. Used to make sure that the strategy and implementation are type compatible.- Parameters:
strategy- The type (interface) of the strategy to be resolved.strategyReference- The reference to the strategy for which we need to resolve an instance.- Returns:
- The strategy instance
-
resolveDefaultableStrategy
<T> T resolveDefaultableStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, T defaultValue)Resolve strategy instances. The incoming reference might be:-
null- in which case defaultValue is returned. - An actual instance of the strategy type - it is returned, as is
-
A reference to the implementation
Class- an instance is created by callingClass.newInstance()(aka, the class's no-arg ctor). -
The name of the implementation class - First the implementation's
Classreference is resolved, and then an instance is created by callingClass.newInstance()
- Type Parameters:
T- The type of the strategy. Used to make sure that the strategy and implementation are type compatible.- Parameters:
strategy- The type (interface) of the strategy to be resolved.strategyReference- The reference to the strategy for which we need to resolve an instance.defaultValue- THe default value to use if strategyReference is null- Returns:
- The strategy instance
-
-
resolveDefaultableStrategy
<T> T resolveDefaultableStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, java.util.concurrent.Callable<T> defaultResolver)Resolve strategy instances. The incoming reference might be:-
null- in which case defaultValue is returned. - An actual instance of the strategy type - it is returned, as is
-
A reference to the implementation
Class- an instance is created by callingClass.newInstance()(aka, the class's no-arg ctor). -
The name of the implementation class - First the implementation's
Classreference is resolved, and then an instance is created by callingClass.newInstance()
- Type Parameters:
T- The type of the strategy. Used to make sure that the strategy and implementation are type compatible.- Parameters:
strategy- The type (interface) of the strategy to be resolved.strategyReference- The reference to the strategy for which we need to resolve an instance.defaultResolver- A strategy for resolving the default value strategyReference resolves to null.- Returns:
- The strategy instance
-
-
resolveStrategy
<T> T resolveStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, java.util.concurrent.Callable<T> defaultResolver, StrategyCreator<T> creator)
-
resolveStrategy
<T> T resolveStrategy(java.lang.Class<T> strategy, java.lang.Object strategyReference, T defaultValue, StrategyCreator<T> creator)
-
getRegisteredStrategyImplementors
<T> java.util.Collection<java.lang.Class<? extends T>> getRegisteredStrategyImplementors(java.lang.Class<T> strategy)
Retrieve all of the registered implementors of the given strategy. Useful to allow defaulting the choice to the single registered implementor when only one is registered- Returns:
- The implementors. Should never return
null
-
-