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.HashSet;
    20  import java.util.Set;
    21
    22  import javax.servlet.jsp.JspException;
    23  import javax.servlet.jsp.PageContext;
    24
    25  import org.apache.logging.log4j.Level;
    26  import org.apache.logging.log4j.Logger;
    27  import org.apache.logging.log4j.LoggingException;
    28  import org.apache.logging.log4j.Marker;
    29  import org.apache.logging.log4j.message.MessageFactory;
    30  import org.apache.logging.log4j.spi.AbstractLogger;
    31  import org.apache.logging.log4j.status.StatusLogger;
    32
    33  /**
    34   * Provides support for logging tags.
    35   *
    36   * @since 2.0
    37   */
    38  final class TagUtils {
    39      private static final StatusLogger LOGGER = StatusLogger.getLogger();
    40
    41      private static final Set<Object> WARNED_FOR = new HashSet<Object>();
    42
    43      private static final String LOGGER_SCOPE_ATTRIBUTE = "org.apache.logging.log4j.taglib.LOGGER_SCOPE_ATTRIBUTE";
    44
    45      private TagUtils() {
    46          throw new RuntimeException("TagUtils cannot be instantiated.");
    47      }
    48
    49      static int getScope(final String scope) {
    50          if ("request".equalsIgnoreCase(scope)) {
    51              return PageContext.REQUEST_SCOPE;
    52          }
    53          if ("session".equalsIgnoreCase(scope)) {
    54              return PageContext.SESSION_SCOPE;
    55          }
    56          if ("application".equalsIgnoreCase(scope)) {
    57              return PageContext.APPLICATION_SCOPE;
    58          }
    59          return PageContext.PAGE_SCOPE;
    60      }
    61
    62      static Level resolveLevel(final Object level) {
    63          if (level instanceof Level) {
    64              return (Level) level;
    65          }
    66          if (level instanceof String) {
    67              return Level.toLevel((String) level);
    68          }
    69          return null;
    70      }
    71
    72      static Log4jTaglibLogger resolveLogger(final Log4jTaglibLoggerContext context, final Object logger,
    73                                             final MessageFactory factory) throws JspException {
    74          if (logger instanceof Logger) {
    75              if (logger instanceof Log4jTaglibLogger) {
    76                  return (Log4jTaglibLogger) logger;
    77              }
    78              if (logger instanceof AbstractLogger) {
    79                  if (LOGGER.isInfoEnabled() && !WARNED_FOR.contains(logger)) {
    80                      LOGGER.info("Constructing new Log4jTaglibLogger from AbstractLogger {} name and message factory.",
    81                              logger.getClass().getName());
    82                      WARNED_FOR.add(logger);
    83                  }
    84                  final AbstractLogger original = (AbstractLogger) logger;
    85                  return getLogger(context, original.getName(), original.getMessageFactory());
    86              }
    87              throw new JspException(
    88                      "Log4j Tag Library requires base logging system to extend Log4j AbstractLogger.");
    89          }
    90          if (logger instanceof String) {
    91              return getLogger(context, (String) logger, factory);
    92          }
    93          throw new JspException("Logger must be of type String or org.apache.logging.log4j.Logger.");
    94      }
    95
    96      private static Log4jTaglibLogger getLogger(final Log4jTaglibLoggerContext context, final String name,
    97                                                 final MessageFactory factory)
    98              throws JspException {
    99          try {
    100             return context.getLogger(name, factory);
    101         } catch (final LoggingException e) {
    102             throw new JspException(e.getMessage(), e);
    103         }
    104     }
    105
    106     static void setDefaultLogger(final PageContext pageContext, final Log4jTaglibLogger logger) {
    107         pageContext.setAttribute(TagUtils.LOGGER_SCOPE_ATTRIBUTE, logger, PageContext.PAGE_SCOPE);
    108     }
    109
    110     static Log4jTaglibLogger getDefaultLogger(final PageContext pageContext) {
    111         return (Log4jTaglibLogger) pageContext.getAttribute(TagUtils.LOGGER_SCOPE_ATTRIBUTE, PageContext.PAGE_SCOPE);
    112     }
    113
    114     static boolean isEnabled(final Log4jTaglibLogger logger, final Level level, final Marker marker) {
    115         if (marker == null) {
    116             return logger.isEnabled(level);
    117         }
    118         return logger.isEnabled(level, marker, (Object) null, null);
    119     }
    120 }