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.LoggerRegistry;
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 = new WeakHashMap<>();
40  
41      private final LoggerRegistry<Log4jTaglibLogger> loggerRegistry = new LoggerRegistry<>(
42              new LoggerRegistry.WeakMapFactory<Log4jTaglibLogger>());
43  
44      private final ServletContext servletContext;
45  
46      private Log4jTaglibLoggerContext(final ServletContext servletContext) {
47          this.servletContext = servletContext;
48      }
49  
50      @Override
51      public Object getExternalContext() {
52          return this.servletContext;
53      }
54  
55      @Override
56      public Log4jTaglibLogger getLogger(final String name) {
57          return this.getLogger(name, null);
58      }
59  
60      @Override
61      public Log4jTaglibLogger getLogger(final String name, final MessageFactory messageFactory) {
62          // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
63          Log4jTaglibLogger logger = this.loggerRegistry.getLogger(name, messageFactory);
64          if (logger != null) {
65              AbstractLogger.checkMessageFactory(logger, messageFactory);
66              return logger;
67          }
68  
69          synchronized (this.loggerRegistry) {
70              logger = this.loggerRegistry.getLogger(name, messageFactory);
71              if (logger == null) {
72                  final LoggerContext context = LogManager.getContext(false);
73                  final ExtendedLogger original = messageFactory == null ?
74                          context.getLogger(name) : context.getLogger(name, messageFactory);
75                  // wrap a logger from an underlying implementation
76                  logger = new Log4jTaglibLogger(original, name, original.getMessageFactory());
77                  this.loggerRegistry.putIfAbsent(name, messageFactory, logger);
78              }
79          }
80  
81          return logger;
82      }
83  
84      @Override
85      public boolean hasLogger(final String name) {
86          return loggerRegistry.hasLogger(name);
87      }
88  
89      @Override
90      public boolean hasLogger(final String name, final MessageFactory messageFactory) {
91          return loggerRegistry.hasLogger(name, messageFactory);
92      }
93  
94      @Override
95      public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) {
96          return loggerRegistry.hasLogger(name, messageFactoryClass);
97      }
98  
99      static synchronized Log4jTaglibLoggerContext getInstance(final ServletContext servletContext) {
100         Log4jTaglibLoggerContext loggerContext = CONTEXTS.get(servletContext);
101         if (loggerContext != null) {
102             return loggerContext;
103         }
104 
105         synchronized (CONTEXTS) {
106             loggerContext = CONTEXTS.get(servletContext);
107             if (loggerContext == null) {
108                 loggerContext = new Log4jTaglibLoggerContext(servletContext);
109                 CONTEXTS.put(servletContext, loggerContext);
110             }
111         }
112 
113         return loggerContext;
114     }
115 }