Issue 731: code cleanup

This commit is contained in:
Adrian Cole 2012-01-15 11:43:08 -08:00
parent 63320f15b6
commit a14ad27258
3 changed files with 24 additions and 34 deletions

View File

@ -18,6 +18,9 @@
*/ */
package org.jclouds.openstack.config; package org.jclouds.openstack.config;
import static com.google.common.base.Suppliers.memoizeWithExpiration;
import static com.google.common.base.Throwables.propagate;
import java.util.Date; import java.util.Date;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -41,12 +44,9 @@ import org.jclouds.rest.AsyncClientFactory;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
@ -102,9 +102,14 @@ public class OpenStackAuthenticationModule extends AbstractModule {
.authenticate(input.identity, input.credential); .authenticate(input.identity, input.credential);
return response.get(30, TimeUnit.SECONDS); return response.get(30, TimeUnit.SECONDS);
} catch (Exception e) { } catch (Exception e) {
throw Throwables.propagate(e); throw propagate(e);
} }
} }
@Override
public String toString() {
return "authenticate()";
}
}); });
} }
@ -113,13 +118,9 @@ public class OpenStackAuthenticationModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
public LoadingCache<Credentials, AuthenticationResponse> provideAuthenticationResponseCache2( public LoadingCache<Credentials, AuthenticationResponse> provideAuthenticationResponseCache2(
final Function<Credentials, AuthenticationResponse> getAuthenticationResponse, Function<Credentials, AuthenticationResponse> getAuthenticationResponse) {
@Provider final Credentials creds) { return CacheBuilder.newBuilder().expireAfterWrite(23, TimeUnit.HOURS).build(
CacheLoader.from(getAuthenticationResponse));
LoadingCache<Credentials, AuthenticationResponse> cache = CacheBuilder.newBuilder().expireAfterWrite(23,
TimeUnit.HOURS).build(CacheLoader.from(getAuthenticationResponse));
return cache;
} }
@Provides @Provides
@ -131,10 +132,8 @@ public class OpenStackAuthenticationModule extends AbstractModule {
public AuthenticationResponse get() { public AuthenticationResponse get() {
try { try {
return cache.get(creds); return cache.get(creds);
} catch (UncheckedExecutionException e) {
throw Throwables.propagate(e.getCause());
} catch (ExecutionException e) { } catch (ExecutionException e) {
throw Throwables.propagate(e.getCause()); throw propagate(e.getCause());
} }
} }
}; };
@ -144,7 +143,7 @@ public class OpenStackAuthenticationModule extends AbstractModule {
@Singleton @Singleton
@TimeStamp @TimeStamp
protected Supplier<Date> provideCacheBusterDate() { protected Supplier<Date> provideCacheBusterDate() {
return Suppliers.memoizeWithExpiration(new Supplier<Date>() { return memoizeWithExpiration(new Supplier<Date>() {
public Date get() { public Date get() {
return new Date(); return new Date();
} }

View File

@ -18,10 +18,9 @@
*/ */
package org.jclouds.openstack.handlers; package org.jclouds.openstack.handlers;
import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream;
import static org.jclouds.http.HttpUtils.releasePayload; import static org.jclouds.http.HttpUtils.releasePayload;
import java.io.IOException;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
@ -31,7 +30,6 @@ import org.jclouds.http.HttpRetryHandler;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse; import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
import org.jclouds.openstack.reference.AuthHeaders; import org.jclouds.openstack.reference.AuthHeaders;
import org.jclouds.util.Strings2;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
@ -49,8 +47,12 @@ public class RetryOnRenew implements HttpRetryHandler {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache;
@Inject @Inject
LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache; protected RetryOnRenew(LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache) {
this.authenticationResponseCache = authenticationResponseCache;
}
@Override @Override
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
@ -64,9 +66,9 @@ public class RetryOnRenew implements HttpRetryHandler {
&& headers.containsKey(AuthHeaders.AUTH_KEY) && !headers.containsKey(AuthHeaders.AUTH_TOKEN)) { && headers.containsKey(AuthHeaders.AUTH_KEY) && !headers.containsKey(AuthHeaders.AUTH_TOKEN)) {
retry = false; retry = false;
} else { } else {
String content = parsePayloadOrNull(response); byte[] content = closeClientButKeepContentStream(response);
if (content != null && content.contains("lease renew")) { if (content != null && new String(content).contains("lease renew")) {
// Otherwise invalidate the token cache, to force reauthentication logger.debug("invalidating authentication token");
authenticationResponseCache.invalidateAll(); authenticationResponseCache.invalidateAll();
retry = true; retry = true;
} else { } else {
@ -82,14 +84,4 @@ public class RetryOnRenew implements HttpRetryHandler {
} }
} }
String parsePayloadOrNull(HttpResponse response) {
if (response.getPayload() != null) {
try {
return Strings2.toStringAndClose(response.getPayload().getInput());
} catch (IOException e) {
logger.warn(e, "exception reading error from response", response);
}
}
return null;
}
} }

View File

@ -62,8 +62,7 @@ public class RetryOnRenewTest {
replay(response); replay(response);
replay(cache); replay(cache);
RetryOnRenew retry = new RetryOnRenew(); RetryOnRenew retry = new RetryOnRenew(cache);
retry.authenticationResponseCache = cache;
assertTrue(retry.shouldRetryRequest(command, response)); assertTrue(retry.shouldRetryRequest(command, response));