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 java.util.Iterator;
20: import java.util.Map;
21:
22: import javax.servlet.ServletContext;
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
50: ServletContextAware {
51:
52: protected final Log logger = LogFactory.getLog(getClass());
53:
54: private Map attributes;
55:
56: /**
57: * Set the ServletContext attributes to expose as key-value pairs.
58: * Each key will be considered a ServletContext attributes key,
59: * and each value will be used as corresponding attribute value.
60: * <p>Usually, you will use bean references for the values,
61: * to export Spring-defined beans as ServletContext attributes.
62: * Of course, it is also possible to define plain values to export.
63: * @param attributes Map with String keys and Object values
64: */
65: public void setAttributes(Map attributes) {
66: this .attributes = attributes;
67: }
68:
69: public void setServletContext(ServletContext servletContext) {
70: for (Iterator it = this .attributes.entrySet().iterator(); it
71: .hasNext();) {
72: Map.Entry entry = (Map.Entry) it.next();
73: String attributeName = (String) entry.getKey();
74: if (logger.isWarnEnabled()) {
75: if (servletContext.getAttribute(attributeName) != null) {
76: logger
77: .warn("Overwriting existing ServletContext attribute with name '"
78: + attributeName + "'");
79: }
80: }
81: servletContext
82: .setAttribute(attributeName, entry.getValue());
83: if (logger.isInfoEnabled()) {
84: logger
85: .info("Exported ServletContext attribute with name '"
86: + attributeName + "'");
87: }
88: }
89: }
90:
91: }
|