1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.tiles;
22
23 import org.apache.tiles.TilesApplicationContext;
24 import org.apache.tiles.TilesException;
25 import org.apache.tiles.context.TilesContextFactory;
26 import org.apache.tiles.context.TilesRequestContext;
27 import org.apache.tiles.definition.DefinitionsFactory;
28 import org.apache.tiles.factory.TilesContainerFactory;
29 import org.apache.tiles.impl.BasicTilesContainer;
30 import org.apache.tiles.preparer.PreparerFactory;
31
32 import javax.servlet.jsp.PageContext;
33 import java.util.Map;
34
35
36 public class StrutsTilesContainerFactory extends TilesContainerFactory {
37
38 /***
39 * Initialize the container in a struts specific manner.
40 *
41 * @param context
42 * @param container
43 * @throws TilesException
44 */
45 public void initializeContainer(Object context,
46 BasicTilesContainer container)
47 throws TilesException {
48
49 Map<String, String> initParmMap =
50 TilesContainerFactory.getInitParameterMap(context);
51
52 TilesContextFactory contextFactory = (TilesContextFactory)
53 TilesContainerFactory.createFactory(
54 initParmMap, TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM);
55
56 contextFactory = new StrutsTilesContextFactory(contextFactory);
57
58 DefinitionsFactory defsFactory = (DefinitionsFactory)
59 TilesContainerFactory.createFactory(
60 initParmMap,
61 TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM);
62
63 PreparerFactory prepFactory =
64 (PreparerFactory) TilesContainerFactory.createFactory(
65 initParmMap,
66 TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM);
67
68 TilesApplicationContext tilesContext =
69 contextFactory.createApplicationContext(context);
70
71 container.setDefinitionsFactory(defsFactory);
72 container.setContextFactory(contextFactory);
73 container.setPreparerFactory(prepFactory);
74 container.setApplicationContext(tilesContext);
75
76 container.init(getInitParameterMap(context));
77
78 }
79
80 /***
81 * Wrapper factory, used to decorate the TilesRequestContext with a
82 * FreemarkerResult aware version.
83 *
84 */
85 class StrutsTilesContextFactory implements TilesContextFactory {
86
87 private TilesContextFactory factory;
88
89 public StrutsTilesContextFactory(TilesContextFactory factory) {
90 this.factory = factory;
91 }
92
93 public void init(Map map) {
94 factory.init(map);
95 }
96
97 public TilesApplicationContext createApplicationContext(Object context) {
98 return factory.createApplicationContext(context);
99 }
100
101 public TilesRequestContext createRequestContext(TilesApplicationContext tilesApplicationContext, Object request, Object response) {
102 TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, request, response);
103 return new StrutsTilesRequestContext(context);
104 }
105
106 public TilesRequestContext createRequestContext(TilesApplicationContext tilesApplicationContext, PageContext pageContext) {
107 TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, pageContext);
108 return new StrutsTilesRequestContext(context);
109 }
110 }
111 }