`ConfigUtils.java`
`001 /* 002 * Copyright 2011-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package griffon.util; 018 019 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; 020 021 import java.util.Map; 022 023 /** 024 * Utility class for reading configuration properties. 025 * 026 * @author Andres Almiray 027 */ 028 public abstract class ConfigUtils { 029 /** 030 * Returns true if there's a on-null value for the specified key. 031 * 032 * @param config the configuration object to be searched upon 033 * @param key the key to be searched 034 * @return true if there's a value for the specified key, false otherwise 035 */ 036 public static boolean isValueDefined(Map config, String key) { 037 String[] keys = key.split("\\."); 038 for (int i = 0; i < keys.length - 1; i++) { 039 if (config != null) { 040 config = (Map) config.get(keys[i]); 041 } else { 042 return false; 043 } 044 } 045 if (config == null) return false; 046 Object value = config.get(keys[keys.length - 1]); 047 return value != null; 048 } 049 050 /** 051 * Returns the value for the specified key. 052 * 053 * @param config the configuration object to be searched upon 054 * @param key the key to be searched 055 * @return the value of the key. May return null 056 */ 057 public static Object getConfigValue(Map config, String key) { 058 return getConfigValue(config, key, null); 059 } 060 061 /** 062 * Returns the value for the specified key with an optional default value if no match is found. 063 * 064 * @param config the configuration object to be searched upon 065 * @param key the key to be searched 066 * @param defaultValue the value to send back if no match is found 067 * @return the value of the key or the default value if no match is found 068 */ 069 public static Object getConfigValue(Map config, String key, Object defaultValue) { 070 String[] keys = key.split("\\."); 071 for (int i = 0; i < keys.length - 1; i++) { 072 if (config != null) { 073 Object node = config.get(keys[i]); 074 if(node instanceof Map) { 075 config = (Map) node; 076 } else { 077 return defaultValue; 078 } 079 } else { 080 return defaultValue; 081 } 082 } 083 if (config == null) return defaultValue; 084 Object value = config.get(keys[keys.length - 1]); 085 return value != null ? value : defaultValue; 086 } 087 088 /** 089 * Returns the value for the specified key coerced to a boolean. 090 * 091 * @param config the configuration object to be searched upon 092 * @param key the key to be searched 093 * @return the value of the key. Returns {@code false} if no match. 094 */ 095 public static boolean getConfigValueAsBoolean(Map config, String key) { 096 return getConfigValueAsBoolean(config, key, false); 097 } 098 099 /** 100 * Returns the value for the specified key with an optional default value if no match is found. 101 * 102 * @param config the configuration object to be searched upon 103 * @param key the key to be searched 104 * @param defaultValue the value to send back if no match is found 105 * @return the value of the key or the default value if no match is found 106 */ 107 public static boolean getConfigValueAsBoolean(Map config, String key, boolean defaultValue) { 108 Object value = getConfigValue(config, key, defaultValue); 109 return DefaultTypeTransformation.castToBoolean(value); 110 } 111 112 /** 113 * Returns the value for the specified key coerced to an int. 114 * 115 * @param config the configuration object to be searched upon 116 * @param key the key to be searched 117 * @return the value of the key. Returns {@code 0} if no match. 118 */ 119 public static int getConfigValueAsInt(Map config, String key) { 120 return getConfigValueAsInt(config, key, 0); 121 } 122 123 /** 124 * Returns the value for the specified key with an optional default value if no match is found. 125 * 126 * @param config the configuration object to be searched upon 127 * @param key the key to be searched 128 * @param defaultValue the value to send back if no match is found 129 * @return the value of the key or the default value if no match is found 130 */ 131 public static int getConfigValueAsInt(Map config, String key, int defaultValue) { 132 Object value = getConfigValue(config, key, defaultValue); 133 return DefaultTypeTransformation.castToNumber(value).intValue(); 134 } 135 136 /** 137 * Returns the value for the specified key converted to a String. 138 * 139 * @param config the configuration object to be searched upon 140 * @param key the key to be searched 141 * @return the value of the key. Returns {@code ""} if no match. 142 */ 143 public static String getConfigValueAsString(Map config, String key) { 144 return getConfigValueAsString(config, key, ""); 145 } 146 147 /** 148 * Returns the value for the specified key with an optional default value if no match is found. 149 * 150 * @param config the configuration object to be searched upon 151 * @param key the key to be searched 152 * @param defaultValue the value to send back if no match is found 153 * @return the value of the key or the default value if no match is found 154 */ 155 public static String getConfigValueAsString(Map config, String key, String defaultValue) { 156 Object value = getConfigValue(config, key, defaultValue); 157 return String.valueOf(value); 158 } 159 }`