KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > Dispatcher


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Log;
7
8 /**
9  * Routes Events to Subscribers.
10  *
11  * @version $Id: Dispatcher.java,v 1.7 2005/02/28 12:45:59 justb Exp $
12  * @author Just van den Broecke - Just Objects &copy;
13  **/

14 public class Dispatcher implements Protocol, ConfigDefs {
15     /** Singleton pattern: single instance. */
16     private static Dispatcher instance;
17
18     static {
19         // Singleton + factory pattern: create single instance
20
// from configured class name
21
String JavaDoc className = Config.getProperty(DISPATCHER_CLASS);
22
23         try {
24             instance = (Dispatcher) Class.forName(className).newInstance();
25             Log.info("Dispatcher created className=" + className);
26         } catch (Throwable JavaDoc t) {
27             Log.fatal("Cannot instantiate Dispatcher className=" + className, t);
28         }
29     }
30
31     /** Singleton pattern: private constructor. */
32     private Dispatcher() {
33     }
34
35     /** Singleton pattern: get single instance. */
36     public static Dispatcher getInstance() {
37         return instance;
38     }
39
40     /** Send event to all subscribers. */
41     public synchronized void broadcast(Event event) {
42         // Get active sessions
43
Session[] sessions = getSessions();
44
45         // Send Event to all Subscribers
46
for (int i = 0; i < sessions.length; i++) {
47
48             // Snapshot array may not be filled entirely.
49
if (sessions[i] == null) {
50                 break;
51             }
52             sessions[i].getSubscriber().onEvent((Event) event.clone());
53         }
54     }
55
56     /** Send event to subscribers matching Event subject. */
57     public synchronized void multicast(Event event) {
58         // Get snapshot active sessions
59
Session[] sessions = getSessions();
60
61         // Send Event to all Subscribers whose Subject match Event
62
Event clonedEvent = null;
63         Subscription subscription = null;
64         Subscriber subscriber = null;
65         for (int i = 0; i < sessions.length; i++) {
66
67             // Snapshot array may not be filled entirely.
68
if (sessions[i] == null) {
69                 break;
70             }
71
72             subscriber = sessions[i].getSubscriber();
73
74             // Send only if the subscriber's criteria
75
// match the event.
76
if ((subscription = subscriber.match(event)) != null) {
77                 // Personalize event
78
clonedEvent = (Event) event.clone();
79
80                 // Set subscription id and optional label
81
clonedEvent.setField(P_SUBSCRIPTION_ID, subscription.getId());
82                 if (subscription.getLabel() != null) {
83                     event.setField(P_SUBSCRIPTION_LABEL, subscription.getLabel());
84                 }
85
86                 subscriber.onEvent(clonedEvent);
87             }
88         }
89
90     }
91
92     /** Send event to specific subscriber. */
93     public synchronized void unicast(Event event, String JavaDoc aSessionId) {
94         // Get subscriber to send event to
95
Session session = SessionManager.getInstance().getSession(aSessionId);
96         if (session == null) {
97             Log.warn("unicast: session with id=" + aSessionId + " does not exist");
98             return;
99         }
100
101         // Send Event to subscriber.
102
session.getSubscriber().onEvent((Event) event.clone());
103     }
104
105     /** Start Dispatcher. */
106     public void start() {
107         Log.info("Dispatcher started");
108     }
109
110     /** Stop Dispatcher. */
111     public void stop() {
112         // Send abort control event to all subscribers.
113
Log.info("Dispatcher stopped: broadcast abort to all subscribers");
114         broadcast(new Event(E_ABORT));
115     }
116
117     private Session[] getSessions() {
118         return SessionManager.getInstance().getSnapshot();
119     }
120 }
121
122 /*
123  * $Log: Dispatcher.java,v $
124  * Revision 1.7 2005/02/28 12:45:59 justb
125  * introduced Command class
126  *
127  * Revision 1.6 2005/02/28 09:14:55 justb
128  * sessmgr/dispatcher factory/singleton support
129  *
130  * Revision 1.5 2005/02/21 16:59:06 justb
131  * SessionManager and session lease introduced
132  *
133  * Revision 1.4 2005/02/21 11:50:46 justb
134  * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
135  *
136  * Revision 1.3 2005/02/18 12:36:47 justb
137  * changes for renaming and configurability
138  *
139  * Revision 1.2 2005/02/18 10:07:23 justb
140  * many renamings of classes (make names compact)
141  *
142  * Revision 1.1 2005/02/18 09:54:15 justb
143  * refactor: rename Publisher Dispatcher and single Subscriber class
144  *
145  * Revision 1.14 2005/02/16 14:39:34 justb
146  * fixed leave handling and added "poll" mode
147  *
148  * Revision 1.13 2004/10/24 20:50:35 justb
149  * refine subscription with label and sending sid and label on events
150  *
151  * Revision 1.12 2004/10/24 12:58:18 justb
152  * revised client and test classes for new protocol
153  *
154  * Revision 1.11 2004/09/26 21:39:43 justb
155  * allow multiple subscriptions and out-of-band requests
156  *
157  * Revision 1.10 2004/09/20 22:01:38 justb
158  * more changes for new protocol
159  *
160  * Revision 1.9 2004/09/03 22:35:37 justb
161  * Almost complete rewrite, just checking in now
162  *
163  * Revision 1.8 2004/08/13 23:36:05 justb
164  * rewrite of Pullet into Pushlet "pull" mode
165  *
166  * Revision 1.7 2004/08/12 13:18:54 justb
167  * cosmetic changes
168  *
169  * Revision 1.6 2004/03/10 15:45:55 justb
170  * many cosmetic changes
171  *
172  * Revision 1.5 2004/03/10 13:59:28 justb
173  * rewrite using Collection classes and finer synchronization
174  *
175  * Revision 1.4 2003/08/15 08:37:40 justb
176  * fix/add Copyright+LGPL file headers and footers
177  *
178  * Revision 1.3 2003/08/12 08:54:40 justb
179  * added getSubscriberCount() and use Log
180  *
181  * Revision 1.2 2003/05/18 16:15:08 justb
182  * support for XML encoded Events
183  *
184  * Revision 1.1.1.1 2002/09/24 21:02:31 justb
185  * import to sourceforge
186  *
187  * Revision 1.1.1.1 2002/09/20 22:48:18 justb
188  * import to SF
189  *
190  * Revision 1.1.1.1 2002/09/20 14:19:04 justb
191  * first import into SF
192  *
193  * Revision 1.3 2002/04/15 20:42:41 just
194  * reformatting and renaming GuardedQueue to EventQueue
195  *
196  * Revision 1.2 2000/08/21 20:48:29 just
197  * added CVS log and id tags plus copyrights
198  *
199  *
200  */

201
Popular Tags