KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > ServletContextPropertyPlaceholderConfigurer


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.context.support;
18
19 import java.util.Properties JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
24 import org.springframework.web.context.ServletContextAware;
25
26 /**
27  * Subclass of PropertyPlaceholderConfigurer that resolves placeholders as
28  * ServletContext init parameters (that is, <code>web.xml</code> context-param
29  * entries).
30  *
31  * <p>Can be combined with "locations" and/or "properties" values in addition
32  * to web.xml context-params. Alternatively, can be defined without local
33  * properties, to resolve all placeholders as <code>web.xml</code> context-params
34  * (or JVM system properties).
35  *
36  * <p>If a placeholder could not be resolved against the provided local
37  * properties within the application, this configurer will fall back to
38  * ServletContext parameters. Can also be configured to let ServletContext
39  * init parameters override local properties (contextOverride=true).
40  *
41  * <p>Optionally supports searching for ServletContext <i>attributes</i>: If turned
42  * on, an otherwise unresolvable placeholder will matched against the corresponding
43  * ServletContext attribute, using its stringified value if found. This can be
44  * used to feed dynamic values into Spring's placeholder resolution.
45  *
46  * <p>If not running within a WebApplicationContext (or any other context that
47  * is able to satisfy the ServletContextAware callback), this class will behave
48  * like the default PropertyPlaceholderConfigurer. This allows for keeping
49  * ServletContextPropertyPlaceholderConfigurer definitions in test suites.
50  *
51  * @author Juergen Hoeller
52  * @since 1.1.4
53  * @see #setLocations
54  * @see #setProperties
55  * @see #setSystemPropertiesModeName
56  * @see #setContextOverride
57  * @see #setSearchContextAttributes
58  * @see javax.servlet.ServletContext#getInitParameter(String)
59  * @see javax.servlet.ServletContext#getAttribute(String)
60  */

61 public class ServletContextPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer
62         implements ServletContextAware {
63
64     private boolean contextOverride = false;
65
66     private boolean searchContextAttributes = false;
67
68     private ServletContext JavaDoc servletContext;
69
70
71     /**
72      * Set whether ServletContext init parameters (and optionally also ServletContext
73      * attributes) should override local properties within the application.
74      * Default is "false": ServletContext settings serve as fallback.
75      * <p>Note that system properties will still override ServletContext settings,
76      * if the system properties mode is set to "SYSTEM_PROPERTIES_MODE_OVERRIDE".
77      * @see #setSearchContextAttributes
78      * @see #setSystemPropertiesModeName
79      * @see #SYSTEM_PROPERTIES_MODE_OVERRIDE
80      */

81     public void setContextOverride(boolean contextOverride) {
82         this.contextOverride = contextOverride;
83     }
84
85     /**
86      * Set whether to search for matching a ServletContext attribute before
87      * checking a ServletContext init parameter. Default is "false": only
88      * checking init parameters.
89      * <p>If turned on, the configurer will look for a ServletContext attribute with
90      * the same name as the placeholder, and use its stringified value if found.
91      * Exposure of such ServletContext attributes can be used to dynamically override
92      * init parameters defined in <code>web.xml</code>, for example in a custom
93      * context listener.
94      * @see javax.servlet.ServletContext#getInitParameter(String)
95      * @see javax.servlet.ServletContext#getAttribute(String)
96      */

97     public void setSearchContextAttributes(boolean searchContextAttributes) {
98         this.searchContextAttributes = searchContextAttributes;
99     }
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 JavaDoc servletContext) {
109         this.servletContext = servletContext;
110     }
111
112
113     protected String JavaDoc resolvePlaceholder(String JavaDoc placeholder, Properties JavaDoc props) {
114         String JavaDoc value = null;
115         if (this.contextOverride && this.servletContext != null) {
116             value = resolvePlaceholder(placeholder, this.servletContext, this.searchContextAttributes);
117         }
118         if (value == null) {
119             value = super.resolvePlaceholder(placeholder, props);
120         }
121         if (value == null && this.servletContext != null) {
122             value = resolvePlaceholder(placeholder, this.servletContext, this.searchContextAttributes);
123         }
124         return value;
125     }
126
127     /**
128      * Resolves the given placeholder using the init parameters
129      * and optionally also the attributes of the given ServletContext.
130      * <p>Default implementation checks ServletContext attributes before
131      * init parameters. Can be overridden to customize this behavior,
132      * potentially also applying specific naming patterns for parameters
133      * and/or attributes (instead of using the exact placeholder name).
134      * @param placeholder the placeholder to resolve
135      * @param servletContext the ServletContext to check
136      * @param searchContextAttributes whether to search for a matching
137      * ServletContext attribute
138      * @return the resolved value, of null if none
139      * @see javax.servlet.ServletContext#getInitParameter(String)
140      * @see javax.servlet.ServletContext#getAttribute(String)
141      */

142     protected String JavaDoc resolvePlaceholder(
143             String JavaDoc placeholder, ServletContext JavaDoc servletContext, boolean searchContextAttributes) {
144
145         String JavaDoc value = null;
146         if (searchContextAttributes) {
147             Object JavaDoc attrValue = servletContext.getAttribute(placeholder);
148             if (attrValue != null) {
149                 value = attrValue.toString();
150             }
151         }
152         if (value == null) {
153             value = servletContext.getInitParameter(placeholder);
154         }
155         return value;
156     }
157
158 }
159
Popular Tags