1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.jcl;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogConfigurationException;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.spi.AbstractLogger;
24 import org.apache.logging.log4j.spi.LoggerContext;
25
26 import java.util.Map;
27 import java.util.WeakHashMap;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentMap;
30
31
32
33
34 public class LogFactoryImpl extends LogFactory {
35
36 private final Map<LoggerContext, ConcurrentMap<String, Log>> contextMap =
37 new WeakHashMap<LoggerContext, ConcurrentMap<String, Log>>();
38
39 private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>();
40
41 @Override
42 public Log getInstance(final String name) throws LogConfigurationException {
43 final LoggerContext context = PrivateManager.getContext();
44 final ConcurrentMap<String, Log> loggers = getLoggersMap();
45 if (loggers.containsKey(name)) {
46 return loggers.get(name);
47 }
48 final org.apache.logging.log4j.Logger logger = PrivateManager.getLogger(name);
49 if (logger instanceof AbstractLogger) {
50 loggers.putIfAbsent(name, new Log4JLog((AbstractLogger) logger, name));
51 return loggers.get(name);
52 }
53 throw new LogConfigurationException(
54 "Commons Logging Adapter requires base logging system to extend Log4J AbstractLogger");
55 }
56
57 private ConcurrentMap<String, Log> getLoggersMap() {
58 final LoggerContext context = PrivateManager.getContext();
59 synchronized (contextMap) {
60 ConcurrentMap<String, Log> map = contextMap.get(context);
61 if (map == null) {
62 map = new ConcurrentHashMap<String, Log>();
63 contextMap.put(context, map);
64 }
65 return map;
66 }
67 }
68
69 @Override
70 public Object getAttribute(final String name) {
71 return attributes.get(name);
72 }
73
74 @Override
75 public String[] getAttributeNames() {
76 return attributes.keySet().toArray(new String[attributes.size()]);
77 }
78
79 @Override
80 public Log getInstance(final Class clazz) throws LogConfigurationException {
81 return getInstance(clazz.getName());
82 }
83
84
85
86
87
88 @Override
89 public void release() {
90 getLoggersMap().clear();
91 }
92
93 @Override
94 public void removeAttribute(final String name) {
95 attributes.remove(name);
96 }
97
98 @Override
99 public void setAttribute(final String name, final Object value) {
100 if (value != null) {
101 attributes.put(name, value);
102 } else {
103 removeAttribute(name);
104 }
105 }
106
107
108
109
110 private static class PrivateManager extends LogManager {
111 private static final String FQCN = LogFactory.class.getName();
112
113 public static LoggerContext getContext() {
114 return getContext(FQCN, false);
115 }
116
117 public static org.apache.logging.log4j.Logger getLogger(final String name) {
118 return getLogger(FQCN, name);
119 }
120 }
121
122 }