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.impl;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.tiles.Definition;
28 import org.apache.tiles.context.TilesRequestContext;
29 import org.apache.tiles.definition.DefinitionsFactory;
30
31 /***
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 using
37 * {@link org.apache.tiles.access.TilesAccess#setContainer(
38 * org.apache.tiles.TilesApplicationContext, org.apache.tiles.TilesContainer, String)}
39 */
40 public class KeyedDefinitionsFactoryTilesContainer extends BasicTilesContainer {
41
42 /***
43 * Constant representing the prefix of the configuration parameter used to
44 * define the tiles definition resources for a specific key.
45 */
46 public static final String DEFINITIONS_CONFIG_PREFIX =
47 "org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG@";
48
49 /***
50 * Maps definition factories to their keys.
51 */
52 protected Map<String, DefinitionsFactory> key2definitionsFactory;
53
54 /***
55 * The key extractor object.
56 */
57 protected KeyExtractor keyExtractor;
58
59 /***
60 * Constructor.
61 */
62 public KeyedDefinitionsFactoryTilesContainer() {
63 key2definitionsFactory = new HashMap<String, DefinitionsFactory>();
64 }
65
66 /***
67 * It represents an object able to return a key from a request. Each key
68 * maps a different {@link DefinitionsFactory}.
69 */
70 public static interface KeyExtractor {
71
72 /***
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 }
80
81 /***
82 * This is the default factory key. Takes the key from the request-scoped
83 * attribute <code>DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME</code>.
84 */
85 public static class DefaultKeyExtractor implements KeyExtractor {
86
87 /***
88 * Name of the attribute inside the request that will be used to get the
89 * key of the definitions factory to be used.
90 */
91 public static final String DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME =
92 "org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DefaultKeyExtractor.KEY";
93
94 /***
95 * Returns the definitions factory key.
96 *
97 * @param request The request object.
98 * @return The needed factory key.
99 */
100 public String getDefinitionsFactoryKey(TilesRequestContext request) {
101 String retValue = null;
102 Map<String, Object> requestScope = request.getRequestScope();
103 if (requestScope != null) {
104 retValue = (String) requestScope.get(
105 DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME);
106 }
107
108 return retValue;
109 }
110 }
111
112 /***
113 * Returns a definition factory given its key.
114 *
115 * @return the definitions factory used by this container. If the key is not
116 * valid, the default factory will be returned.
117 * @param key The key of the needed definitions factory.
118 */
119 public DefinitionsFactory getDefinitionsFactory(String key) {
120 DefinitionsFactory retValue = null;
121
122 if (key != null) {
123 retValue = key2definitionsFactory.get(key);
124 }
125 if (retValue == null) {
126 retValue = getDefinitionsFactory();
127 }
128
129 return retValue;
130 }
131
132 /***
133 * Returns the proper definition factory for the given key, i.e. if the key
134 * is not present, <code>null</code> will be returned.
135 *
136 * @return the definitions factory used by this container. If the key is not
137 * valid, <code>null</code> will be returned.
138 * @param key The key of the needed definitions factory.
139 */
140 public DefinitionsFactory getProperDefinitionsFactory(String key) {
141 DefinitionsFactory retValue = null;
142
143 if (key != null) {
144 retValue = key2definitionsFactory.get(key);
145 }
146
147 return retValue;
148 }
149
150 /***
151 * Set the definitions factory. This method first ensures that the container
152 * 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 definitions
157 * factory.
158 * @deprecated Use {@link #setDefinitionsFactory(String, DefinitionsFactory)}.
159 */
160 @Deprecated
161 public void setDefinitionsFactory(String key,
162 DefinitionsFactory definitionsFactory,
163 Map<String, String> initParameters) {
164 setDefinitionsFactory(key, definitionsFactory);
165 if (key != null) {
166 initializeDefinitionsFactory(definitionsFactory,
167 getResourceString(initParameters), initParameters);
168 }
169 }
170
171 /***
172 * Set the definitions factory. This method first ensures that the container
173 * 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.0
178 */
179 public void setDefinitionsFactory(String key,
180 DefinitionsFactory definitionsFactory) {
181 if (key != null) {
182 key2definitionsFactory.put(key, definitionsFactory);
183 } else {
184 setDefinitionsFactory(definitionsFactory);
185 }
186 }
187
188 /***
189 * Sets the key extractor to use.
190 *
191 * @param keyExtractor The key extractor.
192 */
193 public void setKeyExtractor(KeyExtractor keyExtractor) {
194 this.keyExtractor = keyExtractor;
195 }
196
197 /*** {@inheritDoc} */
198 @Override
199 protected Definition getDefinition(String definitionName,
200 TilesRequestContext request) {
201 Definition retValue = null;
202 String key = getDefinitionsFactoryKey(request);
203 if (key != null) {
204 DefinitionsFactory definitionsFactory =
205 key2definitionsFactory.get(key);
206 if (definitionsFactory != null) {
207 retValue = definitionsFactory.getDefinition(definitionName,
208 request);
209 }
210 }
211 if (retValue == null) {
212 retValue = super.getDefinition(definitionName, request);
213 }
214 return retValue;
215 }
216
217 /***
218 * Returns the definitions factory key.
219 *
220 * @param request The request object.
221 * @return The needed factory key.
222 */
223 protected String getDefinitionsFactoryKey(TilesRequestContext request) {
224 if (keyExtractor == null) {
225 keyExtractor = new DefaultKeyExtractor();
226 }
227 return keyExtractor.getDefinitionsFactoryKey(request);
228 }
229 }