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 org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.core.LogEvent;
21  import org.apache.logging.log4j.core.config.plugins.PluginManager;
22  import org.apache.logging.log4j.core.config.plugins.PluginType;
23  import org.apache.logging.log4j.status.StatusLogger;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /**
29   * The Interpolator is a StrLookup that acts as a proxy for all the other StrLookups.
30   */
31  public class Interpolator implements StrLookup {
32  
33      private static final Logger LOGGER = StatusLogger.getLogger();
34  
35      /** Constant for the prefix separator. */
36      private static final char PREFIX_SEPARATOR = ':';
37  
38      private final Map<String, StrLookup> lookups = new HashMap<String, StrLookup>();
39  
40      private final StrLookup defaultLookup;
41  
42      public Interpolator(final StrLookup defaultLookup) {
43          this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
44          final PluginManager manager = new PluginManager("Lookup");
45          manager.collectPlugins();
46          final Map<String, PluginType> plugins = manager.getPlugins();
47  
48          for (final Map.Entry<String, PluginType> entry : plugins.entrySet()) {
49              @SuppressWarnings("unchecked")
50              final Class<StrLookup> clazz = entry.getValue().getPluginClass();
51              try {
52                  lookups.put(entry.getKey(), clazz.newInstance());
53              } catch (final Exception ex) {
54                  LOGGER.error("Unable to create Lookup for " + entry.getKey(), ex);
55              }
56          }
57      }
58  
59      /**
60       * Create the default Interpolator using only Lookups that work without an event.
61       */
62      public Interpolator() {
63          this.defaultLookup = new MapLookup(new HashMap<String, String>());
64          lookups.put("sys", new SystemPropertiesLookup());
65          lookups.put("env", new EnvironmentLookup());
66      }
67  
68       /**
69       * Resolves the specified variable. This implementation will try to extract
70       * a variable prefix from the given variable name (the first colon (':') is
71       * used as prefix separator). It then passes the name of the variable with
72       * the prefix stripped to the lookup object registered for this prefix. If
73       * no prefix can be found or if the associated lookup object cannot resolve
74       * this variable, the default lookup object will be used.
75       *
76       * @param var the name of the variable whose value is to be looked up
77       * @return the value of this variable or <b>null</b> if it cannot be
78       * resolved
79       */
80      @Override
81      public String lookup(final String var) {
82          return lookup(null, var);
83      }
84  
85      /**
86       * Resolves the specified variable. This implementation will try to extract
87       * a variable prefix from the given variable name (the first colon (':') is
88       * used as prefix separator). It then passes the name of the variable with
89       * the prefix stripped to the lookup object registered for this prefix. If
90       * no prefix can be found or if the associated lookup object cannot resolve
91       * this variable, the default lookup object will be used.
92       *
93       * @param event The current LogEvent or null.
94       * @param var the name of the variable whose value is to be looked up
95       * @return the value of this variable or <b>null</b> if it cannot be
96       * resolved
97       */
98      @Override
99      public String lookup(final LogEvent event, String var) {
100         if (var == null) {
101             return null;
102         }
103 
104         final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
105         if (prefixPos >= 0) {
106             final String prefix = var.substring(0, prefixPos);
107             final String name = var.substring(prefixPos + 1);
108             final StrLookup lookup = lookups.get(prefix);
109             String value = null;
110             if (lookup != null) {
111                 value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
112             }
113 
114             if (value != null) {
115                 return value;
116             }
117             var = var.substring(prefixPos + 1);
118         }
119         if (defaultLookup != null) {
120             return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
121         }
122         return null;
123     }
124 
125     @Override
126     public String toString() {
127         final StringBuilder sb = new StringBuilder();
128         for (final String name : lookups.keySet()) {
129             if (sb.length() == 0) {
130                 sb.append("{");
131             } else {
132                 sb.append(", ");
133             }
134 
135             sb.append(name);
136         }
137         if (sb.length() > 0) {
138             sb.append("}");
139         }
140         return sb.toString();
141     }
142 }