KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 package org.enhydra.barracuda.core.event.helper;
21
22 import org.apache.log4j.*;
23
24 import org.enhydra.barracuda.core.event.*;
25
26 /**
27  * <p> This class associates a specific event handler class with an event by
28  * extending DefaultListenerFactory and making implementation details in the
29  * gateways much simpler... Without this class, each instance of a listener
30  * factory must create a separate anonymous class and provide the
31  * implementation for getInstance() and getListenerID().
32  *
33  * <p>With this class, the association of an event to a handler is a reduced
34  * to a single line:
35  *
36  * <p><code>specifyLocalEventInterests(new EventConnectorFactory(Handler.class), Event.class);</code>
37  *
38  * <p> IMPORTANT NOTE: When using this class, the passed-in event handler must
39  * either be a non-inner class or an inner class which is declared public static.
40  * Otherwise, when the getInstance() method attempts to instantiate the handler a
41  * <code>java.lang.InstantiationException</code> will get thrown which will show up
42  * as a <code>java.lang.NullPointerException</code>. This is because getInstance()
43  * returns null if it fails to instantiate the handler. This won't be caught
44  * at compile time, so be careful!
45  *
46  * @author Stephen Peterson <stephen_peterson@agilent.com>
47  * @author Jacob Kjome <hoju@visi.com>
48  * @version %I%, %G%
49  * @since 1.0.1 (2002-02-15)
50  */

51 public class EventConnectorFactory extends DefaultListenerFactory {
52
53     protected static final Logger logger = Logger.getLogger(EventConnectorFactory.class.getName());
54     protected Class JavaDoc eventHandlerClass = null;
55
56     /**
57      * Constructor
58      *
59      * @param aClass the handler class to associate with an event
60      */

61     public EventConnectorFactory(Class JavaDoc aClass) {
62         logger.debug("Creating new EventConnectorFactory --> "+aClass);
63
64         //sanity check & assignment
65
eventHandlerClass = aClass;
66         if (eventHandlerClass==null) {
67             logger.fatal("Event handler class cannot be null!", new IllegalAccessException JavaDoc());
68         }
69
70         //now go ahead and invoke getInstance(), just to make sure its going
71
//to work later. This will effectively catch errors immediately at
72
//startup, rather than waiting until someone stumbles into the problem
73
//while using the application
74
getInstance();
75     }
76
77     /**
78      * Get an instance of the underlying BaseEventListener
79      *
80      * @return get an instance of the BaseEventListener
81      */

82     public BaseEventListener getInstance() {
83         if (logger.isDebugEnabled()) logger.debug("Creating instance of: " + eventHandlerClass.getName());
84
85         //create an instance of the class
86
BaseEventListener bel = null;
87         Exception JavaDoc e = null;
88         try {
89             bel = (BaseEventListener) eventHandlerClass.newInstance();
90         } catch (IllegalAccessException JavaDoc iae) {
91             logger.fatal("Illegal Access Exception!", iae);
92         } catch (InstantiationException JavaDoc ie) {
93             logger.fatal("Error instantiating "+eventHandlerClass.getName()+"; if you defined it as an inner class, make sure its declared public static", ie);
94         } catch (ClassCastException JavaDoc cce) {
95             logger.fatal("Error casting "+eventHandlerClass.getName()+" to BaseEventListner. This class or its superclass must implement the BaseEventListener interface", cce);
96         }
97
98         //return the event listener
99
return bel;
100     }
101
102     /**
103      * Get the Listener ID associated with this class of listener. This will
104      * generally either be the class name of the listener that the factory
105      * creates
106      *
107      * @return the listener ID that describes this factory
108      */

109     public String JavaDoc getListenerID() {
110         return getID(eventHandlerClass);
111     }
112
113 }
114
Popular Tags