1/*2 * $Id: TilesAccess.java 1044659 2010-12-11 14:16:04Z 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 */21package org.apache.tiles.access;
2223import java.util.Map;
2425import org.apache.tiles.NoSuchContainerException;
26import org.apache.tiles.TilesContainer;
27import org.apache.tiles.request.ApplicationContext;
28import org.apache.tiles.request.Request;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
313233/**34 * Provides static access to the tiles container.35 *36 * @version $Rev: 1044659 $ $Date: 2010-12-12 01:16:04 +1100 (Sun, 12 Dec 2010) $37 */38publicfinalclassTilesAccess {
3940/**41 * Name of the attribute used to store the current used container.42 */43publicstaticfinal String CURRENT_CONTAINER_ATTRIBUTE_NAME =
44"org.apache.tiles.servlet.context.ServletTilesRequestContext.CURRENT_CONTAINER_KEY";
4546/**47 * Constructor, private to avoid instantiation.48 */49privateTilesAccess() {
50 }
5152/**53 * The name of the attribute to use when getting and setting the container54 * object in a context.55 */56publicstaticfinal String CONTAINER_ATTRIBUTE =
57"org.apache.tiles.CONTAINER";
5859/**60 * Configures the default container to be used in the application.61 *62 * @param context The Tiles application context object to use.63 * @param container The container object to set.64 * @since 2.1.265 */66publicstaticvoid setContainer(ApplicationContext context,
67TilesContainer container) {
68 setContainer(context, container, CONTAINER_ATTRIBUTE);
69 }
7071/**72 * Configures the container to be used in the application.73 *74 * @param context The Tiles application context object to use.75 * @param container The container object to set.76 * @param key The key under which the container will be stored.77 * @since 2.1.278 */79publicstaticvoid setContainer(ApplicationContext context,
80TilesContainer container, String key) {
81 Logger log = LoggerFactory.getLogger(TilesAccess.class);
82if (key == null) {
83 key = CONTAINER_ATTRIBUTE;
84 }
8586if (container == null) {
87if (log.isInfoEnabled()) {
88 log.info("Removing TilesContext for context: " + context.getClass().getName());
89 }
90 context.getApplicationScope().remove(key);
91 } else {
92if (log.isInfoEnabled()) {
93 log.info("Publishing TilesContext for context: " + context.getClass().getName());
94 }
95 context.getApplicationScope().put(key, container);
96 }
97 }
9899/**100 * Returns default the container to be used in the application.101 *102 * @param context The Tiles application context object to use.103 * @return The default container object.104 * @since 3.0.0105 */106publicstaticTilesContainer getContainer(ApplicationContext context) {
107return getContainer(context, CONTAINER_ATTRIBUTE);
108 }
109110/**111 * Returns the container to be used in the application registered under a specific key.112 *113 * @param context The Tiles application context object to use.114 * @param key The key under which the container will be stored.115 * @return The container object.116 * @since 3.0.0117 */118publicstaticTilesContainer getContainer(ApplicationContext context,
119 String key) {
120if (key == null) {
121 key = CONTAINER_ATTRIBUTE;
122 }
123124return (TilesContainer) context.getApplicationScope().get(key);
125 }
126127/**128 * Sets the current container to use in web pages.129 *130 * @param request The request to use.131 * @param key The key under which the container is stored.132 * @since 2.1.0133 */134publicstaticvoid setCurrentContainer(Request request,
135 String key) {
136 ApplicationContext applicationContext = request.getApplicationContext();
137TilesContainer container = getContainer(applicationContext, key);
138if (container != null) {
139 request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
140 } else {
141thrownewNoSuchContainerException("The container with the key '"142 + key + "' cannot be found");
143 }
144 }
145146/**147 * Sets the current container to use in web pages.148 *149 * @param request The request to use.150 * @param container The container to use as the current container.151 * @since 2.1.0152 */153publicstaticvoid setCurrentContainer(Request request,
154TilesContainer container) {
155if (container != null) {
156 request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
157 } else {
158thrownew NullPointerException("The container cannot be null");
159 }
160 }
161162/**163 * Returns the current container that has been set, or the default one.164 *165 * @param request The request to use.166 * @return The current Tiles container to use in web pages.167 * @since 2.1.0168 */169publicstaticTilesContainer getCurrentContainer(Request request) {
170 ApplicationContext context = request.getApplicationContext();
171 Map<String, Object> requestScope = request.getContext("request");
172TilesContainer container = (TilesContainer) requestScope.get(CURRENT_CONTAINER_ATTRIBUTE_NAME);
173if (container == null) {
174 container = getContainer(context);
175 requestScope.put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
176 }
177178return container;
179 }
180 }