1 /*
2 * $Id: AbstractTilesInitializer.java 1332134 2012-04-30 09:23:19Z mck $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.startup;
23
24 import org.apache.tiles.TilesContainer;
25 import org.apache.tiles.access.TilesAccess;
26 import org.apache.tiles.factory.AbstractTilesContainerFactory;
27 import org.apache.tiles.request.ApplicationAccess;
28 import org.apache.tiles.request.ApplicationContext;
29
30 /**
31 * Default Tiles initialization delegate implementation under a servlet
32 * environment. It uses init parameters to create the
33 * {@link ApplicationContext} and the {@link TilesContainer}.
34 *
35 * @version $Rev: 1332134 $ $Date: 2012-04-30 19:23:19 +1000 (Mon, 30 Apr 2012) $
36 * @since 2.2.0
37 */
38 public abstract class AbstractTilesInitializer implements TilesInitializer {
39
40 /**
41 * Init parameter to define the key under which the container will be
42 * stored.
43 *
44 * @since 2.1.2
45 */
46 public static final String CONTAINER_KEY_INIT_PARAMETER =
47 "org.apache.tiles.startup.AbstractTilesInitializer.CONTAINER_KEY";
48
49 /**
50 * The initialized application context.
51 */
52 private ApplicationContext applicationContext;
53
54 /**
55 * The initialized container.
56 */
57 private TilesContainer container;
58
59 /** {@inheritDoc} */
60 public void initialize(ApplicationContext applicationContext) {
61 this.applicationContext = createTilesApplicationContext(applicationContext);
62 ApplicationAccess.register(applicationContext);
63 String key = getContainerKey(this.applicationContext);
64 container = createContainer(this.applicationContext);
65 TilesAccess.setContainer(this.applicationContext, container, key);
66 }
67
68 /** {@inheritDoc} */
69 public void destroy() {
70 TilesAccess.setContainer(applicationContext, null,
71 getContainerKey(applicationContext));
72 }
73
74 /**
75 * Creates the Tiles application context, to be used across all the
76 * Tiles-based application. If you override this class, please override this
77 * method or
78 * {@link #createAndInitializeTilesApplicationContextFactory(ApplicationContext)}
79 * .<br>
80 * This implementation returns the preliminary context passed as a parameter
81 *
82 * @param preliminaryContext The preliminary application context to use.
83 * @return The Tiles application context.
84 * @since 2.2.0
85 */
86 protected ApplicationContext createTilesApplicationContext(
87 ApplicationContext preliminaryContext) {
88 return preliminaryContext;
89 }
90
91 /**
92 * Returns the container key under which the container will be stored.
93 * This implementation returns <code>null</code> so that the container will
94 * be the default one.
95 *
96 * @param applicationContext The Tiles application context to use.
97 * @return The container key.
98 * @since 2.2.0
99 */
100 protected String getContainerKey(ApplicationContext applicationContext) {
101 return null;
102 }
103
104 /**
105 * Creates a Tiles container. If you override this class, please override
106 * this method or {@link #createContainerFactory(ApplicationContext)}.
107 *
108 * @param context The servlet context to use.
109 * @return The created container.
110 * @since 2.2.0
111 */
112 protected TilesContainer createContainer(ApplicationContext context) {
113 AbstractTilesContainerFactory factory = createContainerFactory(context);
114 return factory.createContainer(context);
115 }
116
117 /**
118 * Creates a Tiles container factory. If you override this class, please
119 * override this method or {@link #createContainer(ApplicationContext)}.
120 *
121 * @param context The servlet context to use.
122 * @return The created container factory.
123 * @since 2.2.0
124 */
125 protected abstract AbstractTilesContainerFactory createContainerFactory(
126 ApplicationContext context);
127 }