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

1   /*
2    * $Id: CachingKeyedDefinitionsFactoryTilesContainer.java 797754 2009-07-25 11:42:03Z 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.mgmt;
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  import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer;
31  import org.apache.tiles.mgmt.MutableTilesContainer;
32  
33  /***
34   * Container that can be used to store multiple {@link DefinitionsFactory}
35   * instances mapped to different keys, with the addition of being "mutable",
36   * i.e.  caches (in memory) the definitions registered to it.  If a definition
37   * is not found in cache, it will revert back to it's definitions factory.
38   *
39   * @version $Rev: 797754 $ $Date: 2009-07-25 13:42:03 +0200 (sab, 25 lug 2009) $
40   * @deprecated Register different containers using
41   * {@link org.apache.tiles.access.TilesAccess#setContainer(
42   * org.apache.tiles.TilesApplicationContext, org.apache.tiles.TilesContainer, String)}
43   * and using {@link CachingTilesContainer} as container implementation.
44   */
45  public class CachingKeyedDefinitionsFactoryTilesContainer extends
46          KeyedDefinitionsFactoryTilesContainer implements MutableTilesContainer {
47  
48      /***
49       * The name prefix of the attribute that will contain custom definitions for
50       * the current request.
51       */
52      private static final String DEFINITIONS_ATTRIBUTE_NAME_BASE =
53          "org.apache.tiles.impl.mgmt.CachingKeyedDefinitionsFactoryTilesContainer.DEFINITIONS.";
54  
55      /***
56       * The default definition manager, when no key is identified.
57       */
58      private DefinitionManager mgr = new DefinitionManager();
59  
60      /***
61       * Maps a key to its definition manager.
62       */
63      private Map<String, DefinitionManager> key2definitionManager
64              = new HashMap<String, DefinitionManager>();
65  
66      /*** {@inheritDoc} */
67      public void register(Definition definition, Object... requestItems) {
68          TilesRequestContext requestContext = getRequestContextFactory()
69                  .createRequestContext(getApplicationContext(), requestItems);
70          register(definition, requestContext);
71      }
72  
73      /*** {@inheritDoc} */
74      @Override
75      protected Definition getDefinition(String definition,
76              TilesRequestContext context) {
77          DefinitionManager mgr = getProperDefinitionManager(
78                  getDefinitionsFactoryKey(context));
79          return mgr.getDefinition(definition, context);
80      }
81  
82      /*** {@inheritDoc} */
83      @Override
84      public DefinitionsFactory getDefinitionsFactory() {
85          return mgr.getFactory();
86      }
87  
88      /*** {@inheritDoc} */
89      @Override
90      public DefinitionsFactory getDefinitionsFactory(String key) {
91          DefinitionManager mgr = getProperDefinitionManager(key);
92          return mgr.getFactory();
93      }
94  
95      /*** {@inheritDoc} */
96      @Override
97      public void setDefinitionsFactory(DefinitionsFactory definitionsFactory) {
98          super.setDefinitionsFactory(definitionsFactory);
99          mgr.setFactory(definitionsFactory);
100     }
101 
102     /*** {@inheritDoc} */
103     @Override
104     @Deprecated
105     public void setDefinitionsFactory(String key, DefinitionsFactory definitionsFactory,
106             Map<String, String> initParameters) {
107         if (key != null) {
108             initializeDefinitionsFactory(definitionsFactory,
109                     getResourceString(initParameters), initParameters);
110         }
111         DefinitionManager mgr = getOrCreateDefinitionManager(key);
112         mgr.setFactory(definitionsFactory);
113     }
114 
115     /***
116      * Registers a custom definition.
117      *
118      * @param definition The definition to register.
119      * @param request The request inside which the definition should be
120      * registered.
121      */
122     protected void register(Definition definition, TilesRequestContext request) {
123         DefinitionManager mgr = getProperDefinitionManager(
124                 getDefinitionsFactoryKey(request));
125         mgr.addDefinition(definition, request);
126     }
127 
128     /***
129      * Returns a definition manager if found, otherwise it will create a new
130      * one.
131      *
132      * @param key The key of the definition manager.
133      * @return The needed definition manager.
134      */
135     protected DefinitionManager getOrCreateDefinitionManager(String key) {
136         DefinitionManager mgr = key2definitionManager.get(key);
137         if (mgr == null) {
138             mgr = new DefinitionManager(DEFINITIONS_ATTRIBUTE_NAME_BASE + key);
139             key2definitionManager.put(key, mgr);
140         }
141 
142         return mgr;
143     }
144 
145     /***
146      * Returns a definition manager if found.
147      *
148      * @param key The key of the definition manager.
149      * @return The needed definition manager.
150      */
151     protected DefinitionManager getProperDefinitionManager(String key) {
152         DefinitionManager mgr = null;
153 
154         if (key != null) {
155             mgr = key2definitionManager.get(key);
156         }
157         if (mgr == null) {
158             mgr = this.mgr;
159         }
160 
161         return mgr;
162     }
163 }