View Javadoc

1   /*
2    * $Id: KeyedDefinitionsFactoryTilesContainer.java 537196 2007-05-11 14:07:35Z apetrelli $
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.impl;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.tiles.Definition;
28  import org.apache.tiles.TilesException;
29  import org.apache.tiles.context.TilesRequestContext;
30  import org.apache.tiles.definition.DefinitionsFactory;
31  import org.apache.tiles.definition.DefinitionsFactoryException;
32  
33  /***
34   * Container that can be used to store multiple {@link DefinitionsFactory}
35   * instances mapped to different keys.
36   *
37   * @version $Rev: 537196 $ $Date: 2007-05-11 16:07:35 +0200 (Fri, 11 May 2007) $
38   */
39  public class KeyedDefinitionsFactoryTilesContainer extends BasicTilesContainer {
40  
41      /***
42       * Constant representing the prefix of the configuration parameter used to
43       * define the tiles definition resources for a specific key.
44       */
45      public static final String DEFINITIONS_CONFIG_PREFIX =
46          "org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG@";
47  
48      /***
49       * Maps definition factories to their keys.
50       */
51      protected Map<String, DefinitionsFactory> key2definitionsFactory;
52  
53      /***
54       * The key extractor object.
55       */
56      protected KeyExtractor keyExtractor;
57  
58      /***
59       * Constructor.
60       */
61      public KeyedDefinitionsFactoryTilesContainer() {
62          key2definitionsFactory = new HashMap<String, DefinitionsFactory>();
63      }
64  
65      /***
66       * It represents an object able to return a key from a request. Each key
67       * maps a different {@link DefinitionsFactory}.
68       */
69      public static interface KeyExtractor {
70  
71          /***
72           * Returns the definitions factory key.
73           *
74           * @param request The request object.
75           * @return The needed factory key.
76           */
77          String getDefinitionsFactoryKey(TilesRequestContext request);
78      }
79  
80      /***
81       * This is the default factory key. Takes the key from the request-scoped
82       * attribute <code>DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME</code>.
83       */
84      public static class DefaultKeyExtractor implements KeyExtractor {
85  
86          /***
87           * Name of the attribute inside the request that will be used to get the
88           * key of the definitions factory to be used.
89           */
90          public static final String DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME =
91              "org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DefaultKeyExtractor.KEY";
92  
93          /***
94           * Returns the definitions factory key.
95           *
96           * @param request The request object.
97           * @return The needed factory key.
98           */
99          public String getDefinitionsFactoryKey(TilesRequestContext request) {
100             String retValue = null;
101             Map<String, Object> requestScope = request.getRequestScope();
102             if (requestScope != null) { // Probably the request scope does not exist
103                 retValue = (String) requestScope.get(
104                         DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME);
105             }
106 
107             return retValue;
108         }
109     }
110 
111     /***
112      * Returns a definition factory given its key.
113      *
114      * @return the definitions factory used by this container. If the key is not
115      * valid, the default factory will be returned.
116      * @param key The key of the needed definitions factory.
117      */
118     public DefinitionsFactory getDefinitionsFactory(String key) {
119         DefinitionsFactory retValue = null;
120 
121         if (key != null) {
122             retValue = key2definitionsFactory.get(key);
123         }
124         if (retValue == null) {
125             retValue = getDefinitionsFactory();
126         }
127 
128         return retValue;
129     }
130 
131     /***
132      * Returns the proper definition factory for the given key, i.e. if the key
133      * is not present, <code>null</code> will be returned.
134      *
135      * @return the definitions factory used by this container. If the key is not
136      * valid, <code>null</code> will be returned.
137      * @param key The key of the needed definitions factory.
138      */
139     public DefinitionsFactory getProperDefinitionsFactory(String key) {
140         DefinitionsFactory retValue = null;
141 
142         if (key != null) {
143             retValue = key2definitionsFactory.get(key);
144         }
145 
146         return retValue;
147     }
148 
149     /***
150      * Set the definitions factory. This method first ensures
151      * that the container has not yet been initialized.
152      *
153      * @param key The key under which the definitions factory is catalogued.
154      * @param definitionsFactory the definitions factory for this instance.
155      * @param initParameters The init parameters to configure the definitions
156      * factory.
157      * @throws TilesException If something goes wrong during initialization of
158      * the definitions factory.
159      */
160     public void setDefinitionsFactory(String key,
161             DefinitionsFactory definitionsFactory,
162             Map<String, String> initParameters) throws TilesException {
163         if (key != null) {
164             key2definitionsFactory.put(key, definitionsFactory);
165             initializeDefinitionsFactory(definitionsFactory,
166                     getResourceString(initParameters), initParameters);
167         } else {
168             setDefinitionsFactory(definitionsFactory);
169         }
170     }
171 
172     /***
173      * Sets the key extractor to use.
174      *
175      * @param keyExtractor The key extractor.
176      */
177     public void setKeyExtractor(KeyExtractor keyExtractor) {
178         this.keyExtractor = keyExtractor;
179     }
180 
181     /*** {@inheritDoc} */
182     @Override
183     protected Definition getDefinition(String definitionName,
184             TilesRequestContext request) throws DefinitionsFactoryException {
185         Definition retValue = null;
186         String key = getDefinitionsFactoryKey(request);
187         if (key != null) {
188             DefinitionsFactory definitionsFactory =
189                 key2definitionsFactory.get(key);
190             if (definitionsFactory != null) {
191                 retValue = definitionsFactory.getDefinition(definitionName,
192                         request);
193             }
194         }
195         if (retValue == null) {
196             retValue = super.getDefinition(definitionName, request);
197         }
198         return retValue;
199     }
200 
201     /***
202      * Returns the definitions factory key.
203      *
204      * @param request The request object.
205      * @return The needed factory key.
206      */
207     protected String getDefinitionsFactoryKey(TilesRequestContext request) {
208         if (keyExtractor == null) {
209             keyExtractor = new DefaultKeyExtractor();
210         }
211         return keyExtractor.getDefinitionsFactoryKey(request);
212     }
213 }