Coverage Report - org.kuali.rice.krad.util.properties.FilePropertySource
 
Classes in this File Line Coverage Branch Coverage Complexity
FilePropertySource
46%
12/26
50%
3/6
2.6
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 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 org.kuali.rice.krad.util.properties;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.InputStream;
 20  
 import java.net.URL;
 21  
 import java.util.Properties;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.kuali.rice.krad.exception.PropertiesException;
 27  
 
 28  
 /**
 29  
  * This class is used to obtain properties from a properites file.
 30  
  * 
 31  
  * 
 32  
  */
 33  6
 public class FilePropertySource implements PropertySource {
 34  1
     private static Log log = LogFactory.getLog(FilePropertySource.class);
 35  
 
 36  
 
 37  
     private String fileName;
 38  
     private boolean allowOverrides;
 39  
 
 40  
     /**
 41  
      * Set source fileName.
 42  
      * 
 43  
      * @param fileName
 44  
      */
 45  
     public void setFileName(String fileName) {
 46  4
         this.fileName = fileName;
 47  4
     }
 48  
 
 49  
     /**
 50  
      * @return source fileName
 51  
      */
 52  
     public String getFileName() {
 53  12
         return this.fileName;
 54  
     }
 55  
 
 56  
     public boolean isAllowOverrides() {
 57  0
         return this.allowOverrides;
 58  
     }
 59  
 
 60  
     public void setAllowOverrides(boolean allowOverrides) {
 61  0
         this.allowOverrides = allowOverrides;
 62  0
     }
 63  
     
 64  
     /**
 65  
      * Attempts to load properties from a properties file which has the current fileName and is located on the classpath.
 66  
      * 
 67  
      * @see org.kuali.rice.krad.util.properties.PropertySource#loadProperties()
 68  
      * @throws IllegalStateException if the fileName is null or empty
 69  
      */
 70  
     public Properties loadProperties() {
 71  6
         if (StringUtils.isBlank(getFileName())) {
 72  3
             throw new IllegalStateException("invalid (blank) fileName");
 73  
         }
 74  
 
 75  3
         Properties properties = new Properties();
 76  
 
 77  3
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 78  3
         URL url = loader.getResource(getFileName());
 79  3
         if (url == null) {
 80  3
             throw new PropertiesException("unable to locate properties file '" + getFileName() + "'");
 81  
         }
 82  
 
 83  0
         InputStream in = null;
 84  
 
 85  
         try {
 86  0
             in = url.openStream();
 87  0
             properties.load(in);
 88  
         }
 89  0
         catch (IOException e) {
 90  0
             throw new PropertiesException("error loading from properties file '" + getFileName() + "'", e);
 91  
         }
 92  
         finally {
 93  0
             if (in != null) {
 94  
                 try {
 95  0
                     in.close();
 96  
                 }
 97  0
                 catch (IOException e) {
 98  0
                     log.error("caught exception closing InputStream: " + e);
 99  0
                 }
 100  
 
 101  
             }
 102  
         }
 103  
 
 104  0
         return properties;
 105  
     }
 106  
 
 107  
 }