001: /*
002: * Copyright 2002-2005 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: * http://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:
017: package org.springframework.web.context.support;
018:
019: import java.util.Properties;
020:
021: import javax.servlet.ServletContext;
022:
023: import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
024: import org.springframework.web.context.ServletContextAware;
025:
026: /**
027: * Subclass of PropertyPlaceholderConfigurer that resolves placeholders as
028: * ServletContext init parameters (that is, <code>web.xml</code> context-param
029: * entries).
030: *
031: * <p>Can be combined with "locations" and/or "properties" values in addition
032: * to web.xml context-params. Alternatively, can be defined without local
033: * properties, to resolve all placeholders as <code>web.xml</code> context-params
034: * (or JVM system properties).
035: *
036: * <p>If a placeholder could not be resolved against the provided local
037: * properties within the application, this configurer will fall back to
038: * ServletContext parameters. Can also be configured to let ServletContext
039: * init parameters override local properties (contextOverride=true).
040: *
041: * <p>Optionally supports searching for ServletContext <i>attributes</i>: If turned
042: * on, an otherwise unresolvable placeholder will matched against the corresponding
043: * ServletContext attribute, using its stringified value if found. This can be
044: * used to feed dynamic values into Spring's placeholder resolution.
045: *
046: * <p>If not running within a WebApplicationContext (or any other context that
047: * is able to satisfy the ServletContextAware callback), this class will behave
048: * like the default PropertyPlaceholderConfigurer. This allows for keeping
049: * ServletContextPropertyPlaceholderConfigurer definitions in test suites.
050: *
051: * @author Juergen Hoeller
052: * @since 1.1.4
053: * @see #setLocations
054: * @see #setProperties
055: * @see #setSystemPropertiesModeName
056: * @see #setContextOverride
057: * @see #setSearchContextAttributes
058: * @see javax.servlet.ServletContext#getInitParameter(String)
059: * @see javax.servlet.ServletContext#getAttribute(String)
060: */
061: public class ServletContextPropertyPlaceholderConfigurer extends
062: PropertyPlaceholderConfigurer implements ServletContextAware {
063:
064: private boolean contextOverride = false;
065:
066: private boolean searchContextAttributes = false;
067:
068: private ServletContext servletContext;
069:
070: /**
071: * Set whether ServletContext init parameters (and optionally also ServletContext
072: * attributes) should override local properties within the application.
073: * Default is "false": ServletContext settings serve as fallback.
074: * <p>Note that system properties will still override ServletContext settings,
075: * if the system properties mode is set to "SYSTEM_PROPERTIES_MODE_OVERRIDE".
076: * @see #setSearchContextAttributes
077: * @see #setSystemPropertiesModeName
078: * @see #SYSTEM_PROPERTIES_MODE_OVERRIDE
079: */
080: public void setContextOverride(boolean contextOverride) {
081: this .contextOverride = contextOverride;
082: }
083:
084: /**
085: * Set whether to search for matching a ServletContext attribute before
086: * checking a ServletContext init parameter. Default is "false": only
087: * checking init parameters.
088: * <p>If turned on, the configurer will look for a ServletContext attribute with
089: * the same name as the placeholder, and use its stringified value if found.
090: * Exposure of such ServletContext attributes can be used to dynamically override
091: * init parameters defined in <code>web.xml</code>, for example in a custom
092: * context listener.
093: * @see javax.servlet.ServletContext#getInitParameter(String)
094: * @see javax.servlet.ServletContext#getAttribute(String)
095: */
096: public void setSearchContextAttributes(
097: boolean searchContextAttributes) {
098: this .searchContextAttributes = searchContextAttributes;
099: }
100:
101: /**
102: * Set the ServletContext to resolve placeholders against.
103: * Will be auto-populated when running in a WebApplicationContext.
104: * <p>If not set, this configurer will simply not resolve placeholders
105: * against the ServletContext: It will effectively behave like a plain
106: * PropertyPlaceholderConfigurer in such a scenario.
107: */
108: public void setServletContext(ServletContext servletContext) {
109: this .servletContext = servletContext;
110: }
111:
112: protected String resolvePlaceholder(String placeholder,
113: Properties props) {
114: String value = null;
115: if (this .contextOverride && this .servletContext != null) {
116: value = resolvePlaceholder(placeholder,
117: this .servletContext, this .searchContextAttributes);
118: }
119: if (value == null) {
120: value = super .resolvePlaceholder(placeholder, props);
121: }
122: if (value == null && this .servletContext != null) {
123: value = resolvePlaceholder(placeholder,
124: this .servletContext, this .searchContextAttributes);
125: }
126: return value;
127: }
128:
129: /**
130: * Resolves the given placeholder using the init parameters
131: * and optionally also the attributes of the given ServletContext.
132: * <p>Default implementation checks ServletContext attributes before
133: * init parameters. Can be overridden to customize this behavior,
134: * potentially also applying specific naming patterns for parameters
135: * and/or attributes (instead of using the exact placeholder name).
136: * @param placeholder the placeholder to resolve
137: * @param servletContext the ServletContext to check
138: * @param searchContextAttributes whether to search for a matching
139: * ServletContext attribute
140: * @return the resolved value, of null if none
141: * @see javax.servlet.ServletContext#getInitParameter(String)
142: * @see javax.servlet.ServletContext#getAttribute(String)
143: */
144: protected String resolvePlaceholder(String placeholder,
145: ServletContext servletContext,
146: boolean searchContextAttributes) {
147:
148: String value = null;
149: if (searchContextAttributes) {
150: Object attrValue = servletContext.getAttribute(placeholder);
151: if (attrValue != null) {
152: value = attrValue.toString();
153: }
154: }
155: if (value == null) {
156: value = servletContext.getInitParameter(placeholder);
157: }
158: return value;
159: }
160:
161: }
|