KickJava   Java API By Example, From Geeks To Geeks.

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


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 fetches a specific, existing ServletContext attribute.
26  * Exposes that ServletContext attribute when used as bean reference,
27  * effectively making it available as named Spring bean instance.
28  *
29  * <p>Intended to link in ServletContext attributes that exist before
30  * the startup of the Spring application context. Typically, such
31  * attributes will have been put there by third-party web frameworks.
32  * In a purely Spring-based web application, no such linking in of
33  * ServletContext attrutes will be necessary.
34  *
35  * @author Juergen Hoeller
36  * @since 1.1.4
37  * @see ServletContextParameterFactoryBean
38  */

39 public class ServletContextAttributeFactoryBean implements FactoryBean, ServletContextAware {
40
41     private String JavaDoc attributeName;
42
43     private Object JavaDoc attribute;
44
45
46     /**
47      * Set the name of the ServletContext attribute to expose.
48      */

49     public void setAttributeName(String JavaDoc attributeName) {
50         this.attributeName = attributeName;
51     }
52
53     public void setServletContext(ServletContext JavaDoc servletContext) {
54         if (this.attributeName == null) {
55             throw new IllegalArgumentException JavaDoc("attributeName is required");
56         }
57         this.attribute = servletContext.getAttribute(this.attributeName);
58         if (this.attribute == null) {
59             throw new IllegalStateException JavaDoc("No ServletContext attribute '" + this.attributeName + "' found");
60         }
61     }
62
63
64     public Object JavaDoc getObject() throws Exception JavaDoc {
65         return this.attribute;
66     }
67
68     public Class JavaDoc getObjectType() {
69         return (this.attribute != null ? this.attribute.getClass() : null);
70     }
71
72     public boolean isSingleton() {
73         return true;
74     }
75
76 }
77
Popular Tags