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       * An application's {@code public static main(String[])} method calls this method to make its main arguments
41       * available for lookup with the prefix {@code main}.
42       * <p>
43       * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For
44       * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with:
45       * </p>
46       * <ul>
47       * <li>{@code "main:0"} = {@code "--file"}</li>
48       * <li>{@code "main:1"} = {@code "path/file.txt"}</li>
49       * <li>{@code "main:2"} = {@code "-x"}</li>
50       * <li>{@code "main:3"} = {@code "2"}</li>
51       * </ul>
52       * <p>
53       * Second using the argument at position n as the key to access the value at n+1.
54       * </p>
55       * <ul>
56       * <li>{@code "main:--file"} = {@code "path/file.txt"}</li>
57       * <li>{@code "main:-x"} = {@code "2"}</li>
58       * </ul>
59       *
60       * @param args
61       *        An application's {@code public static main(String[])} arguments.
62       */
63      public static void setMainArguments(final String... args) {
64          if (args == null) {
65              return;
66          }
67          initMap(args, MainMapLookup.MAIN_SINGLETON.getMap());
68      }
69  
70      /**
71       * Constructor when used directly as a plugin.
72       */
73      public MainMapLookup() {
74          // no-init
75      }
76  
77      public MainMapLookup(final Map<String, String> map) {
78          super(map);
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  }