001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.lookup;
018
019import java.util.Map;
020
021import org.apache.logging.log4j.core.LogEvent;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023
024/**
025 * A map-based lookup for main arguments.
026 * 
027 * See {@link #setMainArguments(String[])}.
028 * 
029 * @since 2.4
030 */
031@Plugin(name = "main", category = StrLookup.CATEGORY)
032public class MainMapLookup extends MapLookup {
033
034    /**
035     * A singleton used by a main method to save its arguments.
036     */
037    static final MapLookup MAIN_SINGLETON = new MapLookup(MapLookup.newMap(0));
038
039    /**
040     * Constructor when used directly as a plugin.
041     */
042    public MainMapLookup() {
043        // no-init
044    }
045
046    public MainMapLookup(final Map<String, String> map) {
047        super(map);
048    }
049
050    /**
051     * An application's {@code public static main(String[])} method calls this method to make its main arguments
052     * available for lookup with the prefix {@code main}.
053     * <p>
054     * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For
055     * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with:
056     * </p>
057     * <ul>
058     * <li>{@code "main:0"} = {@code "--file"}</li>
059     * <li>{@code "main:1"} = {@code "path/file.txt"}</li>
060     * <li>{@code "main:2"} = {@code "-x"}</li>
061     * <li>{@code "main:3"} = {@code "2"}</li>
062     * </ul>
063     * <p>
064     * Second using the argument at position n as the key to access the value at n+1.
065     * </p>
066     * <ul>
067     * <li>{@code "main:--file"} = {@code "path/file.txt"}</li>
068     * <li>{@code "main:-x"} = {@code "2"}</li>
069     * </ul>
070     *
071     * @param args
072     *        An application's {@code public static main(String[])} arguments.
073     */
074    public static void setMainArguments(final String... args) {
075        if (args == null) {
076            return;
077        }
078        initMap(args, MainMapLookup.MAIN_SINGLETON.getMap());
079    }
080
081    @Override
082    public String lookup(final LogEvent event, final String key) {
083        return MAIN_SINGLETON.getMap().get(key);
084    }
085
086    @Override
087    public String lookup(final String key) {
088        return MAIN_SINGLETON.getMap().get(key);
089    }
090
091}