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 * An application's {@code public static main(String[])} method calls this method to make its main arguments 041 * available for lookup with the prefix {@code main}. 042 * <p> 043 * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For 044 * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with: 045 * </p> 046 * <ul> 047 * <li>{@code "main:0"} = {@code "--file"}</li> 048 * <li>{@code "main:1"} = {@code "path/file.txt"}</li> 049 * <li>{@code "main:2"} = {@code "-x"}</li> 050 * <li>{@code "main:3"} = {@code "2"}</li> 051 * </ul> 052 * <p> 053 * Second using the argument at position n as the key to access the value at n+1. 054 * </p> 055 * <ul> 056 * <li>{@code "main:--file"} = {@code "path/file.txt"}</li> 057 * <li>{@code "main:-x"} = {@code "2"}</li> 058 * </ul> 059 * 060 * @param args 061 * An application's {@code public static main(String[])} arguments. 062 */ 063 public static void setMainArguments(final String... args) { 064 if (args == null) { 065 return; 066 } 067 initMap(args, MainMapLookup.MAIN_SINGLETON.getMap()); 068 } 069 070 /** 071 * Constructor when used directly as a plugin. 072 */ 073 public MainMapLookup() { 074 // no-init 075 } 076 077 public MainMapLookup(final Map<String, String> map) { 078 super(map); 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}