KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.ServletContext JavaDoc;
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, ServletContextAware {
35
36     private String JavaDoc initParamName;
37
38     private String JavaDoc paramValue;
39
40
41     /**
42      * Set the name of the ServletContext init parameter to expose.
43      */

44     public void setInitParamName(String JavaDoc initParamName) {
45         this.initParamName = initParamName;
46     }
47
48     public void setServletContext(ServletContext JavaDoc servletContext) {
49         if (this.initParamName == null) {
50             throw new IllegalArgumentException JavaDoc("initParamName is required");
51         }
52         this.paramValue = servletContext.getInitParameter(this.initParamName);
53         if (this.paramValue == null) {
54             throw new IllegalStateException JavaDoc("No ServletContext init parameter '" + this.initParamName + "' found");
55         }
56     }
57
58
59     public Object JavaDoc getObject() throws Exception JavaDoc {
60         return this.paramValue;
61     }
62
63     public Class JavaDoc getObjectType() {
64         return String JavaDoc.class;
65     }
66
67     public boolean isSingleton() {
68         return true;
69     }
70
71 }
72
Popular Tags