KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > event > LatkaEventPublisher


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.latka.event;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * A {@link LatkaEventListener} that publishes the incoming events to other
25  * listeners
26  *
27  * @author Morgan Delagrange
28  * @author <a HREF="mailto:dion@multitask.com.au">dIon Gillard</a>
29  * @version
30  * $Id: LatkaEventPublisher.java 155424 2005-02-26 13:09:29Z dirkv $
31  */

32 public class LatkaEventPublisher implements LatkaEventListener {
33     
34     /** the listeners to publish events to */
35     protected List JavaDoc _list = new ArrayList JavaDoc();
36
37     /**
38      * add a {@link LatkaEventListener} to the list of publishees.
39      * @param listener a {@link LatkaEventListener}
40      */

41     public void addListener(LatkaEventListener listener) {
42         _list.add(listener);
43     }
44
45     /**
46      * Send the provided event object to all registered listeners
47      * @param event an event to be sent
48      */

49     public void broadcastEvent(LatkaEvent event) {
50         Iterator JavaDoc iter = _list.iterator();
51         while (iter.hasNext()) {
52             broadcastEvent(event, (LatkaEventListener) iter.next());
53         }
54     }
55
56     /**
57      * Send the provided event to the provided listener
58      * @param event an event to be sent
59      * @param listener the listener to send it to
60      */

61     protected void broadcastEvent(LatkaEvent event, LatkaEventListener listener)
62         {
63         if (event instanceof RequestSucceededEvent) {
64             listener.requestSucceeded((RequestEvent) event);
65         } else if (event instanceof RequestFailedEvent) {
66             listener.requestFailed((RequestEvent) event);
67         } else if (event instanceof RequestSkippedEvent) {
68             listener.requestSkipped((RequestEvent) event);
69         } else if (event instanceof RequestErrorEvent) {
70             listener.requestError((RequestEvent) event);
71         } else if (event instanceof SuiteCompletedEvent) {
72             listener.suiteCompleted((SuiteEvent) event);
73         } else if (event instanceof ReportMessageEvent) {
74             listener.reportMessage((ReportMessageEvent) event);
75         }
76     }
77
78     /**
79      * send the supplied event to all listeners
80      * @param event an event to be broadcast
81      */

82     public void requestSucceeded(RequestEvent event) {
83         broadcastEvent(event);
84     }
85     
86     /**
87      * send the supplied event to all listeners
88      * @param event an event to be broadcast
89      */

90     public void requestFailed(RequestEvent event) {
91         broadcastEvent(event);
92     }
93
94     /**
95      * send the supplied event to all listeners
96      * @param event an event to be broadcast
97      */

98     public void requestSkipped(RequestEvent event) {
99         broadcastEvent(event);
100     }
101
102     /**
103      * send the supplied event to all listeners
104      * @param event an event to be broadcast
105      */

106     public void requestError(RequestEvent event) {
107         broadcastEvent(event);
108     }
109
110     /**
111      * send the supplied event to all listeners
112      * @param event a message event to be broadcast
113      */

114     public void reportMessage(ReportMessageEvent event) {
115         broadcastEvent(event);
116     }
117
118     /**
119      * send the supplied event to all listeners
120      * @param event an event to be broadcast
121      */

122     public void suiteCompleted(SuiteEvent event) {
123         broadcastEvent(event);
124     }
125
126 }
127
Popular Tags