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.core.LogEvent;
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.message.MapMessage;
26  
27  /**
28   * A map-based lookup.
29   */
30  @Plugin(name = "map", category = StrLookup.CATEGORY)
31  public class MapLookup implements StrLookup {
32  
33      static Map<String, String> initMap(final String[] srcArgs, final Map<String, String> destMap) {
34          for (int i = 0; i < srcArgs.length; i++) {
35              final int next = i + 1;
36              final String value = srcArgs[i];
37              destMap.put(Integer.toString(i), value);
38              destMap.put(value, next < srcArgs.length ? srcArgs[next] : null);
39          }
40          return destMap;
41      }
42  
43      static HashMap<String, String> newMap(final int initialCapacity) {
44          return new HashMap<>(initialCapacity);
45      }
46  
47      /**
48       * An application's {@code public static main(String[])} method calls this method to make its main arguments
49       * available for lookup with the prefix {@code main}.
50       * <p>
51       * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For
52       * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with:
53       * </p>
54       * <ul>
55       * <li>{@code "main:0"} = {@code "--file"}</li>
56       * <li>{@code "main:1"} = {@code "path/file.txt"}</li>
57       * <li>{@code "main:2"} = {@code "-x"}</li>
58       * <li>{@code "main:3"} = {@code "2"}</li>
59       * </ul>
60       * <p>
61       * Second using the argument at position n as the key to access the value at n+1.
62       * </p>
63       * <ul>
64       * <li>{@code "main:--file"} = {@code "path/file.txt"}</li>
65       * <li>{@code "main:-x"} = {@code "2"}</li>
66       * </ul>
67       *
68       * @param args
69       *        An application's {@code public static main(String[])} arguments.
70       * @since 2.1
71       * @deprecated As of 2.4, use {@link MainMapLookup#setMainArguments(String[])}
72       */
73      @Deprecated
74      public static void setMainArguments(final String... args) {
75          MainMapLookup.setMainArguments(args);
76      }
77  
78      static Map<String, String> toMap(final List<String> args) {
79          if (args == null) {
80              return null;
81          }
82          final int size = args.size();
83          return initMap(args.toArray(new String[size]), newMap(size));
84      }
85  
86      static Map<String, String> toMap(final String[] args) {
87          if (args == null) {
88              return null;
89          }
90          return initMap(args, newMap(args.length));
91      }
92  
93      /**
94       * Map keys are variable names and value.
95       */
96      private final Map<String, String> map;
97  
98      /**
99       * Constructor when used directly as a plugin.
100      */
101     public MapLookup() {
102         this.map = null;
103     }
104 
105     /**
106      * Creates a new instance backed by a Map. Used by the default lookup.
107      *
108      * @param map
109      *        the map of keys to values, may be null
110      */
111     public MapLookup(final Map<String, String> map) {
112         this.map = map;
113     }
114 
115     protected Map<String, String> getMap() {
116         return map;
117     }
118 
119     @Override
120     public String lookup(final LogEvent event, final String key) {
121         final boolean isMapMessage = event.getMessage() instanceof MapMessage;
122         if (map == null && !isMapMessage) {
123             return null;
124         }
125         if (map != null && map.containsKey(key)) {
126             final String obj = map.get(key);
127             if (obj != null) {
128                 return obj;
129             }
130         }
131         if (isMapMessage) {
132             return ((MapMessage) event.getMessage()).get(key);
133         }
134         return null;
135     }
136 
137     /**
138      * Looks up a String key to a String value using the map.
139      * <p>
140      * If the map is null, then null is returned. The map result object is converted to a string using toString().
141      * </p>
142      *
143      * @param key
144      *        the key to be looked up, may be null
145      * @return the matching value, null if no match
146      */
147     @Override
148     public String lookup(final String key) {
149         if (map == null) {
150             return null;
151         }
152         return map.get(key);
153     }
154 
155 }