This project has retired. For details please refer to its Attic page.
BasicTilesContainerFactory xref
View Javadoc

1   /*
2    * $Id: BasicTilesContainerFactory.java 1310865 2012-04-07 21:01:22Z nlebas $
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  package org.apache.tiles.factory;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.apache.tiles.TilesContainer;
28  import org.apache.tiles.definition.DefinitionsFactory;
29  import org.apache.tiles.definition.DefinitionsReader;
30  import org.apache.tiles.definition.UnresolvingLocaleDefinitionsFactory;
31  import org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO;
32  import org.apache.tiles.definition.dao.DefinitionDAO;
33  import org.apache.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO;
34  import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
35  import org.apache.tiles.definition.pattern.BasicPatternDefinitionResolver;
36  import org.apache.tiles.definition.pattern.PatternDefinitionResolver;
37  import org.apache.tiles.definition.pattern.PatternDefinitionResolverAware;
38  import org.apache.tiles.definition.pattern.wildcard.WildcardDefinitionPatternMatcherFactory;
39  import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
40  import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory;
41  import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator;
42  import org.apache.tiles.impl.BasicTilesContainer;
43  import org.apache.tiles.locale.LocaleResolver;
44  import org.apache.tiles.locale.impl.DefaultLocaleResolver;
45  import org.apache.tiles.preparer.factory.BasicPreparerFactory;
46  import org.apache.tiles.preparer.factory.PreparerFactory;
47  import org.apache.tiles.renderer.DefinitionRenderer;
48  import org.apache.tiles.request.ApplicationContext;
49  import org.apache.tiles.request.ApplicationResource;
50  import org.apache.tiles.request.render.BasicRendererFactory;
51  import org.apache.tiles.request.render.ChainedDelegateRenderer;
52  import org.apache.tiles.request.render.DispatchRenderer;
53  import org.apache.tiles.request.render.Renderer;
54  import org.apache.tiles.request.render.RendererFactory;
55  import org.apache.tiles.request.render.StringRenderer;
56  
57  /**
58   * Factory that builds a standard Tiles container using only Java code.
59   *
60   * @version $Rev: 1310865 $ $Date: 2012-04-08 07:01:22 +1000 (Sun, 08 Apr 2012) $
61   * @since 2.1.0
62   */
63  public class BasicTilesContainerFactory extends AbstractTilesContainerFactory {
64  
65      /**
66       * The string renderer name.
67       */
68      protected static final String STRING_RENDERER_NAME = "string";
69  
70      /**
71       * The template renderer name.
72       */
73      protected static final String TEMPLATE_RENDERER_NAME = "template";
74  
75      /**
76       * The definition renderer name.
77       */
78      protected static final String DEFINITION_RENDERER_NAME = "definition";
79  
80      /** {@inheritDoc} */
81      @Override
82      public TilesContainer createContainer(ApplicationContext applicationContext) {
83          BasicTilesContainer container = instantiateContainer(applicationContext);
84          container.setApplicationContext(applicationContext);
85          LocaleResolver resolver = createLocaleResolver(applicationContext);
86          container.setDefinitionsFactory(createDefinitionsFactory(applicationContext,
87                  resolver));
88          AttributeEvaluatorFactory attributeEvaluatorFactory = createAttributeEvaluatorFactory(
89                  applicationContext, resolver);
90          container.setAttributeEvaluatorFactory(attributeEvaluatorFactory);
91          container.setPreparerFactory(createPreparerFactory(applicationContext));
92          TilesContainer injectedContainer = createDecoratedContainer(container, applicationContext);
93          container.setRendererFactory(createRendererFactory(applicationContext,
94                  injectedContainer, attributeEvaluatorFactory));
95          return injectedContainer;
96      }
97  
98      /**
99       * Instantiate the container, without initialization.
100      *
101      * @param context The Tiles application context object.
102      * @return The instantiated container.
103      * @since 2.1.1
104      */
105     protected BasicTilesContainer instantiateContainer(
106             ApplicationContext context) {
107         return new BasicTilesContainer();
108     }
109 
110     /**
111      * Instantiate the container that will be injected to child objects.
112      *
113      * @param originalContainer The original instantiated container.
114      * @param context The Tiles application context object.
115      * @return The instantiated container.
116      * @since 3.0.0
117      */
118     protected TilesContainer createDecoratedContainer(TilesContainer originalContainer,
119             ApplicationContext context) {
120         return originalContainer;
121     }
122 
123     /**
124      * Creates the definitions factory. By default it creates a
125      * {@link UnresolvingLocaleDefinitionsFactory} with default dependencies.
126      *
127      * @param applicationContext The Tiles application context.
128      * @param resolver The locale resolver.
129      * @return The definitions factory.
130      * @since 2.1.1
131      */
132     protected DefinitionsFactory createDefinitionsFactory(ApplicationContext applicationContext,
133             LocaleResolver resolver) {
134         UnresolvingLocaleDefinitionsFactory factory = instantiateDefinitionsFactory(
135                 applicationContext, resolver);
136         factory.setLocaleResolver(resolver);
137         factory.setDefinitionDAO(createLocaleDefinitionDao(applicationContext,
138                 resolver));
139         return factory;
140     }
141 
142     /**
143      * Instantiate a new definitions factory based on Locale.
144      * @param applicationContext The Tiles application context.
145      * @param resolver The locale resolver.
146      * @return The definitions factory.
147      * @since 2.2.1
148      */
149     protected UnresolvingLocaleDefinitionsFactory instantiateDefinitionsFactory(
150             ApplicationContext applicationContext,
151             LocaleResolver resolver) {
152         return new UnresolvingLocaleDefinitionsFactory();
153     }
154 
155 
156     /**
157      * Instantiate (and does not initialize) a Locale-based definition DAO.
158      * @param applicationContext The Tiles application context.
159      * @param resolver The locale resolver.
160      * @return The definition DAO.
161      * @since 2.1.1
162      */
163     protected BaseLocaleUrlDefinitionDAO instantiateLocaleDefinitionDao(ApplicationContext applicationContext,
164             LocaleResolver resolver) {
165         ResolvingLocaleUrlDefinitionDAO dao = new ResolvingLocaleUrlDefinitionDAO(applicationContext);
166         return dao;
167     }
168 
169     /**
170      * Creates a Locale-based definition DAO.
171      * @param applicationContext The Tiles application context.
172      * @param resolver The locale resolver.
173      * @return The definition DAO.
174      * @since 2.1.1
175      */
176     @SuppressWarnings("unchecked")
177     protected DefinitionDAO<Locale> createLocaleDefinitionDao(ApplicationContext applicationContext,
178             LocaleResolver resolver) {
179         BaseLocaleUrlDefinitionDAO definitionDao = instantiateLocaleDefinitionDao(
180                 applicationContext, resolver);
181         definitionDao.setReader(createDefinitionsReader(applicationContext));
182         definitionDao.setSources(getSources(applicationContext));
183         if (definitionDao instanceof PatternDefinitionResolverAware) {
184             ((PatternDefinitionResolverAware<Locale>) definitionDao)
185                     .setPatternDefinitionResolver(createPatternDefinitionResolver(Locale.class));
186         }
187         return definitionDao;
188     }
189 
190     /**
191      * Creates the locale resolver. By default it creates a
192      * {@link DefaultLocaleResolver}.
193      * @param applicationContext The Tiles application context.
194      * @return The locale resolver.
195      * @since 2.1.1
196      */
197     protected LocaleResolver createLocaleResolver(ApplicationContext applicationContext) {
198         return new DefaultLocaleResolver();
199     }
200 
201     /**
202      * Creates the definitions reader. By default it creates a
203      * {@link DigesterDefinitionsReader}.
204      * @param applicationContext The Tiles application context.
205      * @return The definitions reader.
206      * @since 2.1.1
207      */
208     protected DefinitionsReader createDefinitionsReader(
209             ApplicationContext applicationContext) {
210         return new DigesterDefinitionsReader();
211     }
212 
213     /**
214      * Returns a list containing the resources to be parsed. By default, it returns a
215      * list containing the resource at "/WEB-INF/tiles.xml".
216      * @param applicationContext The Tiles application context.
217      * @return The resources.
218      * @since 2.1.1
219      */
220     protected List<ApplicationResource> getSources(ApplicationContext applicationContext) {
221         List<ApplicationResource> retValue = new ArrayList<ApplicationResource>(1);
222         retValue.add(applicationContext.getResource("/WEB-INF/tiles.xml"));
223         return retValue;
224     }
225 
226     /**
227      * Creates the attribute evaluator factory to use. By default it returns a
228      * {@link BasicAttributeEvaluatorFactory} containing the
229      * {@link DirectAttributeEvaluator} as the default evaluator.
230      *
231      * @param applicationContext The Tiles application context.
232      * @param resolver The locale resolver.
233      * @return The evaluator factory.
234      * @since 2.2.0
235      */
236     protected AttributeEvaluatorFactory createAttributeEvaluatorFactory(
237             ApplicationContext applicationContext,
238             LocaleResolver resolver) {
239         return new BasicAttributeEvaluatorFactory(new DirectAttributeEvaluator());
240     }
241 
242     /**
243      * Creates the preparer factory to use. By default it returns a
244      * {@link BasicPreparerFactory}.
245      * @param applicationContext The Tiles application context.
246      * @return The preparer factory.
247      * @since 2.1.1
248      */
249     protected PreparerFactory createPreparerFactory(ApplicationContext applicationContext) {
250         return new BasicPreparerFactory();
251     }
252 
253     /**
254      * Creates a renderer factory. By default it returns a
255      * {@link BasicRendererFactory}, composed of an
256      * {@link UntypedAttributeRenderer} as default, and delegates of
257      * {@link StringRenderer}, {@link DispatchRenderer},
258      * {@link DefinitionRenderer}.
259      *
260      * @param applicationContext The Tiles application context.
261      * @param container The container.
262      * @param attributeEvaluatorFactory The attribute evaluator factory.
263      * @return The renderer factory.
264      * @since 2.2.0
265      */
266     protected RendererFactory createRendererFactory(ApplicationContext applicationContext,
267             TilesContainer container,
268             AttributeEvaluatorFactory attributeEvaluatorFactory) {
269         BasicRendererFactory retValue = new BasicRendererFactory();
270         registerAttributeRenderers(retValue, applicationContext, container,
271                 attributeEvaluatorFactory);
272         retValue.setDefaultRenderer(createDefaultAttributeRenderer(retValue,
273                 applicationContext, container, attributeEvaluatorFactory));
274         return retValue;
275     }
276 
277     /**
278      * Creates the default attribute renderer. By default it is an
279      * {@link ChainedDelegateRenderer}.
280      *
281      * @param rendererFactory The renderer factory to configure.
282      * @param applicationContext The Tiles application context.
283      * @param container The container.
284      * @param attributeEvaluatorFactory The attribute evaluator factory.
285      * @return The default attribute renderer.
286      * @since 3.0.0
287      */
288     protected Renderer createDefaultAttributeRenderer(
289             BasicRendererFactory rendererFactory,
290             ApplicationContext applicationContext,
291             TilesContainer container,
292             AttributeEvaluatorFactory attributeEvaluatorFactory) {
293         ChainedDelegateRenderer retValue = new ChainedDelegateRenderer();
294         retValue.addAttributeRenderer(rendererFactory.getRenderer(DEFINITION_RENDERER_NAME));
295         retValue.addAttributeRenderer(rendererFactory.getRenderer(TEMPLATE_RENDERER_NAME));
296         retValue.addAttributeRenderer(rendererFactory.getRenderer(STRING_RENDERER_NAME));
297         return retValue;
298     }
299 
300     /**
301      * Creates a new pattern definition resolver. By default, it instantiate a
302      * {@link BasicPatternDefinitionResolver} with
303      * {@link WildcardDefinitionPatternMatcherFactory} to manage wildcard
304      * substitution.
305      *
306      * @param <T> The type of the customization key.
307      * @param customizationKeyClass The customization key class.
308      * @return The pattern definition resolver.
309      * @since 2.2.0
310      */
311     protected <T> PatternDefinitionResolver<T> createPatternDefinitionResolver(
312             Class<T> customizationKeyClass) {
313         WildcardDefinitionPatternMatcherFactory definitionPatternMatcherFactory =
314             new WildcardDefinitionPatternMatcherFactory();
315         return new BasicPatternDefinitionResolver<T>(
316                 definitionPatternMatcherFactory,
317                 definitionPatternMatcherFactory);
318     }
319 
320     /**
321      * Registers attribute renderers in a {@link BasicRendererFactory}. By
322      * default, it registers delegates to {@link StringRenderer},
323      * {@link DispatchRenderer} and {@link DefinitionRenderer}.
324      *
325      * @param rendererFactory The renderer factory to configure.
326      * @param applicationContext The Tiles application context.
327      * @param container The container.
328      * @param attributeEvaluatorFactory The attribute evaluator factory.
329      * @since 2.2.0
330      */
331     protected void registerAttributeRenderers(
332             BasicRendererFactory rendererFactory,
333             ApplicationContext applicationContext,
334             TilesContainer container,
335             AttributeEvaluatorFactory attributeEvaluatorFactory) {
336         rendererFactory.registerRenderer(STRING_RENDERER_NAME,
337                 createStringAttributeRenderer(rendererFactory,
338                         applicationContext, container, attributeEvaluatorFactory));
339         rendererFactory.registerRenderer(TEMPLATE_RENDERER_NAME,
340                 createTemplateAttributeRenderer(rendererFactory,
341                         applicationContext, container, attributeEvaluatorFactory));
342         rendererFactory.registerRenderer(DEFINITION_RENDERER_NAME,
343                 createDefinitionAttributeRenderer(rendererFactory,
344                         applicationContext, container, attributeEvaluatorFactory));
345     }
346 
347     /**
348      * Creates an attribute renderer to render strings.
349      *
350      * @param rendererFactory The renderer factory to configure.
351      * @param applicationContext The Tiles application context.
352      * @param container The container.
353      * @param attributeEvaluatorFactory The attribute evaluator factory.
354      * @return The renderer.
355      * @since 3.0.0
356      */
357     protected Renderer createStringAttributeRenderer(
358             BasicRendererFactory rendererFactory,
359             ApplicationContext applicationContext,
360             TilesContainer container,
361             AttributeEvaluatorFactory attributeEvaluatorFactory) {
362         return new StringRenderer();
363     }
364 
365     /**
366      * Creates a {@link AttributeRenderer} that uses a {@link DispatchRenderer}.
367      *
368      * @param rendererFactory The renderer factory to configure.
369      * @param applicationContext The Tiles application context.
370      * @param container The container.
371      * @param attributeEvaluatorFactory The attribute evaluator factory.
372      * @return The renderer.
373      * @since 2.2.1
374      */
375     protected Renderer createTemplateAttributeRenderer(
376             BasicRendererFactory rendererFactory,
377             ApplicationContext applicationContext,
378             TilesContainer container,
379             AttributeEvaluatorFactory attributeEvaluatorFactory) {
380         return new DispatchRenderer();
381     }
382 
383     /**
384      * Creates a {@link AttributeRenderer} using a {@link DefinitionRenderer}.
385      *
386      * @param rendererFactory The renderer factory to configure.
387      * @param applicationContext The Tiles application context.
388      * @param container The container.
389      * @param attributeEvaluatorFactory The attribute evaluator factory.
390      * @return The renderer.
391      * @since 3.0.0
392      */
393     protected Renderer createDefinitionAttributeRenderer(
394             BasicRendererFactory rendererFactory,
395             ApplicationContext applicationContext,
396             TilesContainer container,
397             AttributeEvaluatorFactory attributeEvaluatorFactory) {
398         return new DefinitionRenderer(container);
399     }
400 }