KickJava   Java API By Example, From Geeks To Geeks.

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


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: DefaultEventGateway.java,v 1.14 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
25 import org.apache.log4j.Logger;
26
27 import org.enhydra.barracuda.plankton.data.*;
28
29 /**
30  * Default implementation of an EventGateway. An event
31  * gateway just indicates that the class has an interest in
32  * events, and as such needs access to the EventBroker in
33  * order to register its listeners there.
34  */

35 public class DefaultEventGateway implements EventGateway {
36
37     //public constants
38
private static final Logger logger = Logger.getLogger(DefaultEventGateway.class.getName());
39     
40     private static byte[] sep = System.getProperty("line.separator").getBytes();
41
42     /**
43      * @supplierCardinality 0..*
44      */

45     private EventGateway parent = null;
46     private List gateways = new ArrayList();
47     private List interests = null;
48     private List aliases = null;
49
50     /**
51      * Set the parent event gateway. Null indicates its the root.
52      *
53      * @param eg the parent event gateway
54      */

55     public final void setParent(EventGateway eg) {
56         parent = eg;
57     }
58     
59     /**
60      * Get the parent event gateway. Returns null if it's the root.
61      *
62      * @return the parent event gateway
63      */

64     public final EventGateway getParent() {
65         return parent;
66     }
67     
68     /**
69      * Add an event gateway to this one
70      *
71      * @param eg the event gateway to be added
72      */

73     public final void add(EventGateway eg) {
74         if (eg==null) return;
75         if (logger.isInfoEnabled()) logger.info(this+" adding "+eg);
76         gateways.add(eg);
77         eg.setParent(this);
78     }
79     
80     /**
81      * Remove an event gateway from this one
82      *
83      * @param eg the event gateway to be removed
84      */

85     public final void remove(EventGateway eg) {
86         if (eg==null) return;
87         if (logger.isInfoEnabled()) logger.info(this+" removing "+eg);
88         gateways.remove(eg);
89         eg.setParent(null);
90     }
91     
92     /**
93      * Get a list of child gateways. The list returned is a copy of the
94      * underlying child gateway list.
95      *
96      * @return a list of child gateways
97      */

98     public List getChildren() {
99         return new ArrayList(gateways);
100     }
101
102     /**
103      * Ask all interested parties to register with
104      * the EventBroker
105      *
106      * @param eb the event broker this gateway should use to
107      * register for events
108      */

109     public final void register(EventBroker eb) {
110         if (logger.isInfoEnabled()) logger.info(this+" registering with EventBroker");
111         
112         //register any specified interests
113
if (interests!=null) {
114             Iterator it = interests.iterator();
115             while (it.hasNext()) {
116                 Interest interest = (Interest) it.next();
117                 try {
118                     if (interest.eventClass!=null) eb.addEventListener(interest.factory, interest.eventClass);
119                     else eb.addEventListener(interest.factory);
120                 } catch (org.enhydra.barracuda.core.event.InvalidClassException e) {}
121             }
122         }
123         
124         //register any specified aliases
125
if (aliases!=null) {
126             Iterator it = aliases.iterator();
127             while (it.hasNext()) {
128                 Class JavaDoc event = (Class JavaDoc) it.next();
129                 try {eb.addEventAlias(event);}
130                 catch (org.enhydra.barracuda.core.event.InvalidClassException e) {}
131             }
132             aliases = null;
133         }
134         
135         //register local interests
136
if (logger.isDebugEnabled()) logger.debug(this+"...registering local interests ");
137         registerLocalEventInterests(eb);
138
139         //register local aliases
140
if (logger.isDebugEnabled()) logger.debug(this+"...registering local aliases");
141         registerLocalEventAliases(eb);
142
143         //register child gateways
144
if (logger.isDebugEnabled()) logger.debug(this+"...registering children ");
145         Iterator it = gateways.iterator();
146         while (it.hasNext()) {
147             EventGateway eg = (EventGateway) it.next();
148             if (logger.isDebugEnabled()) logger.debug(this+"...found child gateway:"+eg);
149             eg.register(eb);
150         }
151
152         if (logger.isInfoEnabled()) logger.info("Registration complete!");
153     }
154
155     /**
156      * Register any local interests in the EventBroker
157      *
158      * @param eb the event broker this gateway should use to
159      * register for local events
160      */

161     public void registerLocalEventInterests(EventBroker eb) {
162         //this is where users would extend and override
163
}
164
165     /**
166      * Register any local event aliases in the EventBroker
167      *
168      * @param eb the event broker this gateway should use to
169      * register aliases for local events
170      */

171     public void registerLocalEventAliases(EventBroker eb) {
172         //this is where users would extend and override
173
}
174
175     /**
176      * Rather than overriding the registerLocalEventInterests
177      * method, you can just invoke this method instead for each
178      * interest you'd like to register. The gateway will keep track
179      * of all factories specified, and register/deregister when
180      * appropriate (so you don't have to worry about it). Notice that
181      * this method registers just the listener id (not for a specific
182      * class of event).
183      *
184      * The only real reason for using the registerLocalEventInterests
185      * method would be if you actually need access to the EventBroker.
186      *
187      * Note that if the event class is not an instance of BaseEvent, the
188      * request is just silently ignored (unlike the event broker, which
189      * throws an exception).
190      *
191      * @param factory the factory we wish to register with the event broker
192      */

193     public final void specifyLocalEventInterests(ListenerFactory factory) {
194         specifyLocalEventInterests(factory, null);
195     }
196     
197     /**
198      * Rather than overriding the registerLocalEventInterests
199      * method, you can just invoke this method instead for each
200      * interest you'd like to register. The gateway will keep track
201      * of all factories specified, and register/deregister when
202      * appropriate (so you don't have to worry about it).
203      *
204      * The only real reason for using the registerLocalEventInterests
205      * method would be if you actually need access to the EventBroker.
206      *
207      * Note that if the event class is not an instance of BaseEvent, the
208      * request is just silently ignored (unlike the event broker, which
209      * throws an exception).
210      *
211      * @param factory the factory we wish to register with the event broker
212      * @param event the class of events we are interested in
213      */

214     public final void specifyLocalEventInterests(ListenerFactory factory, Class JavaDoc event) {
215         if (logger.isDebugEnabled()) logger.debug(this+" specifying event interest: "+factory+"-->"+(event!=null ? event.getName() : "null"));
216         if (interests==null) interests = new ArrayList();
217         interests.add(new Interest(factory, event));
218     }
219
220     /**
221      * Rather than overriding the registerLocalEventAliases
222      * method, you can just invoke this method instead for type
223      * of event you want to manually alias.
224      *
225      * The only real reason for using the registerLocalEventAliases
226      * method would be if you actually need access to the EventBroker.
227      *
228      * Note that if the event class is not an instance of BaseEvent, the
229      * request is just silently ignored (unlike the event broker, which
230      * throws an exception).
231      *
232      * @param event the class of events we are interested in registering
233      * aliases for
234      */

235     public final void specifyLocalEventAliases(Class JavaDoc event) {
236         if (logger.isDebugEnabled()) logger.debug(this+" specifying event alias: "+event.getName());
237         if (aliases==null) aliases = new ArrayList();
238         aliases.add(event);
239     }
240
241     /**
242      * Ask all interested parties to de-register with
243      * the EventBroker
244      *
245      * @param eb the event broker this gateway should use to
246      * de-register for events
247      */

248     public final void deregister(EventBroker eb) {
249         if (logger.isInfoEnabled()) logger.info(this+" deregistering with EventBroker");
250         
251         //deregister any specified interests
252
if (interests!=null) {
253             Iterator it = interests.iterator();
254             while (it.hasNext()) {
255                 Interest interest = (Interest) it.next();
256                 try {
257                     if (interest.eventClass!=null) eb.removeEventListener(interest.factory, interest.eventClass);
258                     else eb.removeEventListener(interest.factory);
259                 } catch (org.enhydra.barracuda.core.event.InvalidClassException e) {}
260             }
261             interests = null;
262         }
263         
264         //deregister local interests
265
if (logger.isDebugEnabled()) logger.debug(this+"...deregistering local ");
266         deregisterLocalEventInterests(eb);
267         
268         //deregister child gateways
269
if (logger.isDebugEnabled()) logger.debug(this+"...deregistering children ");
270         Iterator it = gateways.iterator();
271         while (it.hasNext()) {
272             EventGateway eg = (EventGateway) it.next();
273             if (logger.isDebugEnabled()) logger.debug(this+"...found child gateway:"+eg);
274             eg.deregister(eb);
275         }
276
277         if (logger.isInfoEnabled()) logger.info("Deregistration complete!");
278     }
279     
280     /**
281      * Deregister any local interests in the EventBroker
282      *
283      * @param eb the event broker this gateway should use to
284      * de-register for local events
285      */

286     public void deregisterLocalEventInterests(EventBroker eb) {
287         //this is where users would extend and override
288
}
289     
290     class Interest {
291         ListenerFactory factory = null;
292         Class JavaDoc eventClass = null;
293         
294         public Interest(ListenerFactory ifactory, Class JavaDoc ieventClass) {
295             factory = ifactory;
296             eventClass = ieventClass;
297         }
298         
299         public String JavaDoc toString() {
300             return "Interest {factory:"+factory+" event:"+eventClass+"}";
301         }
302     }
303
304     /**
305      * For debugging purposes. Print the basic structure of the
306      * gateway.
307      */

308     public void printStackTrace(int depth, Logger extLogger) {
309         printStackTrace(depth, extLogger, null);
310     }
311     
312     /**
313      * For debugging purposes. Print the basic structure of the
314      * gateway.
315      */

316     public void printStackTrace(int depth, OutputStream out) {
317         printStackTrace(depth, null, out);
318     }
319     
320     /**
321      * For debugging purposes. Print the basic structure of the
322      * gateway. Output is printed to output stream, or if that is null,
323      * to the default logger.
324      */

325     private void printStackTrace(int depth, Logger extLogger, OutputStream out) {
326         if (depth<0) depth = 0;
327         if (depth>25) depth = 25;
328         String JavaDoc spaces = " ";
329         String JavaDoc inset = spaces.substring(0,depth*3);
330         
331         print(extLogger, out, inset+this.getClass().getName() + "@" + Integer.toHexString(this.hashCode()));
332
333         //interests
334
print(extLogger, out, inset+" interests: "+(interests==null ? "null" : ""));
335         if (interests!=null) CollectionsUtil.printStackTrace(interests, depth+2, extLogger, out);
336         print(extLogger, out, inset+" /end interests");
337
338         //aliases
339
print(extLogger, out, inset+" aliases: "+(aliases==null ? "null" : ""));
340         if (aliases!=null) CollectionsUtil.printStackTrace(aliases, depth+2, extLogger, out);
341         print(extLogger, out, inset+" /end aliases");
342
343         //sub-gateways
344
print(extLogger, out, inset+" gateways: "+(gateways==null ? "null" : ""));
345         int cntr = -1;
346         if (gateways!=null) {
347             Iterator it = gateways.iterator();
348             while (it.hasNext()) {
349                 EventGateway eg = (EventGateway) it.next();
350                 if (eg instanceof DefaultEventGateway) {
351                     print(extLogger, out, inset+" ["+(++cntr)+"]:");
352                     ((DefaultEventGateway) eg).printStackTrace(depth+2, extLogger, out);
353                 } else {
354                     print(extLogger, out, inset+" ["+(++cntr)+"]"+eg.getClass().getName()+" (details unknown)");
355                 }
356             }
357         }
358         print(extLogger, out, inset+" /end gateways");
359
360         print(extLogger, out, inset+"/end @" + Integer.toHexString(this.hashCode()));
361     }
362
363     protected static void print(Logger extLogger, OutputStream out, String JavaDoc s) {
364         if (extLogger!=null) {
365             if (extLogger.isDebugEnabled()) extLogger.debug(s);
366         } else if (out!=null) {
367             try {
368                 out.write(s.getBytes());
369                 out.write(sep);
370             } catch (IOException ioe) {}
371         }
372     }
373 }
374
Popular Tags