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.Map;
20  
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  
24  /**
25   * A map-based lookup for main arguments.
26   * 
27   * See {@link #setMainArguments(String[])}.
28   * 
29   * @since 2.4
30   */
31  @Plugin(name = "main", category = StrLookup.CATEGORY)
32  public class MainMapLookup extends MapLookup {
33  
34      /**
35       * A singleton used by a main method to save its arguments.
36       */
37      static final MapLookup MAIN_SINGLETON = new MapLookup(MapLookup.newMap(0));
38  
39      /**
40       * Constructor when used directly as a plugin.
41       */
42      public MainMapLookup() {
43          // no-init
44      }
45  
46      public MainMapLookup(final Map<String, String> map) {
47          super(map);
48      }
49  
50      /**
51       * An application's {@code public static main(String[])} method calls this method to make its main arguments
52       * available for lookup with the prefix {@code main}.
53       * <p>
54       * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For
55       * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with:
56       * </p>
57       * <ul>
58       * <li>{@code "main:0"} = {@code "--file"}</li>
59       * <li>{@code "main:1"} = {@code "path/file.txt"}</li>
60       * <li>{@code "main:2"} = {@code "-x"}</li>
61       * <li>{@code "main:3"} = {@code "2"}</li>
62       * </ul>
63       * <p>
64       * Second using the argument at position n as the key to access the value at n+1.
65       * </p>
66       * <ul>
67       * <li>{@code "main:--file"} = {@code "path/file.txt"}</li>
68       * <li>{@code "main:-x"} = {@code "2"}</li>
69       * </ul>
70       *
71       * @param args
72       *        An application's {@code public static main(String[])} arguments.
73       */
74      public static void setMainArguments(final String... args) {
75          if (args == null) {
76              return;
77          }
78          initMap(args, MainMapLookup.MAIN_SINGLETON.getMap());
79      }
80  
81      @Override
82      public String lookup(final LogEvent event, final String key) {
83          return MAIN_SINGLETON.getMap().get(key);
84      }
85  
86      @Override
87      public String lookup(final String key) {
88          return MAIN_SINGLETON.getMap().get(key);
89      }
90  
91  }