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