Coverage Report - de.smartics.properties.spi.config.support.PropertiesHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesHelper
0%
0/14
N/A
N/A
1.75
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package de.smartics.properties.spi.config.support;
 17  
 
 18  
 import java.io.BufferedInputStream;
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.util.Properties;
 22  
 
 23  
 import org.apache.commons.io.IOUtils;
 24  
 
 25  
 import de.smartics.properties.api.config.domain.ConfigurationLoadingException;
 26  
 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
 27  
 
 28  
 /**
 29  
  * Simple utilities class to help on dealing with properties.
 30  
  */
 31  
 public final class PropertiesHelper
 32  
 {
 33  
   // ********************************* Fields *********************************
 34  
 
 35  
   // --- constants ------------------------------------------------------------
 36  
 
 37  
   // --- members --------------------------------------------------------------
 38  
 
 39  
   /**
 40  
    * The key to the configuration to load properties for.
 41  
    */
 42  
   private final ConfigurationKey<?> key;
 43  
 
 44  
   // ****************************** Initializer *******************************
 45  
 
 46  
   // ****************************** Constructors ******************************
 47  
 
 48  
   /**
 49  
    * Default constructor.
 50  
    *
 51  
    * @param key the key to the configuration to load properties for.
 52  
    */
 53  
   public PropertiesHelper(final ConfigurationKey<?> key)
 54  0
   {
 55  0
     this.key = key;
 56  0
   }
 57  
 
 58  
   // ****************************** Inner Classes *****************************
 59  
 
 60  
   // ********************************* Methods ********************************
 61  
 
 62  
   // --- init -----------------------------------------------------------------
 63  
 
 64  
   // --- get&set --------------------------------------------------------------
 65  
 
 66  
   // --- business -------------------------------------------------------------
 67  
 
 68  
   /**
 69  
    * Reads the properties from the thread context class path location.
 70  
    *
 71  
    * @param resourceId the resource on the class path to read.
 72  
    * @return the properties from the class path.
 73  
    */
 74  
   public Properties readProperties(final String resourceId)
 75  
   {
 76  0
     final ClassLoader loader = Thread.currentThread().getContextClassLoader();
 77  0
     return readProperties(loader, resourceId);
 78  
   }
 79  
 
 80  
   /**
 81  
    * Reads the properties from the given class path location.
 82  
    *
 83  
    * @param classLoader the class loader to access the resource.
 84  
    * @param resourceId the resource on the class path to read.
 85  
    * @return the properties from the class path.
 86  
    */
 87  
   public Properties readProperties(final ClassLoader classLoader,
 88  
       final String resourceId)
 89  
   {
 90  0
     final InputStream input = classLoader.getResourceAsStream(resourceId);
 91  0
     return readProperties(resourceId, input);
 92  
   }
 93  
 
 94  
   /**
 95  
    * Reads the properties from the given stream.
 96  
    * <p>
 97  
    * Closes the stream after the properties are read.
 98  
    * </p>
 99  
    *
 100  
    * @param resourceId the identifier of the stream. Used for error reporting.
 101  
    * @param input the stream to read from.
 102  
    * @return the properties from the stream.
 103  
    */
 104  
   public Properties readProperties(final String resourceId,
 105  
       final InputStream input)
 106  
   {
 107  0
     final BufferedInputStream bin = new BufferedInputStream(input);
 108  
     try
 109  
     {
 110  0
       final Properties properties = new Properties();
 111  0
       properties.load(bin);
 112  0
       return properties;
 113  
     }
 114  0
     catch (final IOException e)
 115  
     {
 116  0
       throw new ConfigurationLoadingException(e, key, resourceId);
 117  
     }
 118  
     finally
 119  
     {
 120  0
       IOUtils.closeQuietly(bin);
 121  
     }
 122  
   }
 123  
 
 124  
   // --- object basics --------------------------------------------------------
 125  
 
 126  
 }