Browse Source

fixed StandardServlet/PortletEnvironment to check for JNDI (for Google App Engine compatibility)

pull/7/head
Juergen Hoeller 13 years ago
parent
commit
2fdc2b5822
  1. 31
      org.springframework.context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java
  2. 16
      org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java
  3. 8
      org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StandardPortletEnvironment.java
  4. 6
      org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java

31
org.springframework.context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -37,4 +38,32 @@ public class JndiLocatorDelegate extends JndiLocatorSupport {
return super.lookup(jndiName, requiredType);
}
/**
* Configure a {@code JndiLocatorDelegate} with its "resourceRef" property set to
* <code>true</code>, 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 <code>true</code> if a default InitialContext can be used,
* <code>false</code> if not
*/
public static boolean isDefaultJndiEnvironmentAvailable() {
try {
new InitialContext();
return true;
}
catch (Throwable ex) {
return false;
}
}
}

16
org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java

@ -58,7 +58,7 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> { @@ -58,7 +58,7 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
* "java:comp/env/".
*/
public JndiPropertySource(String name) {
this(name, createDefaultJndiLocator());
this(name, JndiLocatorDelegate.createDefaultResourceRefLocator());
}
/**
@ -82,22 +82,12 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> { @@ -82,22 +82,12 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
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;
}
}

8
org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StandardPortletEnvironment.java

@ -20,11 +20,12 @@ import javax.portlet.PortletConfig; @@ -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 { @@ -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);
}
}

6
org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java

@ -24,6 +24,7 @@ import org.springframework.core.env.MutablePropertySources; @@ -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 { @@ -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);
}
}

Loading…
Cancel
Save