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