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.web;
018    // Please note that if you move this class, make sure to update the Interpolator class (if still applicable) or remove
019    // this comment if no longer relevant
020    
021    import javax.servlet.ServletContext;
022    
023    import org.apache.logging.log4j.LogManager;
024    import org.apache.logging.log4j.core.LogEvent;
025    import org.apache.logging.log4j.core.LoggerContext;
026    import org.apache.logging.log4j.core.config.plugins.Plugin;
027    import org.apache.logging.log4j.core.impl.ContextAnchor;
028    import org.apache.logging.log4j.core.lookup.StrLookup;
029    
030    
031    @Plugin(name = "web", category = "Lookup")
032    public class WebLookup implements StrLookup {
033        private static final String ATTR_PREFIX = "attr.";
034        private static final String INIT_PARAM_PREFIX = "initParam.";
035    
036        protected ServletContext getServletContext() {
037            LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
038            if (lc == null) {
039                lc = (LoggerContext) LogManager.getContext(false);
040            }
041            if (lc != null) {
042                final Object obj = lc.getExternalContext();
043                return obj != null && obj instanceof ServletContext ? (ServletContext) obj : null;
044            }
045            return null;
046        }
047    
048        @Override
049        public String lookup(final String key) {
050            final ServletContext ctx = getServletContext();
051            if (ctx == null) {
052                return null;
053            }
054    
055            if (key.startsWith(ATTR_PREFIX)) {
056                final String attrName = key.substring(ATTR_PREFIX.length());
057                final Object attrValue = ctx.getAttribute(attrName);
058                return attrValue == null ? null : attrValue.toString();
059            }
060    
061            if (key.startsWith(INIT_PARAM_PREFIX)) {
062                final String paramName = key.substring(INIT_PARAM_PREFIX.length());
063                return ctx.getInitParameter(paramName);
064            }
065    
066            if ("rootDir".equals(key)) {
067                final String root = ctx.getRealPath("/");
068                if (root == null) {
069                    final String msg = "Failed to resolve web:rootDir -- " +
070                            "servlet container unable to translate virtual path " +
071                            " to real path (probably not deployed as exploded";
072                    throw new IllegalStateException(msg);
073                }
074                return root;
075            }
076    
077            if ("contextPath".equals(key)) {
078                return ctx.getContextPath();
079            }
080    
081            if ("servletContextName".equals(key)) {
082                return ctx.getServletContextName();
083            }
084    
085            if ("serverInfo".equals(key)) {
086                return ctx.getServerInfo();
087            }
088    
089            if ("effectiveMajorVersion".equals(key)) {
090                return String.valueOf(ctx.getEffectiveMajorVersion());
091            }
092    
093            if ("effectiveMinorVersion".equals(key)) {
094                return String.valueOf(ctx.getEffectiveMinorVersion());
095            }
096    
097            if ("majorVersion".equals(key)) {
098                return String.valueOf(ctx.getMajorVersion());
099            }
100    
101            if ("minorVersion".equals(key)) {
102                return String.valueOf(ctx.getMinorVersion());
103            }
104    
105            if (ctx.getAttribute(key) != null) {
106                return ctx.getAttribute(key).toString();
107            }
108    
109            if (ctx.getInitParameter(key) != null) {
110                return ctx.getInitParameter(key);
111            }
112    
113            ctx.log(getClass().getName() + " unable to resolve key '" + key + '\'');
114            return null;
115        }
116    
117        @Override
118        public String lookup(final LogEvent event, final String key) {
119            return lookup(key);
120        }
121    }