Search in sources :

Example 1 with AbstractInvocationHandler

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);
}
Also used : TException(org.apache.thrift.TException) Method(java.lang.reflect.Method) IOException(java.io.IOException) IMetaStoreClient(org.apache.hadoop.hive.metastore.IMetaStoreClient) AbstractInvocationHandler(com.google.common.reflect.AbstractInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) AbstractInvocationHandler(com.google.common.reflect.AbstractInvocationHandler)

Example 2 with AbstractInvocationHandler

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);
    }
}
Also used : AbstractInvocationHandler(com.google.common.reflect.AbstractInvocationHandler) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

AbstractInvocationHandler (com.google.common.reflect.AbstractInvocationHandler)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 IOException (java.io.IOException)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 IMetaStoreClient (org.apache.hadoop.hive.metastore.IMetaStoreClient)1 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)1 TException (org.apache.thrift.TException)1