KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > OpenCmsListener


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/OpenCmsListener.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.14 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.main;
33
34 import javax.servlet.ServletContextEvent JavaDoc;
35 import javax.servlet.ServletContextListener JavaDoc;
36 import javax.servlet.http.HttpSessionEvent JavaDoc;
37 import javax.servlet.http.HttpSessionListener JavaDoc;
38
39 import org.apache.commons.logging.Log;
40
41 /**
42  * Provides the OpenCms system with information from the servlet context.<p>
43  *
44  * Used for the following purposes:<ul>
45  * <li>Starting up OpenCms when the servlet container is started.</li>
46  * <li>Shutting down OpenCms when the servlet container is shut down.</li>
47  * <li>Informing the <code>{@link org.opencms.main.CmsSessionManager}</code> if a new session is created.</li>
48  * <li>Informing the <code>{@link org.opencms.main.CmsSessionManager}</code> session is destroyed or invalidated.</li>
49  * </ul>
50  *
51  * @author Alexander Kandzior
52  *
53  * @version $Revision: 1.14 $
54  *
55  * @since 6.0.0
56  */

57 public class OpenCmsListener implements ServletContextListener JavaDoc, HttpSessionListener JavaDoc {
58
59     /** The log object for this class. */
60     private static final Log LOG = CmsLog.getLog(OpenCmsListener.class);
61
62     /**
63      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
64      */

65     public void contextDestroyed(ServletContextEvent JavaDoc event) {
66
67         try {
68             // destroy the OpenCms instance
69
OpenCmsCore.getInstance().shutDown();
70         } catch (CmsInitException e) {
71             if (e.isNewError()) {
72                 LOG.error(e);
73             }
74         } catch (Throwable JavaDoc t) {
75             // make sure all other errors are displayed in the OpenCms log
76
LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_GENERIC_0), t);
77         }
78     }
79
80     /**
81      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
82      */

83     public void contextInitialized(ServletContextEvent JavaDoc event) {
84
85         try {
86             // upgrade the OpenCms runlevel
87
OpenCmsCore.getInstance().upgradeRunlevel(event.getServletContext());
88         } catch (CmsInitException e) {
89             if (e.isNewError()) {
90                 // only log new init errors
91
LOG.error(e);
92             }
93         } catch (Throwable JavaDoc t) {
94             // make sure all other errors are displayed in the OpenCms log
95
LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_GENERIC_0), t);
96             // throw a new init Exception to make sure a "context destroyed" event is triggered
97
throw new CmsInitException(Messages.get().container(Messages.ERR_CRITICAL_INIT_GENERIC_1, t.getMessage()));
98         }
99     }
100
101     /**
102      * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
103      */

104     public void sessionCreated(HttpSessionEvent JavaDoc event) {
105
106         try {
107             // inform the OpenCms session manager
108
OpenCmsCore.getInstance().getSessionManager().sessionCreated(event);
109         } catch (CmsInitException e) {
110             if (e.isNewError()) {
111                 LOG.error(e);
112             }
113         } catch (Throwable JavaDoc t) {
114             // make sure all other errors are displayed in the OpenCms log
115
LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_GENERIC_0), t);
116         }
117     }
118
119     /**
120      * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
121      */

122     public void sessionDestroyed(HttpSessionEvent JavaDoc event) {
123
124         try {
125             // inform the OpenCms session manager
126
OpenCmsCore.getInstance().getSessionManager().sessionDestroyed(event);
127         } catch (CmsInitException e) {
128             if (e.isNewError()) {
129                 LOG.error(e);
130             }
131         } catch (Throwable JavaDoc t) {
132             // make sure all other errors are displayed in the OpenCms log
133
LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_GENERIC_0), t);
134         }
135     }
136 }
Popular Tags