KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > event > helper > EventForwardingFactory


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

20 package org.enhydra.barracuda.core.event.helper;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.log4j.*;
26
27 import org.enhydra.barracuda.core.event.*;
28 import org.enhydra.barracuda.plankton.*;
29
30 /**
31  * This class provides a simple factory that will handle
32  * events by simply adding a new event to the queue, in
33  * effect acting as an event forwarder. This type of functionality
34  * is useful when you have request event handlers that simply
35  * forward flow onto response eventhandlers without any intermediary
36  * processing.
37  */

38 public class EventForwardingFactory extends DefaultListenerFactory {
39
40     //public constants
41
protected static final Logger logger = Logger.getLogger(EventForwardingFactory.class.getName());
42
43     //private vars
44
protected String JavaDoc id = null;
45     protected BaseEvent fevent = null;
46
47     /**
48      * Public constructor. Note that when actually forwarding the
49      * event, a new instance will be generated via reflection.
50      *
51      * @param ifevent the event to be generated
52      */

53     public EventForwardingFactory (BaseEvent ifevent) {
54         if (logger.isInfoEnabled()) logger.info("Creating new EventForwardingFactory -->"+ifevent.getClass().getName());
55         fevent = ifevent;
56     }
57
58     /**
59      * Get an instance of the underlying BaseEventListener
60      *
61      * @return get an instance of the BaseEventListener
62      */

63     public BaseEventListener getInstance() {
64         return new EventHandler();
65     }
66     
67     /**
68      * Get the Listener ID associated with this class of listener. This will
69      * generally either be the class name of the listener that the factory
70      * creates
71      *
72      * @return the listener ID that describes this factory
73      */

74     public String JavaDoc getListenerID() {
75         return getID(fevent.getClass());
76     }
77
78     /**
79      * EventHandler -
80      */

81     class EventHandler extends DefaultBaseEventListener {
82
83         protected String JavaDoc idStr = null;
84
85         public void handleEvent(EventContext context) throws EventException {
86             BaseEvent event = context.getEvent();
87             DispatchQueue queue = context.getQueue();
88             if (logger.isInfoEnabled()) logger.info("Got event:"+event);
89
90             //in this case, we're not really doing anything special, so just
91
//redirect to the RenderLogin view
92
try {
93                 BaseEvent newEvent = (BaseEvent) fevent.getClass().newInstance();
94                 if (logger.isInfoEnabled()) logger.info("Forwarding to:"+newEvent);
95                 newEvent.setSource(event);
96                 queue.addEvent(newEvent);
97                 event.setHandled(true);
98             } catch (Exception JavaDoc e) {
99                 throw new EventException("Error forwarding Event", e);
100             }
101         }
102         
103         /**
104          * Get the ID that identifies this listener. This will typically be the
105          * class name.
106          *
107          * @return a string that uniquely identifies this listener
108          */

109         public String JavaDoc getListenerID() {
110             if (idStr==null) {
111                 idStr = DefaultBaseEvent.getClassID(fevent.getClass());
112             }
113             return idStr;
114         }
115     }
116 }
117
Popular Tags