1/*2 * $Id: KeyedDefinitionsFactoryTilesContainer.java 797754 2009-07-25 11:42:03Z 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.impl;
2324import java.util.HashMap;
25import java.util.Map;
2627import org.apache.tiles.Definition;
28import org.apache.tiles.context.TilesRequestContext;
29import org.apache.tiles.definition.DefinitionsFactory;
3031/***32 * Container that can be used to store multiple {@link DefinitionsFactory}33 * instances mapped to different keys.34 *35 * @version $Rev: 797754 $ $Date: 2009-07-25 13:42:03 +0200 (sab, 25 lug 2009) $36 * @deprecated Register different containers using37 * {@link org.apache.tiles.access.TilesAccess#setContainer(38 * org.apache.tiles.TilesApplicationContext, org.apache.tiles.TilesContainer, String)}39 */40publicclassKeyedDefinitionsFactoryTilesContainerextendsBasicTilesContainer {
4142/***43 * Constant representing the prefix of the configuration parameter used to44 * define the tiles definition resources for a specific key.45 */46publicstaticfinal String DEFINITIONS_CONFIG_PREFIX =
47"org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG@";
4849/***50 * Maps definition factories to their keys.51 */52protected Map<String, DefinitionsFactory> key2definitionsFactory;
5354/***55 * The key extractor object.56 */57protected KeyExtractor keyExtractor;
5859/***60 * Constructor.61 */62publicKeyedDefinitionsFactoryTilesContainer() {
63 key2definitionsFactory = new HashMap<String, DefinitionsFactory>();
64 }
6566/***67 * It represents an object able to return a key from a request. Each key68 * maps a different {@link DefinitionsFactory}.69 */70publicstaticinterface KeyExtractor {
7172/***73 * Returns the definitions factory key.74 *75 * @param request The request object.76 * @return The needed factory key.77 */78 String getDefinitionsFactoryKey(TilesRequestContext request);
79 }
8081/***82 * This is the default factory key. Takes the key from the request-scoped83 * attribute <code>DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME</code>.84 */85publicstaticclass DefaultKeyExtractor implements KeyExtractor {
8687/***88 * Name of the attribute inside the request that will be used to get the89 * key of the definitions factory to be used.90 */91publicstaticfinal String DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME =
92"org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DefaultKeyExtractor.KEY";
9394/***95 * Returns the definitions factory key.96 *97 * @param request The request object.98 * @return The needed factory key.99 */100public String getDefinitionsFactoryKey(TilesRequestContext request) {
101 String retValue = null;
102 Map<String, Object> requestScope = request.getRequestScope();
103if (requestScope != null) { // Probably the request scope does not exist104 retValue = (String) requestScope.get(
105 DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME);
106 }
107108return retValue;
109 }
110 }
111112/***113 * Returns a definition factory given its key.114 *115 * @return the definitions factory used by this container. If the key is not116 * valid, the default factory will be returned.117 * @param key The key of the needed definitions factory.118 */119publicDefinitionsFactory getDefinitionsFactory(String key) {
120DefinitionsFactory retValue = null;
121122if (key != null) {
123 retValue = key2definitionsFactory.get(key);
124 }
125if (retValue == null) {
126 retValue = getDefinitionsFactory();
127 }
128129return retValue;
130 }
131132/***133 * Returns the proper definition factory for the given key, i.e. if the key134 * is not present, <code>null</code> will be returned.135 *136 * @return the definitions factory used by this container. If the key is not137 * valid, <code>null</code> will be returned.138 * @param key The key of the needed definitions factory.139 */140publicDefinitionsFactory getProperDefinitionsFactory(String key) {
141DefinitionsFactory retValue = null;
142143if (key != null) {
144 retValue = key2definitionsFactory.get(key);
145 }
146147return retValue;
148 }
149150/***151 * Set the definitions factory. This method first ensures that the container152 * has not yet been initialized.153 *154 * @param key The key under which the definitions factory is catalogued.155 * @param definitionsFactory the definitions factory for this instance.156 * @param initParameters The init parameters to configure the definitions157 * factory.158 * @deprecated Use {@link #setDefinitionsFactory(String, DefinitionsFactory)}.159 */160 @Deprecated
161publicvoid setDefinitionsFactory(String key,
162DefinitionsFactory definitionsFactory,
163 Map<String, String> initParameters) {
164 setDefinitionsFactory(key, definitionsFactory);
165if (key != null) {
166 initializeDefinitionsFactory(definitionsFactory,
167 getResourceString(initParameters), initParameters);
168 }
169 }
170171/***172 * Set the definitions factory. This method first ensures that the container173 * has not yet been initialized.174 *175 * @param key The key under which the definitions factory is catalogued.176 * @param definitionsFactory the definitions factory for this instance.177 * @since 2.1.0178 */179publicvoid setDefinitionsFactory(String key,
180DefinitionsFactory definitionsFactory) {
181if (key != null) {
182 key2definitionsFactory.put(key, definitionsFactory);
183 } else {
184 setDefinitionsFactory(definitionsFactory);
185 }
186 }
187188/***189 * Sets the key extractor to use.190 *191 * @param keyExtractor The key extractor.192 */193publicvoid setKeyExtractor(KeyExtractor keyExtractor) {
194this.keyExtractor = keyExtractor;
195 }
196197/*** {@inheritDoc} */198 @Override
199protected Definition getDefinition(String definitionName,
200TilesRequestContext request) {
201 Definition retValue = null;
202 String key = getDefinitionsFactoryKey(request);
203if (key != null) {
204DefinitionsFactory definitionsFactory =
205 key2definitionsFactory.get(key);
206if (definitionsFactory != null) {
207 retValue = definitionsFactory.getDefinition(definitionName,
208 request);
209 }
210 }
211if (retValue == null) {
212 retValue = super.getDefinition(definitionName, request);
213 }
214return retValue;
215 }
216217/***218 * Returns the definitions factory key.219 *220 * @param request The request object.221 * @return The needed factory key.222 */223protected String getDefinitionsFactoryKey(TilesRequestContext request) {
224if (keyExtractor == null) {
225 keyExtractor = new DefaultKeyExtractor();
226 }
227return keyExtractor.getDefinitionsFactoryKey(request);
228 }
229 }