View Javadoc

1   /*
2    * $Id: StrutsTilesContainerFactory.java 545186 2007-06-07 14:12:59Z 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.struts2.tiles;
22  
23  import org.apache.tiles.TilesApplicationContext;
24  import org.apache.tiles.TilesException;
25  import org.apache.tiles.context.TilesContextFactory;
26  import org.apache.tiles.context.TilesRequestContext;
27  import org.apache.tiles.definition.DefinitionsFactory;
28  import org.apache.tiles.factory.TilesContainerFactory;
29  import org.apache.tiles.impl.BasicTilesContainer;
30  import org.apache.tiles.preparer.PreparerFactory;
31  
32  import java.util.Map;
33  
34  
35  public class StrutsTilesContainerFactory extends TilesContainerFactory {
36  
37  
38      @Override
39      protected void storeContainerDependencies(Object context, Map<String, String> initParameters, Map<String, String> configuration, BasicTilesContainer container) throws TilesException {
40          TilesContextFactory contextFactory =
41              (TilesContextFactory) createFactory(configuration,
42                  CONTEXT_FACTORY_INIT_PARAM);
43  
44          contextFactory = new StrutsTilesContextFactory(contextFactory);
45  
46          DefinitionsFactory defsFactory =
47              (DefinitionsFactory) createFactory(configuration,
48                  DEFINITIONS_FACTORY_INIT_PARAM);
49  
50          PreparerFactory prepFactory =
51              (PreparerFactory) createFactory(configuration,
52                  PREPARER_FACTORY_INIT_PARAM);
53  
54          contextFactory.init(configuration);
55          TilesApplicationContext tilesContext =
56              contextFactory.createApplicationContext(context);
57  
58          container.setDefinitionsFactory(defsFactory);
59          container.setContextFactory(contextFactory);
60          container.setPreparerFactory(prepFactory);
61          container.setApplicationContext(tilesContext);
62      }
63  
64      /***
65       * Wrapper factory, used to decorate the TilesRequestContext with a
66       * FreemarkerResult aware version.
67       * 
68       */
69      class StrutsTilesContextFactory implements TilesContextFactory {
70  
71          private TilesContextFactory factory;
72  
73          public StrutsTilesContextFactory(TilesContextFactory factory) {
74              this.factory = factory;
75          }
76  
77          public void init(Map<String, String> map) {
78              factory.init(map);
79          }
80  
81          public TilesApplicationContext createApplicationContext(Object context) {
82              return factory.createApplicationContext(context);
83          }
84  
85          public TilesRequestContext createRequestContext(
86                  TilesApplicationContext tilesApplicationContext,
87                  Object... requestItems) {
88              TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, requestItems);
89              return new StrutsTilesRequestContext(context);
90          }
91      }
92  }