Class DefaultHashFormatFactory
- java.lang.Object
-
- org.apache.shiro.crypto.hash.format.DefaultHashFormatFactory
-
- All Implemented Interfaces:
HashFormatFactory
public class DefaultHashFormatFactory extends Object implements HashFormatFactory
This defaultHashFormatFactoryimplementation heuristically determines aHashFormatclass to instantiate based on the input argument and returns a new instance of the discovered class. The heuristics are detailed in thegetInstancemethod documentation.- Since:
- 1.2
-
-
Constructor Summary
Constructors Constructor Description DefaultHashFormatFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidassertHashFormatImpl(Class clazz)Map<String,String>getFormatClassNames()Returns ahashFormatAlias-to-fullyQualifiedHashFormatClassNameImplementationmap.protected ClassgetHashFormatClass(String token)Heuristically determine the fully qualified HashFormat implementation class name based on the specified token.protected ClassgetHashFormatClass(String packageName, String token)Heuristically determine the fully qualifiedHashFormatimplementation class name in the specified package based on the provided token.HashFormatgetInstance(String in)Set<String>getSearchPackages()Returns a set of package names that can be searched forHashFormatimplementations according to heuristics defined in thegetHashFormat(packageName, token)JavaDoc.protected ClasslookupHashFormatClass(String name)protected HashFormatnewHashFormatInstance(Class clazz)voidsetFormatClassNames(Map<String,String> formatClassNames)Sets thehash-format-alias-to-fullyQualifiedHashFormatClassNameImplementationmap to be used in thegetInstance(String)implementation.voidsetSearchPackages(Set<String> searchPackages)Sets a set of package names that can be searched forHashFormatimplementations according to heuristics defined in thegetHashFormat(packageName, token)JavaDoc.
-
-
-
Method Detail
-
getFormatClassNames
public Map<String,String> getFormatClassNames()
Returns ahashFormatAlias-to-fullyQualifiedHashFormatClassNameImplementationmap. This map will be used by thegetInstanceimplementation: 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 newHashFormatinstance.Defaults
Shiro's default HashFormat implementations (as listed by theProvidedHashFormatenum) will be searched automatically independently of this map. You only need to populate this map with customHashFormatimplementations 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 ofHashFormatimplementations that need to be supported by this factory.- Returns:
- a
hashFormatAlias-to-fullyQualifiedHashFormatClassNameImplementationmap.
-
setFormatClassNames
public void setFormatClassNames(Map<String,String> formatClassNames)
Sets thehash-format-alias-to-fullyQualifiedHashFormatClassNameImplementationmap 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 ofHashFormatimplementations that need to be supported by this factory.- Parameters:
formatClassNames- thehash-format-alias-to-fullyQualifiedHashFormatClassNameImplementationmap to be used in thegetInstance(String)implementation.
-
getSearchPackages
public Set<String> getSearchPackages()
Returns a set of package names that can be searched forHashFormatimplementations according to heuristics defined in thegetHashFormat(packageName, token)JavaDoc.Efficiency
Configuring this property is not as efficient as configuring aformatClassNamesmap, but it may be more convenient depending on the number ofHashFormatimplementations that need to be supported by this factory.- Returns:
- a set of package names that can be searched for
HashFormatimplementations - See Also:
getHashFormatClass(String, String)
-
setSearchPackages
public void setSearchPackages(Set<String> searchPackages)
Sets a set of package names that can be searched forHashFormatimplementations according to heuristics defined in thegetHashFormat(packageName, token)JavaDoc.Efficiency
Configuring this property is not as efficient as configuring aformatClassNamesmap, but it may be more convenient depending on the number ofHashFormatimplementations that need to be supported by this factory.- Parameters:
searchPackages- a set of package names that can be searched forHashFormatimplementations
-
getInstance
public HashFormat getInstance(String in)
- Specified by:
getInstancein 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
formatClassNamesmap. If a value (a fully qualified class nameHashFormatimplementation) is found,lookupthe class and return it. -
Check to see if the token argument is a
ProvidedHashFormatenum value. If so, acquire the correspondingHashFormatclass 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
searchPackagesusing heuristics defined in thegetHashFormatClass(packageName, token)method documentation (relaying thetokenargument to that method for each configured package).
nullis 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
nullif 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 qualifiedHashFormatimplementation 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
HashFormatimplementation 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.whateverMyBarFormatcom.foo.whatever.MyBarFormatToken is a complete unqualified class name com.foo.whateverBarcom.foo.whatever.BarFormatorcom.foo.whatever.BarHashFormatorcom.foo.whatever.BarCryptFormatThe token is only part of the unqualified class name - i.e. all characters in front of the *Format*HashFormator*CryptFormatsuffix. Note that the*Formatvariant will be tried before*HashFormatand then finally*CryptFormatcom.foo.whateverbarcom.foo.whatever.BarFormatorcom.foo.whatever.BarHashFormatorcom.foo.whatever.BarCryptFormatExact same output as the above Barinput example. (The token differs only by the first character)- Parameters:
packageName- the package to search for matchingHashFormatimplementations.token- the string token from which a class name will be heuristically determined.- Returns:
- the discovered HashFormat class implementation or
nullif no class could be heuristically determined.
- The
-
assertHashFormatImpl
protected final void assertHashFormatImpl(Class clazz)
-
newHashFormatInstance
protected final HashFormat newHashFormatInstance(Class clazz)
-
-