KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > page > PageEventListener


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: PageEventListener.java,v 1.10 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.page;
21
22 import java.io.IOException JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26 import org.enhydra.barracuda.core.event.BaseEvent;
27 import org.enhydra.barracuda.core.event.ControlEventContext;
28 import org.enhydra.barracuda.core.event.DefaultBaseEventListener;
29 import org.enhydra.barracuda.core.event.EventException;
30 import org.apache.log4j.Logger;
31
32
33 /**
34  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
35  * @version 1.0
36  */

37 public abstract class PageEventListener extends DefaultBaseEventListener {
38
39     private static final Logger logger = Logger.getLogger(PageEventListener.class.getName());
40
41     public final void handleControlEvent(ControlEventContext context)
42             throws EventException, ServletException JavaDoc, IOException JavaDoc {
43         HttpServletRequest JavaDoc req = context.getRequest();
44         HttpSession JavaDoc session = req.getSession(false);
45
46         Page page = null;
47         //track state of pages; new OR cached and differs from Page defined for
48
//non-auxilary current event would make this true. Default to false,
49
//meaning cached and same as that defined for the current event.
50
boolean pageNewOrDiffers = false;
51         if (session != null) {
52             page = (Page)session.getAttribute(PageFactory.PAGE_KEY);
53         }
54         if (page == null) {
55             page = (Page)context.getState(PageFactory.PAGE_KEY);
56         }
57
58         BaseEvent event = context.getEvent();
59         if (logger.isDebugEnabled()) logger.debug("Processing event " + event.getClass().getName());
60         if (page == null) {
61             pageNewOrDiffers = true;
62             if (logger.isDebugEnabled()) logger.debug("No cached PAGE found, need to create fresh PAGE for event");
63             page = PageFactory.createPage(event);
64             if (page != null) {
65                 if (logger.isDebugEnabled()) logger.debug("Page creation successful!");
66                 page.createDefaultModels(context);
67             } else {
68                 //a null pointer exception would occur below if we didn't throw this exception...
69
String JavaDoc errMsg = "Failed to create fresh PAGE for event";
70                 logger.fatal(errMsg);
71                 throw new PageException(errMsg);
72             }
73         } else {
74             if (logger.isDebugEnabled()) logger.debug("Cached Page found, checking if it differs from the one specified for event");
75             if (!page.getClass().equals(PageFactory.getPageClass(event))) {
76                 pageNewOrDiffers = true;
77                 if (logger.isDebugEnabled()) logger.debug("Page differs, may need to create fresh Page to hand over for event");
78                 Page newPage = PageFactory.createPage(event);
79                 if (newPage != null) {
80                     if (logger.isInfoEnabled()) logger.info("Handing over");
81                     newPage.handOver(context, page);
82                     page = newPage;
83                 } else {
84                     pageNewOrDiffers = false;
85                     //this condition happens when the given event is auxilary,
86
//meaning it is not mapped to a page. Only functional
87
//events are mapped to pages. See javadoc comments in
88
//PageFactory.createPage(BaseEvent) for details.
89
}
90             } else {
91                 if (logger.isInfoEnabled()) logger.info("Cached Page class matches that associated with requested event " + event.getClass().getName() + ", using cached Page");
92             }
93         }
94
95         if (page instanceof SessionPage) {
96             if (logger.isDebugEnabled()) logger.debug("Page " + page.getClass().getName() + " implements SessionPage");
97             if (session == null) {
98                 //Need to do this or a session might never be created to store session pages.
99
//This is because whenever we check for a session page, we attempt to get the
100
//session but tell theh request object not to create one if it doesn't exist
101
//already. So, the only way a session would exist, other than doing this, is
102
//if another part of the running application happens to create a session. That's
103
//a bit too hit-or-miss. Need to be sure to support SessionPage functionality.
104
if (logger.isDebugEnabled()) logger.debug("Creating session for SessionPage storage");
105                 session = req.getSession(true);
106             }
107             if (pageNewOrDiffers) {
108                 //avoid re-setting the same object in the session constantly. No need to
109
//set it if it is already there.
110
if (logger.isDebugEnabled()) logger.debug("Storing Page in the HttpSession");
111                 session.setAttribute(PageFactory.PAGE_KEY, page);
112             }
113         } else {
114             if (pageNewOrDiffers && session != null) {
115                 //only need to bother removing the attribute from an existing session if the page
116
//is not the same as one stored in cache for the current event. If it is the same
117
//page as before and we get into this conditional block, then we know the last page
118
//wasn't a SessionPage and, therefore it is impossible for there to be a page stored
119
//in the session. It would have been previously removed the last time the page was
120
//different from that stored in cache for the current event.
121
if (logger.isDebugEnabled()) logger.debug("Ensuring any existing SessionPage is Removed from the HttpSession");
122                 session.removeAttribute(PageFactory.PAGE_KEY);
123             }
124             context.putState(PageFactory.PAGE_KEY, page);
125         }
126
127         handleControlEvent(context, page);
128     }
129
130     public abstract void handleControlEvent(ControlEventContext context, Page page)
131             throws EventException, ServletException JavaDoc, IOException JavaDoc;
132
133 }
134
Popular Tags