View Javadoc

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