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.InitialContext;
020import javax.naming.NamingException;
021
022import org.springframework.core.SpringProperties;
023
024/**
025 * {@link JndiLocatorSupport} subclass with public lookup methods,
026 * for convenient use as a delegate.
027 *
028 * @author Juergen Hoeller
029 * @since 3.0.1
030 */
031public class JndiLocatorDelegate extends JndiLocatorSupport {
032
033        /**
034         * System property that instructs Spring to ignore a default JNDI environment, i.e.
035         * to always return {@code false} from {@link #isDefaultJndiEnvironmentAvailable()}.
036         * <p>The default is "false", allowing for regular default JNDI access e.g. in
037         * {@link JndiPropertySource}. Switching this flag to {@code true} is an optimization
038         * for scenarios where nothing is ever to be found for such JNDI fallback searches
039         * to begin with, avoiding the repeated JNDI lookup overhead.
040         * <p>Note that this flag just affects JNDI fallback searches, not explicitly configured
041         * JNDI lookups such as for a {@code DataSource} or some other environment resource.
042         * The flag literally just affects code which attempts JNDI searches based on the
043         * {@code JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()} check: in particular,
044         * {@code StandardServletEnvironment} and {@code StandardPortletEnvironment}.
045         * @since 4.3
046         * @see #isDefaultJndiEnvironmentAvailable()
047         * @see JndiPropertySource
048         */
049        public static final String IGNORE_JNDI_PROPERTY_NAME = "spring.jndi.ignore";
050
051
052        private static final boolean shouldIgnoreDefaultJndiEnvironment =
053                        SpringProperties.getFlag(IGNORE_JNDI_PROPERTY_NAME);
054
055
056        @Override
057        public Object lookup(String jndiName) throws NamingException {
058                return super.lookup(jndiName);
059        }
060
061        @Override
062        public <T> T lookup(String jndiName, Class<T> requiredType) throws NamingException {
063                return super.lookup(jndiName, requiredType);
064        }
065
066
067        /**
068         * Configure a {@code JndiLocatorDelegate} with its "resourceRef" property set to
069         * {@code true}, meaning that all names will be prefixed with "java:comp/env/".
070         * @see #setResourceRef
071         */
072        public static JndiLocatorDelegate createDefaultResourceRefLocator() {
073                JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
074                jndiLocator.setResourceRef(true);
075                return jndiLocator;
076        }
077
078        /**
079         * Check whether a default JNDI environment, as in a J2EE environment,
080         * is available on this JVM.
081         * @return {@code true} if a default InitialContext can be used,
082         * {@code false} if not
083         */
084        public static boolean isDefaultJndiEnvironmentAvailable() {
085                if (shouldIgnoreDefaultJndiEnvironment) {
086                        return false;
087                }
088                try {
089                        new InitialContext().getEnvironment();
090                        return true;
091                }
092                catch (Throwable ex) {
093                        return false;
094                }
095        }
096
097}