0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 package org.apache.hive.service.cli.session;
0020
0021
0022
0023
0024
0025 import java.lang.reflect.InvocationHandler;
0026 import java.lang.reflect.InvocationTargetException;
0027 import java.lang.reflect.Method;
0028 import java.lang.reflect.Proxy;
0029 import java.lang.reflect.UndeclaredThrowableException;
0030 import java.security.PrivilegedActionException;
0031 import java.security.PrivilegedExceptionAction;
0032
0033 import org.apache.hadoop.security.UserGroupInformation;
0034 import org.apache.hive.service.cli.HiveSQLException;
0035
0036 public class HiveSessionProxy implements InvocationHandler {
0037 private final HiveSession base;
0038 private final UserGroupInformation ugi;
0039
0040 public HiveSessionProxy(HiveSession hiveSession, UserGroupInformation ugi) {
0041 this.base = hiveSession;
0042 this.ugi = ugi;
0043 }
0044
0045 public static HiveSession getProxy(HiveSession hiveSession, UserGroupInformation ugi)
0046 throws IllegalArgumentException, HiveSQLException {
0047 return (HiveSession)Proxy.newProxyInstance(HiveSession.class.getClassLoader(),
0048 new Class<?>[] {HiveSession.class},
0049 new HiveSessionProxy(hiveSession, ugi));
0050 }
0051
0052 @Override
0053 public Object invoke(Object arg0, final Method method, final Object[] args)
0054 throws Throwable {
0055 try {
0056 if (method.getDeclaringClass() == HiveSessionBase.class) {
0057 return invoke(method, args);
0058 }
0059 return ugi.doAs(
0060 new PrivilegedExceptionAction<Object>() {
0061 @Override
0062 public Object run() throws HiveSQLException {
0063 return invoke(method, args);
0064 }
0065 });
0066 } catch (UndeclaredThrowableException e) {
0067 Throwable innerException = e.getCause();
0068 if (innerException instanceof PrivilegedActionException) {
0069 throw innerException.getCause();
0070 } else {
0071 throw e.getCause();
0072 }
0073 }
0074 }
0075
0076 private Object invoke(final Method method, final Object[] args) throws HiveSQLException {
0077 try {
0078 return method.invoke(base, args);
0079 } catch (InvocationTargetException e) {
0080 if (e.getCause() instanceof HiveSQLException) {
0081 throw (HiveSQLException)e.getCause();
0082 }
0083 throw new RuntimeException(e.getCause());
0084 } catch (IllegalArgumentException e) {
0085 throw new RuntimeException(e);
0086 } catch (IllegalAccessException e) {
0087 throw new RuntimeException(e);
0088 }
0089 }
0090 }
0091