1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.servlet.context;
23
24 import org.apache.tiles.TilesApplicationContext;
25 import org.apache.tiles.context.TilesContextFactory;
26 import org.apache.tiles.context.TilesRequestContext;
27
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.util.Map;
32
33 /***
34 * Creates an instance of the appropriate TilesApplicationContext implementation.
35 *
36 * @version $Rev: 531864 $ $Date: 2007-04-24 12:24:30 +0200 (Tue, 24 Apr 2007) $
37 */
38 public class ServletTilesContextFactory implements TilesContextFactory {
39
40 /*** {@inheritDoc} */
41 public void init(Map<String, String> configParameters) {
42 }
43
44 /*** {@inheritDoc} */
45 public TilesApplicationContext createApplicationContext(Object context) {
46 if (context instanceof ServletContext) {
47 ServletContext servletContext = (ServletContext) context;
48 return new ServletTilesApplicationContext(servletContext);
49 }
50 return null;
51 }
52
53 /*** {@inheritDoc} */
54 public TilesRequestContext createRequestContext(TilesApplicationContext context,
55 Object... requestItems) {
56 if (requestItems.length == 2) {
57 ServletContext servletContext = getServletContext(context);
58 if (servletContext != null) {
59 return new ServletTilesRequestContext(servletContext,
60 (HttpServletRequest) requestItems[0],
61 (HttpServletResponse) requestItems[1]);
62 }
63 }
64
65 return null;
66 }
67
68 /***
69 * Returns the original servlet context.
70 *
71 * @param context The application context.
72 * @return The original servlet context, if found.
73 */
74 protected ServletContext getServletContext(TilesApplicationContext context) {
75 if (context instanceof ServletTilesApplicationContext) {
76 ServletTilesApplicationContext app = (ServletTilesApplicationContext) context;
77 return app.getServletContext();
78 }
79 return null;
80
81 }
82 }