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.core.LogEvent;
20  import org.apache.logging.log4j.core.config.plugins.Plugin;
21  import org.apache.logging.log4j.message.MapMessage;
22  
23  import java.util.Map;
24  
25  /**
26   * The basis for a lookup based on a Map.
27   * @param <V> The type of object contained in the Map.
28   */
29  @Plugin(name = "map", type = "Lookup")
30  public class MapLookup<V> implements StrLookup<V> {
31      /**
32       * Map keys are variable names and value.
33       */
34      private final Map<String, V> map;
35  
36      /**
37       * Creates a new instance backed by a Map. Used by the default lookup.
38       *
39       * @param map the map of keys to values, may be null
40       */
41      public MapLookup(Map<String, V> map) {
42          this.map = map;
43      }
44  
45      /**
46       * Constructor when used directly as a plugin.
47       */
48      public MapLookup() {
49          this.map = null;
50      }
51  
52      /**
53       * Looks up a String key to a String value using the map.
54       * <p/>
55       * If the map is null, then null is returned.
56       * The map result object is converted to a string using toString().
57       *
58       * @param key the key to be looked up, may be null
59       * @return the matching value, null if no match
60       */
61      public String lookup(String key) {
62          if (map == null) {
63              return null;
64          }
65          Object obj = map.get(key);
66          if (obj == null) {
67              return null;
68          }
69          return obj.toString();
70      }
71  
72      public String lookup(LogEvent event, String key) {
73          if (map == null && !(event.getMessage() instanceof MapMessage)) {
74              return null;
75          }
76          if (map != null && map.containsKey(key)) {
77              Object obj = map.get(key);
78              if (obj != null) {
79                  return obj.toString();
80              }
81          }
82          if (event.getMessage() instanceof MapMessage) {
83              return ((MapMessage) event.getMessage()).get(key);
84          }
85          return null;
86      }
87  }