From af2530d57d5c491a6168c5f7ded663f14e555591 Mon Sep 17 00:00:00 2001 From: Scott Battaglia Date: Wed, 27 Aug 2008 16:37:36 +0000 Subject: [PATCH] CASC-66 added support for configuration via JNDI --- .../util/AbstractConfigurationFilter.java | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/util/AbstractConfigurationFilter.java b/cas-client-core/src/main/java/org/jasig/cas/client/util/AbstractConfigurationFilter.java index 4b24931..c196213 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/util/AbstractConfigurationFilter.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/util/AbstractConfigurationFilter.java @@ -1,8 +1,13 @@ package org.jasig.cas.client.util; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.servlet.Filter; import javax.servlet.FilterConfig; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + /** * Abstracts out the ability to configure the filters from the initial properties provided. * @@ -11,11 +16,15 @@ import javax.servlet.FilterConfig; * @since 3.1 */ public abstract class AbstractConfigurationFilter implements Filter { + + protected final Log log = LogFactory.getLog(getClass()); /** * Retrieves the property from the FilterConfig. First it checks the FilterConfig's initParameters to see if it * has a value. - * If it does, it returns that, otherwise it trieves the ServletContext's initParameters and returns that value if any. + * If it does, it returns that, otherwise it retrieves the ServletContext's initParameters and returns that value if any. + *

+ * Finally, it will check JNDI if all other methods fail. All the JNDI properties should be stored under java:comp/env/cas/{propertyName} * * @param filterConfig the Filter Configuration. * @param propertyName the property to retrieve. @@ -34,8 +43,37 @@ public abstract class AbstractConfigurationFilter implements Filter { if (CommonUtils.isNotBlank(value2)) { return value2; } + InitialContext context = null; + try { + context = new InitialContext(); + } catch (final NamingException e) { + log.warn(e,e); + return defaultValue; + } + + + final String shortName = this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".")+1); + final String value3 = loadFromContext(context, "java:comp/env/cas/" + shortName + "/" + propertyName); + + if (CommonUtils.isNotBlank(value3)) { + return value3; + } + + final String value4 = loadFromContext(context, "java:comp/env/cas/" + propertyName); + + if (CommonUtils.isNotBlank(value4)) { + return value4; + } return defaultValue; } - + + protected final String loadFromContext(final InitialContext context, String path) { + try { + return (String) context.lookup(path); + } catch (final NamingException e) { + log.warn(e,e); + return null; + } + } }