001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jndi;
018
019import javax.naming.NamingException;
020
021import org.springframework.core.env.PropertySource;
022
023/**
024 * {@link PropertySource} implementation that reads properties from an underlying Spring
025 * {@link JndiLocatorDelegate}.
026 *
027 * <p>By default, the underlying {@code JndiLocatorDelegate} will be configured with its
028 * {@link JndiLocatorDelegate#setResourceRef(boolean) "resourceRef"} property set to
029 * {@code true}, meaning that names looked up will automatically be prefixed with
030 * "java:comp/env/" in alignment with published
031 * <a href="https://download.oracle.com/javase/jndi/tutorial/beyond/misc/policy.html">JNDI
032 * naming conventions</a>. To override this setting or to change the prefix, manually
033 * configure a {@code JndiLocatorDelegate} and provide it to one of the constructors here
034 * that accepts it. The same applies when providing custom JNDI properties. These should
035 * be specified using {@link JndiLocatorDelegate#setJndiEnvironment(java.util.Properties)}
036 * prior to construction of the {@code JndiPropertySource}.
037 *
038 * <p>Note that {@link org.springframework.web.context.support.StandardServletEnvironment
039 * StandardServletEnvironment} includes a {@code JndiPropertySource} by default, and any
040 * customization of the underlying {@link JndiLocatorDelegate} may be performed within an
041 * {@link org.springframework.context.ApplicationContextInitializer
042 * ApplicationContextInitializer} or {@link org.springframework.web.WebApplicationInitializer
043 * WebApplicationInitializer}.
044 *
045 * @author Chris Beams
046 * @author Juergen Hoeller
047 * @since 3.1
048 * @see JndiLocatorDelegate
049 * @see org.springframework.context.ApplicationContextInitializer
050 * @see org.springframework.web.WebApplicationInitializer
051 * @see org.springframework.web.context.support.StandardServletEnvironment
052 */
053public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
054
055        /**
056         * Create a new {@code JndiPropertySource} with the given name
057         * and a {@link JndiLocatorDelegate} configured to prefix any names with
058         * "java:comp/env/".
059         */
060        public JndiPropertySource(String name) {
061                this(name, JndiLocatorDelegate.createDefaultResourceRefLocator());
062        }
063
064        /**
065         * Create a new {@code JndiPropertySource} with the given name and the given
066         * {@code JndiLocatorDelegate}.
067         */
068        public JndiPropertySource(String name, JndiLocatorDelegate jndiLocator) {
069                super(name, jndiLocator);
070        }
071
072
073        /**
074         * This implementation looks up and returns the value associated with the given
075         * name from the underlying {@link JndiLocatorDelegate}. If a {@link NamingException}
076         * is thrown during the call to {@link JndiLocatorDelegate#lookup(String)}, returns
077         * {@code null} and issues a DEBUG-level log statement with the exception message.
078         */
079        @Override
080        public Object getProperty(String name) {
081                if (getSource().isResourceRef() && name.indexOf(':') != -1) {
082                        // We're in resource-ref (prefixing with "java:comp/env") mode. Let's not bother
083                        // with property names with a colon it since they're probably just containing a
084                        // default value clause, very unlikely to match including the colon part even in
085                        // a textual property source, and effectively never meant to match that way in
086                        // JNDI where a colon indicates a separator between JNDI scheme and actual name.
087                        return null;
088                }
089
090                try {
091                        Object value = this.source.lookup(name);
092                        if (logger.isDebugEnabled()) {
093                                logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]");
094                        }
095                        return value;
096                }
097                catch (NamingException ex) {
098                        if (logger.isDebugEnabled()) {
099                                logger.debug("JNDI lookup for name [" + name + "] threw NamingException " +
100                                                "with message: " + ex.getMessage() + ". Returning null.");
101                        }
102                        return null;
103                }
104        }
105
106}