Coverage Report - org.mule.util.PropertiesUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesUtils
55%
46/84
50%
23/46
3.154
 
 1  
 /*
 2  
  * $Id: PropertiesUtils.java 10489 2008-01-23 17:53:38Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.config.i18n.Message;
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.io.InputStream;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.Properties;
 23  
 
 24  
 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
 25  
 
 26  
 /**
 27  
  * <code>PropertiesHelper</code> is a utility class for manipulating and filtering
 28  
  * property Maps.
 29  
  */
 30  
 // @ThreadSafe
 31  
 public final class PropertiesUtils
 32  
 {
 33  
     // @GuardedBy(itself)
 34  2
     private static final List maskedProperties = new CopyOnWriteArrayList();
 35  
 
 36  
     static
 37  
     {
 38  
         // When printing property lists mask password fields
 39  
         // Users can register their own fields to mask
 40  2
         registerMaskedPropertyName("password");
 41  2
     }
 42  
 
 43  
     /** Do not instanciate. */
 44  
     protected PropertiesUtils()
 45  0
     {
 46  
         // no-op
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Register a property name for masking. This will prevent certain values from
 51  
      * leaking e.g. into debugging output or logfiles.
 52  
      *
 53  
      * @param name the key of the property to be masked.
 54  
      * @throws IllegalArgumentException is name is null or empty.
 55  
      */
 56  
     public static void registerMaskedPropertyName(String name)
 57  
     {
 58  4
         if (StringUtils.isNotEmpty(name))
 59  
         {
 60  4
             maskedProperties.add(name);
 61  
         }
 62  
         else
 63  
         {
 64  0
             throw new IllegalArgumentException("Cannot mask empty property name.");
 65  
         }
 66  4
     }
 67  
 
 68  
     /**
 69  
      * Returns the String representation of the property value or a masked String if
 70  
      * the property key has been registered previously via
 71  
      * {@link #registerMaskedPropertyName(String)}.
 72  
      *
 73  
      * @param property a key/value pair
 74  
      * @return String of the property value or a "masked" String that hides the
 75  
      *         contents, or <code>null</code> if the property, its key or its value
 76  
      *         is <code>null</code>.
 77  
      */
 78  
     public static String maskedPropertyValue(Map.Entry property)
 79  
     {
 80  30
         if (property == null)
 81  
         {
 82  2
             return null;
 83  
         }
 84  
 
 85  28
         Object key = property.getKey();
 86  28
         Object value = property.getValue();
 87  
 
 88  28
         if (key == null || value == null)
 89  
         {
 90  8
             return null;
 91  
         }
 92  
 
 93  20
         if (maskedProperties.contains(key))
 94  
         {
 95  2
             return ("*****");
 96  
         }
 97  
         else
 98  
         {
 99  18
             return value.toString();
 100  
         }
 101  
     }
 102  
 
 103  
     /**
 104  
      * Read in the properties from a properties file. The file may be on the file
 105  
      * system or the classpath.
 106  
      *
 107  
      * @param fileName     - The name of the properties file
 108  
      * @param callingClass - The Class which is calling this method. This is used to
 109  
      *                     determine the classpath.
 110  
      * @return a java.util.Properties object containing the properties.
 111  
      */
 112  
     public static synchronized Properties loadProperties(String fileName, final Class callingClass)
 113  
             throws IOException
 114  
     {
 115  0
         InputStream is = IOUtils.getResourceAsStream(fileName, callingClass,
 116  
                 /* tryAsFile */true, /* tryAsUrl */false);
 117  0
         if (is == null)
 118  
         {
 119  0
             Message error = CoreMessages.cannotLoadFromClasspath(fileName);
 120  0
             throw new IOException(error.toString());
 121  
         }
 122  
 
 123  
         try
 124  
         {
 125  0
             Properties props = new Properties();
 126  0
             props.load(is);
 127  0
             return props;
 128  
         }
 129  
         finally
 130  
         {
 131  0
             is.close();
 132  
         }
 133  
     }
 134  
 
 135  
     public static String removeXmlNamespacePrefix(String eleName)
 136  
     {
 137  2
         int i = eleName.indexOf(':');
 138  2
         return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length()));
 139  
     }
 140  
 
 141  
     public static String removeNamespacePrefix(String eleName)
 142  
     {
 143  22
         int i = eleName.lastIndexOf('.');
 144  22
         return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length()));
 145  
     }
 146  
 
 147  
     public static Map removeNamespaces(Map properties)
 148  
     {
 149  2
         HashMap props = new HashMap(properties.size());
 150  
         Map.Entry entry;
 151  2
         for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();)
 152  
         {
 153  10
             entry = (Map.Entry) iter.next();
 154  10
             props.put(removeNamespacePrefix((String) entry.getKey()), entry.getValue());
 155  
 
 156  
         }
 157  2
         return props;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Will create a map of properties where the names have a prefix Allows the
 162  
      * callee to supply the target map so a comparator can be set
 163  
      *
 164  
      * @param props    the source set of properties
 165  
      * @param prefix   the prefix to filter on
 166  
      * @param newProps return map containing the filtered list of properties or an
 167  
      *                 empty map if no properties matched the prefix
 168  
      */
 169  
     public static void getPropertiesWithPrefix(Map props, String prefix, Map newProps)
 170  
     {
 171  0
         if (props == null)
 172  
         {
 173  0
             return;
 174  
         }
 175  
 
 176  0
         for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
 177  
         {
 178  0
             Map.Entry entry = (Map.Entry) iterator.next();
 179  0
             Object key = entry.getKey();
 180  0
             if (key.toString().startsWith(prefix))
 181  
             {
 182  0
                 newProps.put(key, entry.getValue());
 183  
             }
 184  0
         }
 185  0
     }
 186  
 
 187  
     public static Map getPropertiesWithoutPrefix(Map props, String prefix)
 188  
     {
 189  0
         Map newProps = new HashMap();
 190  0
         for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
 191  
         {
 192  0
             Map.Entry entry = (Map.Entry) iterator.next();
 193  0
             Object key = entry.getKey();
 194  0
             if (!key.toString().startsWith(prefix))
 195  
             {
 196  0
                 newProps.put(key, entry.getValue());
 197  
             }
 198  0
         }
 199  0
         return newProps;
 200  
     }
 201  
 
 202  
     public static Properties getPropertiesFromQueryString(String query)
 203  
     {
 204  498
         Properties props = new Properties();
 205  
 
 206  498
         if (query == null)
 207  
         {
 208  474
             return props;
 209  
         }
 210  
 
 211  24
         query = new StringBuffer(query.length() + 1).append('&').append(query).toString();
 212  
 
 213  24
         int x = 0;
 214  24
         while ((x = addProperty(query, x, '&', props)) != -1)
 215  
         {
 216  
             // run
 217  
         }
 218  
 
 219  24
         return props;
 220  
     }
 221  
 
 222  
     public static Properties getPropertiesFromString(String query, char separator)
 223  
     {
 224  0
         Properties props = new Properties();
 225  
 
 226  0
         if (query == null)
 227  
         {
 228  0
             return props;
 229  
         }
 230  
 
 231  0
         query = new StringBuffer(query.length() + 1).append(separator).append(query).toString();
 232  
 
 233  0
         int x = 0;
 234  0
         while ((x = addProperty(query, x, separator, props)) != -1)
 235  
         {
 236  
             // run
 237  
         }
 238  
 
 239  0
         return props;
 240  
     }
 241  
 
 242  
     private static int addProperty(String query, int start, char separator, Properties properties)
 243  
     {
 244  24
         int i = query.indexOf(separator, start);
 245  24
         int i2 = query.indexOf(separator, i + 1);
 246  
         String pair;
 247  24
         if (i > -1 && i2 > -1)
 248  
         {
 249  0
             pair = query.substring(i + 1, i2);
 250  
         }
 251  24
         else if (i > -1)
 252  
         {
 253  24
             pair = query.substring(i + 1);
 254  
         }
 255  
         else
 256  
         {
 257  0
             return -1;
 258  
         }
 259  24
         int eq = pair.indexOf('=');
 260  
 
 261  24
         if (eq <= 0)
 262  
         {
 263  8
             String key = pair;
 264  8
             String value = StringUtils.EMPTY;
 265  8
             properties.setProperty(key, value);
 266  8
         }
 267  
         else
 268  
         {
 269  16
             String key = pair.substring(0, eq);
 270  16
             String value = (eq == pair.length() ? StringUtils.EMPTY : pair.substring(eq + 1));
 271  16
             properties.setProperty(key, value);
 272  
         }
 273  24
         return i2;
 274  
     }
 275  
 
 276  
     /** @deprecated Use {@link MapUtils#toString(Map, boolean)} instead */
 277  
     public static String propertiesToString(Map props, boolean newline)
 278  
     {
 279  0
         return MapUtils.toString(props, newline);
 280  
     }
 281  
 
 282  
 }