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