use of com.google.common.reflect.AbstractInvocationHandler in project hive by apache.
the class UgiMetaStoreClientFactory method createProxy.
private IMetaStoreClient createProxy(final IMetaStoreClient delegate, final String user, final UserGroupInformation authenticatedUser) {
InvocationHandler handler = new AbstractInvocationHandler() {
@Override
protected Object handleInvocation(Object proxy, final Method method, final Object[] args) throws Throwable {
try {
if (!I_META_STORE_CLIENT_METHODS.contains(method) || authenticatedUser == null) {
return method.invoke(delegate, args);
}
try {
return authenticatedUser.doAs(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
return method.invoke(delegate, args);
}
});
} catch (IOException | InterruptedException e) {
throw new TException("PrivilegedExceptionAction failed as user '" + user + "'.", e);
}
} catch (UndeclaredThrowableException | InvocationTargetException e) {
throw e.getCause();
}
}
};
ClassLoader classLoader = IMetaStoreClient.class.getClassLoader();
Class<?>[] interfaces = new Class<?>[] { IMetaStoreClient.class };
Object proxy = Proxy.newProxyInstance(classLoader, interfaces, handler);
return IMetaStoreClient.class.cast(proxy);
}
use of com.google.common.reflect.AbstractInvocationHandler in project guava by google.
the class ForwardingWrapperTester method testExceptionPropagation.
private static <T> void testExceptionPropagation(Class<T> interfaceType, Method method, Function<? super T, ? extends T> wrapperFunction) {
final RuntimeException exception = new RuntimeException();
T proxy = Reflection.newProxy(interfaceType, new AbstractInvocationHandler() {
@Override
protected Object handleInvocation(Object p, Method m, Object[] args) throws Throwable {
throw exception;
}
});
T wrapper = wrapperFunction.apply(proxy);
try {
method.invoke(wrapper, getParameterValues(method));
fail(method + " failed to throw exception as is.");
} catch (InvocationTargetException e) {
if (exception != e.getCause()) {
throw new RuntimeException(e);
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
Aggregations