01: package net.javacoding.jspider.core.logging;
02:
03: import net.javacoding.jspider.core.logging.impl.SystemOutLogProvider;
04: import net.javacoding.jspider.core.util.config.*;
05:
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: /**
10: * $Id: LogFactory.java,v 1.6 2003/04/02 20:55:07 vanrogu Exp $
11: */
12: public class LogFactory {
13:
14: public static String[][] replacements = { {
15: "net.javacoding.jspider.", "" } };
16:
17: public static Map loggers = new HashMap();
18: public static LogFactory instance;
19:
20: protected LogProvider provider;
21:
22: protected LogFactory() {
23: PropertySet props = ConfigurationFactory.getConfiguration()
24: .getJSpiderConfiguration();
25: PropertySet logProps = new MappedPropertySet(
26: ConfigConstants.CONFIG_LOG, props);
27: Class providerClass = logProps.getClass(
28: ConfigConstants.CONFIG_LOG_PROVIDER,
29: SystemOutLogProvider.class);
30: try {
31: provider = (LogProvider) providerClass.newInstance();
32: } catch (InstantiationException e) {
33: // CANNOT HAPPEN - WE CHECKED BEFORE
34: } catch (IllegalAccessException e) {
35: // SHOULDN'T HAPPEN - NOT GONNA HANDLE IT
36: }
37: }
38:
39: protected static synchronized LogFactory getInstance() {
40: if (instance == null) {
41: instance = new LogFactory();
42: }
43: return instance;
44: }
45:
46: public static synchronized Log getLog(Class category) {
47: return getLog(category.getName());
48: }
49:
50: public static synchronized Log getLog(String category) {
51: String effectiveCategory = translate(category);
52: Log log = (Log) loggers.get(effectiveCategory);
53: if (log == null) {
54: log = getInstance().instantiate(effectiveCategory);
55: loggers.put(effectiveCategory, log);
56: }
57: return log;
58: }
59:
60: protected Log instantiate(String category) {
61: return provider.createLog(category);
62: }
63:
64: protected static String translate(String category) {
65: String effectiveCategory = category;
66: for (int i = 0; i < replacements.length; i++) {
67: String[] replacement = replacements[i];
68: if (category.startsWith(replacement[0])) {
69: effectiveCategory = replacement[1]
70: + category.substring(replacement[0].length());
71: break;
72: }
73: }
74: return effectiveCategory;
75: }
76:
77: }
|