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