1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.logging;
12
13 import java.lang.ref.PhantomReference;
14 import java.lang.ref.ReferenceQueue;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogConfigurationException;
22 import org.apache.commons.logging.impl.MuleLocationAwareLog;
23 import org.apache.commons.logging.impl.MuleLog;
24 import org.apache.commons.logging.impl.SLF4JLogFactory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.spi.LocationAwareLogger;
28
29 public class MuleLogFactory extends SLF4JLogFactory
30 {
31 protected ConcurrentHashMap<Integer, ConcurrentMap<String, Log>> repository = new ConcurrentHashMap<Integer, ConcurrentMap<String, Log>>();
32
33 protected static final Integer NO_CCL_CLASSLOADER = 0;
34
35 protected ReferenceQueue<ClassLoader> referenceQueue = new ReferenceQueue<ClassLoader>();
36
37 protected Map<PhantomReference<ClassLoader>, Integer> refs = new HashMap<PhantomReference<ClassLoader>, Integer>();
38
39 public MuleLogFactory()
40 {
41 new LoggerReferenceHandler("Mule.log.clogging.ref.handler", referenceQueue, refs, repository);
42 }
43
44 public Log getInstance(String name) throws LogConfigurationException
45 {
46 final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
47 ConcurrentMap<String, Log> loggerMap = repository.get(ccl == null ? NO_CCL_CLASSLOADER : ccl.hashCode());
48
49 if (loggerMap == null)
50 {
51 loggerMap = new ConcurrentHashMap<String, Log>();
52
53 final ConcurrentMap<String, Log> previous = repository.putIfAbsent(ccl == null ? NO_CCL_CLASSLOADER : ccl.hashCode(), loggerMap);
54 if (previous != null)
55 {
56 loggerMap = previous;
57 }
58
59 if (ccl != null)
60 {
61
62 refs.put(new PhantomReference<ClassLoader>(ccl, referenceQueue), ccl.hashCode());
63 }
64
65 }
66
67 Log instance = loggerMap.get(name);
68
69 if (instance == null)
70 {
71 Logger logger = LoggerFactory.getLogger(name);
72 if (logger instanceof LocationAwareLogger)
73 {
74 instance = new MuleLocationAwareLog((LocationAwareLogger) logger);
75 }
76 else
77 {
78 instance = new MuleLog(logger);
79 }
80 final Log previous = loggerMap.putIfAbsent(name, instance);
81 if (previous != null)
82 {
83
84 instance = previous;
85 }
86 }
87
88 return instance;
89 }
90 }