Coverage Report - org.naftulin.configmgr.parsers.ConfigurationRecordsToEntriesConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationRecordsToEntriesConverter
80%
36/45
62%
10/16
0
 
 1  
 package org.naftulin.configmgr.parsers;
 2  
 
 3  
 import java.lang.management.ManagementFactory;
 4  
 import java.net.URL;
 5  
 import java.util.Iterator;
 6  
 import java.util.List;
 7  
 
 8  
 import javax.management.JMException;
 9  
 import javax.management.MBeanServer;
 10  
 import javax.management.ObjectName;
 11  
 
 12  
 import org.naftulin.configmgr.ConfigurationManagementEntry;
 13  
 import org.naftulin.configmgr.ConfigurationManagementEntryImpl;
 14  
 import org.naftulin.configmgr.ConfigurationManagerConsole;
 15  
 import org.naftulin.configmgr.ConfigurationManagerException;
 16  
 import org.naftulin.configmgr.ConfigurationType;
 17  
 import org.naftulin.configmgr.content.AbstractRecordImpl;
 18  
 import org.naftulin.configmgr.content.MasterRecordImpl;
 19  
 import org.apache.log4j.Logger;
 20  
 
 21  
 /**
 22  
  * Converts records that were parsed from the configuration file into configuration entries.
 23  
  * Given a master record produces an array of configuration entries.
 24  
  * 
 25  
  * @author Henry Naftulin
 26  
  * @since 1.0
 27  
  */
 28  90
 public class ConfigurationRecordsToEntriesConverter {
 29  5
         private static final Logger log = Logger.getLogger(ConfigurationRecordsToEntriesConverter.class);
 30  5
         private static final MasterRecordParser PARSER = new MasterRecordParser();
 31  
         
 32  
         /**
 33  
          * Returns configuration management entry. Checks the record passed and if the
 34  
          * record is ok delegates to parser to create configuration management entry.
 35  
          * @param record a record from master configuration to be converted.
 36  
          * @return configuration management entry.
 37  
          * @throws ConfigurationManagerException if the record is in error, or if the
 38  
          *                 configuration cannot be parsed.
 39  
          */
 40  
         private ConfigurationManagementEntry convertRecordToEntry(final AbstractRecordImpl record) throws ConfigurationManagerException {
 41  345
                 final ConfigEntryParser parser = record.getParser();
 42  345
                 if (parser == null) {
 43  0
                         throw new ConfigurationManagerException("Parser for " + record + " is not defined. Please check the configuration manager configuartion file and remove this record.");
 44  
                 }
 45  345
                 URL fileUrl= null;
 46  345
                 if (record.isGetUrlForFile()) {
 47  265
                         fileUrl =ConfigurationRecordsToEntriesConverter.class.getClassLoader().getResource(record.getFileName());
 48  265
                         if (fileUrl == null) {
 49  10
                                 throw new ConfigurationManagerException("Could not find file " + record.getFileName() + " for record " + record + ". Please check your configuration manager config file and point this configuratoin to an existing file.");
 50  
                         }
 51  
                 }
 52  335
                 return parser.getConfigurationManagementEntry(record.getKey(), fileUrl);
 53  
         }
 54  
         
 55  
         /**
 56  
          * Parses the master configuration file and creates configuration management entries.
 57  
          * If JMX is set to true, will register Configuration manager with MBean server.
 58  
          * @param configurationFileName the file name for the master configuration file.
 59  
          * @return configuration management entries described in the master configuration file.
 60  
          * @throws ConfigurationManagerException if master configuration cannot be read.
 61  
          */
 62  
         public ConfigurationManagementEntry[] createConfigurationEntriesFromConfigurationFile(final String configurationFileName) 
 63  
                 throws ConfigurationManagerException{
 64  80
                 final MasterRecordImpl masterRecord = PARSER.digestMasterRecord(configurationFileName);
 65  80
                 final ConfigurationManagementEntry[] entries = createEntiresFromMasterRecord(masterRecord);
 66  80
                 if (masterRecord.isJmx()) {
 67  0
                         final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
 68  
                     ObjectName name;
 69  
                         try {
 70  0
                                 name = new ObjectName("configurationManager:type=Manager");                                
 71  0
                                 final ConfigurationManagerConsole mbean = new ConfigurationManagerConsole();
 72  0
                                 if (! mbs.isRegistered(name)) {
 73  0
                                         mbs.registerMBean(mbean, name);
 74  
                                 }
 75  0
                         }  catch (JMException e) {
 76  0
                                 log.error("Could not register with JMX MBeanServer", e);
 77  
                         }
 78  
                 }
 79  
                 
 80  80
                 return entries;
 81  
         }
 82  
 
 83  
         /**
 84  
          * Parses the configuration files described in the master configuration entry. 
 85  
          * @param masterRecord the master record
 86  
          * @return configuration entries parsed from configuration file described by master configuration entry.
 87  
          * @throws ConfigurationManagerException if master configuration entry is in error.
 88  
          */
 89  
         ConfigurationManagementEntry[] createEntiresFromMasterRecord(final MasterRecordImpl masterRecord ) throws ConfigurationManagerException{
 90  85
                 log.debug("starting to parse the configuration master record");
 91  
                 
 92  85
                 if (masterRecord == null) {
 93  0
                         throw new IllegalArgumentException("Master record is null in convertMasterRecord.");
 94  
                 }
 95  85
                 final List<AbstractRecordImpl> records = masterRecord.getRecords();
 96  85
                 final ConfigurationManagementEntry entries[] = new ConfigurationManagementEntry[ records.size() + 1];
 97  
                 
 98  
                                 
 99  85
                 AbstractRecordImpl record = masterRecord;
 100  
                 
 101  85
                 entries[0] = convertRecordToEntry(record);
 102  85
                 log.debug("parsed master record " + entries[0]);
 103  
                 
 104  85
                 final Iterator<AbstractRecordImpl> it = records.iterator();
 105  85
                 int i=1; //0th record is a master record
 106  85
                 boolean errorOccured = false;
 107  430
                 while(it.hasNext()) {
 108  260
                         record =  it.next();
 109  
                         try {
 110  260
                                 entries[i] = convertRecordToEntry(record);
 111  170
                                 log.debug("parsed record: " + entries[i]);
 112  90
                         } catch(ConfigurationManagerException e)  {
 113  90
                                 log.error("ERROR Cannot load configuration entry " + record, e);
 114  90
                                 entries[i] = new ConfigurationManagementEntryImpl(record.getKey(), record.getFileName(), e, null, ConfigurationType.ERROR);
 115  90
                                 errorOccured = true;
 116  
                         } 
 117  260
                         ++i;
 118  
                 }
 119  85
                 log.debug("Finished loading all entries " + (errorOccured ? "with errors": ""));
 120  85
                 return entries;
 121  
         }
 122  
 
 123  
         
 124  
 }