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              final Class<StrLookup> clazz = entry.getValue().getPluginClass();
50              try {
51                  lookups.put(entry.getKey(), clazz.newInstance());
52              } catch (final Exception ex) {
53                  LOGGER.error("Unable to create Lookup for " + entry.getKey(), ex);
54              }
55          }
56      }
57  
58      public Interpolator() {
59          this.defaultLookup = new MapLookup(new HashMap<String, String>());
60          lookups.put("sys", new SystemPropertiesLookup());
61      }
62  
63       /**
64       * Resolves the specified variable. This implementation will try to extract
65       * a variable prefix from the given variable name (the first colon (':') is
66       * used as prefix separator). It then passes the name of the variable with
67       * the prefix stripped to the lookup object registered for this prefix. If
68       * no prefix can be found or if the associated lookup object cannot resolve
69       * this variable, the default lookup object will be used.
70       *
71       * @param var the name of the variable whose value is to be looked up
72       * @return the value of this variable or <b>null</b> if it cannot be
73       * resolved
74       */
75      public String lookup(final String var) {
76          return lookup(null, var);
77      }
78  
79      /**
80       * Resolves the specified variable. This implementation will try to extract
81       * a variable prefix from the given variable name (the first colon (':') is
82       * used as prefix separator). It then passes the name of the variable with
83       * the prefix stripped to the lookup object registered for this prefix. If
84       * no prefix can be found or if the associated lookup object cannot resolve
85       * this variable, the default lookup object will be used.
86       *
87       * @param event The current LogEvent or null.
88       * @param var the name of the variable whose value is to be looked up
89       * @return the value of this variable or <b>null</b> if it cannot be
90       * resolved
91       */
92      public String lookup(final LogEvent event, String var) {
93          if (var == null) {
94              return null;
95          }
96  
97          final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
98          if (prefixPos >= 0) {
99              final String prefix = var.substring(0, prefixPos);
100             final String name = var.substring(prefixPos + 1);
101             final StrLookup lookup = lookups.get(prefix);
102             String value = null;
103             if (lookup != null) {
104                 value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
105             }
106 
107             if (value != null) {
108                 return value;
109             }
110             var = var.substring(prefixPos + 1);
111         }
112         if (defaultLookup != null) {
113             return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
114         }
115         return null;
116     }
117 
118     @Override
119     public String toString() {
120         final StringBuilder sb = new StringBuilder();
121         for (final String name : lookups.keySet()) {
122             if (sb.length() == 0) {
123                 sb.append("{");
124             } else {
125                 sb.append(", ");
126             }
127 
128             sb.append(name);
129         }
130         if (sb.length() > 0) {
131             sb.append("}");
132         }
133         return sb.toString();
134     }
135 }