Coverage Report - org.naftulin.configmgr.parsers.PropertyFileParserImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyFileParserImpl
70%
14/20
50%
3/6
3.667
 
 1  
 package org.naftulin.configmgr.parsers;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.InputStream;
 5  
 import java.net.URL;
 6  
 import java.util.Properties;
 7  
 
 8  
 import org.apache.log4j.Logger;
 9  
 import org.naftulin.configmgr.ConfigurationManagementEntryImpl;
 10  
 import org.naftulin.configmgr.ConfigurationManagerException;
 11  
 import org.naftulin.configmgr.ConfigurationManagementEntry;
 12  
 import org.naftulin.configmgr.ConfigurationType;
 13  
 
 14  
 /**
 15  
  * Configuration entry parser converts property configuration record into property configuration management enttry.
 16  
  * It parses the property file that the record describes, and stores the configuration
 17  
  * as a properties in content of an {@link org.naftulin.configmgr.ConfigurationManagementEntry entry}.
 18  
  * 
 19  
  * @author Henry Naftulin
 20  
  * @since 1.0
 21  
  */
 22  75
 public class PropertyFileParserImpl implements ConfigEntryParser {
 23  
         private static final long serialVersionUID = 1L;
 24  5
         private static final Logger log = Logger.getLogger(PropertyFileParserImpl.class);
 25  
         
 26  
         /**
 27  
          * Retrurns a configuration managment entry by parsing the property file passed in.
 28  
          * @param key the key configuration entry will be assigned
 29  
          * @param fileUrl the file URL to be parsed.
 30  
          * @return a configuration managment entry by parsing the record passed in.
 31  
          * @throws ConfigurationManagerException if an error occurs while parsing an entry.
 32  
          */
 33  
         public ConfigurationManagementEntry getConfigurationManagementEntry(final String key, 
 34  
                         final URL fileUrl) throws ConfigurationManagerException {
 35  85
                 validateParameters(key, fileUrl);
 36  
                 
 37  85
                 final String fileName = fileUrl.getFile();
 38  
                 
 39  85
                 final Properties content = new Properties();
 40  
                 InputStream stream;
 41  
                 try {
 42  85
                         stream = fileUrl.openStream();                
 43  85
                         content.load(stream );
 44  0
                 } catch (IOException e) {
 45  0
                         log.warn("Error while reading property file", e);
 46  0
                         throw new ConfigurationManagerException("Error while reading property file",e);
 47  
                 }
 48  85
                 final ConfigurationManagementEntry entry = new ConfigurationManagementEntryImpl(key, fileName, content, this, ConfigurationType.PROPERTIES);
 49  
                 
 50  85
                 return entry;
 51  
         }
 52  
 
 53  
         private void validateParameters(final String key, final URL fileUrl) throws ConfigurationManagerException {
 54  85
                 if (fileUrl == null) {
 55  0
                         throw new ConfigurationManagerException("file URL is null");
 56  
                 }
 57  85
                 if (fileUrl.getFile() == null) {
 58  0
                         throw new ConfigurationManagerException("file name passed in the URL " + fileUrl + " is null");
 59  
                 }
 60  85
                 if (key == null) { 
 61  0
                         throw new ConfigurationManagerException("key is null");
 62  
                 }
 63  85
         }
 64  
         
 65  
         /**
 66  
          * Returns a string representation of this parser.
 67  
          * @return a string representation of this parser.
 68  
          */
 69  
         public String toString() {
 70  190
                 return "Property file parser";
 71  
         }
 72  
 }