KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > event > DefaultBaseEventListener


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
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: DefaultBaseEventListener.java,v 1.11 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.event;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.apache.log4j.*;
28
29 /**
30  * <p>This class provides the default implementation for
31  * a BaseEventListener. This is an abstract class, meaning
32  * you have to extend it if you intend to use it. Typically
33  * you would implement it as an inner class and extend
34  * handleReqEvent, handleRespEvent, or handleOtherEvent. Or,
35  * you can always just override handleEvent and do whatever
36  * you want.
37  */

38 public abstract class DefaultBaseEventListener implements BaseEventListener {
39
40     //public vars
41
protected static final Logger localLogger = Logger.getLogger(DefaultBaseEventListener.class.getName());
42
43     //private vars
44
protected String JavaDoc idStr = null;
45
46     /**
47      * Handle all events. This represents a default implementation
48      * that will route the event on to the convenience respective
49      * convenience methods.
50      *
51      * @param context the EventContext for the base event we are handling
52      * @throws EventException
53      */

54     public void handleEvent(EventContext context) throws EventException {
55         //custom code here
56
if (context==null) return;
57         BaseEvent event = context.getEvent();
58         try {
59             if (localLogger.isDebugEnabled()) localLogger.debug("Entering "+this.getListenerID());
60             if (event instanceof ControlEvent && context instanceof ControlEventContext) {
61                 handleControlEvent((ControlEventContext) context);
62             } else if (event instanceof ViewEvent && context instanceof ViewEventContext) {
63                 handleViewEvent((ViewEventContext) context);
64             } else {
65                 handleOtherEvent(context);
66             }
67         } catch (ServletException e) {
68             throw new EventException("Unexpected Servlet Error", e);
69         } catch (IOException e) {
70             throw new EventException("Unexpected IOException", e);
71         } finally {
72             event.setHandled(isHandled());
73             if (localLogger.isDebugEnabled()) localLogger.debug("Exiting "+this.getListenerID());
74         }
75     }
76
77     
78     /**
79      * Handle HttpRequestEvents
80      *
81      * @param context the ControlEventContext
82      * @throws EventException
83      * @throws ServletException
84      * @throws IOException
85      */

86     public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
87         //custom code here (we throw an exception so that if a developer fails
88
//to extend the proper method, they'll at least get a nice error message
89
//at runtime pointing out the problem. This should help prevent the case
90
//where the developer writes handling code but extends the wrong handling
91
//method for the particular type of event being handled)
92
String JavaDoc msg = "Invoked handleControlEvent()...this method should have been extended";
93         localLogger.warn(msg);
94         throw new UnhandledEventException(msg, context);
95     }
96     
97     /**
98      * Handle HttpResponseEvents
99      *
100      * @param context the ViewEventContext
101      * @throws EventException
102      * @throws ServletException
103      * @throws IOException
104      */

105     public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException {
106         //custom code here (we throw an exception so that if a developer fails
107
//to extend the proper method, they'll at least get a nice error message
108
//at runtime pointing out the problem. This should help prevent the case
109
//where the developer writes handling code but extends the wrong handling
110
//method for the particular type of event being handled)
111
String JavaDoc msg = "Invoked handleViewEvent()...this method should have been extended";
112         localLogger.warn(msg);
113         throw new UnhandledEventException(msg, context);
114     }
115     
116     /**
117      * Handle all Other events
118      *
119      * @param context the EventContext
120      * @throws EventException
121      */

122     public void handleOtherEvent(EventContext context) throws EventException {
123         //custom code here (we throw an exception so that if a developer fails
124
//to extend the proper method, they'll at least get a nice error message
125
//at runtime pointing out the problem. This should help prevent the case
126
//where the developer writes handling code but extends the wrong handling
127
//method for the particular type of event being handled)
128
String JavaDoc msg = "Invoked handleOtherEvent()...this method should have been extended";
129         localLogger.warn(msg);
130         throw new UnhandledEventException(msg, context);
131     }
132
133     /**
134      * Return true if the event was handled in the handleEvent method.
135      * By default, most implementations will return true for you, so
136      * the only time you'd need to override is if the handler wanted
137      * to specifically indicate that it had NOT handled the event
138      * (ie in logging scenarios).
139      *
140      * @return true if the event was handled in the handleEvent method
141      */

142     public boolean isHandled() {
143         return true;
144     }
145
146     /**
147      * Get the ID that identifies this listener. This will typically be the
148      * class name.
149      *
150      * @return a string that uniquely identifies this listener
151      */

152     public String JavaDoc getListenerID() {
153         if (idStr==null) {
154             idStr = DefaultBaseEvent.getClassID(this.getClass());
155         }
156         return idStr;
157     }
158 }
159
Popular Tags