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