01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 javax.servlet.ServletContext;
20:
21: import org.springframework.beans.factory.FactoryBean;
22: import org.springframework.web.context.ServletContextAware;
23:
24: /**
25: * FactoryBean that retrieves a specific ServletContext init parameter
26: * (that is, a "context-param" defined in <code>web.xml</code>).
27: * Exposes that ServletContext init parameter when used as bean reference,
28: * effectively making it available as named Spring bean instance.
29: *
30: * @author Juergen Hoeller
31: * @since 1.2.4
32: * @see ServletContextAttributeFactoryBean
33: */
34: public class ServletContextParameterFactoryBean implements FactoryBean,
35: ServletContextAware {
36:
37: private String initParamName;
38:
39: private String paramValue;
40:
41: /**
42: * Set the name of the ServletContext init parameter to expose.
43: */
44: public void setInitParamName(String initParamName) {
45: this .initParamName = initParamName;
46: }
47:
48: public void setServletContext(ServletContext servletContext) {
49: if (this .initParamName == null) {
50: throw new IllegalArgumentException(
51: "initParamName is required");
52: }
53: this .paramValue = servletContext
54: .getInitParameter(this .initParamName);
55: if (this .paramValue == null) {
56: throw new IllegalStateException(
57: "No ServletContext init parameter '"
58: + this .initParamName + "' found");
59: }
60: }
61:
62: public Object getObject() throws Exception {
63: return this .paramValue;
64: }
65:
66: public Class getObjectType() {
67: return String.class;
68: }
69:
70: public boolean isSingleton() {
71: return true;
72: }
73:
74: }
|