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.security;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import javax.security.auth.login.AppConfigurationEntry;
025import javax.security.auth.login.Configuration;
026
027public class DynamicLoginConfiguration extends Configuration {
028  private final Configuration baseConfig;
029  private final Map<String, ?> overrideOptions;
030
031  public class OverrideAppConfigurationEntry extends AppConfigurationEntry {
032
033    public OverrideAppConfigurationEntry(AppConfigurationEntry base) {
034      super(base.getLoginModuleName(), base.getControlFlag(), base.getOptions());
035    }
036
037    @Override
038    public Map<String, ?> getOptions() {
039      Map<String, ?> baseOptions = super.getOptions();
040      Map<String, Object> newOption = new HashMap<String, Object>();
041      newOption.putAll(baseOptions);
042      newOption.putAll(overrideOptions);
043      return newOption;
044    }
045  }
046
047  public DynamicLoginConfiguration(Configuration base,  Map<String,?> options) {
048    this.baseConfig = base;
049    this.overrideOptions = options;
050  }
051
052  /**
053   * Only goal here is to override the values of options at runtime.
054   *
055   * @param appName
056   * @return
057   */
058  @Override
059  public AppConfigurationEntry[] getAppConfigurationEntry(String appName) {
060    AppConfigurationEntry[] app = baseConfig.getAppConfigurationEntry(appName);
061    if (app == null) {
062      return null;
063    }
064
065    AppConfigurationEntry[] newEntries = new AppConfigurationEntry[app.length];
066    for (int i = 0; i < app.length; i++ ) {
067      AppConfigurationEntry entry = app[i];
068      newEntries[i] = new OverrideAppConfigurationEntry(entry);
069    }
070    return newEntries;
071  }
072}