    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.logging.log4j.taglib;
    018
    019    import javax.servlet.jsp.JspException;
    020    import javax.servlet.jsp.PageContext;
    021    import javax.servlet.jsp.tagext.BodyTagSupport;
    022    import javax.servlet.jsp.tagext.Tag;
    023
    024    import org.apache.logging.log4j.message.MessageFactory;
    025
    026    /**
    027     * This class implements the {@code <log:setLogger>} tag.
    028     *
    029     * @since 2.0
    030     */
    031    public class SetLoggerTag extends BodyTagSupport {
    032        private static final long serialVersionUID = 1L;
    033
    034        private transient Log4jTaglibLoggerContext loggerContext;
    035
    036        private transient Object logger;
    037
    038        private transient MessageFactory factory;
    039
    040        private String var;
    041
    042        private int scope;
    043
    044        public SetLoggerTag() {
    045            super();
    046            init();
    047        }
    048
    049        private void init() {
    050            this.logger = null;
    051            this.var = null;
    052            this.scope = PageContext.PAGE_SCOPE;
    053        }
    054
    055        @Override
    056        public void release() {
    057            super.release();
    058            this.init();
    059        }
    060
    061        @Override
    062        public void setPageContext(final PageContext pageContext) {
    063            super.setPageContext(pageContext);
    064            this.loggerContext = Log4jTaglibLoggerContext.getInstance(pageContext.getServletContext());
    065        }
    066
    067        public void setLogger(final Object logger) {
    068            this.logger = logger;
    069        }
    070
    071        public void setFactory(final MessageFactory factory) {
    072            this.factory = factory;
    073        }
    074
    075        public void setVar(final String var) {
    076            this.var = var;
    077        }
    078
    079        public void setScope(final String scope) {
    080            this.scope = TagUtils.getScope(scope);
    081        }
    082
    083        @Override
    084        public int doEndTag() throws JspException {
    085            final Log4jTaglibLogger logger = TagUtils.resolveLogger(this.loggerContext, this.logger, this.factory);
    086
    087            if (this.var != null) {
    088                this.pageContext.setAttribute(this.var, logger, this.scope);
    089            } else {
    090                TagUtils.setDefaultLogger(this.pageContext, logger);
    091            }
    092
    093            return Tag.EVAL_PAGE;
    094        }
    095    }