Coverage Report - org.kuali.spring.util.PropertiesConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesConverter
91%
21/23
66%
4/6
1.429
 
 1  
 package org.kuali.spring.util;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Collections;
 5  
 import java.util.List;
 6  
 import java.util.Properties;
 7  
 
 8  
 import org.slf4j.Logger;
 9  
 import org.slf4j.LoggerFactory;
 10  
 import org.springframework.util.ObjectUtils;
 11  
 
 12  3
 public class PropertiesConverter {
 13  3
         final Logger logger = LoggerFactory.getLogger(PropertiesConverter.class);
 14  
 
 15  
         public static final boolean DEFAULT_IS_SORT = true;
 16  3
         boolean sort = DEFAULT_IS_SORT;
 17  
 
 18  3
         PropertyLogger plogger = new PropertyLogger();
 19  
 
 20  
         /**
 21  
          * Perform any conversion on the supplied Properties as needed.
 22  
          * <p>
 23  
          * The default implementation invokes {@link #convert(String,String)} for each property value and replaces the
 24  
          * original with the converted value if the converted value is different
 25  
          * 
 26  
          * @param properties
 27  
          *            the Properties to convert
 28  
          */
 29  
         public void convert(Properties properties) {
 30  
                 // Get a handle to the property names
 31  1
                 List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
 32  
                 // Sort if needed
 33  1
                 if (isSort()) {
 34  1
                         Collections.sort(keys);
 35  
                 }
 36  
                 // Iterate through the properties
 37  1
                 for (String key : keys) {
 38  
                         // Extract the current value
 39  122
                         String oldValue = properties.getProperty(key);
 40  
                         // Get the converted value
 41  122
                         String newValue = convert(key, oldValue);
 42  
                         // Check them for equality
 43  122
                         if (!ObjectUtils.nullSafeEquals(newValue, oldValue)) {
 44  
                                 // The converted value is different, update our properties object
 45  0
                                 logger.info("Converted value for '" + key + "' [{}]->[{}]", plogger.getLogValue(key, oldValue),
 46  
                                                 plogger.getLogValue(key, newValue));
 47  0
                                 properties.setProperty(key, newValue);
 48  
                         }
 49  122
                 }
 50  1
         }
 51  
 
 52  
         /**
 53  
          * Convert the given property from the properties source to the value which should be used.
 54  
          * <p>
 55  
          * The default implementation calls {@link #convert(String)}.
 56  
          * 
 57  
          * @param propertyName
 58  
          *            the name of the property
 59  
          * @param propertyValue
 60  
          *            the original value from the properties source
 61  
          * 
 62  
          * @return the converted value, to be used for processing
 63  
          * @see #convert(String)
 64  
          */
 65  
         protected String convert(String propertyName, String propertyValue) {
 66  122
                 return convert(propertyValue);
 67  
         }
 68  
 
 69  
         /**
 70  
          * Convert the given property value from the properties source to the value which should be used.
 71  
          * <p>
 72  
          * The default implementation simply returns the original value. Can be overridden in subclasses, for example to
 73  
          * detect encrypted values and decrypt them accordingly.
 74  
          * 
 75  
          * @param originalValue
 76  
          *            the original value from the properties source
 77  
          * 
 78  
          * @return the converted value, to be used for processing
 79  
          * 
 80  
          * @see #convert(String, String)
 81  
          */
 82  
         protected String convert(String originalValue) {
 83  122
                 return originalValue;
 84  
         }
 85  
 
 86  
         public boolean isSort() {
 87  2
                 return sort;
 88  
         }
 89  
 
 90  
         public void setSort(boolean sort) {
 91  1
                 this.sort = sort;
 92  1
         }
 93  
 
 94  
         public PropertyLogger getPlogger() {
 95  1
                 return plogger;
 96  
         }
 97  
 
 98  
         public void setPlogger(PropertyLogger plogger) {
 99  2
                 this.plogger = plogger;
 100  2
         }
 101  
 
 102  
 }