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 import org.apache.commons.lang.StringUtils;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.classification.InterfaceStability;
26
27 import java.util.AbstractMap;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33 * Utilities for storing more complex collection types in
34 * {@link org.apache.hadoop.conf.Configuration} instances.
35 */
36 @InterfaceAudience.Public
37 @InterfaceStability.Evolving
38 public final class ConfigurationUtil {
39 // TODO: hopefully this is a good delimiter; it's not in the base64 alphabet,
40 // nor is it valid for paths
41 public static final char KVP_DELIMITER = '^';
42
43 // Disallow instantiation
44 private ConfigurationUtil() {
45
46 }
47
48 /**
49 * Store a collection of Map.Entry's in conf, with each entry separated by ','
50 * and key values delimited by {@link #KVP_DELIMITER}
51 *
52 * @param conf configuration to store the collection in
53 * @param key overall key to store keyValues under
54 * @param keyValues kvps to be stored under key in conf
55 */
56 public static void setKeyValues(Configuration conf, String key,
57 Collection<Map.Entry<String, String>> keyValues) {
58 setKeyValues(conf, key, keyValues, KVP_DELIMITER);
59 }
60
61 /**
62 * Store a collection of Map.Entry's in conf, with each entry separated by ','
63 * and key values delimited by delimiter.
64 *
65 * @param conf configuration to store the collection in
66 * @param key overall key to store keyValues under
67 * @param keyValues kvps to be stored under key in conf
68 * @param delimiter character used to separate each kvp
69 */
70 public static void setKeyValues(Configuration conf, String key,
71 Collection<Map.Entry<String, String>> keyValues, char delimiter) {
72 List<String> serializedKvps = Lists.newArrayList();
73
74 for (Map.Entry<String, String> kvp : keyValues) {
75 serializedKvps.add(kvp.getKey() + delimiter + kvp.getValue());
76 }
77
78 conf.setStrings(key, serializedKvps.toArray(new String[serializedKvps.size()]));
79 }
80
81 /**
82 * Retrieve a list of key value pairs from configuration, stored under the provided key
83 *
84 * @param conf configuration to retrieve kvps from
85 * @param key key under which the key values are stored
86 * @return the list of kvps stored under key in conf, or null if the key isn't present.
87 * @see #setKeyValues(Configuration, String, Collection, char)
88 */
89 public static List<Map.Entry<String, String>> getKeyValues(Configuration conf, String key) {
90 return getKeyValues(conf, key, KVP_DELIMITER);
91 }
92
93 /**
94 * Retrieve a list of key value pairs from configuration, stored under the provided key
95 *
96 * @param conf configuration to retrieve kvps from
97 * @param key key under which the key values are stored
98 * @param delimiter character used to separate each kvp
99 * @return the list of kvps stored under key in conf, or null if the key isn't present.
100 * @see #setKeyValues(Configuration, String, Collection, char)
101 */
102 public static List<Map.Entry<String, String>> getKeyValues(Configuration conf, String key,
103 char delimiter) {
104 String[] kvps = conf.getStrings(key);
105
106 if (kvps == null) {
107 return null;
108 }
109
110 List<Map.Entry<String, String>> rtn = Lists.newArrayList();
111
112 for (String kvp : kvps) {
113 String[] splitKvp = StringUtils.split(kvp, delimiter);
114
115 if (splitKvp.length != 2) {
116 throw new IllegalArgumentException(
117 "Expected key value pair for configuration key '" + key + "'" + " to be of form '<key>"
118 + delimiter + "<value>; was " + kvp + " instead");
119 }
120
121 rtn.add(new AbstractMap.SimpleImmutableEntry<String, String>(splitKvp[0], splitKvp[1]));
122 }
123 return rtn;
124 }
125 }