001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2002-2005 Julian Hyde
008// Copyright (C) 2005-2007 Pentaho and others
009// All Rights Reserved.
010*/
011package mondrian.web.taglib;
012
013import javax.servlet.ServletContextEvent;
014import javax.servlet.ServletContextListener;
015
016/**
017 * <code>Listener</code> creates and destroys a {@link ApplResources} at the
018 * appropriate times in the servlet's life-cycle.
019 *
020 * <p>NOTE: This class must not depend upon any non-standard packages (such as
021 * <code>javax.transform</code>) because it is loaded when Tomcat starts, not
022 * when the servlet is loaded. (This might be a bug in Tomcat 4.0.3, because
023 * it worked in 4.0.1. But anyway.)
024 */
025public class Listener implements ServletContextListener {
026
027    ApplicationContext applicationContext;
028
029    public Listener() {
030    }
031
032    public void contextInitialized(ServletContextEvent event) {
033        Class clazz;
034        try {
035            clazz = Class.forName("mondrian.web.taglib.ApplResources");
036        } catch (ClassNotFoundException e) {
037            throw new Error(
038                "Received [" + e.toString() + "] while initializing servlet");
039        }
040        Object o = null;
041        try {
042            o = clazz.newInstance();
043        } catch (InstantiationException e) {
044            throw new Error(
045                "Received [" + e.toString() + "] while initializing servlet");
046        } catch (IllegalAccessException e) {
047            throw new Error(
048                "Received [" + e.toString() + "] while initializing servlet");
049        }
050        applicationContext = (ApplicationContext) o;
051        applicationContext.init(event);
052    }
053
054    public void contextDestroyed(ServletContextEvent event) {
055        if (applicationContext != null) {
056            applicationContext.destroy(event);
057        }
058    }
059
060    interface ApplicationContext {
061        void init(ServletContextEvent event);
062        void destroy(ServletContextEvent event);
063    }
064}
065
066// End Listener.java