1 /* 2 * $Id: AbstractTilesListener.java 797540 2009-07-24 15:42:00Z 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 javax.servlet.ServletContext; 24 import javax.servlet.ServletContextEvent; 25 import javax.servlet.ServletContextListener; 26 27 import org.apache.tiles.TilesException; 28 import org.apache.tiles.servlet.context.ServletTilesApplicationContext; 29 import org.apache.tiles.servlet.context.ServletUtil; 30 import org.apache.tiles.startup.TilesInitializer; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 /** 35 * Listener for the initialization of the Tiles container. 36 * 37 * @version $Rev: 797540 $ $Date: 2009-07-24 17:42:00 +0200 (ven, 24 lug 2009) $ 38 */ 39 public abstract class AbstractTilesListener implements ServletContextListener { 40 41 /** 42 * Log instance. 43 */ 44 private Logger log = LoggerFactory.getLogger(AbstractTilesListener.class); 45 46 /** 47 * The initializer object. 48 * 49 * @since 2.1.2 50 */ 51 protected TilesInitializer initializer; 52 53 /** 54 * Initialize the TilesContainer and place it 55 * into service. 56 * 57 * @param event The intercepted event. 58 */ 59 public void contextInitialized(ServletContextEvent event) { 60 ServletContext servletContext = event.getServletContext(); 61 initializer = createTilesInitializer(); 62 initializer.initialize(new ServletTilesApplicationContext( 63 servletContext)); 64 } 65 66 /** 67 * Remove the tiles container from service. 68 * 69 * @param event The intercepted event. 70 */ 71 public void contextDestroyed(ServletContextEvent event) { 72 ServletContext servletContext = event.getServletContext(); 73 try { 74 ServletUtil.setContainer(servletContext, null); 75 } catch (TilesException e) { 76 log.warn("Unable to remove tiles container from service.", e); 77 } 78 } 79 80 /** 81 * Creates a new instance of {@link TilesInitializer}. Implement it to use a 82 * different initializer. 83 * 84 * @return The Tiles servlet-based initializer. 85 * @since 2.2.0 86 */ 87 protected abstract TilesInitializer createTilesInitializer(); 88 }