View Javadoc

1   /*
2    * $Id: ContextListener.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.listeners;
24  
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletContextEvent;
27  import javax.servlet.ServletContextListener;
28  
29  import org.talika.tarsis.Globals;
30  import org.talika.tarsis.context.ConfigException;
31  import org.talika.tarsis.context.ContextImpl;
32  import org.talika.tarsis.service.ServiceException;
33  
34  /**
35   * This implementation of <code>ServletContextListener</code> is responsible
36   * for loading and initializing <code>Context</code> object in web application.
37   *
38   * @author  Jose M. Palomar
39   * @version $Revision: 269 $
40   */
41  public final class ContextListener implements ServletContextListener {
42  
43      // Constants
44  
45      // Fields
46  
47      // Constructors
48      /**
49       * Creates a new <code>ContextListener</code> object.
50       */
51      public ContextListener() {
52      }
53  
54      // Methods
55      /**
56       * Notification that the web application is ready to process requests.<br>
57       * <br>
58       * Obtains <code>ContextImpl</code> instance initializes it and saves it
59       * as a servlet context attribute.
60       *
61       * @param servletContextEvent ServletContextEvent servlet context event.
62       * @see javax.servlet.ServletContextListener#contextInitialized(ServletContextEvent)
63       */
64      public void contextInitialized(ServletContextEvent servletContextEvent) {
65  
66          ServletContext servletContext = servletContextEvent.getServletContext();
67          ContextImpl context = ContextImpl.getInstance();
68  
69          if (context.getLogger().isInfoEnabled()) {
70              context.getLogger().logInfo("Tarsis Context: initializing");
71          }
72  
73          try {
74  
75              context.init(servletContext);
76              servletContext.setAttribute(Globals.CONTEXT_ATTR, context);
77  
78              if (context.getLogger().isInfoEnabled()) {
79                  context.getLogger().logInfo("Tarsis Context: initialized");
80              }
81              
82          }
83          catch (ConfigException ce) {
84              if (context.getLogger().isPanicEnabled()) {
85                  context.getLogger().logPanic("Tarsis Context : error initializing (" +
86                                            ce.getMessage() + ")");
87              }
88          }
89          catch (ServiceException se) {
90              if (context.getLogger().isPanicEnabled()) {
91                  context.getLogger().logPanic("Tarsis Context : error initializing (" +
92                                           se.getMessage() + ")");
93              }
94          }
95  
96      }
97  
98      /**
99       * Notification that the servlet context is about to be shut down.<br>
100      * <br>
101      * Obtains <code>ContextImpl</code> instance and finalizes it.
102      *
103      * @param servletContextEvent ServletContextEvent servlet context event.
104      * @see javax.servlet.ServletContextListener#contextDestroyed(ServletContextEvent)
105      */
106     public void contextDestroyed(ServletContextEvent servletContextEvent) {
107 
108 
109         ServletContext servletContext = servletContextEvent.getServletContext();
110         ContextImpl context = (ContextImpl) servletContext.getAttribute(Globals.CONTEXT_ATTR);
111 
112         if (context != null) {
113 
114             if (context.getLogger().isInfoEnabled()) {
115                 context.getLogger().logInfo("Tarsis Context: destroying");
116             }
117 
118             context.destroy();
119             servletContext.removeAttribute(Globals.CONTEXT_ATTR);
120 
121             if (context.getLogger().isInfoEnabled()) {
122                 context.getLogger().logInfo("Tarsis Context: destroyed");
123             }
124 
125         }
126 
127     }
128 
129 }