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     */
017    package org.apache.logging.log4j.core.lookup;
018    
019    import org.apache.logging.log4j.core.LogEvent;
020    import org.apache.logging.log4j.core.config.plugins.Plugin;
021    import org.apache.logging.log4j.message.MapMessage;
022    
023    import java.util.Map;
024    
025    /**
026     * The basis for a lookup based on a Map.
027     */
028    @Plugin(name = "map", type = "Lookup")
029    public class MapLookup implements StrLookup {
030        /**
031         * Map keys are variable names and value.
032         */
033        private final Map<String, String> map;
034    
035        /**
036         * Creates a new instance backed by a Map. Used by the default lookup.
037         *
038         * @param map the map of keys to values, may be null
039         */
040        public MapLookup(final Map<String, String> map) {
041            this.map = map;
042        }
043    
044        /**
045         * Constructor when used directly as a plugin.
046         */
047        public MapLookup() {
048            this.map = null;
049        }
050    
051        /**
052         * Looks up a String key to a String value using the map.
053         * <p/>
054         * If the map is null, then null is returned.
055         * The map result object is converted to a string using toString().
056         *
057         * @param key the key to be looked up, may be null
058         * @return the matching value, null if no match
059         */
060        public String lookup(final String key) {
061            if (map == null) {
062                return null;
063            }
064            final String obj = map.get(key);
065            if (obj == null) {
066                return null;
067            }
068            return obj;
069        }
070    
071        public String lookup(final LogEvent event, final String key) {
072            if (map == null && !(event.getMessage() instanceof MapMessage)) {
073                return null;
074            }
075            if (map != null && map.containsKey(key)) {
076                final String obj = map.get(key);
077                if (obj != null) {
078                    return obj;
079                }
080            }
081            if (event.getMessage() instanceof MapMessage) {
082                return ((MapMessage) event.getMessage()).get(key);
083            }
084            return null;
085        }
086    }