Class IdentityResolver

java.lang.Object
dev.orne.beans.IdentityResolver

@API(status=STABLE, since="0.1") public class IdentityResolver extends Object

Identity resolver that converts an instance of Identity of unknown type to an instance of a concrete type.

The target identity type must have a public constructor that accepts a single String argument. If such constructor is not an identity token resolution constructor a public, static method that accepts a single String argument and returns instances of identity type can be annotated with IdentityTokenResolver to provide an alternative identity token resolution method.

If no constructor and no static method is valid a warning is logged and every future resolution attempt for that identity type will fail.

Valid examples:

 class MyIdentity
 implements Identity {
   ...
   public MyIdentity(String token)
   throws UnrecognizedIdentityTokenException {
     ...
   }
   ...
 }
 
 class MyIdentity
 implements Identity {
   ...
   public MyIdentity(String notAToken) {
     ...
   }
   ...
   @IdentityTokenResolver
   public static MyIdentity resolve(String identityToken)
   throws UnrecognizedIdentityTokenException {
     // Resolve identity token
   }
   ...
 }
 
Since:
0.1
Version:
1.0, 2020-05
Author:
(w) Iker Hernaez
See Also:
  • Constructor Details

    • IdentityResolver

      protected IdentityResolver()
      Creates a new instance.
  • Method Details

    • getInstance

      @NotNull public static @NotNull IdentityResolver getInstance()
      Returns the shared, singleton instance.
      Returns:
      The singleton instance.
    • getCache

      @NotNull protected @NotNull IdentityResolver.Cache getCache()
      Returns the cache to be used by this instance.
      Returns:
      The cache to be used by this instance
    • setCache

      @NotNull protected @NotNull IdentityResolver setCache(IdentityResolver.Cache cache)

      Sets the identity type resolve methods cache for this instance. If cache is null shared cache will be used.

      Parameters:
      cache - The cache to be used by this instance
      Returns:
      This instance for method chaining
    • resolve

      public <T extends Identity> T resolve(Identity identity, @NotNull @NotNull Class<T> targetType) throws UnrecognizedIdentityTokenException
      Resolves the specified source identity to an identity of the specified target type.
      Type Parameters:
      T - The target identity type
      Parameters:
      identity - The source identity
      targetType - The target identity type
      Returns:
      An instance of target identity type or null if source identity is null or invalid
      Throws:
      UnrecognizedIdentityTokenException - If the source identity's token cannot be resolved to target identity type
    • resolve

      public <T extends Identity> T resolve(String identityToken, @NotNull @NotNull Class<T> targetType) throws UnrecognizedIdentityTokenException
      Resolves the specified source identity token to an identity of the specified target type.
      Type Parameters:
      T - The target identity type
      Parameters:
      identityToken - The source identity token
      targetType - The target identity type
      Returns:
      An instance of target identity type or null if source identity token is null
      Throws:
      UnrecognizedIdentityTokenException - If the source identity token cannot be resolved to target identity type
    • getResolver

      @NotNull protected @NotNull Executable getResolver(@NotNull @NotNull Class<? extends Identity> targetType) throws IdentityResolver.UnresolvableIdentityException
      Returns the method or constructor to be used to resolve identity tokens for the specified target identity type.
      Parameters:
      targetType - The target identity type
      Returns:
      The identity type resolution method or constructor
      Throws:
      IdentityResolver.UnresolvableIdentityException - If the identity type is misconfigured
    • findTokenResolverMethod

      protected Method findTokenResolverMethod(@NotNull @NotNull Class<?> targetType) throws IdentityResolver.UnresolvableIdentityException
      Finds a method annotated with IdentityTokenResolver in the specified target identity type. The method must be public and static, have a single String argument and return a
      Parameters:
      targetType - The target identity type
      Returns:
      The found identity token resolution method
      Throws:
      IdentityResolver.UnresolvableIdentityException - If the annotated method doesn't fulfill the requirements
      SecurityException - If a security exception occurs accessing the class methods
      See Also:
    • findTokenConstructor

      protected <T> Constructor<T> findTokenConstructor(@NotNull @NotNull Class<T> targetType) throws IdentityResolver.UnresolvableIdentityException
      Finds an identity token constructor in the specified target identity type. The constructor must be public and have a single String argument.
      Type Parameters:
      T - The target identity type
      Parameters:
      targetType - The target identity type
      Returns:
      The found identity token constructor
      Throws:
      IdentityResolver.UnresolvableIdentityException - If no constructor is found or it doesn't fulfill the requirements
      SecurityException - If a security exception occurs accessing the constructor