View Javadoc

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  public class ConfigurationRecordsToEntriesConverter {
29  	private static final Logger log = Logger.getLogger(ConfigurationRecordsToEntriesConverter.class);
30  	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  		final ConfigEntryParser parser = record.getParser();
42  		if (parser == null) {
43  			throw new ConfigurationManagerException("Parser for " + record + " is not defined. Please check the configuration manager configuartion file and remove this record.");
44  		}
45  		URL fileUrl= null;
46  		if (record.isGetUrlForFile()) {
47  			fileUrl =ConfigurationRecordsToEntriesConverter.class.getClassLoader().getResource(record.getFileName());
48  			if (fileUrl == null) {
49  				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  		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  		final MasterRecordImpl masterRecord = PARSER.digestMasterRecord(configurationFileName);
65  		final ConfigurationManagementEntry[] entries = createEntiresFromMasterRecord(masterRecord);
66  		if (masterRecord.isJmx()) {
67  			final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
68  		    ObjectName name;
69  			try {
70  				name = new ObjectName("configurationManager:type=Manager");				
71  				final ConfigurationManagerConsole mbean = new ConfigurationManagerConsole();
72  				if (! mbs.isRegistered(name)) {
73  					mbs.registerMBean(mbean, name);
74  				}
75  			}  catch (JMException e) {
76  				log.error("Could not register with JMX MBeanServer", e);
77  			}
78  		}
79  		
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  		log.debug("starting to parse the configuration master record");
91  		
92  		if (masterRecord == null) {
93  			throw new IllegalArgumentException("Master record is null in convertMasterRecord.");
94  		}
95  		final List<AbstractRecordImpl> records = masterRecord.getRecords();
96  		final ConfigurationManagementEntry entries[] = new ConfigurationManagementEntry[ records.size() + 1];
97  		
98  				
99  		AbstractRecordImpl record = masterRecord;
100 		
101 		entries[0] = convertRecordToEntry(record);
102 		log.debug("parsed master record " + entries[0]);
103 		
104 		final Iterator<AbstractRecordImpl> it = records.iterator();
105 		int i=1; //0th record is a master record
106 		boolean errorOccured = false;
107 		while(it.hasNext()) {
108 			record =  it.next();
109 			try {
110 				entries[i] = convertRecordToEntry(record);
111 				log.debug("parsed record: " + entries[i]);
112 			} catch(ConfigurationManagerException e)  {
113 				log.error("ERROR Cannot load configuration entry " + record, e);
114 				entries[i] = new ConfigurationManagementEntryImpl(record.getKey(), record.getFileName(), e, null, ConfigurationType.ERROR);
115 				errorOccured = true;
116 			} 
117 			++i;
118 		}
119 		log.debug("Finished loading all entries " + (errorOccured ? "with errors": ""));
120 		return entries;
121 	}
122 
123 	
124 }