Coverage Report - net.admin4j.util.PropertyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyUtils
94%
53/56
82%
28/34
5.5
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.util;
 15  
 
 16  
 import java.util.HashSet;
 17  
 import java.util.Properties;
 18  
 import java.util.Set;
 19  
 
 20  
 /**
 21  
  * Provides generic utilities for Properties
 22  
  * @author D. Ashmore
 23  
  *
 24  
  */
 25  0
 public class PropertyUtils {
 26  
     
 27  
     /**
 28  
      * Supports property values having variable references.
 29  
      * 
 30  
      * <p>Properties can have variables of the syntax ${variable-name}.  Variables are resolved in the following sequence:</p>
 31  
      * <li>Variables are resolved against the property file itself.</li>
 32  
      * <li>Variables are resolved against system properties.</li>
 33  
      * 
 34  
      * <p>Example:  Propertires config.file=${user.dir}/foo.xml.  First, if the properties object has a definition for 'user.dir' it is used.
 35  
      * Otherwise, System.getProperty('user.dir') is used.  Null is used and a warning generated if the variable isn't defined.
 36  
      * In this case, if the user.dir is '/home/dashmore', the property value will be converted to '/home/dashmore/foo.xml'.</p>
 37  
      * @param props
 38  
      */
 39  
     public static Set<String> resolveVariableReferences(Properties props) {
 40  6
         Set<String> propsWithRefsSet = findSettingsNeedingResolution(props);
 41  
         Set<String> variableSet;
 42  
         
 43  6
         Set<String> unresolvedVariables = new HashSet<String>();
 44  
         String currentValue;
 45  
         String currentVariableValue;
 46  6
         int nbrVariablesLastIteration = Integer.MAX_VALUE;
 47  
         
 48  14
         while (propsWithRefsSet.size() > 0 && propsWithRefsSet.size() != nbrVariablesLastIteration) {
 49  8
             nbrVariablesLastIteration = propsWithRefsSet.size();
 50  
             
 51  8
             for (String propName: propsWithRefsSet) {
 52  14
                 currentValue = props.getProperty(propName);
 53  14
                 variableSet = findReferencedVariableNames(currentValue, 0);
 54  
                 
 55  14
                 for (String variableName: variableSet) {
 56  14
                     currentVariableValue = props.getProperty(variableName);
 57  14
                     if (currentVariableValue == null) {
 58  8
                         currentVariableValue = System.getProperty(variableName);
 59  
                     }
 60  
                     
 61  14
                     if (currentVariableValue == null) {
 62  3
                         currentVariableValue = "";
 63  3
                         unresolvedVariables.add(variableName);
 64  
                     }
 65  
                                         
 66  14
                     currentValue = replaceVariableWithValue(variableName, currentVariableValue, currentValue);
 67  14
                     props.setProperty(propName, currentValue);
 68  
                 }
 69  
             }
 70  
             
 71  
             
 72  
             
 73  8
             propsWithRefsSet = findSettingsNeedingResolution(props);
 74  
         }
 75  
         
 76  6
         return unresolvedVariables;
 77  
         
 78  
     }
 79  
     
 80  
     protected static Set<String> findSettingsNeedingResolution(Properties props) {
 81  17
         Set<String> nameSet = new HashSet<String>();
 82  
         String value;
 83  
         int firstOffset;
 84  
         
 85  17
         for (Object propName: props.keySet()) {
 86  224
             value = props.getProperty( (String)propName);
 87  224
             firstOffset = value.indexOf("${");
 88  224
             if (firstOffset >= 0 && value.indexOf("}", firstOffset + 1) > 0) {
 89  20
                 nameSet.add( (String)propName);
 90  
             }
 91  
         }
 92  
         
 93  17
         return nameSet;
 94  
     }
 95  
     
 96  
     protected static Set<String> findReferencedVariableNames(String value, int beginningOffset) {
 97  
         int firstOffset, lastOffset;
 98  26
         Set<String> set = new HashSet<String>();
 99  26
         firstOffset = value.indexOf("${", beginningOffset);
 100  26
         lastOffset = value.indexOf("}", firstOffset + 1);
 101  
         
 102  26
         if (firstOffset < 0 || lastOffset < 0) {
 103  3
             return set;
 104  
         }
 105  
         
 106  23
         set.add(value.substring(firstOffset + 2, lastOffset).trim());
 107  
         
 108  23
         if (value.indexOf("${", lastOffset) > 0) {
 109  6
             set.addAll(findReferencedVariableNames(value, lastOffset));
 110  
         }
 111  
         
 112  23
         return set;
 113  
     }
 114  
     
 115  
     protected static String replaceVariableWithValue(String variableName, String variableValue, String baseString) {
 116  
         int firstOffset, lastOffset;
 117  17
         StringBuffer buffer = new StringBuffer();
 118  17
         int beginOffset = 0;        
 119  17
         firstOffset = baseString.indexOf("${", beginOffset);
 120  
         String tempVariableName;
 121  17
         int lastCopiedOffset = 0;
 122  
         
 123  17
         lastOffset = 0;
 124  40
         while (firstOffset >= 0 && lastCopiedOffset < baseString.length()) {
 125  23
             buffer.append(baseString.substring(lastCopiedOffset, firstOffset));
 126  23
             lastCopiedOffset = firstOffset;
 127  
             
 128  23
             lastOffset = baseString.indexOf("}", firstOffset + 1);
 129  23
             tempVariableName = baseString.substring(firstOffset + 2, lastOffset).trim();
 130  23
             if (variableName.equals(tempVariableName)) {
 131  20
                 buffer.append(variableValue);
 132  
             }
 133  
             else {
 134  3
                 if (lastOffset + 1 > baseString.length()) {
 135  0
                     buffer.append(baseString.substring(lastCopiedOffset));
 136  
                 }
 137  3
                 else buffer.append(baseString.substring(lastCopiedOffset, lastOffset + 1));
 138  
             }
 139  23
             lastCopiedOffset = lastOffset + 1;
 140  
             
 141  23
             firstOffset = baseString.indexOf("${", lastCopiedOffset);
 142  
         }
 143  
         
 144  17
         if (lastCopiedOffset < baseString.length()) {
 145  0
             buffer.append(baseString.substring(lastCopiedOffset));
 146  
         }
 147  
         
 148  17
         return buffer.toString();
 149  
     }
 150  
 
 151  
 }