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.AbstractLogger;
29  import org.apache.logging.log4j.spi.LoggerContext;
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          final org.apache.logging.log4j.Logger logger = PrivateManager.getLogger(name);
48          if (logger instanceof AbstractLogger) {
49              loggers.putIfAbsent(name, new Log4jLog((AbstractLogger) logger, name));
50              return loggers.get(name);
51          }
52          throw new LogConfigurationException(
53              "Commons Logging Adapter requires base logging system to extend Log4j AbstractLogger");
54      }
55  
56      private ConcurrentMap<String, Log> getLoggersMap() {
57          final LoggerContext context = PrivateManager.getContext();
58          synchronized (contextMap) {
59              ConcurrentMap<String, Log> map = contextMap.get(context);
60              if (map == null) {
61                  map = new ConcurrentHashMap<String, Log>();
62                  contextMap.put(context, map);
63              }
64              return map;
65          }
66      }
67  
68      @Override
69      public Object getAttribute(final String name) {
70          return attributes.get(name);
71      }
72  
73      @Override
74      public String[] getAttributeNames() {
75          return attributes.keySet().toArray(new String[attributes.size()]);
76      }
77  
78      @Override
79      public Log getInstance(@SuppressWarnings("rawtypes") final Class clazz) throws LogConfigurationException {
80          return getInstance(clazz.getName());
81      }
82  
83      /**
84       * This method is supposed to clear all loggers. In this implementation it will clear all the logger
85       * wrappers but the loggers managed by the underlying logger context will not be.
86       */
87      @Override
88      public void release() {
89          getLoggersMap().clear();
90      }
91  
92      @Override
93      public void removeAttribute(final String name) {
94          attributes.remove(name);
95      }
96  
97      @Override
98      public void setAttribute(final String name, final Object value) {
99          if (value != null) {
100             attributes.put(name, value);
101         } else {
102             removeAttribute(name);
103         }
104     }
105 
106     /**
107      * The real bridge between commons logging and Log4j.
108      */
109     private static class PrivateManager extends LogManager {
110         private static final String FQCN = LogFactory.class.getName();
111 
112         public static LoggerContext getContext() {
113             return getContext(FQCN, false);
114         }
115 
116         public static org.apache.logging.log4j.Logger getLogger(final String name) {
117             return getLogger(FQCN, name);
118         }
119     }
120 
121 }