View Javadoc

1   /*
2    * $Id: StrutsTilesListener.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.TilesContainer;
24  import org.apache.tiles.TilesException;
25  import org.apache.tiles.factory.TilesContainerFactory;
26  import org.apache.tiles.web.startup.TilesListener;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import javax.servlet.ServletContext;
31  import java.util.Map;
32  import java.util.HashMap;
33  
34  /***
35   * Listener used to automatically inject ServletContext
36   * init parameters so that they don't need to be configured
37   * explicitly for tiles integration.  This is provided
38   * mainly for backwards compatibility with Struts 2.0.1
39   * configuration.
40   *
41   * @since Struts 2.0.2
42   * @version $Rev: 545186 $
43   *
44   */
45  public class StrutsTilesListener extends TilesListener {
46  
47      private static final Log LOG =
48          LogFactory.getLog(StrutsTilesListener.class);
49  
50      private static final Map<String, String> INIT;
51  
52      static {
53          INIT = new HashMap<String, String>();
54          INIT.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
55                   StrutsTilesContainerFactory.class.getName());
56      }
57  
58      protected TilesContainer createContainer(ServletContext context)
59      throws TilesException {
60          if(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM) == null) {
61              context = decorate(context);
62          }
63          else {
64              LOG.warn("Tiles container factory is explicitly set.  Not injecting struts configuration.");
65          }
66          return super.createContainer(context);
67      }
68  
69      protected ServletContext decorate(ServletContext context) {
70          return new ConfiguredServletContext(context, INIT);
71      }
72  
73  }