1 | |
package org.naftulin.configmgr.parsers; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.io.InputStream; |
5 | |
import java.net.URL; |
6 | |
|
7 | |
import org.apache.log4j.Logger; |
8 | |
import org.naftulin.configmgr.ConfigurationManagementEntryImpl; |
9 | |
import org.naftulin.configmgr.ConfigurationManagerException; |
10 | |
import org.naftulin.configmgr.ConfigurationManagementEntry; |
11 | |
import org.naftulin.configmgr.ConfigurationType; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | 25 | public abstract class AbstractLog4JParser extends AbstractConfigEntryParser { |
23 | 5 | private static final Logger log = Logger.getLogger(AbstractLog4JParser.class); |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
abstract void doCallLog4JConfigurator(URL fileUrl); |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public ConfigurationManagementEntry getConfigurationManagementEntry(final String key, final URL fileUrl) throws ConfigurationManagerException { |
39 | 120 | checkParameters(key, fileUrl); |
40 | |
|
41 | 120 | final String fileName = fileUrl.getFile(); |
42 | 120 | String content = null; |
43 | |
try { |
44 | 120 | final InputStream stream = fileUrl.openStream(); |
45 | 120 | log.debug("reading log4j configuration from file " + fileName); |
46 | 120 | content = readStreamContentAsString(stream); |
47 | 120 | log.debug("log4j configuration is " + content); |
48 | 120 | doCallLog4JConfigurator(fileUrl); |
49 | 120 | log.info("reloaded log4j configuratoin from " + fileName); |
50 | 0 | } catch (IOException e) { |
51 | 0 | log.warn("Error while reading log4j file", e); |
52 | 0 | throw new ConfigurationManagerException("Error while reading log4j file",e); |
53 | |
} |
54 | 120 | final ConfigurationManagementEntry entry = new ConfigurationManagementEntryImpl(key, fileName , content, this, getConfigurationType()); |
55 | 120 | log.info("configured entry " + entry); |
56 | 120 | return entry; |
57 | |
} |
58 | |
|
59 | |
private void checkParameters(final String key, final URL fileUrl) |
60 | |
throws ConfigurationManagerException { |
61 | 120 | if (fileUrl == null) { |
62 | 0 | throw new ConfigurationManagerException("file URL is null"); |
63 | |
} |
64 | 120 | if (fileUrl.getFile() == null) { |
65 | 0 | throw new ConfigurationManagerException("file name passed in the URL " + fileUrl + " is null"); |
66 | |
} |
67 | 120 | if (key == null) { |
68 | 0 | throw new ConfigurationManagerException("key is null"); |
69 | |
} |
70 | 120 | } |
71 | |
|
72 | |
protected abstract ConfigurationType getConfigurationType(); |
73 | |
|
74 | |
} |