001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.client;
020
021import java.io.IOException;
022import java.net.InetSocketAddress;
023import java.security.PrivilegedAction;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.apache.hadoop.classification.InterfaceAudience;
028import org.apache.hadoop.classification.InterfaceStability;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.security.UserGroupInformation;
031import org.apache.hadoop.yarn.ipc.YarnRPC;
032
033@InterfaceAudience.Public
034@InterfaceStability.Evolving
035@SuppressWarnings("unchecked")
036public class AHSProxy<T> {
037
038  private static final Log LOG = LogFactory.getLog(AHSProxy.class);
039
040  public static <T> T createAHSProxy(final Configuration conf,
041      final Class<T> protocol, InetSocketAddress ahsAddress) throws IOException {
042    LOG.info("Connecting to Application History server at " + ahsAddress);
043    return (T) getProxy(conf, protocol, ahsAddress);
044  }
045
046  protected static <T> T getProxy(final Configuration conf,
047      final Class<T> protocol, final InetSocketAddress rmAddress)
048      throws IOException {
049    return UserGroupInformation.getCurrentUser().doAs(
050      new PrivilegedAction<T>() {
051        @Override
052        public T run() {
053          return (T) YarnRPC.create(conf).getProxy(protocol, rmAddress, conf);
054        }
055      });
056  }
057}