This project has retired. For details please refer to its Attic page.
TilesServlet xref
View Javadoc

1   /*
2    * $Id: TilesServlet.java 734996 2009-01-16 13:27:28Z 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.tiles.web.startup;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.tiles.TilesException;
26  import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
27  import org.apache.tiles.servlet.context.ServletUtil;
28  import org.apache.tiles.startup.BasicTilesInitializer;
29  import org.apache.tiles.startup.TilesInitializer;
30  import org.apache.tiles.web.util.ServletContextAdapter;
31  
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.ServletContext;
34  import javax.servlet.ServletException;
35  
36  /***
37   * Initialization Servlet.
38   *
39   * @see org.apache.tiles.web.startup.TilesListener
40   * @version $Rev: 734996 $ $Date: 2009-01-16 14:27:28 +0100 (ven, 16 gen 2009) $
41   */
42  public class TilesServlet extends HttpServlet {
43  
44      /***
45       * Log instance.
46       */
47      protected final Log log =
48          LogFactory.getLog(TilesServlet.class);
49  
50      /***
51       * The private listener instance, that is used to initialize Tiles
52       * container.
53       */
54      private TilesInitializer initializer;
55  
56      /***
57       * Constructor.
58       *
59       * @since 2.1.2
60       */
61      public TilesServlet() {
62          initializer = createTilesInitializer();
63      }
64  
65      /*** {@inheritDoc} */
66      @Override
67      public void destroy() {
68          try {
69              ServletUtil.setContainer(getServletContext(), null);
70          } catch (TilesException e) {
71              log.warn("Unable to remove tiles container from service.", e);
72          }
73      }
74  
75      /*** {@inheritDoc} */
76      @Override
77      public void init() throws ServletException {
78          ServletContext adaptedContext = new ServletContextAdapter(
79                  getServletConfig());
80          ServletTilesApplicationContext preliminaryContext = new ServletTilesApplicationContext(
81                  adaptedContext);
82          initializer.initialize(preliminaryContext);
83      }
84  
85      /***
86       * Creates a new instance of {@link BasicTilesInitializer}. Override it to use a different initializer.
87       *
88       * @return The Tiles servlet-based initializer.
89       * @since 2.1.2
90       */
91      protected TilesInitializer createTilesInitializer() {
92          return new BasicTilesInitializer();
93      }
94  }