Coverage Report - org.apache.maven.plugin.assembly.utils.PropertyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyUtils
82%
19/23
87%
7/8
4,5
 
 1  
 package org.apache.maven.plugin.assembly.utils;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
 23  
 import org.codehaus.plexus.interpolation.InterpolationException;
 24  
 import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
 25  
 import org.codehaus.plexus.interpolation.StringSearchInterpolator;
 26  
 import org.codehaus.plexus.util.IOUtil;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileInputStream;
 30  
 import java.io.FileNotFoundException;
 31  
 import java.io.IOException;
 32  
 import java.util.Enumeration;
 33  
 import java.util.Properties;
 34  
 
 35  
 
 36  
 /**
 37  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 38  
  * @version $Id: PropertyUtils.java 826512 2009-10-18 20:03:32Z bentmann $
 39  
  */
 40  
 public final class PropertyUtils
 41  
 {
 42  
     private PropertyUtils()
 43  0
     {
 44  
         // prevent instantiation
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Reads a property file, resolving all internal variables.
 49  
      *
 50  
      * @param propfile       The property file to load
 51  
      * @param fail           wheter to throw an exception when the file cannot be loaded or to return null
 52  
      * @param useSystemProps wheter to incorporate System.getProperties settings into the returned Properties object.
 53  
      * @return the loaded and fully resolved Properties object
 54  
      */
 55  
     public static Properties getInterpolatedPropertiesFromFile( File propfile, boolean fail, boolean useSystemProps )
 56  
         throws IOException, AssemblyFormattingException
 57  
     {
 58  
         Properties props;
 59  
 
 60  12
         if ( useSystemProps )
 61  
         {
 62  2
             props = new Properties( System.getProperties() );
 63  
         }
 64  
         else
 65  
         {
 66  10
             props = new Properties();
 67  
         }
 68  
 
 69  12
         if ( propfile.exists() )
 70  
         {
 71  10
             FileInputStream inStream = new FileInputStream( propfile );
 72  
             try
 73  
             {
 74  10
                 props.load( inStream );
 75  
             }
 76  
             finally
 77  
             {
 78  10
                 IOUtil.close( inStream );
 79  10
             }
 80  
         }
 81  2
         else if ( fail )
 82  
         {
 83  2
             throw new FileNotFoundException( propfile.toString() );
 84  
         }
 85  
 
 86  10
         StringSearchInterpolator interpolator = new StringSearchInterpolator();
 87  10
         interpolator.addValueSource( new PropertiesBasedValueSource( props ) );
 88  
 
 89  10
         for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); )
 90  
         {
 91  128
             String key = (String) n.nextElement();
 92  128
             String value = props.getProperty( key );
 93  
             try
 94  
             {
 95  128
                 value = interpolator.interpolate( value );
 96  
             }
 97  0
             catch ( InterpolationException e )
 98  
             {
 99  0
                 throw new AssemblyFormattingException( "Failed to interpolate property value: '" + value + "' for key: '" + key + "'. Reason: " + e.getMessage(), e );
 100  128
             }
 101  
 
 102  128
             props.setProperty( key, value );
 103  
         }
 104  
 
 105  10
         return props;
 106  
     }
 107  
 
 108  
 }