This project has retired. For details please refer to its
Attic page.
ServletTilesApplicationContext xref
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
30 import java.io.IOException;
31 import java.net.URL;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.HashSet;
35
36 /***
37 * Servlet-based implementation of the TilesApplicationContext interface.
38 *
39 * @version $Rev: 709140 $ $Date: 2008-10-30 12:07:50 +0100 (gio, 30 ott 2008) $
40 */
41 public class ServletTilesApplicationContext implements TilesApplicationContext {
42
43 /***
44 * The servlet context to use.
45 */
46 private ServletContext servletContext;
47
48
49 /***
50 * <p>The lazily instantiated <code>Map</code> of application scope
51 * attributes.</p>
52 */
53 private Map<String, Object> applicationScope = null;
54
55
56 /***
57 * <p>The lazily instantiated <code>Map</code> of context initialization
58 * parameters.</p>
59 */
60 private Map<String, String> initParam = null;
61
62
63 /***
64 * Creates a new instance of ServletTilesApplicationContext.
65 *
66 * @param servletContext The servlet context to use.
67 */
68 public ServletTilesApplicationContext(ServletContext servletContext) {
69 initialize(servletContext);
70 }
71
72 /*** {@inheritDoc} */
73 public Object getContext() {
74 return servletContext;
75 }
76
77 /*** {@inheritDoc} */
78 public Map<String, Object> getApplicationScope() {
79
80 if ((applicationScope == null) && (servletContext != null)) {
81 applicationScope = new ServletApplicationScopeMap(servletContext);
82 }
83 return (applicationScope);
84
85 }
86
87
88 /*** {@inheritDoc} */
89 public Map<String, String> getInitParams() {
90
91 if ((initParam == null) && (servletContext != null)) {
92 initParam = new ServletInitParamMap(servletContext);
93 }
94 return (initParam);
95
96 }
97
98 /*** {@inheritDoc} */
99 public URL getResource(String path) throws IOException {
100 return servletContext.getResource(path);
101 }
102
103 /*** {@inheritDoc} */
104 public Set<URL> getResources(String path) throws IOException {
105 HashSet<URL> urls = new HashSet<URL>();
106 urls.add(getResource(path));
107 return urls;
108 }
109
110 /***
111 * Returns the servlet context.
112 *
113 * @return The servlet context.
114 * @deprecated Use {@link #getContext()}.
115 */
116 public ServletContext getServletContext() {
117 return servletContext;
118 }
119
120
121 /***
122 * <p>Initialize (or reinitialize) this {@link TilesApplicationContext} instance
123 * for the specified Servlet API objects.</p>
124 *
125 * @param context The <code>ServletContext</code> for this web application
126 */
127 public void initialize(ServletContext context) {
128
129 this.servletContext = context;
130
131
132 }
133
134
135 /***
136 * <p>Release references to allocated resources acquired in
137 * <code>initialize()</code> of via subsequent processing. After this
138 * method is called, subsequent calls to any other method than
139 * <code>initialize()</code> will return undefined results.</p>
140 */
141 public void release() {
142
143
144 applicationScope = null;
145 initParam = null;
146
147
148 servletContext = null;
149
150 }
151
152 /***
153 * Creates a servlet context for a given request/response pair.
154 *
155 * @param request The request object.
156 * @param response The response object.
157 * @return The corresponding Tiles request context.
158 * @deprecated Use
159 * {@link org.apache.tiles.context.TilesContextFactory#createRequestContext(TilesApplicationContext, Object...)}
160 * .
161 */
162 public TilesRequestContext createRequestContext(Object request, Object response) {
163 if (request instanceof HttpServletRequest) {
164 return new ServletTilesRequestContext(
165 servletContext,
166 (HttpServletRequest) request,
167 (HttpServletResponse) response
168 );
169 } else {
170 throw new IllegalArgumentException("Invalid context specified. "
171 + servletContext.getClass().getName());
172 }
173 }
174 }