View Javadoc

1   /*
2    * $Id: StrutsTilesContainerFactory.java 488747 2006-12-19 18:07:22Z ddewolf $
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 javax.servlet.jsp.PageContext;
33  import java.util.Map;
34  
35  
36  public class StrutsTilesContainerFactory extends TilesContainerFactory {
37  
38      /***
39       * Initialize the container in a struts specific manner.
40       * 
41       * @param context
42       * @param container
43       * @throws TilesException
44       */
45      public void initializeContainer(Object context,
46                                      BasicTilesContainer container)
47              throws TilesException {
48  
49          Map<String, String> initParmMap =
50                 TilesContainerFactory.getInitParameterMap(context);
51  
52          TilesContextFactory contextFactory = (TilesContextFactory)
53                  TilesContainerFactory.createFactory(
54                          initParmMap, TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM);
55  
56          contextFactory = new StrutsTilesContextFactory(contextFactory);
57  
58          DefinitionsFactory defsFactory = (DefinitionsFactory)
59                  TilesContainerFactory.createFactory(
60                           initParmMap,
61                           TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM);
62  
63          PreparerFactory prepFactory =
64                  (PreparerFactory) TilesContainerFactory.createFactory(
65                          initParmMap,
66                          TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM);
67  
68          TilesApplicationContext tilesContext =
69                  contextFactory.createApplicationContext(context);
70  
71          container.setDefinitionsFactory(defsFactory);
72          container.setContextFactory(contextFactory);
73          container.setPreparerFactory(prepFactory);
74          container.setApplicationContext(tilesContext);
75  
76          container.init(getInitParameterMap(context));
77  
78      }
79  
80      /***
81       * Wrapper factory, used to decorate the TilesRequestContext with a
82       * FreemarkerResult aware version.
83       * 
84       */
85      class StrutsTilesContextFactory implements TilesContextFactory {
86  
87          private TilesContextFactory factory;
88  
89          public StrutsTilesContextFactory(TilesContextFactory factory) {
90              this.factory = factory;
91          }
92  
93          public void init(Map map) {
94              factory.init(map);
95          }
96  
97          public TilesApplicationContext createApplicationContext(Object context) {
98              return factory.createApplicationContext(context);
99          }
100 
101         public TilesRequestContext createRequestContext(TilesApplicationContext tilesApplicationContext, Object request, Object response) {
102             TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, request, response);
103             return new StrutsTilesRequestContext(context);
104         }
105 
106         public TilesRequestContext createRequestContext(TilesApplicationContext tilesApplicationContext, PageContext pageContext) {
107             TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, pageContext);
108             return new StrutsTilesRequestContext(context);
109         }
110     }
111 }