Improvements to error handling in cache key generation
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/ed4e67c8 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/ed4e67c8 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/ed4e67c8 Branch: refs/heads/master Commit: ed4e67c855a1d1062d0cfa5976d05db6b044f11a Parents: aae8fdf Author: Dave Johnson <[email protected]> Authored: Mon May 23 11:41:44 2016 -0400 Committer: Dave Johnson <[email protected]> Committed: Mon May 23 11:41:44 2016 -0400 ---------------------------------------------------------------------- .../usergrid/security/shiro/ShiroCache.java | 80 ++++++++++++-------- 1 file changed, 49 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/ed4e67c8/stack/services/src/main/java/org/apache/usergrid/security/shiro/ShiroCache.java ---------------------------------------------------------------------- diff --git a/stack/services/src/main/java/org/apache/usergrid/security/shiro/ShiroCache.java b/stack/services/src/main/java/org/apache/usergrid/security/shiro/ShiroCache.java index b4803b1..6c5ac6c 100644 --- a/stack/services/src/main/java/org/apache/usergrid/security/shiro/ShiroCache.java +++ b/stack/services/src/main/java/org/apache/usergrid/security/shiro/ShiroCache.java @@ -184,51 +184,69 @@ public class ShiroCache<K, V> implements Cache<K,V> { String ret = null; - final String typeName = typeRef.getType().getTypeName(); + Throwable throwable = null; - if ( key instanceof SimplePrincipalCollection) { + String errorMessage = null; - SimplePrincipalCollection spc = (SimplePrincipalCollection)key; + try { - if ( spc.getPrimaryPrincipal() instanceof UserPrincipal) { + final String typeName = typeRef.getType().getTypeName(); - // principal is a user, use UUID as cache key - UserPrincipal p = (UserPrincipal) spc.getPrimaryPrincipal(); - ret = p.getUser().getUuid().toString() + "_" + typeName; - } + if (key instanceof SimplePrincipalCollection) { + + SimplePrincipalCollection spc = (SimplePrincipalCollection) key; + + if (spc.getPrimaryPrincipal() instanceof UserPrincipal) { + + // principal is a user, use UUID as cache key + UserPrincipal p = (UserPrincipal) spc.getPrimaryPrincipal(); + ret = p.getUser().getUuid().toString() + "_" + typeName; + + } else if (spc.getPrimaryPrincipal() instanceof PrincipalIdentifier) { - else if ( spc.getPrimaryPrincipal() instanceof PrincipalIdentifier ) { + // principal is not user, try to get something unique as cache key + PrincipalIdentifier p = (PrincipalIdentifier) spc.getPrimaryPrincipal(); + if (p.getAccessTokenCredentials() != null) { + ret = p.getAccessTokenCredentials().getToken() + "_" + typeName; + } else { + ret = p.getApplicationId() + "_" + typeName; + } - // principal is not user, try to get something unique as cache key - PrincipalIdentifier p = (PrincipalIdentifier) spc.getPrimaryPrincipal(); - if (p.getAccessTokenCredentials() != null) { - ret = p.getAccessTokenCredentials().getToken() + "_" + typeName; } else { - ret = p.getApplicationId() + "_" + typeName; + errorMessage = "Unknown principal type: " + key.getClass().getSimpleName(); } - } - } else if ( key instanceof ApplicationGuestPrincipal ) { - ApplicationGuestPrincipal agp = (ApplicationGuestPrincipal) key; - ret = agp.getApplicationId() + "_" + typeName; + } else if (key instanceof ApplicationGuestPrincipal) { + ApplicationGuestPrincipal agp = (ApplicationGuestPrincipal) key; + ret = agp.getApplicationId() + "_" + typeName; + + } else if (key instanceof ApplicationPrincipal) { + ApplicationPrincipal ap = (ApplicationPrincipal) key; + ret = ap.getApplicationId() + "_" + typeName; + + } else if (key instanceof OrganizationPrincipal) { + OrganizationPrincipal op = (OrganizationPrincipal) key; + ret = op.getOrganizationId() + "_" + typeName; - } else if ( key instanceof ApplicationPrincipal ) { - ApplicationPrincipal ap = (ApplicationPrincipal) key; - ret = ap.getApplicationId() + "_" + typeName; + } else if (key instanceof UserPrincipal) { + UserPrincipal up = (UserPrincipal) key; + ret = up.getUser().getUuid() + "_" + typeName; - } else if ( key instanceof OrganizationPrincipal ) { - OrganizationPrincipal op = (OrganizationPrincipal) key; - ret = op.getOrganizationId() + "_" + typeName; + } else { + errorMessage = "Unknown key type: " + key.getClass().getSimpleName(); + } + + } catch ( Throwable t ) { + throwable = t; + } - } else if ( key instanceof UserPrincipal ) { - UserPrincipal up = (UserPrincipal)key; - ret = up.getUser().getUuid() + "_" + typeName; + if ( throwable != null ) { + errorMessage = "Error generating cache key for key type " + key.getClass().getSimpleName(); + throw new CacheException( errorMessage, throwable ); } - if ( ret == null) { - String msg = "Unknown key type: " + key.getClass().getSimpleName(); - logger.error(msg); - throw new RuntimeException(msg); + if ( ret == null ) { + throw new CacheException( errorMessage ); } return ret;
