View Javadoc

1   /*
2    * $Id: KeyedDefinitionsFactoryTilesContainerFactory.java 680124 2008-07-27 15:20:00Z 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  package org.apache.tiles.factory;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.tiles.TilesContainer;
27  import org.apache.tiles.TilesException;
28  import org.apache.tiles.definition.DefinitionsFactory;
29  import org.apache.tiles.impl.BasicTilesContainer;
30  import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer;
31  import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.KeyExtractor;
32  import org.apache.tiles.impl.mgmt.CachingKeyedDefinitionsFactoryTilesContainer;
33  import org.apache.tiles.mgmt.MutableTilesContainer;
34  import org.apache.tiles.reflect.ClassUtil;
35  
36  /***
37   * Factory that creates instances of container that will extend the
38   * {@link KeyedDefinitionsFactoryTilesContainer} class.
39   *
40   * @version $Rev: 680124 $ $Date: 2008-07-27 17:20:00 +0200 (Sun, 27 Jul 2008) $
41   */
42  public class KeyedDefinitionsFactoryTilesContainerFactory extends
43          TilesContainerFactory {
44  
45      /***
46       * The name of the initialization parameter that will contain a
47       * comma-separated list of keys to use.
48       */
49      public static final String CONTAINER_KEYS_INIT_PARAM =
50          "org.apache.tiles.factory.KeyedDefinitionsFactoryTilesContainerFactory.KEYS";
51  
52      /***
53       * Init parameter name that contains the class name for the key extractor.
54       */
55      public static final String KEY_EXTRACTOR_CLASS_INIT_PARAM =
56          "org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.KeyExtractor";
57  
58      /*** {@inheritDoc} */
59      @Override
60      public MutableTilesContainer createMutableTilesContainer(Object context) throws TilesException {
61          CachingKeyedDefinitionsFactoryTilesContainer container =
62              new CachingKeyedDefinitionsFactoryTilesContainer();
63          initializeContainer(context, container);
64          return container;
65      }
66  
67      /*** {@inheritDoc} */
68      @Override
69      public TilesContainer createTilesContainer(Object context) throws TilesException {
70          KeyedDefinitionsFactoryTilesContainer container =
71              new KeyedDefinitionsFactoryTilesContainer();
72          initializeContainer(context, container);
73          return container;
74      }
75  
76      // FIXME Probably we should create some sort of "FactoryUtils" to create
77      // factories dynamically depending on a configuration.
78      // I think this method does not belong here.
79      /***
80       * Creates a definitions factory.
81       * @param context The context object to use.
82       * @return The newly created definitions factory.
83       * @throws TilesException If something goes wrong.
84       */
85      public DefinitionsFactory createDefinitionsFactory(Object context)
86              throws TilesException {
87          DefinitionsFactory retValue;
88          Map<String, String> config = new HashMap<String, String>(defaultConfiguration);
89          config.putAll(getInitParameterMap(context));
90          retValue = (DefinitionsFactory) createFactory(config,
91                      DEFINITIONS_FACTORY_INIT_PARAM);
92  
93          return retValue;
94      }
95  
96      /*** {@inheritDoc} */
97      @Override
98      protected void storeContainerDependencies(Object context,
99              Map<String, String> initParameters,
100             Map<String, String> configuration, BasicTilesContainer container)
101             throws TilesException {
102         super.storeContainerDependencies(context, initParameters, configuration, container);
103 
104         String keyExtractorClassName = configuration.get(
105                 KEY_EXTRACTOR_CLASS_INIT_PARAM);
106         if (keyExtractorClassName != null
107                 && container instanceof KeyedDefinitionsFactoryTilesContainer) {
108             ((KeyedDefinitionsFactoryTilesContainer) container).setKeyExtractor(
109                     (KeyExtractor) ClassUtil.instantiate(keyExtractorClassName));
110         }
111 
112         String keysString = initParameters.get(CONTAINER_KEYS_INIT_PARAM);
113         if (keysString != null
114                 && container instanceof KeyedDefinitionsFactoryTilesContainer) {
115             String[] keys = keysString.split(",");
116             Map<String, String> config = new HashMap<String, String>(defaultConfiguration);
117             config.putAll(getInitParameterMap(context));
118             for (int i = 0; i < keys.length; i++) {
119                 Map<String, String> initParams = new HashMap<String, String>();
120                 String param = initParameters.get(
121                         KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX + keys[i]);
122                 if (param != null) {
123                     initParams.put(BasicTilesContainer.DEFINITIONS_CONFIG,
124                             param);
125                 }
126 
127                 DefinitionsFactory defsFactory =
128                     (DefinitionsFactory) createFactory(config,
129                             DEFINITIONS_FACTORY_INIT_PARAM);
130                 ((KeyedDefinitionsFactoryTilesContainer) container)
131                         .setDefinitionsFactory(keys[i], defsFactory,
132                                 initParams);
133             }
134         }
135     }
136 }