1 /*
2 * $Id: TilesListener.java 609762 2008-01-07 19:53:08Z 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.TilesContainer;
26 import org.apache.tiles.TilesException;
27 import org.apache.tiles.access.TilesAccess;
28 import org.apache.tiles.factory.TilesContainerFactory;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.ServletContextEvent;
32 import javax.servlet.ServletContextListener;
33
34 /**
35 * Listener for the initialization of the Tiles container.
36 *
37 * @version $Rev: 609762 $ $Date: 2008-01-07 20:53:08 +0100 (Mon, 07 Jan 2008) $
38 */
39 public class TilesListener
40 implements ServletContextListener {
41
42 /**
43 * Log instance.
44 */
45 protected static final Log LOG =
46 LogFactory.getLog(TilesListener.class);
47
48 /**
49 * Initialize the TilesContainer and place it
50 * into service.
51 *
52 * @param event The intercepted event.
53 */
54 public void contextInitialized(ServletContextEvent event) {
55 ServletContext servletContext = event.getServletContext();
56 try {
57 TilesContainer container = createContainer(servletContext);
58 TilesAccess.setContainer(servletContext, container);
59 } catch (TilesException e) {
60 throw new IllegalStateException("Unable to instantiate container.",
61 e);
62 }
63 }
64
65 /**
66 * Remove the tiles container from service.
67 *
68 * @param event The intercepted event.
69 */
70 public void contextDestroyed(ServletContextEvent event) {
71 ServletContext servletContext = event.getServletContext();
72 try {
73 TilesAccess.setContainer(servletContext, null);
74 } catch (TilesException e) {
75 LOG.warn("Unable to remove tiles container from service.");
76 }
77 }
78
79 /**
80 * Creates a Tiles container.
81 *
82 * @param context The servlet context to use.
83 * @return The created container
84 * @throws TilesException If something goes wrong during creation.
85 */
86 protected TilesContainer createContainer(ServletContext context)
87 throws TilesException {
88 TilesContainerFactory factory =
89 TilesContainerFactory.getFactory(context);
90 return factory.createContainer(context);
91 }
92
93 }