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.taglib;
18  
19  import java.util.WeakHashMap;
20  
21  import javax.servlet.ServletContext;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.message.MessageFactory;
25  import org.apache.logging.log4j.spi.AbstractLogger;
26  import org.apache.logging.log4j.spi.ExtendedLogger;
27  import org.apache.logging.log4j.spi.LoggerContext;
28  import org.apache.logging.log4j.spi.LoggerContextKey;
29  
30  /**
31   * This bridge between the tag library and the Log4j API ensures that instances of {@link Log4jTaglibLogger} are
32   * appropriately held in memory and not constantly recreated.
33   *
34   * @since 2.0
35   */
36  final class Log4jTaglibLoggerContext implements LoggerContext {
37      // These were change to WeakHashMaps to avoid ClassLoader (memory) leak, something that's particularly
38      // important in Servlet containers.
39      private static final WeakHashMap<ServletContext, Log4jTaglibLoggerContext> CONTEXTS =
40              new WeakHashMap<>();
41  
42      private final WeakHashMap<String, Log4jTaglibLogger> loggers =
43              new WeakHashMap<>();
44  
45      private final ServletContext servletContext;
46  
47      private Log4jTaglibLoggerContext(final ServletContext servletContext) {
48          this.servletContext = servletContext;
49      }
50  
51      @Override
52      public Object getExternalContext() {
53          return this.servletContext;
54      }
55  
56      @Override
57      public Log4jTaglibLogger getLogger(final String name) {
58          return this.getLogger(name, null);
59      }
60  
61      @Override
62      public Log4jTaglibLogger getLogger(final String name, final MessageFactory messageFactory) {
63          // Note: This is the only method where we add entries to the 'loggers' ivar. 
64          // The loggers map key is the logger name plus the messageFactory FQCN.
65          String key = LoggerContextKey.create(name, messageFactory);
66          Log4jTaglibLogger logger = this.loggers.get(key);
67          if (logger != null) {
68              AbstractLogger.checkMessageFactory(logger, messageFactory);
69              return logger;
70          }
71  
72          synchronized (this.loggers) {
73              logger = this.loggers.get(key);
74              if (logger == null) {
75                  final LoggerContext context = LogManager.getContext(false);
76                  final ExtendedLogger original = messageFactory == null ?
77                          context.getLogger(name) : context.getLogger(name, messageFactory);
78                  // wrap a logger from an underlying implementation
79                  logger = new Log4jTaglibLogger(original, name, original.getMessageFactory());
80                  key = LoggerContextKey.create(name, original.getMessageFactory());
81                  this.loggers.put(key, logger);
82              }
83          }
84  
85          return logger;
86      }
87  
88      @Override
89      public boolean hasLogger(final String name) {
90          return loggers.containsKey(LoggerContextKey.create(name));
91      }
92  
93      @Override
94      public boolean hasLogger(String name, MessageFactory messageFactory) {
95          return loggers.containsKey(LoggerContextKey.create(name, messageFactory));
96      }
97  
98      @Override
99      public boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass) {
100         return loggers.containsKey(LoggerContextKey.create(name, messageFactoryClass));
101     }
102 
103     static synchronized Log4jTaglibLoggerContext getInstance(final ServletContext servletContext) {
104         Log4jTaglibLoggerContext loggerContext = CONTEXTS.get(servletContext);
105         if (loggerContext != null) {
106             return loggerContext;
107         }
108 
109         synchronized (CONTEXTS) {
110             loggerContext = CONTEXTS.get(servletContext);
111             if (loggerContext == null) {
112                 loggerContext = new Log4jTaglibLoggerContext(servletContext);
113                 CONTEXTS.put(servletContext, loggerContext);
114             }
115         }
116 
117         return loggerContext;
118     }
119 }