KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.web.context.ServletContextAware;
28
29 /**
30  * Exporter that takes Spring-defined objects and exposes them as
31  * ServletContext attributes. Usually, bean references will be used
32  * to export Spring-defined beans as ServletContext attributes.
33  *
34  * <p>Useful to make Spring-defined beans available to code that is
35  * not aware of Spring at all, but rather just of the Servlet API.
36  * Client code can then use plain ServletContext attribute lookups
37  * to access those objects, despite them being defined in a Spring
38  * application context.
39  *
40  * <p>Alternatively, consider using the WebApplicationContextUtils
41  * class to access Spring-defined beans via the WebApplicationContext
42  * interface. This makes client code aware of Spring API, of course.
43  *
44  * @author Juergen Hoeller
45  * @since 1.1.4
46  * @see javax.servlet.ServletContext#getAttribute
47  * @see WebApplicationContextUtils#getWebApplicationContext
48  */

49 public class ServletContextAttributeExporter implements ServletContextAware {
50
51     protected final Log logger = LogFactory.getLog(getClass());
52
53     private Map JavaDoc attributes;
54
55     /**
56      * Set the ServletContext attributes to expose as key-value pairs.
57      * Each key will be considered a ServletContext attributes key,
58      * and each value will be used as corresponding attribute value.
59      * <p>Usually, you will use bean references for the values,
60      * to export Spring-defined beans as ServletContext attributes.
61      * Of course, it is also possible to define plain values to export.
62      * @param attributes Map with String keys and Object values
63      */

64     public void setAttributes(Map JavaDoc attributes) {
65         this.attributes = attributes;
66     }
67
68     public void setServletContext(ServletContext JavaDoc servletContext) {
69         for (Iterator JavaDoc it = this.attributes.entrySet().iterator(); it.hasNext();) {
70             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
71             String JavaDoc attributeName = (String JavaDoc) entry.getKey();
72             if (logger.isWarnEnabled()) {
73                 if (servletContext.getAttribute(attributeName) != null) {
74                     logger.warn("Overwriting existing ServletContext attribute with name '" + attributeName + "'");
75                 }
76             }
77             servletContext.setAttribute(attributeName, entry.getValue());
78             if (logger.isInfoEnabled()) {
79                 logger.info("Exported ServletContext attribute with name '" + attributeName + "'");
80             }
81         }
82     }
83
84 }
85
Popular Tags