Coverage Report - org.naftulin.configmgr.content.Log4JRecordImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
Log4JRecordImpl
93%
14/15
67%
4/6
2.667
 
 1  
 package org.naftulin.configmgr.content;
 2  
 
 3  
 import org.naftulin.configmgr.ConfigurationManagerException;
 4  
 import org.naftulin.configmgr.parsers.ConfigEntryParser;
 5  
 import org.naftulin.configmgr.parsers.Log4JDomParserImpl;
 6  
 import org.naftulin.configmgr.parsers.Log4JPropertiesParserImpl;
 7  
 
 8  
 /**
 9  
  * Represents a log4j entry, with it's key and log4j file name.
 10  
  * Intended to mirror master configuration rules for log4j xml or properties configuration.
 11  
  * The external configuraton xml is:
 12  
  * <pre>
 13  
  *         &lt;log4j key="log4j_key" fileName="log4j xml or property file name" /&gt;
 14  
  * </pre>
 15  
  * 
 16  
  * @author Henry Henry
 17  
  * @since 1.0
 18  
  */
 19  5
 public class Log4JRecordImpl extends AbstractRecordImpl {
 20  5
         private static final ConfigEntryParser PARSER_DOM = new Log4JDomParserImpl();
 21  5
         private static final ConfigEntryParser PARSER_PROPS = new Log4JPropertiesParserImpl();
 22  
         
 23  
         /**
 24  
          * Constructs a record without specifying the key or file name.
 25  
          */
 26  
         public Log4JRecordImpl() {
 27  100
                 super();
 28  100
         }
 29  
 
 30  
         /**
 31  
          * Constructs a record with the key and file name specified.
 32  
          * @param key the key associated with the record.
 33  
          * @param fileName the file name associated with the record.
 34  
          */
 35  
         public Log4JRecordImpl(final String key, final String fileName) {
 36  10
                 super(key, fileName);
 37  10
         }
 38  
 
 39  
         /**
 40  
          * Returns an instance of either {@link Log4JDomParserImpl Dom} or 
 41  
          * {@link Log4JPropertiesParserImpl properties} parser that can parse the log4j file
 42  
          * and (re)initialize log4j. 
 43  
          * @return the parser that can parse the file to extract the configuration.
 44  
          * @throws ConfigurationManagerException if an error occured while instantiating the parser.
 45  
          */
 46  
         public ConfigEntryParser getParser() throws ConfigurationManagerException {
 47  90
                 if (getFileName() == null) throw new ConfigurationManagerException("Cannot determine the configuraton since fileName is null. Please check log4j in you configuration file under key " + getKey());
 48  90
                 ConfigEntryParser parser = null;
 49  90
                 if (getFileName().endsWith(".xml")) {
 50  85
                         parser = PARSER_DOM;
 51  5
                 } else if (getFileName().endsWith(".properties")) {
 52  5
                         parser = PARSER_PROPS;
 53  
                 } else {
 54  0
                         throw new ConfigurationManagerException("The log configuration for key " + getKey() + " and fileName " + getFileName() + " -> the file name should end with either .xml or .properties");
 55  
                 }
 56  90
                 return parser;
 57  
         }
 58  
 
 59  
 }