From 2fdc2b58221585bfba578a811d0a2261733db09b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 20 Oct 2011 10:46:16 +0000 Subject: [PATCH] fixed StandardServlet/PortletEnvironment to check for JNDI (for Google App Engine compatibility) --- .../jndi/JndiLocatorDelegate.java | 31 ++++++++++++++++++- .../jndi/JndiPropertySource.java | 16 ++-------- .../context/StandardPortletEnvironment.java | 8 +++-- .../support/StandardServletEnvironment.java | 6 +++- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java b/org.springframework.context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java index 907b63df20..fd2687e033 100644 --- a/org.springframework.context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java +++ b/org.springframework.context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.jndi; +import javax.naming.InitialContext; import javax.naming.NamingException; /** @@ -37,4 +38,32 @@ public class JndiLocatorDelegate extends JndiLocatorSupport { return super.lookup(jndiName, requiredType); } + + /** + * Configure a {@code JndiLocatorDelegate} with its "resourceRef" property set to + * true, meaning that all names will be prefixed with "java:comp/env/". + * @see #setResourceRef + */ + public static JndiLocatorDelegate createDefaultResourceRefLocator() { + JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); + jndiLocator.setResourceRef(true); + return jndiLocator; + } + + /** + * Check whether a default JNDI environment, as in a J2EE environment, + * is available on this JVM. + * @return true if a default InitialContext can be used, + * false if not + */ + public static boolean isDefaultJndiEnvironmentAvailable() { + try { + new InitialContext(); + return true; + } + catch (Throwable ex) { + return false; + } + } + } diff --git a/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java b/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java index adce69cc6f..46886023a4 100644 --- a/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java +++ b/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java @@ -58,7 +58,7 @@ public class JndiPropertySource extends PropertySource { * "java:comp/env/". */ public JndiPropertySource(String name) { - this(name, createDefaultJndiLocator()); + this(name, JndiLocatorDelegate.createDefaultResourceRefLocator()); } /** @@ -82,22 +82,12 @@ public class JndiPropertySource extends PropertySource { Object value = this.source.lookup(name); logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]"); return value; - } catch (NamingException ex) { + } + catch (NamingException ex) { logger.debug("JNDI lookup for name [" + name + "] threw NamingException " + "with message: " + ex.getMessage() + ". Returning null."); return null; } } - /** - * Configure a {@code JndiLocatorDelegate} with its "resourceRef" property set to true - * meaning that all names will be prefixed with "java:comp/env/". - * @return - */ - private static JndiLocatorDelegate createDefaultJndiLocator() { - JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); - jndiLocator.setResourceRef(true); - return jndiLocator; - } - } diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StandardPortletEnvironment.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StandardPortletEnvironment.java index 13b3af62e7..137d54dc41 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StandardPortletEnvironment.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StandardPortletEnvironment.java @@ -20,11 +20,12 @@ import javax.portlet.PortletConfig; import javax.portlet.PortletContext; import javax.servlet.ServletContext; -import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource.StubPropertySource; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.jndi.JndiLocatorDelegate; import org.springframework.jndi.JndiPropertySource; import org.springframework.web.context.support.StandardServletEnvironment; @@ -82,7 +83,10 @@ public class StandardPortletEnvironment extends StandardEnvironment { propertySources.addLast(new StubPropertySource(PORTLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); - propertySources.addLast(new JndiPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)); + if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { + propertySources.addLast(new JndiPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)); + } super.customizePropertySources(propertySources); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java b/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java index c7bd56b815..0885fadd40 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java @@ -24,6 +24,7 @@ import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource.StubPropertySource; import org.springframework.core.env.StandardEnvironment; +import org.springframework.jndi.JndiLocatorDelegate; import org.springframework.jndi.JndiPropertySource; /** @@ -81,7 +82,10 @@ public class StandardServletEnvironment extends StandardEnvironment { protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); - propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); + if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { + propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); + } super.customizePropertySources(propertySources); } + }