/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hive.service.auth;

import javax.security.sasl.AuthenticationException;

import org.apache.hadoop.hive.conf.HiveConf;

/**
 * This class helps select a {@link PasswdAuthenticationProvider} for a given {@code AuthMethod}.
 */
public final class AuthenticationProviderFactory {

  public enum AuthMethods {
    LDAP("LDAP"),
    PAM("PAM"),
    CUSTOM("CUSTOM"),
    NONE("NONE");

    private final String authMethod;

    AuthMethods(String authMethod) {
      this.authMethod = authMethod;
    }

    public String getAuthMethod() {
      return authMethod;
    }

    public static AuthMethods getValidAuthMethod(String authMethodStr)
      throws AuthenticationException {
      for (AuthMethods auth : AuthMethods.values()) {
        if (authMethodStr.equals(auth.getAuthMethod())) {
          return auth;
        }
      }
      throw new AuthenticationException("Not a valid authentication method");
    }
  }

  private AuthenticationProviderFactory() {
  }

  public static PasswdAuthenticationProvider getAuthenticationProvider(AuthMethods authMethod)
    throws AuthenticationException {
    return getAuthenticationProvider(authMethod, new HiveConf());
  }
  public static PasswdAuthenticationProvider getAuthenticationProvider(AuthMethods authMethod, HiveConf conf)
    throws AuthenticationException {
    if (authMethod == AuthMethods.LDAP) {
      return new LdapAuthenticationProviderImpl(conf);
    } else if (authMethod == AuthMethods.PAM) {
      return new PamAuthenticationProviderImpl(conf);
    } else if (authMethod == AuthMethods.CUSTOM) {
      return new CustomAuthenticationProviderImpl(conf);
    } else if (authMethod == AuthMethods.NONE) {
      return new AnonymousAuthenticationProviderImpl();
    } else {
      throw new AuthenticationException("Unsupported authentication method");
    }
  }
}

@@ -1236,16 +1236,67 @@
 ethod;%0A%0A
+    private final HiveConf conf = new HiveConf();%0A%0A
     Auth
@@ -1427,32 +1427,90 @@
 hMethod;%0A    %7D%0A%0A
+    public HiveConf getConf() %7B%0A      return conf;%0A    %7D%0A%0A
     public stati
@@ -2076,29 +2076,19 @@
 ethod, n
-ew HiveConf()
+ull
 );%0A  %7D%0A 
@@ -2315,32 +2315,78 @@
 ionProviderImpl(
+(conf == null) ? AuthMethods.LDAP.getConf() : 
 conf);%0A    %7D els
@@ -2463,32 +2463,77 @@
 ionProviderImpl(
+(conf == null) ? AuthMethods.PAM.getConf() : 
 conf);%0A    %7D els
@@ -2624,16 +2624,64 @@
 derImpl(
+(conf == null) ? AuthMethods.CUSTOM.getConf() : 
 conf);%0A 

Optimise AuthenticationProviderFactory::getAuthenticationProvider (Rajesh Balamohan, reviewed by Ashutosh Chauhan, Gopal V)