View Javadoc

1   /*
2    * $Id: CachingTilesContainer.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  package org.apache.tiles.impl.mgmt;
22  
23  import org.apache.tiles.Definition;
24  import org.apache.tiles.TilesException;
25  import org.apache.tiles.context.TilesRequestContext;
26  import org.apache.tiles.definition.DefinitionsFactoryException;
27  import org.apache.tiles.definition.DefinitionsFactory;
28  import org.apache.tiles.impl.BasicTilesContainer;
29  import org.apache.tiles.mgmt.MutableTilesContainer;
30  
31  /***
32   * Mutable container which caches (in memory) the definitions
33   * registered to it.  If a definition is not found in cache, it
34   * will revert back to it's definitions factory.
35   *
36   * @since Tiles 2.0
37   * @version $Rev: 537196 $ $Date: 2007-05-11 16:07:35 +0200 (Fri, 11 May 2007) $
38   */
39  public class CachingTilesContainer extends BasicTilesContainer
40      implements MutableTilesContainer {
41  
42      /***
43       * The definition manager to store custom and main definitions.
44       */
45      private DefinitionManager mgr = new DefinitionManager();
46  
47      /*** {@inheritDoc} */
48      public void register(Definition definition, Object... requestItems) throws TilesException {
49          TilesRequestContext requestContext = getContextFactory().createRequestContext(
50                  getApplicationContext(),
51                  requestItems
52              );
53          register(definition, requestContext);
54      }
55  
56      /*** {@inheritDoc} */
57      @Override
58      protected Definition getDefinition(String definition,
59                                                  TilesRequestContext context)
60          throws DefinitionsFactoryException {
61          return mgr.getDefinition(definition, context);
62      }
63  
64  
65      /*** {@inheritDoc} */
66      @Override
67      public DefinitionsFactory getDefinitionsFactory() {
68          return mgr.getFactory();
69      }
70  
71      /*** {@inheritDoc} */
72      @Override
73      public void setDefinitionsFactory(DefinitionsFactory definitionsFactory) {
74          super.setDefinitionsFactory(definitionsFactory);
75          mgr.setFactory(definitionsFactory);
76      }
77  
78      /***
79       * Registers a custom definition.
80       *
81       * @param definition The definition to register.
82       * @param request The request inside which the definition should be
83       * registered.
84       * @throws DefinitionsFactoryException If something goes wrong during adding
85       * a definition, such as missing parent definitions.
86       */
87      protected void register(Definition definition,
88              TilesRequestContext request) throws DefinitionsFactoryException {
89          Definition def = new Definition(definition);
90          mgr.addDefinition(def, request);
91      }
92  }