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.startup;
23
24 import org.apache.tiles.TilesApplicationContext;
25 import org.apache.tiles.TilesContainer;
26 import org.apache.tiles.TilesException;
27 import org.apache.tiles.access.TilesAccess;
28 import org.apache.tiles.factory.AbstractTilesContainerFactory;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /***
33 * Default Tiles initialization delegate implementation under a servlet
34 * environment. It uses init parameters to create the
35 * {@link TilesApplicationContext} and the {@link TilesContainer}.
36 *
37 * @version $Rev: 797540 $ $Date: 2009-07-24 17:42:00 +0200 (ven, 24 lug 2009) $
38 * @since 2.2.0
39 */
40 public abstract class AbstractTilesInitializer implements TilesInitializer {
41
42 /***
43 * The logging object.
44 */
45 private Logger log = LoggerFactory.getLogger(getClass());
46
47 /***
48 * Init parameter to define the key under which the container will be
49 * stored.
50 *
51 * @since 2.1.2
52 */
53 public static final String CONTAINER_KEY_INIT_PARAMETER =
54 "org.apache.tiles.startup.AbstractTilesInitializer.CONTAINER_KEY";
55
56 /***
57 * The initialized application context.
58 */
59 private TilesApplicationContext applicationContext;
60
61 /***
62 * The initialized container.
63 */
64 private TilesContainer container;
65
66 /*** {@inheritDoc} */
67 public void initialize(TilesApplicationContext applicationContext) {
68 this.applicationContext = createTilesApplicationContext(applicationContext);
69 String key = getContainerKey(this.applicationContext);
70 container = createContainer(this.applicationContext);
71 TilesAccess.setContainer(this.applicationContext, container, key);
72 }
73
74 /*** {@inheritDoc} */
75 public void destroy() {
76 try {
77 TilesAccess.setContainer(applicationContext, null,
78 getContainerKey(applicationContext));
79 } catch (TilesException e) {
80 log.warn("Unable to remove tiles container from service.", e);
81 }
82 }
83
84 /***
85 * Creates the Tiles application context, to be used across all the
86 * Tiles-based application. If you override this class, please override this
87 * method or
88 * {@link #createAndInitializeTilesApplicationContextFactory(TilesApplicationContext)}
89 * .<br>
90 * This implementation returns the preliminary context passed as a parameter
91 *
92 * @param preliminaryContext The preliminary application context to use.
93 * @return The Tiles application context.
94 * @since 2.2.0
95 */
96 protected TilesApplicationContext createTilesApplicationContext(
97 TilesApplicationContext preliminaryContext) {
98 return preliminaryContext;
99 }
100
101 /***
102 * Returns the container key under which the container will be stored.
103 * This implementation returns <code>null</code> so that the container will
104 * be the default one.
105 *
106 * @param applicationContext The Tiles application context to use.
107 * @return The container key.
108 * @since 2.2.0
109 */
110 protected String getContainerKey(TilesApplicationContext applicationContext) {
111 return null;
112 }
113
114 /***
115 * Creates a Tiles container. If you override this class, please override
116 * this method or {@link #createContainerFactory(TilesApplicationContext)}.
117 *
118 * @param context The servlet context to use.
119 * @return The created container.
120 * @since 2.2.0
121 */
122 protected TilesContainer createContainer(TilesApplicationContext context) {
123 AbstractTilesContainerFactory factory = createContainerFactory(context);
124 return factory.createContainer(context);
125 }
126
127 /***
128 * Creates a Tiles container factory. If you override this class, please
129 * override this method or {@link #createContainer(TilesApplicationContext)}.
130 *
131 * @param context The servlet context to use.
132 * @return The created container factory.
133 * @since 2.2.0
134 */
135 protected abstract AbstractTilesContainerFactory createContainerFactory(
136 TilesApplicationContext context);
137 }