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 org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.Logger;
21  import org.apache.logging.log4j.LoggingException;
22  import org.apache.logging.log4j.message.MessageFactory;
23  import org.apache.logging.log4j.spi.AbstractLogger;
24  import org.apache.logging.log4j.spi.LoggerContext;
25  
26  import javax.servlet.ServletContext;
27  import java.util.WeakHashMap;
28  
29  /**
30   * This bridge between the tag library and the Log4j API ensures that instances of {@link Log4jTaglibLogger} are
31   * appropriately held in memory and not constantly recreated.
32   *
33   * @since 2.0
34   */
35  final class Log4jTaglibLoggerContext implements LoggerContext {
36      // These were change to WeakHashMaps to avoid ClassLoader (memory) leak, something that's particularly
37      // important in Servlet containers.
38      private static final WeakHashMap<ServletContext, Log4jTaglibLoggerContext> CONTEXTS =
39              new WeakHashMap<ServletContext, Log4jTaglibLoggerContext>();
40  
41      private final WeakHashMap<String, Log4jTaglibLogger> loggers =
42              new WeakHashMap<String, Log4jTaglibLogger>();
43  
44      private final ServletContext servletContext;
45  
46      private Log4jTaglibLoggerContext(ServletContext servletContext) {
47          this.servletContext = servletContext;
48      }
49  
50      public Object getExternalContext() {
51          return this.servletContext;
52      }
53  
54      public Log4jTaglibLogger getLogger(String name) {
55          return this.getLogger(name, null);
56      }
57  
58      public Log4jTaglibLogger getLogger(String name, MessageFactory factory) {
59          Log4jTaglibLogger logger = this.loggers.get(name);
60          if (logger != null) {
61              AbstractLogger.checkMessageFactory(logger, factory);
62              return logger;
63          }
64  
65          synchronized (this.loggers) {
66              logger = this.loggers.get(name);
67              if (logger == null) {
68                  Logger original = factory == null ? LogManager.getLogger(name) : LogManager.getLogger(name, factory);
69                  if (!(original instanceof AbstractLogger)) {
70                      throw new LoggingException(
71                              "Log4j Tag Library requires base logging system to extend Log4j AbstractLogger."
72                      );
73                  }
74                  // wrap a logger from an underlying implementation
75                  logger = new Log4jTaglibLogger((AbstractLogger) original, name, original.getMessageFactory());
76                  this.loggers.put(name, logger);
77              }
78          }
79  
80          return logger;
81      }
82  
83      public boolean hasLogger(String name) {
84          return this.loggers.containsKey(name);
85      }
86  
87      static synchronized Log4jTaglibLoggerContext getInstance(ServletContext servletContext) {
88          Log4jTaglibLoggerContext loggerContext = CONTEXTS.get(servletContext);
89          if (loggerContext != null) {
90              return loggerContext;
91          }
92  
93          synchronized (CONTEXTS) {
94              loggerContext = CONTEXTS.get(servletContext);
95              if (loggerContext == null) {
96                  loggerContext = new Log4jTaglibLoggerContext(servletContext);
97                  CONTEXTS.put(servletContext, loggerContext);
98              }
99          }
100 
101         return loggerContext;
102     }
103 }