1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.lookup;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.logging.log4j.Logger;
23 import org.apache.logging.log4j.core.LogEvent;
24 import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
25 import org.apache.logging.log4j.core.config.plugins.util.PluginType;
26 import org.apache.logging.log4j.core.util.Loader;
27 import org.apache.logging.log4j.status.StatusLogger;
28
29
30
31
32 public class Interpolator implements StrLookup {
33
34 private static final Logger LOGGER = StatusLogger.getLogger();
35
36
37 private static final char PREFIX_SEPARATOR = ':';
38
39 private final Map<String, StrLookup> lookups = new HashMap<String, StrLookup>();
40
41 private final StrLookup defaultLookup;
42
43 public Interpolator(final StrLookup defaultLookup) {
44 this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
45 final PluginManager manager = new PluginManager("Lookup");
46 manager.collectPlugins();
47 final Map<String, PluginType<?>> plugins = manager.getPlugins();
48
49 for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
50 @SuppressWarnings("unchecked")
51 final Class<? extends StrLookup> clazz = (Class<? extends StrLookup>) entry.getValue().getPluginClass();
52 try {
53 lookups.put(entry.getKey(), clazz.getConstructor().newInstance());
54 } catch (final Exception ex) {
55 LOGGER.error("Unable to create Lookup for {}", entry.getKey(), ex);
56 }
57 }
58 }
59
60
61
62
63 public Interpolator() {
64 this((Map<String, String>) null);
65 }
66
67
68
69
70 public Interpolator(final Map<String, String> properties) {
71 this.defaultLookup = new MapLookup(properties == null ? new HashMap<String, String>() : properties);
72
73 lookups.put("sys", new SystemPropertiesLookup());
74 lookups.put("env", new EnvironmentLookup());
75 try {
76
77 lookups.put("jndi",
78 Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JndiLookup", StrLookup.class));
79 } catch (Throwable e) {
80
81 LOGGER.warn(
82 "JNDI lookup class is not available because this JRE does not support JNDI. JNDI string lookups will not be available, continuing configuration.",
83 e);
84 }
85 lookups.put("date", new DateLookup());
86 lookups.put("ctx", new ContextMapLookup());
87 if (Loader.isClassAvailable("javax.servlet.ServletContext")) {
88 try {
89 lookups.put("web",
90 Loader.newCheckedInstanceOf("org.apache.logging.log4j.web.WebLookup", StrLookup.class));
91 } catch (final Exception ignored) {
92 LOGGER.info("Log4j appears to be running in a Servlet environment, but there's no log4j-web module " +
93 "available. If you want better web container support, please add the log4j-web JAR to your " +
94 "web archive or server lib directory.");
95 }
96 } else {
97 LOGGER.debug("Not in a ServletContext environment, thus not loading WebLookup plugin.");
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113 @Override
114 public String lookup(final String var) {
115 return lookup(null, var);
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 @Override
132 public String lookup(final LogEvent event, String var) {
133 if (var == null) {
134 return null;
135 }
136
137 final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
138 if (prefixPos >= 0) {
139 final String prefix = var.substring(0, prefixPos);
140 final String name = var.substring(prefixPos + 1);
141 final StrLookup lookup = lookups.get(prefix);
142 String value = null;
143 if (lookup != null) {
144 value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
145 }
146
147 if (value != null) {
148 return value;
149 }
150 var = var.substring(prefixPos + 1);
151 }
152 if (defaultLookup != null) {
153 return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
154 }
155 return null;
156 }
157
158 @Override
159 public String toString() {
160 final StringBuilder sb = new StringBuilder();
161 for (final String name : lookups.keySet()) {
162 if (sb.length() == 0) {
163 sb.append('{');
164 } else {
165 sb.append(", ");
166 }
167
168 sb.append(name);
169 }
170 if (sb.length() > 0) {
171 sb.append('}');
172 }
173 return sb.toString();
174 }
175 }