From 5c0b4b04cabdc85ce4e67cd820e0b583c9c9b092 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 1 Oct 2015 02:50:19 +0200 Subject: [PATCH] Remove use of com.google.common.cache.* This commit removes the use of com.google.common.cache.Cache, com.google.common.cache.CacheBuilder and com.google.common.cache.CacheLoader. Relates elastic/elasticsearchelastic/elasticsearch#13224, elastic/elasticsearchelastic/elasticsearch#13717 Original commit: elastic/x-pack-elasticsearch@04c254ee95aed16e5d6e0182d43f1d66c49f6315 --- .../support/CachingUsernamePasswordRealm.java | 61 ++++++++----------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java b/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java index 8c01081eda5..9e36ff79ef9 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java @@ -5,15 +5,14 @@ */ package org.elasticsearch.shield.authc.support; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.util.concurrent.UncheckedExecutionException; +import org.elasticsearch.common.cache.Cache; +import org.elasticsearch.common.cache.CacheBuilder; +import org.elasticsearch.common.cache.CacheLoader; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.shield.User; import org.elasticsearch.shield.authc.RealmConfig; import org.elasticsearch.shield.support.Exceptions; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -34,9 +33,9 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm hasher = Hasher.resolve(config.settings().get(CACHE_HASH_ALGO_SETTING, null), Hasher.SSHA256); TimeValue ttl = config.settings().getAsTime(CACHE_TTL_SETTING, DEFAULT_TTL); if (ttl.millis() > 0) { - cache = CacheBuilder.newBuilder() - .expireAfterWrite(ttl.getMillis(), TimeUnit.MILLISECONDS) - .maximumSize(config.settings().getAsInt(CACHE_MAX_USERS_SETTING, DEFAULT_MAX_USERS)) + cache = CacheBuilder.builder() + .setExpireAfterAccess(TimeUnit.MILLISECONDS.toNanos(ttl.getMillis())) + .setMaximumWeight(config.settings().getAsInt(CACHE_MAX_USERS_SETTING, DEFAULT_MAX_USERS)) .build(); } else { cache = null; @@ -69,22 +68,19 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm return doAuthenticate(token); } - Callable callback = new Callable() { - @Override - public UserWithHash call() throws Exception { - if (logger.isDebugEnabled()) { - logger.debug("user not found in cache, proceeding with normal authentication"); - } - User user = doAuthenticate(token); - if (user == null) { - throw Exceptions.authenticationError("could not authenticate [{}]", token.principal()); - } - return new UserWithHash(user, token.credentials(), hasher); + CacheLoader callback = key -> { + if (logger.isDebugEnabled()) { + logger.debug("user not found in cache, proceeding with normal authentication"); } + User user = doAuthenticate(token); + if (user == null) { + throw Exceptions.authenticationError("could not authenticate [{}]", token.principal()); + } + return new UserWithHash(user, token.credentials(), hasher); }; try { - UserWithHash userWithHash = cache.get(token.principal(), callback); + UserWithHash userWithHash = cache.computeIfAbsent(token.principal(), callback); final boolean hadHash = userWithHash.hasHash(); if (hadHash) { if (userWithHash.verify(token.credentials())) { @@ -96,7 +92,7 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm } //this handles when a user's password has changed or the user was looked up for run as and not authenticated expire(token.principal()); - userWithHash = cache.get(token.principal(), callback); + userWithHash = cache.computeIfAbsent(token.principal(), callback); if (logger.isDebugEnabled()) { if (hadHash) { @@ -107,7 +103,7 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm } return userWithHash.user; - } catch (ExecutionException | UncheckedExecutionException ee) { + } catch (ExecutionException ee) { if (logger.isTraceEnabled()) { logger.trace("realm [" + type() + "] could not authenticate [" + token.principal() + "]", ee); } else if (logger.isDebugEnabled()) { @@ -123,24 +119,21 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm return null; } - Callable callback = new Callable() { - @Override - public UserWithHash call() throws Exception { - if (logger.isDebugEnabled()) { - logger.debug("user not found in cache, proceeding with normal lookup"); - } - User user = doLookupUser(username); - if (user == null) { - throw Exceptions.authenticationError("could not lookup [{}]", username); - } - return new UserWithHash(user, null, null); + CacheLoader callback = key -> { + if (logger.isDebugEnabled()) { + logger.debug("user not found in cache, proceeding with normal lookup"); } + User user = doLookupUser(username); + if (user == null) { + throw Exceptions.authenticationError("could not lookup [{}]", username); + } + return new UserWithHash(user, null, null); }; try { - UserWithHash userWithHash = cache.get(username, callback); + UserWithHash userWithHash = cache.computeIfAbsent(username, callback); return userWithHash.user; - } catch (ExecutionException | UncheckedExecutionException ee) { + } catch (ExecutionException ee) { if (logger.isTraceEnabled()) { logger.trace("realm [" + name() + "] could not lookup [" + username + "]", ee); } else if (logger.isDebugEnabled()) {