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