1 10 11 package org.mule.util; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Properties ; 20 21 import org.apache.commons.lang.StringUtils; 22 import org.mule.config.i18n.Message; 23 import org.mule.config.i18n.Messages; 24 25 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 26 27 31 public class PropertiesUtils 33 { 34 private static final List maskedProperties = new CopyOnWriteArrayList(); 36 37 static 38 { 39 registerMaskedPropertyName("password"); 42 } 43 44 51 public static void registerMaskedPropertyName(String name) 52 { 53 if (StringUtils.isNotEmpty(name)) 54 { 55 maskedProperties.add(name); 56 } 57 else 58 { 59 throw new IllegalArgumentException ("Cannot mask empty property name."); 60 } 61 } 62 63 72 public static String maskedPropertyValue(Map.Entry property) 73 { 74 if (maskedProperties.contains(property.getKey())) 75 { 76 return ("*****"); 77 } 78 else 79 { 80 return property.getValue().toString(); 81 } 82 } 83 84 93 public static synchronized Properties loadProperties(String fileName, final Class callingClass) 94 throws IOException 95 { 96 InputStream is = IOUtils.getResourceAsStream(fileName, callingClass, 97 true, false); 98 if (is == null) 99 { 100 Message error = new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, fileName); 101 throw new IOException (error.toString()); 102 } 103 104 try 105 { 106 Properties props = new Properties (); 107 props.load(is); 108 return props; 109 } 110 finally 111 { 112 is.close(); 113 } 114 } 115 116 public static String removeXmlNamespacePrefix(String eleName) 117 { 118 int i = eleName.indexOf(':'); 119 return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length())); 120 } 121 122 public static String removeNamespacePrefix(String eleName) 123 { 124 int i = eleName.lastIndexOf('.'); 125 return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length())); 126 } 127 128 public static Map removeNamespaces(Map properties) 129 { 130 HashMap props = new HashMap (properties.size()); 131 Map.Entry entry; 132 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) 133 { 134 entry = (Map.Entry )iter.next(); 135 props.put(removeNamespacePrefix((String )entry.getKey()), entry.getValue()); 136 137 } 138 return props; 139 } 140 141 151 public static Map getPropertiesWithPrefix(Map props, String prefix) 152 { 153 Map newProps = new HashMap (); 154 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) 155 { 156 Map.Entry entry = (Map.Entry )iterator.next(); 157 Object key = entry.getKey(); 158 if (key.toString().startsWith(prefix)) 159 { 160 newProps.put(key, entry.getValue()); 161 } 162 } 163 return newProps; 164 } 165 166 175 public static void getPropertiesWithPrefix(Map props, String prefix, Map newProps) 176 { 177 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) 178 { 179 Map.Entry entry = (Map.Entry )iterator.next(); 180 Object key = entry.getKey(); 181 if (key.toString().startsWith(prefix)) 182 { 183 newProps.put(key, entry.getValue()); 184 } 185 } 186 } 187 188 public static Map getPropertiesWithoutPrefix(Map props, String prefix) 189 { 190 Map newProps = new HashMap (); 191 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) 192 { 193 Map.Entry entry = (Map.Entry )iterator.next(); 194 Object key = entry.getKey(); 195 if (!key.toString().startsWith(prefix)) 196 { 197 newProps.put(key, entry.getValue()); 198 } 199 } 200 return newProps; 201 } 202 203 public static Properties getPropertiesFromQueryString(String query) 204 { 205 Properties props = new Properties (); 206 207 if (query == null) 208 { 209 return props; 210 } 211 212 query = new StringBuffer (query.length() + 1).append('&').append(query).toString(); 213 214 int x = 0; 215 while ((x = addProperty(query, x, props)) != -1) 216 { 217 } 219 220 return props; 221 } 222 223 private static int addProperty(String query, int start, Properties properties) 224 { 225 int i = query.indexOf('&', start); 226 int i2 = query.indexOf('&', i + 1); 227 String pair; 228 if (i > -1 && i2 > -1) 229 { 230 pair = query.substring(i + 1, i2); 231 } 232 else if (i > -1) 233 { 234 pair = query.substring(i + 1); 235 } 236 else 237 { 238 return -1; 239 } 240 int eq = pair.indexOf('='); 241 242 if (eq <= 0) 243 { 244 String key = pair; 245 String value = StringUtils.EMPTY; 246 properties.setProperty(key, value); 247 } 248 else 249 { 250 String key = pair.substring(0, eq); 251 String value = (eq == pair.length() ? StringUtils.EMPTY : pair.substring(eq + 1)); 252 properties.setProperty(key, value); 253 } 254 return i2; 255 } 256 257 260 public static String propertiesToString(Map props, boolean newline) 261 { 262 return MapUtils.toString(props, newline); 263 } 264 265 } 266 | Popular Tags |