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.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.logging.log4j.core.LogEvent;
024import org.apache.logging.log4j.core.config.plugins.Plugin;
025import org.apache.logging.log4j.message.MapMessage;
026
027/**
028 * A map-based lookup.
029 */
030@Plugin(name = "map", category = StrLookup.CATEGORY)
031public class MapLookup implements StrLookup {
032
033    /**
034     * Map keys are variable names and value.
035     */
036    private final Map<String, String> map;
037
038    /**
039     * Constructor when used directly as a plugin.
040     */
041    public MapLookup() {
042        this.map = null;
043    }
044
045    /**
046     * Creates a new instance backed by a Map. Used by the default lookup.
047     *
048     * @param map
049     *        the map of keys to values, may be null
050     */
051    public MapLookup(final Map<String, String> map) {
052        this.map = map;
053    }
054
055    static Map<String, String> initMap(final String[] srcArgs, final Map<String, String> destMap) {
056        for (int i = 0; i < srcArgs.length; i++) {
057            final int next = i + 1;
058            final String value = srcArgs[i];
059            destMap.put(Integer.toString(i), value);
060            destMap.put(value, next < srcArgs.length ? srcArgs[next] : null);
061        }
062        return destMap;
063    }
064
065    static HashMap<String, String> newMap(final int initialCapacity) {
066        return new HashMap<>(initialCapacity);
067    }
068
069    /**
070     * An application's {@code public static main(String[])} method calls this method to make its main arguments
071     * available for lookup with the prefix {@code main}.
072     * <p>
073     * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For
074     * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with:
075     * </p>
076     * <ul>
077     * <li>{@code "main:0"} = {@code "--file"}</li>
078     * <li>{@code "main:1"} = {@code "path/file.txt"}</li>
079     * <li>{@code "main:2"} = {@code "-x"}</li>
080     * <li>{@code "main:3"} = {@code "2"}</li>
081     * </ul>
082     * <p>
083     * Second using the argument at position n as the key to access the value at n+1.
084     * </p>
085     * <ul>
086     * <li>{@code "main:--file"} = {@code "path/file.txt"}</li>
087     * <li>{@code "main:-x"} = {@code "2"}</li>
088     * </ul>
089     *
090     * @param args
091     *        An application's {@code public static main(String[])} arguments.
092     * @since 2.1
093     * @deprecated As of 2.4, use {@link MainMapLookup#setMainArguments(String[])}
094     */
095    @Deprecated
096    public static void setMainArguments(final String... args) {
097        MainMapLookup.setMainArguments(args);
098    }
099
100    static Map<String, String> toMap(final List<String> args) {
101        if (args == null) {
102            return null;
103        }
104        final int size = args.size();
105        return initMap(args.toArray(new String[size]), newMap(size));
106    }
107
108    static Map<String, String> toMap(final String[] args) {
109        if (args == null) {
110            return null;
111        }
112        return initMap(args, newMap(args.length));
113    }
114
115    protected Map<String, String> getMap() {
116        return map;
117    }
118
119    @Override
120    public String lookup(final LogEvent event, final String key) {
121        final boolean isMapMessage = event != null && event.getMessage() instanceof MapMessage;
122        if (map == null && !isMapMessage) {
123            return null;
124        }
125        if (map != null && map.containsKey(key)) {
126            final String obj = map.get(key);
127            if (obj != null) {
128                return obj;
129            }
130        }
131        if (isMapMessage) {
132            return ((MapMessage) event.getMessage()).get(key);
133        }
134        return null;
135    }
136
137    /**
138     * Looks up a String key to a String value using the map.
139     * <p>
140     * If the map is null, then null is returned. The map result object is converted to a string using toString().
141     * </p>
142     *
143     * @param key
144     *        the key to be looked up, may be null
145     * @return the matching value, null if no match
146     */
147    @Override
148    public String lookup(final String key) {
149        if (map == null) {
150            return null;
151        }
152        return map.get(key);
153    }
154
155}