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 * <log4j key="log4j_key" fileName="log4j xml or property file name" />
14 * </pre>
15 *
16 * @author Henry Henry
17 * @since 1.0
18 */
19 public class Log4JRecordImpl extends AbstractRecordImpl {
20 private static final ConfigEntryParser PARSER_DOM = new Log4JDomParserImpl();
21 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 super();
28 }
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 super(key, fileName);
37 }
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 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 ConfigEntryParser parser = null;
49 if (getFileName().endsWith(".xml")) {
50 parser = PARSER_DOM;
51 } else if (getFileName().endsWith(".properties")) {
52 parser = PARSER_PROPS;
53 } else {
54 throw new ConfigurationManagerException("The log configuration for key " + getKey() + " and fileName " + getFileName() + " -> the file name should end with either .xml or .properties");
55 }
56 return parser;
57 }
58
59 }