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 javax.naming.InitialContext;
020import javax.naming.NamingException;
021
022import org.apache.logging.log4j.core.LogEvent;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024
025/**
026 * Looks up keys from JNDI resources.
027 */
028@Plugin(name = "jndi", category = "Lookup")
029public class JndiLookup implements StrLookup {
030
031    /** JNDI resourcce path prefix used in a J2EE container */
032    static final String CONTAINER_JNDI_RESOURCE_PATH_PREFIX = "java:comp/env/";
033
034    /**
035     * Looks up the value of the JNDI resource.
036     * @param key  the JNDI resource name to be looked up, may be null
037     * @return The value of the JNDI resource.
038     */
039    @Override
040    public String lookup(final String key) {
041        return lookup(null, key);
042    }
043
044    /**
045     * Looks up the value of the JNDI resource.
046     * @param event The current LogEvent (is ignored by this StrLookup).
047     * @param key  the JNDI resource name to be looked up, may be null
048     * @return The value of the JNDI resource.
049     */
050    @Override
051    public String lookup(final LogEvent event, final String key) {
052        if (key == null) {
053            return null;
054        }
055
056        try {
057            InitialContext ctx = new InitialContext();
058            return (String) ctx.lookup(convertJndiName(key));
059        } catch (NamingException e) {
060            return null;
061        }
062    }
063
064    /**
065     * Convert the given JNDI name to the actual JNDI name to use.
066     * Default implementation applies the "java:comp/env/" prefix
067     * unless other scheme like "java:" is given.
068     * @param jndiName The name of the resource.
069     * @return The fully qualified name to look up.
070     */
071    private String convertJndiName(String jndiName) {
072        if (!jndiName.startsWith(CONTAINER_JNDI_RESOURCE_PATH_PREFIX) && jndiName.indexOf(':') == -1) {
073            jndiName = CONTAINER_JNDI_RESOURCE_PATH_PREFIX + jndiName;
074        }
075
076        return jndiName;
077    }
078}