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.Logger;
020    import org.apache.logging.log4j.core.LogEvent;
021    import org.apache.logging.log4j.core.config.plugins.PluginManager;
022    import org.apache.logging.log4j.core.config.plugins.PluginType;
023    import org.apache.logging.log4j.status.StatusLogger;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    /**
029     * The Interpolator is a StrLookup that acts as a proxy for all the other StrLookups.
030     */
031    public class Interpolator implements StrLookup {
032    
033        private static final Logger LOGGER = StatusLogger.getLogger();
034    
035        /** Constant for the prefix separator. */
036        private static final char PREFIX_SEPARATOR = ':';
037    
038        private final Map<String, StrLookup> lookups = new HashMap<String, StrLookup>();
039    
040        private final StrLookup defaultLookup;
041    
042        public Interpolator(StrLookup defaultLookup) {
043            this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
044            PluginManager manager = new PluginManager("Lookup");
045            manager.collectPlugins();
046            Map<String, PluginType> plugins = manager.getPlugins();
047    
048            for (Map.Entry<String, PluginType> entry : plugins.entrySet()) {
049                Class<StrLookup> clazz = entry.getValue().getPluginClass();
050                try {
051                    lookups.put(entry.getKey(), clazz.newInstance());
052                } catch (Exception ex) {
053                    LOGGER.error("Unable to create Lookup for " + entry.getKey(), ex);
054                }
055            }
056        }
057    
058        public Interpolator() {
059            this.defaultLookup = new MapLookup(new HashMap<String, String>());
060            lookups.put("sys", new SystemPropertiesLookup());
061        }
062    
063         /**
064         * Resolves the specified variable. This implementation will try to extract
065         * a variable prefix from the given variable name (the first colon (':') is
066         * used as prefix separator). It then passes the name of the variable with
067         * the prefix stripped to the lookup object registered for this prefix. If
068         * no prefix can be found or if the associated lookup object cannot resolve
069         * this variable, the default lookup object will be used.
070         *
071         * @param var the name of the variable whose value is to be looked up
072         * @return the value of this variable or <b>null</b> if it cannot be
073         * resolved
074         */
075        public String lookup(String var) {
076            return lookup(null, var);
077        }
078    
079        /**
080         * Resolves the specified variable. This implementation will try to extract
081         * a variable prefix from the given variable name (the first colon (':') is
082         * used as prefix separator). It then passes the name of the variable with
083         * the prefix stripped to the lookup object registered for this prefix. If
084         * no prefix can be found or if the associated lookup object cannot resolve
085         * this variable, the default lookup object will be used.
086         *
087         * @param event The current LogEvent or null.
088         * @param var the name of the variable whose value is to be looked up
089         * @return the value of this variable or <b>null</b> if it cannot be
090         * resolved
091         */
092        public String lookup(LogEvent event, String var) {
093            if (var == null) {
094                return null;
095            }
096    
097            int prefixPos = var.indexOf(PREFIX_SEPARATOR);
098            if (prefixPos >= 0) {
099                String prefix = var.substring(0, prefixPos);
100                String name = var.substring(prefixPos + 1);
101                StrLookup lookup = lookups.get(prefix);
102                String value = null;
103                if (lookup != null) {
104                    value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
105                }
106    
107                if (value != null) {
108                    return value;
109                }
110                var = var.substring(prefixPos + 1);
111            }
112            if (defaultLookup != null) {
113                return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
114            }
115            return null;
116        }
117    
118        @Override
119        public String toString() {
120            StringBuilder sb = new StringBuilder();
121            for (String name : lookups.keySet()) {
122                if (sb.length() == 0) {
123                    sb.append("{");
124                } else {
125                    sb.append(", ");
126                }
127    
128                sb.append(name);
129            }
130            if (sb.length() > 0) {
131                sb.append("}");
132            }
133            return sb.toString();
134        }
135    }