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;
import static com.google.common.base.Suppliers.memoizeWithExpiration;
import static com.google.common.base.Throwables.propagate;
import java.util.Date;
import java.util.concurrent.ExecutionException;
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.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
@ -102,9 +102,14 @@ public class OpenStackAuthenticationModule extends AbstractModule {
.authenticate(input.identity, input.credential);
return response.get(30, TimeUnit.SECONDS);
} 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
@Singleton
public LoadingCache<Credentials, AuthenticationResponse> provideAuthenticationResponseCache2(
final Function<Credentials, AuthenticationResponse> getAuthenticationResponse,
@Provider final Credentials creds) {
LoadingCache<Credentials, AuthenticationResponse> cache = CacheBuilder.newBuilder().expireAfterWrite(23,
TimeUnit.HOURS).build(CacheLoader.from(getAuthenticationResponse));
return cache;
Function<Credentials, AuthenticationResponse> getAuthenticationResponse) {
return CacheBuilder.newBuilder().expireAfterWrite(23, TimeUnit.HOURS).build(
CacheLoader.from(getAuthenticationResponse));
}
@Provides
@ -131,10 +132,8 @@ public class OpenStackAuthenticationModule extends AbstractModule {
public AuthenticationResponse get() {
try {
return cache.get(creds);
} catch (UncheckedExecutionException e) {
throw Throwables.propagate(e.getCause());
} catch (ExecutionException e) {
throw Throwables.propagate(e.getCause());
throw propagate(e.getCause());
}
}
};
@ -144,7 +143,7 @@ public class OpenStackAuthenticationModule extends AbstractModule {
@Singleton
@TimeStamp
protected Supplier<Date> provideCacheBusterDate() {
return Suppliers.memoizeWithExpiration(new Supplier<Date>() {
return memoizeWithExpiration(new Supplier<Date>() {
public Date get() {
return new Date();
}

View File

@ -18,10 +18,9 @@
*/
package org.jclouds.openstack.handlers;
import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream;
import static org.jclouds.http.HttpUtils.releasePayload;
import java.io.IOException;
import javax.annotation.Resource;
import org.jclouds.domain.Credentials;
@ -31,7 +30,6 @@ import org.jclouds.http.HttpRetryHandler;
import org.jclouds.logging.Logger;
import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
import org.jclouds.openstack.reference.AuthHeaders;
import org.jclouds.util.Strings2;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Multimap;
@ -49,8 +47,12 @@ public class RetryOnRenew implements HttpRetryHandler {
@Resource
protected Logger logger = Logger.NULL;
private final LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache;
@Inject
LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache;
protected RetryOnRenew(LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache) {
this.authenticationResponseCache = authenticationResponseCache;
}
@Override
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)) {
retry = false;
} else {
String content = parsePayloadOrNull(response);
if (content != null && content.contains("lease renew")) {
// Otherwise invalidate the token cache, to force reauthentication
byte[] content = closeClientButKeepContentStream(response);
if (content != null && new String(content).contains("lease renew")) {
logger.debug("invalidating authentication token");
authenticationResponseCache.invalidateAll();
retry = true;
} 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(cache);
RetryOnRenew retry = new RetryOnRenew();
retry.authenticationResponseCache = cache;
RetryOnRenew retry = new RetryOnRenew(cache);
assertTrue(retry.shouldRetryRequest(command, response));