Class DefaultHashFormatFactory

  • All Implemented Interfaces:
    HashFormatFactory

    public class DefaultHashFormatFactory
    extends Object
    implements HashFormatFactory
    This default HashFormatFactory implementation heuristically determines a HashFormat class to instantiate based on the input argument and returns a new instance of the discovered class. The heuristics are detailed in the getInstance method documentation.
    Since:
    1.2
    • Method Detail

      • getFormatClassNames

        public Map<String,​StringgetFormatClassNames()
        Returns a hashFormatAlias-to-fullyQualifiedHashFormatClassNameImplementation map.

        This map will be used by the getInstance implementation: that method's argument will be used as a lookup key to this map. If the map returns a value, that value will be used to instantiate and return a new HashFormat instance.

        Defaults

        Shiro's default HashFormat implementations (as listed by the ProvidedHashFormat enum) will be searched automatically independently of this map. You only need to populate this map with custom HashFormat implementations that are not already represented by a ProvidedHashFormat.

        Efficiency

        Populating this map will be more efficient than configuring searchPackages, but search packages may be more convenient depending on the number of HashFormat implementations that need to be supported by this factory.
        Returns:
        a hashFormatAlias-to-fullyQualifiedHashFormatClassNameImplementation map.
      • setFormatClassNames

        public void setFormatClassNames​(Map<String,​String> formatClassNames)
        Sets the hash-format-alias-to-fullyQualifiedHashFormatClassNameImplementation map to be used in the getInstance(String) implementation. See the getFormatClassNames() JavaDoc for more information.

        Efficiency

        Populating this map will be more efficient than configuring searchPackages, but search packages may be more convenient depending on the number of HashFormat implementations that need to be supported by this factory.
        Parameters:
        formatClassNames - the hash-format-alias-to-fullyQualifiedHashFormatClassNameImplementation map to be used in the getInstance(String) implementation.
      • setSearchPackages

        public void setSearchPackages​(Set<String> searchPackages)
        Sets a set of package names that can be searched for HashFormat implementations according to heuristics defined in the getHashFormat(packageName, token) JavaDoc.

        Efficiency

        Configuring this property is not as efficient as configuring a formatClassNames map, but it may be more convenient depending on the number of HashFormat implementations that need to be supported by this factory.
        Parameters:
        searchPackages - a set of package names that can be searched for HashFormat implementations
      • getHashFormatClass

        protected Class getHashFormatClass​(String token)
        Heuristically determine the fully qualified HashFormat implementation class name based on the specified token.

        This implementation functions as follows (in order):

        1. See if the argument can be used as a lookup key in the formatClassNames map. If a value (a fully qualified class name HashFormat implementation) is found, lookup the class and return it.
        2. Check to see if the token argument is a ProvidedHashFormat enum value. If so, acquire the corresponding HashFormat class and return it.
        3. Check to see if the token argument is itself a fully qualified class name. If so, try to load the class and return it.
        4. If the above options do not result in a discovered class, search all all configured searchPackages using heuristics defined in the getHashFormatClass(packageName, token) method documentation (relaying the token argument to that method for each configured package).

        If a class is not discovered via any of the above means, null is returned to indicate the class could not be found.

        Parameters:
        token - the string token from which a class name will be heuristically determined.
        Returns:
        the discovered HashFormat class implementation or null if no class could be heuristically determined.
      • getHashFormatClass

        protected Class getHashFormatClass​(String packageName,
                                           String token)
        Heuristically determine the fully qualified HashFormat implementation class name in the specified package based on the provided token.

        The token is expected to be a relevant fragment of an unqualified class name in the specified package. A 'relevant fragment' can be one of the following:

        • The HashFormat implementation unqualified class name
        • The prefix of an unqualified class name ending with the text Format. The first character of this prefix can be upper or lower case and both options will be tried.
        • The prefix of an unqualified class name ending with the text HashFormat. The first character of this prefix can be upper or lower case and both options will be tried.
        • The prefix of an unqualified class name ending with the text CryptoFormat. The first character of this prefix can be upper or lower case and both options will be tried.

        Some examples:

        Package Name Token Expected Output Class Notes
        com.foo.whatever MyBarFormat com.foo.whatever.MyBarFormat Token is a complete unqualified class name
        com.foo.whatever Bar com.foo.whatever.BarFormat or com.foo.whatever.BarHashFormat or com.foo.whatever.BarCryptFormat The token is only part of the unqualified class name - i.e. all characters in front of the *Format *HashFormat or *CryptFormat suffix. Note that the *Format variant will be tried before *HashFormat and then finally *CryptFormat
        com.foo.whatever bar com.foo.whatever.BarFormat or com.foo.whatever.BarHashFormat or com.foo.whatever.BarCryptFormat Exact same output as the above Bar input example. (The token differs only by the first character)
        Parameters:
        packageName - the package to search for matching HashFormat implementations.
        token - the string token from which a class name will be heuristically determined.
        Returns:
        the discovered HashFormat class implementation or null if no class could be heuristically determined.