View Javadoc

1   /*
2    * $Id: MuleLogFactory.java 21512 2011-03-09 19:21:20Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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      // map ref back to the classloader hash for cleanup of repository map, as both Weak- and SoftReference's get() return null by this time
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                  // must save a strong ref to the PhantomReference in order for it to stay alive and work
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                  // someone got there before us
84                  instance = previous;
85              }
86          }
87  
88          return instance;
89      }
90  }