Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 
0019 package org.apache.hive.service.cli.session;
0020 
0021 /**
0022  * Proxy wrapper on HiveSession to execute operations
0023  * by impersonating given user
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