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

1   /*
2    * $Id: LocaleDefinitionsFactory.java 822631 2009-10-07 09:21:12Z 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.definition;
23  
24  import java.util.Locale;
25  import java.util.Map;
26  
27  import org.apache.tiles.Definition;
28  import org.apache.tiles.Initializable;
29  import org.apache.tiles.awareness.TilesApplicationContextAware;
30  import org.apache.tiles.context.TilesRequestContext;
31  import org.apache.tiles.definition.dao.CachingLocaleUrlDefinitionDAO;
32  import org.apache.tiles.definition.dao.DefinitionDAO;
33  import org.apache.tiles.locale.LocaleResolver;
34  import org.apache.tiles.locale.impl.DefaultLocaleResolver;
35  import org.apache.tiles.reflect.ClassUtil;
36  
37  /***
38   * {@link DefinitionsFactory DefinitionsFactory} implementation that manages
39   * Definitions configuration data from URLs, but resolving definition
40   * inheritance when a definition is returned.. <p/>
41   * <p>
42   * The Definition objects are read from the
43   * {@link org.apache.tiles.definition.digester.DigesterDefinitionsReader DigesterDefinitionsReader}
44   * class unless another implementation is specified.
45   * </p>
46   *
47   * @version $Rev: 822631 $ $Date: 2009-10-07 11:21:12 +0200 (mer, 07 ott 2009) $
48   * @since 2.1.0
49   */
50  @SuppressWarnings("deprecation")
51  public class LocaleDefinitionsFactory extends
52          UnresolvingLocaleDefinitionsFactory implements Initializable {
53  
54      /***
55       * Initializes the DefinitionsFactory and its subcomponents.
56       * <p/>
57       * Implementations may support configuration properties to be passed in via
58       * the params Map.
59       *
60       * @param params The Map of configuration properties.
61       * @throws DefinitionsFactoryException if an initialization error occurs.
62       */
63      @SuppressWarnings("unchecked")
64      public void init(Map<String, String> params) {
65          String definitionDaoClassName = params
66                  .get(DefinitionsFactory.DEFINITION_DAO_INIT_PARAM);
67          if (definitionDaoClassName != null) {
68              definitionDao = (DefinitionDAO<Locale>) ClassUtil
69                      .instantiate(definitionDaoClassName);
70          } else {
71              definitionDao = createDefaultDefinitionDAO();
72          }
73          if (definitionDao instanceof TilesApplicationContextAware) {
74              ((TilesApplicationContextAware) definitionDao)
75                      .setApplicationContext(applicationContext);
76          }
77          if (definitionDao instanceof Initializable) {
78              ((Initializable) definitionDao).init(params);
79          }
80  
81          String resolverClassName = params
82                  .get(DefinitionsFactory.LOCALE_RESOLVER_IMPL_PROPERTY);
83          if (resolverClassName != null) {
84              localeResolver = (LocaleResolver) ClassUtil.instantiate(resolverClassName);
85          } else {
86              localeResolver = createDefaultLocaleResolver();
87          }
88          localeResolver.init(params);
89      }
90  
91      /*** {@inheritDoc} */
92      public Definition getDefinition(String name,
93              TilesRequestContext tilesContext) {
94          Definition retValue;
95          Locale locale = null;
96  
97          if (tilesContext != null) {
98              locale = localeResolver.resolveLocale(tilesContext);
99          }
100 
101         retValue = definitionDao.getDefinition(name, locale);
102         if (retValue != null) {
103             retValue = new Definition(retValue);
104             String parentDefinitionName = retValue.getExtends();
105             while (parentDefinitionName != null) {
106                 Definition parent = definitionDao.getDefinition(
107                         parentDefinitionName, locale);
108                 if (parent == null) {
109                     throw new NoSuchDefinitionException("Cannot find definition '"
110                             + parentDefinitionName + "' ancestor of '"
111                             + retValue.getName() + "'");
112                 }
113                 retValue.inherit(parent);
114                 parentDefinitionName = parent.getExtends();
115             }
116         }
117 
118         return retValue;
119     }
120 
121     /***
122      * Creates the default locale resolver, if it has not been specified
123      * outside.
124      *
125      * @return The default locale resolver.
126      * @since 2.1.0
127      */
128     protected LocaleResolver createDefaultLocaleResolver() {
129         return new DefaultLocaleResolver();
130     }
131 
132     /***
133      * Creates the default definition DAO, if it has not been specified outside.
134      *
135      * @return The default definition DAO.
136      * @since 2.1.0
137      */
138     protected DefinitionDAO<Locale> createDefaultDefinitionDAO() {
139         return new CachingLocaleUrlDefinitionDAO();
140     }
141 }