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 */
018package org.apache.hadoop.hbase.util;
019
020import java.util.AbstractMap;
021import java.util.Collection;
022import java.util.List;
023import java.util.Map;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.util.StringUtils;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
030
031/**
032 * Utilities for storing more complex collection types in
033 * {@link org.apache.hadoop.conf.Configuration} instances.
034 */
035@InterfaceAudience.Public
036public final class ConfigurationUtil {
037  // TODO: hopefully this is a good delimiter; it's not in the base64 alphabet,
038  // nor is it valid for paths
039  public static final char KVP_DELIMITER = '^';
040
041  // Disallow instantiation
042  private ConfigurationUtil() {
043
044  }
045
046  /**
047   * Store a collection of Map.Entry's in conf, with each entry separated by ',' and key values
048   * delimited by {@link #KVP_DELIMITER}
049   * @param conf      configuration to store the collection in
050   * @param key       overall key to store keyValues under
051   * @param keyValues kvps to be stored under key in conf
052   */
053  public static void setKeyValues(Configuration conf, String key,
054    Collection<Map.Entry<String, String>> keyValues) {
055    setKeyValues(conf, key, keyValues, KVP_DELIMITER);
056  }
057
058  /**
059   * Store a collection of Map.Entry's in conf, with each entry separated by ',' and key values
060   * delimited by delimiter.
061   * @param conf      configuration to store the collection in
062   * @param key       overall key to store keyValues under
063   * @param keyValues kvps to be stored under key in conf
064   * @param delimiter character used to separate each kvp
065   */
066  public static void setKeyValues(Configuration conf, String key,
067    Collection<Map.Entry<String, String>> keyValues, char delimiter) {
068    List<String> serializedKvps = Lists.newArrayList();
069
070    for (Map.Entry<String, String> kvp : keyValues) {
071      serializedKvps.add(kvp.getKey() + delimiter + kvp.getValue());
072    }
073
074    conf.setStrings(key, serializedKvps.toArray(new String[serializedKvps.size()]));
075  }
076
077  /**
078   * Retrieve a list of key value pairs from configuration, stored under the provided key
079   * @param conf configuration to retrieve kvps from
080   * @param key  key under which the key values are stored
081   * @return the list of kvps stored under key in conf, or null if the key isn't present.
082   * @see #setKeyValues(Configuration, String, Collection, char)
083   */
084  public static List<Map.Entry<String, String>> getKeyValues(Configuration conf, String key) {
085    return getKeyValues(conf, key, KVP_DELIMITER);
086  }
087
088  /**
089   * Retrieve a list of key value pairs from configuration, stored under the provided key
090   * @param conf      configuration to retrieve kvps from
091   * @param key       key under which the key values are stored
092   * @param delimiter character used to separate each kvp
093   * @return the list of kvps stored under key in conf, or null if the key isn't present.
094   * @see #setKeyValues(Configuration, String, Collection, char)
095   */
096  public static List<Map.Entry<String, String>> getKeyValues(Configuration conf, String key,
097    char delimiter) {
098    String[] kvps = conf.getStrings(key);
099
100    if (kvps == null) {
101      return null;
102    }
103
104    List<Map.Entry<String, String>> rtn = Lists.newArrayList();
105
106    for (String kvp : kvps) {
107      String[] splitKvp = StringUtils.split(kvp, delimiter);
108
109      if (splitKvp.length != 2) {
110        throw new IllegalArgumentException("Expected key value pair for configuration key '" + key
111          + "'" + " to be of form '<key>" + delimiter + "<value>; was " + kvp + " instead");
112      }
113
114      rtn.add(new AbstractMap.SimpleImmutableEntry<>(splitKvp[0], splitKvp[1]));
115    }
116    return rtn;
117  }
118
119  public static boolean isReadOnlyModeEnabledInConf(Configuration conf) {
120    return conf.getBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY,
121      HConstants.HBASE_GLOBAL_READONLY_ENABLED_DEFAULT);
122  }
123}