View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.util;
20  
21  import com.google.common.collect.Lists;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  
28  import java.util.AbstractMap;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Utilities for storing more complex collection types in
35   * {@link org.apache.hadoop.conf.Configuration} instances.
36   */
37  @InterfaceAudience.Public
38  @InterfaceStability.Evolving
39  public final class ConfigurationUtil {
40    // TODO: hopefully this is a good delimiter; it's not in the base64 alphabet, 
41    // nor is it valid for paths
42    public static final char KVP_DELIMITER = '^';
43  
44    // Disallow instantiation
45    private ConfigurationUtil() {
46  
47    }
48  
49    /**
50     * Store a collection of Map.Entry's in conf, with each entry separated by ','
51     * and key values delimited by {@link #KVP_DELIMITER}
52     *
53     * @param conf      configuration to store the collection in
54     * @param key       overall key to store keyValues under
55     * @param keyValues kvps to be stored under key in conf
56     */
57    public static void setKeyValues(Configuration conf, String key,
58        Collection<Map.Entry<String, String>> keyValues) {
59      setKeyValues(conf, key, keyValues, KVP_DELIMITER);
60    }
61  
62    /**
63     * Store a collection of Map.Entry's in conf, with each entry separated by ','
64     * and key values delimited by delimiter.
65     *
66     * @param conf      configuration to store the collection in
67     * @param key       overall key to store keyValues under
68     * @param keyValues kvps to be stored under key in conf
69     * @param delimiter character used to separate each kvp
70     */
71    public static void setKeyValues(Configuration conf, String key,
72        Collection<Map.Entry<String, String>> keyValues, char delimiter) {
73      List<String> serializedKvps = Lists.newArrayList();
74  
75      for (Map.Entry<String, String> kvp : keyValues) {
76        serializedKvps.add(kvp.getKey() + delimiter + kvp.getValue());
77      }
78  
79      conf.setStrings(key, serializedKvps.toArray(new String[serializedKvps.size()]));
80    }
81  
82    /**
83     * Retrieve a list of key value pairs from configuration, stored under the provided key
84     *
85     * @param conf configuration to retrieve kvps from
86     * @param key  key under which the key values are stored
87     * @return the list of kvps stored under key in conf, or null if the key isn't present.
88     * @see #setKeyValues(Configuration, String, Collection, char)
89     */
90    public static List<Map.Entry<String, String>> getKeyValues(Configuration conf, String key) {
91      return getKeyValues(conf, key, KVP_DELIMITER);
92    }
93  
94    /**
95     * Retrieve a list of key value pairs from configuration, stored under the provided key
96     *
97     * @param conf      configuration to retrieve kvps from
98     * @param key       key under which the key values are stored
99     * @param delimiter character used to separate each kvp
100    * @return the list of kvps stored under key in conf, or null if the key isn't present.
101    * @see #setKeyValues(Configuration, String, Collection, char)
102    */
103   public static List<Map.Entry<String, String>> getKeyValues(Configuration conf, String key,
104       char delimiter) {
105     String[] kvps = conf.getStrings(key);
106 
107     if (kvps == null) {
108       return null;
109     }
110 
111     List<Map.Entry<String, String>> rtn = Lists.newArrayList();
112 
113     for (String kvp : kvps) {
114       String[] splitKvp = StringUtils.split(kvp, delimiter);
115 
116       if (splitKvp.length != 2) {
117         throw new IllegalArgumentException(
118             "Expected key value pair for configuration key '" + key + "'" + " to be of form '<key>"
119                 + delimiter + "<value>; was " + kvp + " instead");
120       }
121 
122       rtn.add(new AbstractMap.SimpleImmutableEntry<String, String>(splitKvp[0], splitKvp[1]));
123     }
124     return rtn;
125   }
126 }