View Javadoc

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