Package dev.orne.beans
Class IdentityResolver
java.lang.Object
dev.orne.beans.IdentityResolver
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceInterface for identity resolve methods cache.static classException for misconfigured identity types.protected static classImplementation ofCachebased onWeakHashMap. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> Constructor<T>findTokenConstructor(@NotNull Class<T> targetType) Finds an identity token constructor in the specified target identity type.protected MethodfindTokenResolverMethod(@NotNull Class<?> targetType) Finds a method annotated withIdentityTokenResolverin the specified target identity type.protected @NotNull IdentityResolver.CachegetCache()Returns the cache to be used by this instance.static @NotNull IdentityResolverReturns the shared, singleton instance.protected @NotNull ExecutablegetResolver(@NotNull Class<? extends Identity> targetType) Returns the method or constructor to be used to resolve identity tokens for the specified target identity type.<T extends Identity>
TResolves the specified source identity to an identity of the specified target type.<T extends Identity>
TResolves the specified source identity token to an identity of the specified target type.protected @NotNull IdentityResolversetCache(IdentityResolver.Cache cache) Sets the identity type resolve methods cache for this instance.
-
Constructor Details
-
IdentityResolver
protected IdentityResolver()Creates a new instance.
-
-
Method Details
-
getInstance
Returns the shared, singleton instance.- Returns:
- The singleton instance.
-
getCache
Returns the cache to be used by this instance.- Returns:
- The cache to be used by this instance
-
setCache
Sets the identity type resolve methods cache for this instance. If
cacheisnullshared 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 identitytargetType- The target identity type- Returns:
- An instance of target identity type or
nullif 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 tokentargetType- The target identity type- Returns:
- An instance of target identity type or
nullif 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 withIdentityTokenResolverin the specified target identity type. The method must be public and static, have a singleStringargument 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 requirementsSecurityException- 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 singleStringargument.- 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 requirementsSecurityException- If a security exception occurs accessing the constructor
-