Coverage Report - org.apache.maven.plugin.assembly.utils.PropertyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyUtils
84%
21/25
88%
7/8
0
 
 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  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 37  
  * @version $Id: PropertyUtils.java 999612 2010-09-21 20:34:50Z jdcasey $
 38  
  */
 39  
 public final class PropertyUtils
 40  
 {
 41  
     private PropertyUtils()
 42  0
     {
 43  
         // prevent instantiation
 44  0
     }
 45  
 
 46  
     /**
 47  
      * Reads a property file, resolving all internal variables.
 48  
      * 
 49  
      * @param propfile
 50  
      *            The property file to load
 51  
      * @param fail
 52  
      *            wheter to throw an exception when the file cannot be loaded or to return null
 53  
      * @param useSystemProps
 54  
      *            wheter to incorporate System.getProperties settings into the returned Properties object.
 55  
      * @return the loaded and fully resolved Properties object
 56  
      */
 57  
     public static Properties getInterpolatedPropertiesFromFile( final File propfile, final boolean fail,
 58  
                                                                 final boolean useSystemProps )
 59  
         throws IOException, AssemblyFormattingException
 60  
     {
 61  
         Properties props;
 62  
 
 63  6
         if ( useSystemProps )
 64  
         {
 65  1
             props = new Properties( System.getProperties() );
 66  
         }
 67  
         else
 68  
         {
 69  5
             props = new Properties();
 70  
         }
 71  
 
 72  6
         if ( propfile.exists() )
 73  
         {
 74  5
             final FileInputStream inStream = new FileInputStream( propfile );
 75  
             try
 76  
             {
 77  5
                 props.load( inStream );
 78  
             }
 79  
             finally
 80  
             {
 81  5
                 IOUtil.close( inStream );
 82  5
             }
 83  5
         }
 84  1
         else if ( fail )
 85  
         {
 86  1
             throw new FileNotFoundException( propfile.toString() );
 87  
         }
 88  
 
 89  5
         final StringSearchInterpolator interpolator = new StringSearchInterpolator();
 90  5
         interpolator.addValueSource( new PropertiesBasedValueSource( props ) );
 91  
 
 92  
         for ( @SuppressWarnings( "rawtypes" )
 93  5
         final Enumeration n = props.propertyNames(); n.hasMoreElements(); )
 94  
         {
 95  66
             final String key = (String) n.nextElement();
 96  66
             String value = props.getProperty( key );
 97  
             try
 98  
             {
 99  66
                 value = interpolator.interpolate( value );
 100  
             }
 101  0
             catch ( final InterpolationException e )
 102  
             {
 103  0
                 throw new AssemblyFormattingException( "Failed to interpolate property value: '" + value
 104  
                                 + "' for key: '" + key + "'. Reason: " + e.getMessage(), e );
 105  66
             }
 106  
 
 107  66
             props.setProperty( key, value );
 108  66
         }
 109  
 
 110  5
         return props;
 111  
     }
 112  
 
 113  
 }