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  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.logging.log4j.Logger;
24  import org.apache.logging.log4j.core.LogEvent;
25  import org.apache.logging.log4j.core.config.ConfigurationAware;
26  import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
27  import org.apache.logging.log4j.core.config.plugins.util.PluginType;
28  import org.apache.logging.log4j.core.util.Loader;
29  import org.apache.logging.log4j.core.util.ReflectionUtil;
30  import org.apache.logging.log4j.status.StatusLogger;
31  
32  /**
33   * Proxies all the other {@link StrLookup}s.
34   */
35  public class Interpolator extends AbstractConfigurationAwareLookup {
36  
37      private static final String LOOKUP_KEY_WEB = "web";
38  
39      private static final String LOOKUP_KEY_JNDI = "jndi";
40  
41      private static final String LOOKUP_KEY_JVMRUNARGS = "jvmrunargs";
42  
43      private static final Logger LOGGER = StatusLogger.getLogger();
44  
45      /** Constant for the prefix separator. */
46      private static final char PREFIX_SEPARATOR = ':';
47  
48      private final Map<String, StrLookup> lookups = new HashMap<>();
49  
50      private final StrLookup defaultLookup;
51  
52      public Interpolator(final StrLookup defaultLookup) {
53          this(defaultLookup, null);
54      }
55  
56      /**
57       * Constructs an Interpolator using a given StrLookup and a list of packages to find Lookup plugins in.
58       *
59       * @param defaultLookup  the default StrLookup to use as a fallback
60       * @param pluginPackages a list of packages to scan for Lookup plugins
61       * @since 2.1
62       */
63      public Interpolator(final StrLookup defaultLookup, final List<String> pluginPackages) {
64          this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
65          final PluginManager manager = new PluginManager(CATEGORY);
66          manager.collectPlugins(pluginPackages);
67          final Map<String, PluginType<?>> plugins = manager.getPlugins();
68  
69          for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
70              try {
71                  final Class<? extends StrLookup> clazz = entry.getValue().getPluginClass().asSubclass(StrLookup.class);
72                  lookups.put(entry.getKey(), ReflectionUtil.instantiate(clazz));
73              } catch (final Throwable t) {
74                  handleError(entry.getKey(), t);
75              }
76          }
77      }
78  
79      /**
80       * Create the default Interpolator using only Lookups that work without an event.
81       */
82      public Interpolator() {
83          this((Map<String, String>) null);
84      }
85  
86      /**
87       * Creates the Interpolator using only Lookups that work without an event and initial properties.
88       */
89      public Interpolator(final Map<String, String> properties) {
90          this.defaultLookup = new MapLookup(properties == null ? new HashMap<String, String>() : properties);
91          // TODO: this ought to use the PluginManager
92          lookups.put("log4j", new Log4jLookup());
93          lookups.put("sys", new SystemPropertiesLookup());
94          lookups.put("env", new EnvironmentLookup());
95          lookups.put("main", MainMapLookup.MAIN_SINGLETON);
96          lookups.put("marker", new MarkerLookup());
97          lookups.put("java", new JavaLookup());
98          // JNDI
99          try {
100             // [LOG4J2-703] We might be on Android
101             lookups.put(LOOKUP_KEY_JNDI,
102                 Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JndiLookup", StrLookup.class));
103         } catch (final LinkageError | Exception e) {
104             handleError(LOOKUP_KEY_JNDI, e);
105         }
106         // JMX input args
107         try {
108             // We might be on Android
109             lookups.put(LOOKUP_KEY_JVMRUNARGS,
110                 Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JmxRuntimeInputArgumentsLookup",
111                         StrLookup.class));
112         } catch (final LinkageError | Exception e) {
113             handleError(LOOKUP_KEY_JVMRUNARGS, e);
114         }
115         lookups.put("date", new DateLookup());
116         lookups.put("ctx", new ContextMapLookup());
117         if (Loader.isClassAvailable("javax.servlet.ServletContext")) {
118             try {
119                 lookups.put(LOOKUP_KEY_WEB,
120                     Loader.newCheckedInstanceOf("org.apache.logging.log4j.web.WebLookup", StrLookup.class));
121             } catch (final Exception ignored) {
122                 handleError(LOOKUP_KEY_WEB, ignored);
123             }
124         } else {
125             LOGGER.debug("Not in a ServletContext environment, thus not loading WebLookup plugin.");
126         }
127     }
128 
129     private void handleError(final String lookupKey, final Throwable t) {
130         switch (lookupKey) {
131             case LOOKUP_KEY_JNDI:
132                 // java.lang.VerifyError: org/apache/logging/log4j/core/lookup/JndiLookup
133                 LOGGER.warn( // LOG4J2-1582 don't print the whole stack trace (it is just a warning...)
134                         "JNDI lookup class is not available because this JRE does not support JNDI." +
135                         " JNDI string lookups will not be available, continuing configuration. Ignoring " + t);
136                 break;
137             case LOOKUP_KEY_JVMRUNARGS:
138                 // java.lang.VerifyError: org/apache/logging/log4j/core/lookup/JmxRuntimeInputArgumentsLookup
139                 LOGGER.warn(
140                         "JMX runtime input lookup class is not available because this JRE does not support JMX. " +
141                         "JMX lookups will not be available, continuing configuration. Ignoring " + t);
142                 break;
143             case LOOKUP_KEY_WEB:
144                 LOGGER.info("Log4j appears to be running in a Servlet environment, but there's no log4j-web module " +
145                         "available. If you want better web container support, please add the log4j-web JAR to your " +
146                         "web archive or server lib directory.");
147                 break;
148             default:
149                 LOGGER.error("Unable to create Lookup for {}", lookupKey, t);
150         }
151     }
152 
153     /**
154      * Resolves the specified variable. This implementation will try to extract
155      * a variable prefix from the given variable name (the first colon (':') is
156      * used as prefix separator). It then passes the name of the variable with
157      * the prefix stripped to the lookup object registered for this prefix. If
158      * no prefix can be found or if the associated lookup object cannot resolve
159      * this variable, the default lookup object will be used.
160      *
161      * @param event The current LogEvent or null.
162      * @param var the name of the variable whose value is to be looked up
163      * @return the value of this variable or <b>null</b> if it cannot be
164      * resolved
165      */
166     @Override
167     public String lookup(final LogEvent event, String var) {
168         if (var == null) {
169             return null;
170         }
171 
172         final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
173         if (prefixPos >= 0) {
174             final String prefix = var.substring(0, prefixPos);
175             final String name = var.substring(prefixPos + 1);
176             final StrLookup lookup = lookups.get(prefix);
177             if (lookup instanceof ConfigurationAware) {
178                 ((ConfigurationAware) lookup).setConfiguration(configuration);
179             }
180             String value = null;
181             if (lookup != null) {
182                 value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
183             }
184 
185             if (value != null) {
186                 return value;
187             }
188             var = var.substring(prefixPos + 1);
189         }
190         if (defaultLookup != null) {
191             return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
192         }
193         return null;
194     }
195 
196     @Override
197     public String toString() {
198         final StringBuilder sb = new StringBuilder();
199         for (final String name : lookups.keySet()) {
200             if (sb.length() == 0) {
201                 sb.append('{');
202             } else {
203                 sb.append(", ");
204             }
205 
206             sb.append(name);
207         }
208         if (sb.length() > 0) {
209             sb.append('}');
210         }
211         return sb.toString();
212     }
213 }