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 javax.servlet.jsp.JspException;
    20  import javax.servlet.jsp.PageContext;
    21  import javax.servlet.jsp.tagext.BodyTagSupport;
    22
    23  /**
    24   * An abstract class for all tags that are logger-aware.
    25   *
    26   * @since 2.0
    27   */
    28  abstract class LoggerAwareTagSupport extends BodyTagSupport {
    29      private static final long serialVersionUID = 1L;
    30
    31      private transient Log4jTaglibLoggerContext loggerContext;
    32
    33      private transient Object logger;
    34
    35      protected LoggerAwareTagSupport() {
    36          this.init();
    37      }
    38
    39      protected void init() {
    40          this.logger = null;
    41      }
    42
    43      @Override
    44      public final void release() {
    45          super.release();
    46          this.init();
    47      }
    48
    49      @Override
    50      public final void setPageContext(final PageContext pageContext) {
    51          super.setPageContext(pageContext);
    52          this.loggerContext = Log4jTaglibLoggerContext.getInstance(pageContext.getServletContext());
    53      }
    54
    55      protected final Log4jTaglibLogger getLogger() throws JspException {
    56          if (this.logger != null) {
    57              return TagUtils.resolveLogger(this.loggerContext, this.logger, null);
    58          }
    59          Log4jTaglibLogger logger = TagUtils.getDefaultLogger(this.pageContext);
    60          if (logger == null) {
    61              final String name = this.pageContext.getPage().getClass().getName();
    62              logger = TagUtils.resolveLogger(this.loggerContext, name, null);
    63              TagUtils.setDefaultLogger(this.pageContext, logger);
    64          }
    65          return logger;
    66      }
    67
    68      public final void setLogger(final Object logger) {
    69          this.logger = logger;
    70      }
    71  }