org.apache.shiro.crypto.hash.format
Class DefaultHashFormatFactory

java.lang.Object
  extended by org.apache.shiro.crypto.hash.format.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

Constructor Summary
DefaultHashFormatFactory()
           
 
Method Summary
protected  void assertHashFormatImpl(Class clazz)
           
 Map<String,String> getFormatClassNames()
          Returns a hashFormatAlias-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 qualified HashFormat 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 for HashFormat implementations according to heuristics defined in the getHashFormat(packageName, token) JavaDoc.
protected  Class lookupHashFormatClass(String name)
           
protected  HashFormat newHashFormatInstance(Class clazz)
           
 void setFormatClassNames(Map<String,String> formatClassNames)
          Sets the hash-format-alias-to-fullyQualifiedHashFormatClassNameImplementation map to be used in the getInstance(String) implementation.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultHashFormatFactory

public DefaultHashFormatFactory()
Method Detail

getFormatClassNames

public Map<String,String> getFormatClassNames()
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.

getSearchPackages

public Set<String> getSearchPackages()
Returns 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.

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 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

getInstance

public HashFormat getInstance(String in)
Specified by:
getInstance in interface HashFormatFactory

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:

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.

lookupHashFormatClass

protected Class lookupHashFormatClass(String name)

assertHashFormatImpl

protected final void assertHashFormatImpl(Class clazz)

newHashFormatInstance

protected final HashFormat newHashFormatInstance(Class clazz)


Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.