KickJava   Java API By Example, From Geeks To Geeks.

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


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: EventRedirectFactory.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 throwing a client side redirect, in
33  * effect acting as an event forwarder.
34  */

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

50 //TODO - I don't think we should actually be using a BaseEvent here in the constructor,
51
//since by default the ApplicationGateway will only dispatch HttpRequestEvents - csc
52
public EventRedirectFactory (BaseEvent ifevent) {
53         if (logger.isInfoEnabled()) logger.info("Creating new EventRedirectFactory -->"+ifevent.getClass().getName());
54         fevent = ifevent;
55     }
56
57     /**
58      * Get an instance of the underlying BaseEventListener
59      *
60      * @return get an instance of the BaseEventListener
61      */

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

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

80     class EventHandler extends DefaultBaseEventListener {
81
82         protected String JavaDoc idStr = null;
83
84         public void handleEvent(EventContext context) throws EventException {
85             
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("Redirecting to:"+newEvent);
95                 throw new ClientSideRedirectException(newEvent);
96             } catch (InstantiationException JavaDoc e) {
97                 throw new EventException("Error redirecting Event", e);
98             } catch (IllegalAccessException JavaDoc e) {
99                 throw new EventException("Error redirecting 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