类 TodayStrategies

java.lang.Object
cn.taketoday.lang.TodayStrategies
直接已知子类:
TodayStrategiesRuntimeHints.ExtendedTodayStrategies

public class TodayStrategies extends Object
today-framework Strategies

General purpose strategy loading mechanism for internal use within the framework.

Reads a META-INF/today.strategies file from the root of the library classpath, and also allows for programmatically setting properties through setProperty(java.lang.String, java.lang.String). When checking a property, local entries are being checked first, then falling back to JVM-level system properties through a System.getProperty(java.lang.String) check.

Get keyed strategies

从以下版本开始:
4.0 2021/9/5 13:57
作者:
Harry Yang
  • 字段详细资料

  • 构造器详细资料

    • TodayStrategies

      protected TodayStrategies(@Nullable ClassLoader classLoader, Map<String,List<String>> strategies)
      Create a new TodayStrategies instance.
      参数:
      classLoader - the classloader used to instantiate the factories
      strategies - a map of strategy class name to implementation class names
  • 方法详细资料

    • getFlag

      public static boolean getFlag(String key)
      Retrieve the flag for the given property key.
      参数:
      key - the property key
      返回:
      true if the property is set to "true", false otherwise
    • getFlag

      public static boolean getFlag(String key, boolean defaultFlag)
      Retrieve the flag for the given property key.

      If there isn't a key returns defaultFlag

      参数:
      key - the property key
      返回:
      true if the property is set to "true", false otherwise ,If there isn't a key returns defaultFlag
    • setFlag

      public static void setFlag(String key)
      Programmatically set a local flag to "true", overriding an entry in the PROPERTIES_RESOURCE_LOCATION file (if any).
      参数:
      key - the property key
    • setProperty

      public static void setProperty(String key, @Nullable String value)
      Programmatically set a local property, overriding an entry in the PROPERTIES_RESOURCE_LOCATION file (if any).
      参数:
      key - the property key
      value - the associated property value, or null to reset it
    • getProperty

      @Nullable public static String getProperty(String key)
      Retrieve the property value for the given key, checking local properties first and falling back to JVM-level system properties.
      参数:
      key - the property key
      返回:
      the associated property value, or null if none found
    • getProperty

      public static String getProperty(String key, String def)
      Retrieve the property value for the given key, checking local properties first and falling back to JVM-level system properties.
      参数:
      key - the name of the system property.
      def - a default value.
      返回:
      the string value of the system property, or the default value if there is no property with that key.
      另请参阅:
    • getInteger

      @Nullable public static Integer getInteger(String key)
      Determines the integer value of the property with the specified name.

      The first argument is treated as the name of a today.properties or system property. properties are accessible through the getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned.

      If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

      In other words, this method returns an Integer object equal to the value of:

      getInteger(nm, null)
      参数:
      key - property name.
      返回:
      the Integer value of the property.
      抛出:
      SecurityException - for the same reasons as System.getProperty
      另请参阅:
    • getInt

      public static int getInt(String key, int val)
      Determines the integer value of the system property with the specified name.

      The first argument is treated as the name of a today.properties or system property. properties are accessible through the getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned.

      The second argument is the default value. An Integer object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

      In other words, this method returns an Integer object equal to the value of:

      getInteger(nm, new Integer(val))
      but in practice it may be implemented in a manner such as:
       Integer result = getInteger(nm, null);
       return (result == null) ? new Integer(val) : result;
       
      to avoid the unnecessary allocation of an Integer object when the default value is not needed.
      参数:
      key - property name.
      val - default value.
      返回:
      the Integer value of the property.
      抛出:
      SecurityException - for the same reasons as System.getProperty
      另请参阅:
    • getInteger

      @Nullable public static Integer getInteger(String key, @Nullable Integer val)
      Returns the integer value of the property with the specified name.

      The first argument is treated as the name of a today.properties or system property. properties are accessible through the getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned. in summary:

      • If the property value begins with the two ASCII characters 0x or the ASCII character #, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as by the method Integer.valueOf(java.lang.String, int) with radix 16.
      • If the property value begins with the ASCII character 0 followed by another character, it is parsed as an octal integer exactly as by the method Integer.valueOf(java.lang.String, int) with radix 8.
      • Otherwise, the property value is parsed as a decimal integer exactly as by the method Integer.valueOf(java.lang.String, int) with radix 10.

      The second argument is the default value. The default value is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

      参数:
      key - property name.
      val - default value.
      返回:
      the Integer value of the property. System.getProperty
      另请参阅:
    • getLong

      @Nullable public static Long getLong(String nm)
      Determines the long value of the system property with the specified name.

      The first argument is treated as the name of a today.properties or system property. properties are accessible through the getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned.

      If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

      In other words, this method returns a Long object equal to the value of:

      getLong(nm, null)
      参数:
      nm - property name.
      返回:
      the Long value of the property.
      抛出:
      SecurityException - for the same reasons as System.getProperty
      另请参阅:
    • getLong

      public static long getLong(String key, long val)
      Determines the long value of the system property with the specified name.

      The first argument is treated as the name of a today.properties or system property. properties are accessible through the getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned.

      The second argument is the default value. A Long object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

      In other words, this method returns a Long object equal to the value of:

      getLong(nm, new Long(val))
      but in practice it may be implemented in a manner such as:
       Long result = getLong(nm, null);
       return (result == null) ? new Long(val) : result;
       
      to avoid the unnecessary allocation of a Long object when the default value is not needed.
      参数:
      key - property name.
      val - default value.
      返回:
      the Long value of the property.
      抛出:
      SecurityException - for the same reasons as System.getProperty
      另请参阅:
    • getLong

      @Nullable public static Long getLong(String key, Long val)
      Returns the long value of the system property with the specified name.

      The first argument is treated as the name of a today.properties or system property. properties are accessible through the getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned. in summary:

      • If the property value begins with the two ASCII characters 0x or the ASCII character #, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as for the method Long.valueOf(java.lang.String, int) with radix 16.
      • If the property value begins with the ASCII character 0 followed by another character, it is parsed as an octal integer exactly as by the method Long.valueOf(java.lang.String, int) with radix 8.
      • Otherwise the property value is parsed as a decimal integer exactly as by the method Long.valueOf(java.lang.String, int) with radix 10.

      Note that, in every case, neither L ('\u004C') nor l ('\u006C') is permitted to appear at the end of the property value as a type indicator, as would be permitted in Java programming language source code.

      The second argument is the default value. The default value is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

      参数:
      key - property name.
      val - default value.
      返回:
      the Long value of the property.
      抛出:
      SecurityException - for the same reasons as System.getProperty
      另请参阅:
    • readStrategies

      public static void readStrategies(MultiValueMap<String,String> strategies, Properties properties)
      参数:
      strategies - output
      properties - input
    • load

      public <T> List<T> load(Class<T> strategyType)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the configured class loader and a default argument resolver that expects a no-arg constructor.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      If a custom instantiation strategy is required, use load(...) with a custom ArgumentResolver and/or FailureHandler.

      if duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      参数:
      strategyType - the interface or abstract class representing the strategy
      返回:
      Returns strategy object list that can be modified
      抛出:
      IllegalArgumentException - if any strategy implementation class cannot be loaded or if an error occurs while instantiating any strategy
    • load

      public <T> List<T> load(Class<T> strategyType, @Nullable TodayStrategies.ArgumentResolver argumentResolver)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the configured class loader and the given argument resolver.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      If duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      参数:
      strategyType - the interface or abstract class representing the strategy
      argumentResolver - strategy used to resolve constructor arguments by their type
      返回:
      Returns strategy object list that can be modified
      抛出:
      IllegalArgumentException - if any strategy implementation class cannot be loaded or if an error occurs while instantiating any strategy
    • load

      public <T> List<T> load(Class<T> strategyType, ClassInstantiator instantiator)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the configured class loader and the given argument resolver.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      If duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      参数:
      strategyType - the interface or abstract class representing the strategy
      instantiator - strategy used it to instantiate instance
      返回:
      Returns strategy object list that can be modified
      抛出:
      IllegalArgumentException - if any strategy implementation class cannot be loaded or if an error occurs while instantiating any strategy
    • load

      public <T> List<T> load(Class<T> strategyType, @Nullable TodayStrategies.FailureHandler failureHandler)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the configured class loader with custom failure handling provided by the given failure handler.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      If duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      For any strategy implementation class that cannot be loaded or error that occurs while instantiating it, the given failure handler is called.

      参数:
      strategyType - the interface or abstract class representing the strategy
      failureHandler - strategy used to handle strategy instantiation failures
      返回:
      Returns strategy object list that can be modified
    • load

      public <T> List<T> load(Class<T> strategyType, @Nullable TodayStrategies.ArgumentResolver resolver, @Nullable TodayStrategies.FailureHandler failureHandler)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the configured class loader, the given argument resolver, and custom failure handling provided by the given failure handler.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      If duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      For any strategy implementation class that cannot be loaded or error that occurs while instantiating it, the given failure handler is called.

      参数:
      strategyType - the interface or abstract class representing the strategy
      resolver - strategy used to resolve constructor arguments by their type
      failureHandler - strategy used to handle strategy instantiation failures
      返回:
      Returns strategy object list that can be modified
    • load

      public <T> List<T> load(Class<T> strategyType, ClassInstantiator instantiator, @Nullable TodayStrategies.FailureHandler failureHandler)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the configured class loader, the given argument resolver, and custom failure handling provided by the given failure handler.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      If duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      For any strategy implementation class that cannot be loaded or error that occurs while instantiating it, the given failure handler is called.

      参数:
      strategyType - the interface or abstract class representing the strategy
      instantiator - strategy used it to instantiate instance
      failureHandler - strategy used to handle strategy instantiation failures
      返回:
      Returns strategy object list that can be modified
    • getStrategyNames

      private List<String> getStrategyNames(String strategyKey)
    • getStrategyNames

      private List<String> getStrategyNames(Class<?> strategyType)
    • instantiateStrategy

      @Nullable protected <T> T instantiateStrategy(String implementationName, Class<T> type, ClassInstantiator instantiator, TodayStrategies.FailureHandler failureHandler)
    • findFirst

      @Nullable public static String findFirst(String strategyKey)
      get first strategy
      另请参阅:
    • findFirst

      @Nullable public static <T> T findFirst(Class<T> strategyClass, @Nullable Supplier<T> defaultValue)
      get first strategy
      另请参阅:
    • findFirst

      public static <T> Optional<T> findFirst(Class<T> strategyClass)
    • find

      public static <T> List<T> find(Class<T> strategyClass)
      get none repeatable strategies by given class
      类型参数:
      T - target type
      参数:
      strategyClass - strategy class
      返回:
      returns none repeatable strategies by given class
    • find

      public static <T> List<T> find(Class<T> strategyType, @Nullable ClassLoader classLoader)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      if duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      For more advanced strategy loading with TodayStrategies.ArgumentResolver or TodayStrategies.FailureHandler support use forDefaultResourceLocation(ClassLoader) to obtain a TodayStrategies instance.

      参数:
      strategyType - the interface or abstract class representing the strategy
      classLoader - the ClassLoader to use for loading (can be null to use the default)
      抛出:
      IllegalArgumentException - if any strategy implementation class cannot be loaded or if an error occurs while instantiating any strategy
    • find

      public static <T> List<T> find(Class<T> strategyType, @Nullable ClassLoader classLoader, ClassInstantiator instantiator)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      if duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      For more advanced strategy loading with TodayStrategies.ArgumentResolver or TodayStrategies.FailureHandler support use forDefaultResourceLocation(ClassLoader) to obtain a TodayStrategies instance.

      参数:
      strategyType - the interface or abstract class representing the strategy
      classLoader - the ClassLoader to use for loading (can be null to use the default)
      抛出:
      IllegalArgumentException - if any strategy implementation class cannot be loaded or if an error occurs while instantiating any strategy
    • find

      public static <T> List<T> find(Class<T> strategyType, ClassInstantiator instantiator)
      Load and instantiate the strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      The returned factories are sorted through AnnotationAwareOrderComparator.

      if duplicate implementation class names are discovered for a given strategy type, only one instance of the duplicated implementation type will be instantiated.

      For more advanced strategy loading with TodayStrategies.ArgumentResolver or TodayStrategies.FailureHandler support use forDefaultResourceLocation(ClassLoader) to obtain a TodayStrategies instance.

      参数:
      strategyType - the interface or abstract class representing the strategy
      抛出:
      IllegalArgumentException - if any strategy implementation class cannot be loaded or if an error occurs while instantiating any strategy
    • findNames

      public static List<String> findNames(Class<?> strategyType)
      Load the fully qualified class names of strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      if a particular implementation class name is discovered more than once for the given strategy type, duplicates will be ignored.

      参数:
      strategyType - the interface or abstract class representing the strategy
      抛出:
      IllegalArgumentException - if an error occurs while loading strategy names
      另请参阅:
    • findNames

      public static List<String> findNames(Class<?> strategyType, @Nullable ClassLoader classLoader)
      Load the fully qualified class names of strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      if a particular implementation class name is discovered more than once for the given strategy type, duplicates will be ignored.

      参数:
      strategyType - the interface or abstract class representing the strategy
      classLoader - the ClassLoader to use for loading resources; can be null to use the default
      抛出:
      IllegalArgumentException - if an error occurs while loading strategy names
      另请参阅:
    • findNames

      public static List<String> findNames(String strategyKey)
      Load the fully qualified class names of strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      if a particular implementation class name is discovered more than once for the given strategy type, duplicates will be ignored.

      参数:
      strategyKey - the key representing the strategy
      抛出:
      IllegalArgumentException - if an error occurs while loading strategy names
      另请参阅:
    • findNames

      public static List<String> findNames(String strategyKey, @Nullable ClassLoader classLoader)
      Load the fully qualified class names of strategy implementations of the given type from "META-INF/today.strategies", using the given class loader.

      if a particular implementation class name is discovered more than once for the given strategy type, duplicates will be ignored.

      参数:
      strategyKey - the key representing the strategy
      classLoader - the ClassLoader to use for loading resources; can be null to use the default
      抛出:
      IllegalArgumentException - if an error occurs while loading strategy names
      另请参阅:
    • forDefaultResourceLocation

      public static TodayStrategies forDefaultResourceLocation()
      Create a TodayStrategies instance that will load and instantiate the strategy implementations from "META-INF/today.strategies", using the default class loader.
      返回:
      a TodayStrategies instance
      另请参阅:
    • forDefaultResourceLocation

      public static TodayStrategies forDefaultResourceLocation(@Nullable ClassLoader classLoader)
      Create a TodayStrategies instance that will load and instantiate the strategy implementations from "META-INF/today.strategies", using the given class loader.
      参数:
      classLoader - the ClassLoader to use for loading resources; can be null to use the default
      返回:
      a TodayStrategies instance
      另请参阅:
    • forLocation

      public static TodayStrategies forLocation(String resourceLocation)
      Create a TodayStrategies instance that will load and instantiate the strategy implementations from the given location, using the default class loader.
      参数:
      resourceLocation - the resource location to look for factories
      返回:
      a TodayStrategies instance
      另请参阅:
    • forLocation

      public static TodayStrategies forLocation(String resourceLocation, @Nullable ClassLoader classLoader)
      Create a TodayStrategies instance that will load and instantiate the strategy implementations from the given location, using the given class loader.
      参数:
      resourceLocation - the resource location to look for factories
      classLoader - the ClassLoader to use for loading resources; can be null to use the default
      返回:
      a TodayStrategies instance
      另请参阅:
    • loadResource

      protected static Map<String,List<String>> loadResource(ClassLoader classLoader, String resourceLocation)
    • toDistinctUnmodifiableList

      private static List<String> toDistinctUnmodifiableList(String strategyType, List<String> implementations)