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.core.lookup;
18  
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.LoggerContext;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.impl.ContextAnchor;
25  import org.apache.logging.log4j.core.lookup.StrLookup;
26  
27  import javax.servlet.ServletContext;
28  
29  
30  @Plugin(name = "web", category = "Lookup")
31  public class WebLookup implements StrLookup {
32      private static final String ATTR_PREFIX = "attr.";
33      private static final String INIT_PARAM_PREFIX = "initParam.";
34  
35      protected ServletContext getServletContext() {
36          LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
37          if (lc == null) {
38              lc = (LoggerContext) LogManager.getContext(false);
39          }
40          if (lc != null) {
41              Object obj = lc.getExternalContext();
42              return obj != null && obj instanceof ServletContext ? (ServletContext) obj : null;
43          }
44          return null;
45      }
46  
47      @Override
48      public String lookup(String key) {
49          ServletContext ctx = getServletContext();
50          if (ctx == null) {
51              return null;
52          }
53  
54          if (key.startsWith(ATTR_PREFIX)) {
55              String attrName = key.substring(ATTR_PREFIX.length());
56              Object attrValue = ctx.getAttribute(attrName);
57              return attrValue == null ? null : attrValue.toString();
58          }
59  
60          if (key.startsWith(INIT_PARAM_PREFIX)) {
61              String paramName = key.substring(INIT_PARAM_PREFIX.length());
62              return ctx.getInitParameter(paramName);
63          }
64  
65          if ("rootDir".equals(key)) {
66              String root = ctx.getRealPath("/");
67              if (root == null) {
68                  String msg = "failed to resolve web:rootDir -- " +
69                          "servlet container unable to translate virtual path " +
70                          " to real path (probably not deployed as exploded";
71                  throw new RuntimeException(msg);
72  
73              }
74              return root;
75          }
76  
77          if ("contextPath".equals(key)) {
78              return ctx.getContextPath();
79          }
80  
81          if ("servletContextName".equals(key)) {
82              return ctx.getServletContextName();
83          }
84  
85          if ("serverInfo".equals(key)) {
86              return ctx.getServerInfo();
87          }
88  
89          if ("effectiveMajorVersion".equals(key)) {
90              return String.valueOf(ctx.getEffectiveMajorVersion());
91          }
92  
93          if ("effectiveMinorVersion".equals(key)) {
94              return String.valueOf(ctx.getEffectiveMinorVersion());
95          }
96  
97          if ("majorVersion".equals(key)) {
98              return String.valueOf(ctx.getMajorVersion());
99          }
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(LogEvent event, String key) {
119         return lookup(key);
120     }
121 }