Class DefaultHashFormatFactory
- java.lang.Object
-
- org.apache.shiro.crypto.hash.format.DefaultHashFormatFactory
-
- All Implemented Interfaces:
HashFormatFactory
public class DefaultHashFormatFactory extends Object implements HashFormatFactory
This defaultHashFormatFactory
implementation heuristically determines aHashFormat
class to instantiate based on the input argument and returns a new instance of the discovered class. The heuristics are detailed in thegetInstance
method documentation.- Since:
- 1.2
-
-
Constructor Summary
Constructors Constructor Description DefaultHashFormatFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
assertHashFormatImpl(Class clazz)
Map<String,String>
getFormatClassNames()
Returns ahashFormatAlias
-to-fullyQualifiedHashFormatClassNameImplementation
map.protected Class
getHashFormatClass(String token)
Heuristically determine the fully qualified HashFormat implementation class name based on the specified token.protected Class
getHashFormatClass(String packageName, String token)
Heuristically determine the fully qualifiedHashFormat
implementation class name in the specified package based on the provided token.HashFormat
getInstance(String in)
Set<String>
getSearchPackages()
Returns a set of package names that can be searched forHashFormat
implementations according to heuristics defined in thegetHashFormat(packageName, token)
JavaDoc.protected Class
lookupHashFormatClass(String name)
protected HashFormat
newHashFormatInstance(Class clazz)
void
setFormatClassNames(Map<String,String> formatClassNames)
Sets thehash-format-alias
-to-fullyQualifiedHashFormatClassNameImplementation
map to be used in thegetInstance(String)
implementation.void
setSearchPackages(Set<String> searchPackages)
Sets a set of package names that can be searched forHashFormat
implementations according to heuristics defined in thegetHashFormat(packageName, token)
JavaDoc.
-
-
-
Constructor Detail
-
DefaultHashFormatFactory
public DefaultHashFormatFactory()
-
-
Method Detail
-
getFormatClassNames
public Map<String,String> getFormatClassNames()
Returns ahashFormatAlias
-to-fullyQualifiedHashFormatClassNameImplementation
map. This map will be used by thegetInstance
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 newHashFormat
instance.Defaults
Shiro's default HashFormat implementations (as listed by theProvidedHashFormat
enum) will be searched automatically independently of this map. You only need to populate this map with customHashFormat
implementations that are not already represented by aProvidedHashFormat
.Efficiency
Populating this map will be more efficient than configuringsearchPackages
, but search packages may be more convenient depending on the number ofHashFormat
implementations that need to be supported by this factory.- Returns:
- a
hashFormatAlias
-to-fullyQualifiedHashFormatClassNameImplementation
map.
-
setFormatClassNames
public void setFormatClassNames(Map<String,String> formatClassNames)
Sets thehash-format-alias
-to-fullyQualifiedHashFormatClassNameImplementation
map to be used in thegetInstance(String)
implementation. See thegetFormatClassNames()
JavaDoc for more information.Efficiency
Populating this map will be more efficient than configuringsearchPackages
, but search packages may be more convenient depending on the number ofHashFormat
implementations that need to be supported by this factory.- Parameters:
formatClassNames
- thehash-format-alias
-to-fullyQualifiedHashFormatClassNameImplementation
map to be used in thegetInstance(String)
implementation.
-
getSearchPackages
public Set<String> getSearchPackages()
Returns a set of package names that can be searched forHashFormat
implementations according to heuristics defined in thegetHashFormat(packageName, token)
JavaDoc.Efficiency
Configuring this property is not as efficient as configuring aformatClassNames
map, but it may be more convenient depending on the number ofHashFormat
implementations that need to be supported by this factory.- Returns:
- a set of package names that can be searched for
HashFormat
implementations - See Also:
getHashFormatClass(String, String)
-
setSearchPackages
public void setSearchPackages(Set<String> searchPackages)
Sets a set of package names that can be searched forHashFormat
implementations according to heuristics defined in thegetHashFormat(packageName, token)
JavaDoc.Efficiency
Configuring this property is not as efficient as configuring aformatClassNames
map, but it may be more convenient depending on the number ofHashFormat
implementations that need to be supported by this factory.- Parameters:
searchPackages
- a set of package names that can be searched forHashFormat
implementations
-
getInstance
public HashFormat getInstance(String in)
- Specified by:
getInstance
in interfaceHashFormatFactory
-
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):- See if the argument can be used as a lookup key in the
formatClassNames
map. If a value (a fully qualified class nameHashFormat
implementation) is found,lookup
the class and return it. -
Check to see if the token argument is a
ProvidedHashFormat
enum value. If so, acquire the correspondingHashFormat
class and return it. - Check to see if the token argument is itself a fully qualified class name. If so, try to load the class and return it.
- If the above options do not result in a discovered class, search all all configured
searchPackages
using heuristics defined in thegetHashFormatClass(packageName, token)
method documentation (relaying thetoken
argument to that method for each configured package).
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.
- See if the argument can be used as a lookup key in the
-
getHashFormatClass
protected Class getHashFormatClass(String packageName, String token)
Heuristically determine the fully qualifiedHashFormat
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.
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
orcom.foo.whatever.BarHashFormat
orcom.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
orcom.foo.whatever.BarHashFormat
orcom.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 matchingHashFormat
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.
- The
-
lookupHashFormatClass
protected Class lookupHashFormatClass(String name)
-
assertHashFormatImpl
protected final void assertHashFormatImpl(Class clazz)
-
newHashFormatInstance
protected final HashFormat newHashFormatInstance(Class clazz)
-
-