View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.jcl;
18  
19  import java.util.Map;
20  import java.util.WeakHashMap;
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.ConcurrentMap;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogConfigurationException;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.spi.LoggerContext;
29  import org.apache.logging.log4j.spi.ExtendedLogger;
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 ConcurrentMap<String, Log> loggers = getLoggersMap();
44          if (loggers.containsKey(name)) {
45              return loggers.get(name);
46          }
47          loggers.putIfAbsent(name, new Log4jLog(PrivateManager.getLogger(name)));
48          return loggers.get(name);
49      }
50  
51      private ConcurrentMap<String, Log> getLoggersMap() {
52          final LoggerContext context = PrivateManager.getContext();
53          synchronized (contextMap) {
54              ConcurrentMap<String, Log> map = contextMap.get(context);
55              if (map == null) {
56                  map = new ConcurrentHashMap<String, Log>();
57                  contextMap.put(context, map);
58              }
59              return map;
60          }
61      }
62  
63      @Override
64      public Object getAttribute(final String name) {
65          return attributes.get(name);
66      }
67  
68      @Override
69      public String[] getAttributeNames() {
70          return attributes.keySet().toArray(new String[attributes.size()]);
71      }
72  
73      @Override
74      public Log getInstance(@SuppressWarnings("rawtypes") final Class clazz) throws LogConfigurationException {
75          return getInstance(clazz.getName());
76      }
77  
78      /**
79       * This method is supposed to clear all loggers. In this implementation it will clear all the logger
80       * wrappers but the loggers managed by the underlying logger context will not be.
81       */
82      @Override
83      public void release() {
84          getLoggersMap().clear();
85      }
86  
87      @Override
88      public void removeAttribute(final String name) {
89          attributes.remove(name);
90      }
91  
92      @Override
93      public void setAttribute(final String name, final Object value) {
94          if (value != null) {
95              attributes.put(name, value);
96          } else {
97              removeAttribute(name);
98          }
99      }
100 
101     /**
102      * The real bridge between commons logging and Log4j.
103      */
104     private static class PrivateManager extends LogManager {
105         private static final String FQCN = LogFactory.class.getName();
106 
107         public static LoggerContext getContext() {
108             return getContext(FQCN, false);
109         }
110 
111         public static ExtendedLogger getLogger(final String name) {
112             return getContext().getLogger(name);
113         }
114     }
115 
116 }