mirror of https://github.com/apache/jclouds.git
Issue 731: unravel dependency cycle to only use interfaces
This commit is contained in:
parent
de1e8a7ce3
commit
63320f15b6
|
@ -30,6 +30,7 @@ import org.jclouds.cloudloadbalancers.CloudLoadBalancersClient;
|
|||
import org.jclouds.cloudloadbalancers.config.CloudLoadBalancersRestClientModule;
|
||||
import org.jclouds.cloudloadbalancers.functions.ConvertLB;
|
||||
import org.jclouds.cloudloadbalancers.reference.Region;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.RequiresHttp;
|
||||
import org.jclouds.internal.ClassMethodArgs;
|
||||
|
@ -78,7 +79,7 @@ public abstract class BaseCloudLoadBalancersAsyncClientTest<T> extends RestClien
|
|||
install(new OpenStackAuthenticationModule() {
|
||||
@Override
|
||||
protected Supplier<AuthenticationResponse> provideAuthenticationResponseSupplier(
|
||||
final LoadingCache<String,AuthenticationResponse> cache) {
|
||||
LoadingCache<Credentials, AuthenticationResponse> cache, Credentials in) {
|
||||
return Suppliers.ofInstance(new AuthenticationResponse("token", ImmutableMap.<String, URI> of()));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -29,14 +29,17 @@ import javax.inject.Named;
|
|||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.Constants;
|
||||
import org.jclouds.concurrent.RetryOnTimeOutExceptionSupplier;
|
||||
import org.jclouds.concurrent.RetryOnTimeOutExceptionFunction;
|
||||
import org.jclouds.date.TimeStamp;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.http.RequiresHttp;
|
||||
import org.jclouds.location.Provider;
|
||||
import org.jclouds.openstack.Authentication;
|
||||
import org.jclouds.openstack.OpenStackAuthAsyncClient;
|
||||
import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
|
||||
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;
|
||||
|
@ -46,6 +49,7 @@ 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;
|
||||
|
||||
/**
|
||||
* Configures the Rackspace authentication service connection, including logging and http transport.
|
||||
|
@ -57,6 +61,8 @@ public class OpenStackAuthenticationModule extends AbstractModule {
|
|||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(new TypeLiteral<Function<Credentials, AuthenticationResponse>>() {
|
||||
}).to(GetAuthenticationResponse.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,68 +80,57 @@ public class OpenStackAuthenticationModule extends AbstractModule {
|
|||
};
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Provider
|
||||
protected Credentials provideAuthenticationCredentials(@Named(Constants.PROPERTY_IDENTITY) String user,
|
||||
@Named(Constants.PROPERTY_CREDENTIAL) String key) {
|
||||
return new Credentials(user, key);
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class GetAuthenticationResponse implements Supplier<AuthenticationResponse> {
|
||||
protected final OpenStackAuthAsyncClient client;
|
||||
protected final String user;
|
||||
protected final String key;
|
||||
public static class GetAuthenticationResponse extends
|
||||
RetryOnTimeOutExceptionFunction<Credentials, AuthenticationResponse> {
|
||||
|
||||
@Inject
|
||||
public GetAuthenticationResponse(AsyncClientFactory factory, @Named(Constants.PROPERTY_IDENTITY) String user,
|
||||
@Named(Constants.PROPERTY_CREDENTIAL) String key) {
|
||||
this.client = factory.create(OpenStackAuthAsyncClient.class);
|
||||
this.user = user;
|
||||
this.key = key;
|
||||
}
|
||||
public GetAuthenticationResponse(final AsyncClientFactory factory) {
|
||||
super(new Function<Credentials, AuthenticationResponse>() {
|
||||
|
||||
@Override
|
||||
public AuthenticationResponse get() {
|
||||
try {
|
||||
Future<AuthenticationResponse> response = authenticate();
|
||||
return response.get(30, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
Throwables.propagate(e);
|
||||
assert false : e;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public AuthenticationResponse apply(Credentials input) {
|
||||
try {
|
||||
Future<AuthenticationResponse> response = factory.create(OpenStackAuthAsyncClient.class)
|
||||
.authenticate(input.identity, input.credential);
|
||||
return response.get(30, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
throw Throwables.propagate(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
protected Future<AuthenticationResponse> authenticate() {
|
||||
return client.authenticate(user, key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public LoadingCache<String,AuthenticationResponse> provideAuthenticationResponseCache2(
|
||||
final GetAuthenticationResponse getAuthenticationResponse) {
|
||||
|
||||
final RetryOnTimeOutExceptionSupplier<AuthenticationResponse> delegate =
|
||||
new RetryOnTimeOutExceptionSupplier<AuthenticationResponse>(getAuthenticationResponse);
|
||||
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));
|
||||
|
||||
CacheLoader<String, AuthenticationResponse> cacheLoader = new CacheLoader<String, AuthenticationResponse>() {
|
||||
@Override
|
||||
public AuthenticationResponse load(String key) throws Exception {
|
||||
return delegate.get();
|
||||
}
|
||||
};
|
||||
|
||||
LoadingCache<String, AuthenticationResponse> cache = CacheBuilder.newBuilder().expireAfterWrite(23, TimeUnit.HOURS)
|
||||
.build(cacheLoader);
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected Supplier<AuthenticationResponse> provideAuthenticationResponseSupplier(
|
||||
final LoadingCache<String,AuthenticationResponse> cache) {
|
||||
final LoadingCache<Credentials, AuthenticationResponse> cache, @Provider final Credentials creds) {
|
||||
return new Supplier<AuthenticationResponse>() {
|
||||
@Override
|
||||
public AuthenticationResponse get() {
|
||||
try {
|
||||
return cache.get("key");
|
||||
return cache.get(creds);
|
||||
} catch (UncheckedExecutionException e) {
|
||||
throw Throwables.propagate(e.getCause());
|
||||
} catch (ExecutionException e) {
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.io.IOException;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.http.HttpCommand;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.HttpRetryHandler;
|
||||
|
@ -48,12 +49,8 @@ public class RetryOnRenew implements HttpRetryHandler {
|
|||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
// This doesn't work yet
|
||||
// @Inject
|
||||
// Supplier<AuthenticationResponse> providedAuthenticationResponseCache;
|
||||
|
||||
@Inject
|
||||
LoadingCache<String,AuthenticationResponse> authenticationResponseCache;
|
||||
LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache;
|
||||
|
||||
@Override
|
||||
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
|
||||
|
@ -63,8 +60,8 @@ public class RetryOnRenew implements HttpRetryHandler {
|
|||
case 401:
|
||||
// Do not retry on 401 from authentication request
|
||||
Multimap<String, String> headers = command.getCurrentRequest().getHeaders();
|
||||
if (headers != null && headers.containsKey(AuthHeaders.AUTH_USER) && headers.containsKey(AuthHeaders.AUTH_KEY) &&
|
||||
!headers.containsKey(AuthHeaders.AUTH_TOKEN)) {
|
||||
if (headers != null && headers.containsKey(AuthHeaders.AUTH_USER)
|
||||
&& headers.containsKey(AuthHeaders.AUTH_KEY) && !headers.containsKey(AuthHeaders.AUTH_TOKEN)) {
|
||||
retry = false;
|
||||
} else {
|
||||
String content = parsePayloadOrNull(response);
|
||||
|
@ -79,12 +76,12 @@ public class RetryOnRenew implements HttpRetryHandler {
|
|||
break;
|
||||
}
|
||||
return retry;
|
||||
|
||||
|
||||
} finally {
|
||||
releasePayload(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String parsePayloadOrNull(HttpResponse response) {
|
||||
if (response.getPayload() != null) {
|
||||
try {
|
||||
|
|
|
@ -18,13 +18,14 @@
|
|||
*/
|
||||
package org.jclouds.openstack.handlers;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.classextension.EasyMock.createMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.easymock.classextension.EasyMock.verify;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.http.HttpCommand;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
|
@ -47,23 +48,23 @@ public class RetryOnRenewTest {
|
|||
HttpRequest request = createMock(HttpRequest.class);
|
||||
HttpResponse response = createMock(HttpResponse.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
LoadingCache<String,AuthenticationResponse> cache = createMock(LoadingCache.class);
|
||||
LoadingCache<Credentials, AuthenticationResponse> cache = createMock(LoadingCache.class);
|
||||
|
||||
expect(command.getCurrentRequest()).andReturn(request);
|
||||
|
||||
cache.invalidateAll();
|
||||
expectLastCall();
|
||||
|
||||
|
||||
expect(response.getPayload()).andReturn(Payloads.newStringPayload("token expired, please renew")).anyTimes();
|
||||
expect(response.getStatusCode()).andReturn(401).atLeastOnce();
|
||||
|
||||
replay(command);
|
||||
replay(response);
|
||||
replay(cache);
|
||||
|
||||
|
||||
RetryOnRenew retry = new RetryOnRenew();
|
||||
retry.authenticationResponseCache = cache;
|
||||
|
||||
|
||||
assertTrue(retry.shouldRetryRequest(command, response));
|
||||
|
||||
verify(command);
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jclouds.concurrent;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Throwables.propagate;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.jclouds.util.Throwables2;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ForwardingObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class RetryOnTimeOutExceptionFunction<K,V> extends ForwardingObject implements Function<K,V>{
|
||||
private final Function<K,V> delegate;
|
||||
|
||||
public RetryOnTimeOutExceptionFunction(Function<K,V> delegate) {
|
||||
this.delegate = checkNotNull(delegate, "delegate");
|
||||
}
|
||||
|
||||
//TODO: backoff limited retry handler
|
||||
@Override
|
||||
public V apply(K key) {
|
||||
TimeoutException ex = null;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
ex = null;
|
||||
return delegate().apply(key);
|
||||
} catch (Exception e) {
|
||||
if ((ex = Throwables2.getFirstThrowableOfType(e, TimeoutException.class)) != null)
|
||||
continue;
|
||||
throw propagate(e);
|
||||
}
|
||||
}
|
||||
if (ex != null)
|
||||
throw propagate(ex);
|
||||
assert false;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return delegate.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return delegate.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<K,V> delegate() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,126 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jclouds.concurrent;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.classextension.EasyMock.createMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.easymock.classextension.EasyMock.verify;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.jclouds.rest.AuthorizationException;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
/**
|
||||
* Tests behavior of RetryOnTimeOutExceptionFunction
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit", singleThreaded = true, testName = "RetryOnTimeOutExceptionFunctionTest")
|
||||
public class RetryOnTimeOutExceptionFunctionTest {
|
||||
ExecutorService executorService = MoreExecutors.sameThreadExecutor();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testGetThrowsOriginalExceptionButRetriesOnTimeoutException() throws InterruptedException, ExecutionException {
|
||||
Function<String, String> delegate = createMock(Function.class);
|
||||
TimeoutException timeout = createMock(TimeoutException.class);
|
||||
RuntimeException throwable = new RuntimeException(timeout);
|
||||
|
||||
expect(delegate.apply("baz")).andThrow(throwable);
|
||||
expect(timeout.getCause()).andReturn(null).anyTimes();
|
||||
expect(delegate.apply("baz")).andThrow(throwable);
|
||||
expect(delegate.apply("baz")).andThrow(throwable);
|
||||
|
||||
replay(delegate);
|
||||
replay(timeout);
|
||||
|
||||
RetryOnTimeOutExceptionFunction<String, String> supplier = new RetryOnTimeOutExceptionFunction<String, String>(
|
||||
delegate);
|
||||
try {
|
||||
supplier.apply("baz");
|
||||
assert false;
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals(e.getCause(), timeout);
|
||||
}
|
||||
|
||||
verify(delegate);
|
||||
verify(timeout);
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testGetAllowsTwoFailuresOnTimeoutException() throws InterruptedException, ExecutionException {
|
||||
Function<String, String> delegate = createMock(Function.class);
|
||||
TimeoutException timeout = createMock(TimeoutException.class);
|
||||
RuntimeException throwable = new RuntimeException(timeout);
|
||||
|
||||
expect(delegate.apply("baz")).andThrow(throwable);
|
||||
expect(timeout.getCause()).andReturn(null).anyTimes();
|
||||
expect(delegate.apply("baz")).andThrow(throwable);
|
||||
expect(delegate.apply("baz")).andReturn("foo");
|
||||
|
||||
replay(delegate);
|
||||
replay(timeout);
|
||||
|
||||
RetryOnTimeOutExceptionFunction<String, String> supplier = new RetryOnTimeOutExceptionFunction<String, String>(
|
||||
delegate);
|
||||
assertEquals(supplier.apply("baz"), "foo");
|
||||
|
||||
verify(delegate);
|
||||
verify(timeout);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testGetAllowsNoFailuresOnOtherExceptions() throws InterruptedException, ExecutionException {
|
||||
Function<String, String> delegate = createMock(Function.class);
|
||||
AuthorizationException auth = createMock(AuthorizationException.class);
|
||||
RuntimeException throwable = new RuntimeException(auth);
|
||||
|
||||
expect(delegate.apply("baz")).andThrow(throwable);
|
||||
expect(auth.getCause()).andReturn(null).anyTimes();
|
||||
|
||||
|
||||
replay(delegate);
|
||||
replay(auth);
|
||||
|
||||
RetryOnTimeOutExceptionFunction<String, String> supplier = new RetryOnTimeOutExceptionFunction<String, String>(
|
||||
delegate);
|
||||
|
||||
try {
|
||||
supplier.apply("baz");
|
||||
assert false;
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals(e.getCause(), auth);
|
||||
}
|
||||
|
||||
verify(delegate);
|
||||
verify(auth);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue