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