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