1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.servlet.context;
22
23 import org.apache.tiles.TilesApplicationContext;
24 import org.apache.tiles.context.TilesRequestContext;
25
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.HashSet;
34
35 /***
36 * Servlet-based implementation of the TilesApplicationContext interface.
37 *
38 * @version $Rev: 531864 $ $Date: 2007-04-24 12:24:30 +0200 (Tue, 24 Apr 2007) $
39 */
40 public class ServletTilesApplicationContext implements TilesApplicationContext {
41
42 /***
43 * The servlet context to use.
44 */
45 private ServletContext servletContext;
46
47
48 /***
49 * <p>The lazily instantiated <code>Map</code> of application scope
50 * attributes.</p>
51 */
52 private Map<String, Object> applicationScope = null;
53
54
55 /***
56 * <p>The lazily instantiated <code>Map</code> of context initialization
57 * parameters.</p>
58 */
59 private Map<String, String> initParam = null;
60
61
62 /***
63 * Creates a new instance of ServletTilesApplicationContext.
64 *
65 * @param servletContext The servlet context to use.
66 */
67 public ServletTilesApplicationContext(ServletContext servletContext) {
68 initialize(servletContext);
69 }
70
71
72 /*** {@inheritDoc} */
73 public Map<String, Object> getApplicationScope() {
74
75 if ((applicationScope == null) && (servletContext != null)) {
76 applicationScope = new ServletApplicationScopeMap(servletContext);
77 }
78 return (applicationScope);
79
80 }
81
82
83 /*** {@inheritDoc} */
84 public Map<String, String> getInitParams() {
85
86 if ((initParam == null) && (servletContext != null)) {
87 initParam = new ServletInitParamMap(servletContext);
88 }
89 return (initParam);
90
91 }
92
93 /*** {@inheritDoc} */
94 public URL getResource(String path) throws MalformedURLException {
95 return servletContext.getResource(path);
96 }
97
98 /*** {@inheritDoc} */
99 public Set<URL> getResources(String path) throws MalformedURLException {
100 HashSet<URL> urls = new HashSet<URL>();
101 urls.add(getResource(path));
102 return urls;
103 }
104
105 /***
106 * Returns the servlet context.
107 *
108 * @return The servlet context.
109 */
110 public ServletContext getServletContext() {
111 return servletContext;
112 }
113
114
115 /***
116 * <p>Initialize (or reinitialize) this {@link TilesApplicationContext} instance
117 * for the specified Servlet API objects.</p>
118 *
119 * @param context The <code>ServletContext</code> for this web application
120 */
121 public void initialize(ServletContext context) {
122
123 this.servletContext = context;
124
125
126 }
127
128
129 /***
130 * <p>Release references to allocated resources acquired in
131 * <code>initialize()</code> of via subsequent processing. After this
132 * method is called, subsequent calls to any other method than
133 * <code>initialize()</code> will return undefined results.</p>
134 */
135 public void release() {
136
137
138 applicationScope = null;
139 initParam = null;
140
141
142 servletContext = null;
143
144 }
145
146 /***
147 * Creates a servlet context for a given request/response pair.
148 *
149 * @param request The request object.
150 * @param response The response object.
151 * @return The corresponding Tiles request context.
152 */
153 public TilesRequestContext createRequestContext(Object request, Object response) {
154 if (request instanceof HttpServletRequest) {
155 return new ServletTilesRequestContext(
156 servletContext,
157 (HttpServletRequest) request,
158 (HttpServletResponse) response
159 );
160 } else {
161 throw new IllegalArgumentException("Invalid context specified. "
162 + servletContext.getClass().getName());
163 }
164 }
165 }