mirror of https://github.com/apache/jclouds.git
reveal underlying exception that causes a guice provider to fail
This commit is contained in:
parent
f96638ff44
commit
41e4a369e7
|
@ -21,6 +21,7 @@ package org.jclouds.concurrent.internal;
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -41,6 +42,7 @@ import org.jclouds.util.Throwables2;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
@ -158,7 +160,11 @@ public class SyncProxy implements InvocationHandler {
|
||||||
}
|
}
|
||||||
return returnVal;
|
return returnVal;
|
||||||
} else if (syncMethodMap.containsKey(method)) {
|
} else if (syncMethodMap.containsKey(method)) {
|
||||||
|
try {
|
||||||
return syncMethodMap.get(method).invoke(delegate, args);
|
return syncMethodMap.get(method).invoke(delegate, args);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
throw Throwables.propagate(e.getCause());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return ((ListenableFuture<?>) methodMap.get(method).invoke(delegate, args)).get(timeoutMap.get(method),
|
return ((ListenableFuture<?>) methodMap.get(method).invoke(delegate, args)).get(timeoutMap.get(method),
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.location.suppliers.derived;
|
package org.jclouds.location.suppliers.derived;
|
||||||
|
|
||||||
|
import java.lang.reflect.UndeclaredThrowableException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -28,6 +29,7 @@ import org.jclouds.location.Region;
|
||||||
import org.jclouds.location.suppliers.RegionIdsSupplier;
|
import org.jclouds.location.suppliers.RegionIdsSupplier;
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,6 +47,10 @@ public class RegionIdsFromRegionIdToURIKeySet implements RegionIdsSupplier {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> get() {
|
public Set<String> get() {
|
||||||
|
try {
|
||||||
return regionIdToURISupplier.get().keySet();
|
return regionIdToURISupplier.get().keySet();
|
||||||
|
} catch (UndeclaredThrowableException e) {
|
||||||
|
throw Throwables.propagate(e.getCause());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -22,7 +22,6 @@ import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
@ -49,12 +48,14 @@ import com.google.common.base.Function;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.util.concurrent.Futures;
|
import com.google.common.util.concurrent.Futures;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import com.google.inject.Binding;
|
import com.google.inject.Binding;
|
||||||
|
import com.google.inject.ConfigurationException;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import com.google.inject.Key;
|
import com.google.inject.Key;
|
||||||
|
@ -176,8 +177,10 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
|
||||||
try {
|
try {
|
||||||
Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent);
|
Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent);
|
||||||
return getInstanceOfTypeWithQualifier(genericReturnType, qualifier);
|
return getInstanceOfTypeWithQualifier(genericReturnType, qualifier);
|
||||||
} catch (NoSuchElementException e) {
|
} catch (ProvisionException e) {
|
||||||
return getInstanceOfType(genericReturnType);
|
throw Throwables.propagate(e.getCause());
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return instanceOfTypeOrPropagate(genericReturnType, e);
|
||||||
}
|
}
|
||||||
} catch (ProvisionException e) {
|
} catch (ProvisionException e) {
|
||||||
AuthorizationException aex = Throwables2.getFirstThrowableOfType(e, AuthorizationException.class);
|
AuthorizationException aex = Throwables2.getFirstThrowableOfType(e, AuthorizationException.class);
|
||||||
|
@ -188,7 +191,8 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: tidy
|
// TODO: tidy
|
||||||
private Object getInstanceOfType(Type genericReturnType) {
|
private Object instanceOfTypeOrPropagate(Type genericReturnType, RuntimeException e) {
|
||||||
|
try {
|
||||||
// look for an existing binding
|
// look for an existing binding
|
||||||
Binding<?> binding = injector.getExistingBinding(Key.get(genericReturnType));
|
Binding<?> binding = injector.getExistingBinding(Key.get(genericReturnType));
|
||||||
if (binding != null)
|
if (binding != null)
|
||||||
|
@ -201,6 +205,9 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
|
||||||
|
|
||||||
// else try to create an instance
|
// else try to create an instance
|
||||||
return injector.getInstance(Key.get(genericReturnType));
|
return injector.getInstance(Key.get(genericReturnType));
|
||||||
|
} catch (ConfigurationException ce) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: tidy
|
// TODO: tidy
|
||||||
|
|
|
@ -73,7 +73,6 @@ import org.jclouds.providers.ProviderMetadata;
|
||||||
import org.jclouds.rest.RestApiMetadata;
|
import org.jclouds.rest.RestApiMetadata;
|
||||||
import org.jclouds.rest.config.CredentialStoreModule;
|
import org.jclouds.rest.config.CredentialStoreModule;
|
||||||
import org.jclouds.util.Strings2;
|
import org.jclouds.util.Strings2;
|
||||||
import org.testng.annotations.Test;
|
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
|
|
|
@ -43,6 +43,7 @@ import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
@ -2476,6 +2477,10 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
|
||||||
@Provides
|
@Provides
|
||||||
Set<String> exception();
|
Set<String> exception();
|
||||||
|
|
||||||
|
@Named("NoSuchElementException")
|
||||||
|
@Provides
|
||||||
|
Set<String> noSuchElementException();
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/")
|
@Path("/")
|
||||||
void oneForm(@PathParam("bucket") String path);
|
void oneForm(@PathParam("bucket") String path);
|
||||||
|
@ -2498,6 +2503,11 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
|
||||||
injector.getInstance(AsyncClientFactory.class).create(TestClassForm.class).exception();
|
injector.getInstance(AsyncClientFactory.class).create(TestClassForm.class).exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = NoSuchElementException.class)
|
||||||
|
public void testProvidesWithGenericQualifiedNoSuchElementException() throws SecurityException, NoSuchMethodException {
|
||||||
|
injector.getInstance(AsyncClientFactory.class).create(TestClassForm.class).noSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildOneClassForm() throws SecurityException, NoSuchMethodException {
|
public void testBuildOneClassForm() throws SecurityException, NoSuchMethodException {
|
||||||
Method oneForm = TestClassForm.class.getMethod("oneForm", String.class);
|
Method oneForm = TestClassForm.class.getMethod("oneForm", String.class);
|
||||||
|
@ -2665,6 +2675,12 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
|
||||||
throw new AuthorizationException();
|
throw new AuthorizationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Named("NoSuchElementException")
|
||||||
|
Set<String> noSuchElementException() {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
})).buildInjector();
|
})).buildInjector();
|
||||||
parserFactory = injector.getInstance(ParseSax.Factory.class);
|
parserFactory = injector.getInstance(ParseSax.Factory.class);
|
||||||
crypto = injector.getInstance(Crypto.class);
|
crypto = injector.getInstance(Crypto.class);
|
||||||
|
|
Loading…
Reference in New Issue