1/*2 * $Id: AbstractTilesInitializer.java 797540 2009-07-24 15:42:00Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.startup;
2324import org.apache.tiles.TilesApplicationContext;
25import org.apache.tiles.TilesContainer;
26import org.apache.tiles.TilesException;
27import org.apache.tiles.access.TilesAccess;
28import org.apache.tiles.factory.AbstractTilesContainerFactory;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
3132/***33 * Default Tiles initialization delegate implementation under a servlet34 * environment. It uses init parameters to create the35 * {@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.039 */40publicabstractclassAbstractTilesInitializer implements TilesInitializer {
4142/***43 * The logging object.44 */45private Logger log = LoggerFactory.getLogger(getClass());
4647/***48 * Init parameter to define the key under which the container will be49 * stored.50 *51 * @since 2.1.252 */53publicstaticfinal String CONTAINER_KEY_INIT_PARAMETER =
54"org.apache.tiles.startup.AbstractTilesInitializer.CONTAINER_KEY";
5556/***57 * The initialized application context.58 */59private TilesApplicationContext applicationContext;
6061/***62 * The initialized container.63 */64private TilesContainer container;
6566/*** {@inheritDoc} */67publicvoid initialize(TilesApplicationContext applicationContext) {
68this.applicationContext = createTilesApplicationContext(applicationContext);
69 String key = getContainerKey(this.applicationContext);
70 container = createContainer(this.applicationContext);
71 TilesAccess.setContainer(this.applicationContext, container, key);
72 }
7374/*** {@inheritDoc} */75publicvoid destroy() {
76try {
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 }
8384/***85 * Creates the Tiles application context, to be used across all the86 * Tiles-based application. If you override this class, please override this87 * method or88 * {@link #createAndInitializeTilesApplicationContextFactory(TilesApplicationContext)}89 * .<br>90 * This implementation returns the preliminary context passed as a parameter91 *92 * @param preliminaryContext The preliminary application context to use.93 * @return The Tiles application context.94 * @since 2.2.095 */96protected TilesApplicationContext createTilesApplicationContext(
97 TilesApplicationContext preliminaryContext) {
98return preliminaryContext;
99 }
100101/***102 * Returns the container key under which the container will be stored.103 * This implementation returns <code>null</code> so that the container will104 * be the default one.105 *106 * @param applicationContext The Tiles application context to use.107 * @return The container key.108 * @since 2.2.0109 */110protected String getContainerKey(TilesApplicationContext applicationContext) {
111returnnull;
112 }
113114/***115 * Creates a Tiles container. If you override this class, please override116 * this method or {@link #createContainerFactory(TilesApplicationContext)}.117 *118 * @param context The servlet context to use.119 * @return The created container.120 * @since 2.2.0121 */122protected TilesContainer createContainer(TilesApplicationContext context) {
123AbstractTilesContainerFactory factory = createContainerFactory(context);
124return factory.createContainer(context);
125 }
126127/***128 * Creates a Tiles container factory. If you override this class, please129 * 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.0134 */135protectedabstractAbstractTilesContainerFactory createContainerFactory(
136 TilesApplicationContext context);
137 }