KickJava   Java API By Example, From Geeks To Geeks.

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


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: PageEventDispatcher.java,v 1.8 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.page;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import org.enhydra.barracuda.core.event.ControlEventContext;
24 import org.enhydra.barracuda.core.event.DefaultEventDispatcher;
25 import org.enhydra.barracuda.core.event.EventBroker;
26 import org.enhydra.barracuda.core.event.EventContext;
27 import org.enhydra.barracuda.core.event.EventException;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30
31 /**
32  * The <code>PageEventDispatcher</code> is used to suppress errors that can result
33  * from using the page model with session based pages. If a user makes two requests short
34  * after each other it can happen that concurrent modifications of the page stored in the
35  * session occur. Then for example a control event that expects a certain page to be stored in
36  * the session can get into trouble, usually resulting in classcastexceptions.
37  * <p>
38  * The <code>PageEventDispatcher</code> solves this by synchronizing the request around the
39  * session object.
40  * <p>
41  * Future enhacements might include the possibility to turn the behaviour on or
42  * off on a per-session basis.
43  *
44  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
45  * @version 1.0
46  */

47 public class PageEventDispatcher extends DefaultEventDispatcher {
48
49     /**
50      * <code>PAGE_SYNCHRONIZATION_ACTIVATED</code> can be used to globally
51      * deactivate the synchronization. Default value is true.
52      */

53     public static boolean PAGE_SYNCHRONIZATION_ACTIVATED = true;
54     private RenderPage _rpe = new RenderPage();
55
56     /**
57      * Synchronizes around the session if <code>PAGE_SYNCHRONIZATION_ACTIVATED</code> is true.
58      *
59      * @param eb an <code>EventBroker</code> value
60      * @param context an <code>EventContext</code> value
61      * @exception EventException if an error occurs
62      */

63     public void dispatchEvent(EventBroker eb, EventContext context) throws EventException {
64         context.putState(DefaultEventDispatcher.DEFAULT_RESPONSE_EVENT, _rpe);
65
66         if (PAGE_SYNCHRONIZATION_ACTIVATED) {
67             HttpServletRequest JavaDoc req = ((ControlEventContext)context).getRequest();
68             HttpSession JavaDoc session = req.getSession(false);
69             if (session != null && session.getAttribute(PageFactory.PAGE_KEY) != null) {
70                 Object JavaDoc synchee = new Object JavaDoc();
71                 if (session.getAttribute(PageFactory.SYNCHEE) == null) {
72                     session.setAttribute(PageFactory.SYNCHEE, synchee);
73                 }
74                 synchronized(synchee) {
75                     super.dispatchEvent(eb, context);
76                 }
77                 return;
78             }
79         }
80         super.dispatchEvent(eb, context);
81     }
82
83 }
84
Popular Tags