KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > HomeLocalEventsImpl


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p></p>
36  **/

37 public class HomeLocalEventsImpl
38 {
39     // event
40
static private String JavaDoc _class_name = "HomeLocalEventsImpl";
41     private java.util.Hashtable JavaDoc _id2channel;
42     private java.util.Hashtable JavaDoc _id2inter;
43     private java.util.Hashtable JavaDoc _name2id;
44     private int _id_count;
45     private CallInterceptorFactory _factory;
46  
47     // empty constructor
48
public
49     HomeLocalEventsImpl()
50     {
51         // event
52
_id2channel = null;
53         _id2inter = null;
54         _name2id = null;
55         _id_count = 0;
56         _factory = null;
57     }
58
59     // default constructor
60
public
61     HomeLocalEventsImpl(CallInterceptorFactory ifact)
62     {
63         // event
64
_id2channel = new java.util.Hashtable JavaDoc();
65         _id2inter = new java.util.Hashtable JavaDoc();
66         _name2id = new java.util.Hashtable JavaDoc();
67         _id_count = 0;
68         _factory = ifact;
69     }
70
71     //
72
// IDL:omg.org/Components/LocalEvents:1.0
73
//
74

75     final public int
76     get_channel(byte[] cid, String JavaDoc name)
77     throws org.omg.Components.ChannelUnavailable
78     {
79         // check if a channel is already created
80
String JavaDoc rname = new String JavaDoc(cid)+":"+name;
81         Integer JavaDoc chid = (Integer JavaDoc)_name2id.get(rname);
82         if (chid!=null) {
83             return chid.intValue();
84         }
85
86         // create id
87
int nchid = _id_count++;
88
89         // create instance
90
HomeFakeEventChannel channel = new HomeFakeEventChannel(nchid);
91
92         // create interceptor if available
93
if (_factory!=null) {
94             // NOTE: used an untyped interceptor as the API of the context disable the use
95
// of typed operations (therefore a typed interceptor is useless)
96
org.omg.CORBA.Object JavaDoc inter = _factory.createUntypedStubInterceptor(name, channel);
97             _id2inter.put(new Integer JavaDoc(nchid), inter);
98         }
99
100         // store
101
_name2id.put(rname, new Integer JavaDoc(nchid));
102         _id2channel.put(new Integer JavaDoc(nchid), channel);
103
104         return nchid;
105     }
106
107     final public org.omg.Components.Cookie
108     subscribe(byte[] cid, org.omg.Components.EventConsumerBase consumer, int chid)
109     throws org.omg.Components.InvalidChannel
110     {
111         // get channel
112
HomeFakeEventChannel channel = (HomeFakeEventChannel)_id2channel.get(new Integer JavaDoc(chid));
113
114         if (channel==null) {
115             throw new org.omg.Components.InvalidChannel();
116         }
117
118         return channel.subscribe(consumer);
119     }
120
121     final public org.omg.Components.EventConsumerBase
122     unsubscribe(byte[] cid, org.omg.Components.Cookie ck)
123     throws org.omg.Components.InvalidSubscription
124     {
125         // NOTE: ck must be a CookieImpl
126
if (!(ck instanceof CookieImpl)) {
127             // NOTE: is this the right exception ? there's no other one ...
128
throw new org.omg.Components.InvalidSubscription();
129         }
130
131         // get channel id from cookie
132
CookieImpl lck = (CookieImpl)ck;
133         int chid = lck.getChannelId();
134
135         // get channel
136
HomeFakeEventChannel channel = (HomeFakeEventChannel)_id2channel.get(new Integer JavaDoc(chid));
137
138         if (channel==null) {
139             throw new org.omg.Components.InvalidSubscription();
140         }
141
142         return channel.unsubscribe(ck);
143     }
144
145     final public void
146     push(byte[] cid, org.omg.Components.EventBase evt, int chid)
147     throws org.omg.Components.InvalidChannel
148     {
149         org.omg.Components.EventConsumerBase cons = null;
150         if (_factory!=null) {
151             // use interceptor as EventConsumerBase
152
cons = (org.omg.Components.EventConsumerBase)_id2inter.get(new Integer JavaDoc(chid));
153         } else {
154             // use directly the channel
155
cons = (org.omg.Components.EventConsumerBase)_id2channel.get(new Integer JavaDoc(chid));
156         }
157
158         if (cons==null) {
159             throw new org.omg.Components.InvalidChannel();
160         }
161
162         try {
163             cons.push_event(evt);
164         } catch (org.omg.Components.BadEventType ex) {
165             // ignore
166
}
167     }
168 }
169
170
171 //
172
class HomeFakeEventChannel
173 extends org.omg.Components._EventConsumerBaseStub
174 {
175     // event channel
176
private String JavaDoc _class_name = "ECMFakeEventChannel";
177     private int _channel_id;
178     private java.util.Hashtable JavaDoc _ck2consumers;
179
180     // default constructor
181
public
182     HomeFakeEventChannel(int chid)
183     {
184         // event channel
185
_channel_id = chid;
186         _ck2consumers = new java.util.Hashtable JavaDoc();
187     }
188
189     //
190
// public operations
191
//
192

193     final public org.omg.Components.Cookie
194     subscribe(org.omg.Components.EventConsumerBase consumer)
195     {
196         // create local cookie
197
org.omg.Components.Cookie lck = new CookieImpl(_channel_id);
198
199         // store
200
_ck2consumers.put(lck, consumer);
201
202         return lck;
203     }
204
205     final public org.omg.Components.EventConsumerBase
206     unsubscribe(org.omg.Components.Cookie ck)
207     throws org.omg.Components.InvalidSubscription
208     {
209         // remove from storage
210
org.omg.Components.EventConsumerBase cons = (org.omg.Components.EventConsumerBase)_ck2consumers.remove(ck);
211
212         // check cookie
213
if (cons==null) {
214             throw new org.omg.Components.InvalidSubscription();
215         }
216
217         //
218
return cons;
219     }
220
221     //
222
// IDL:omg.org/Components/EventConsumerBase:1.0
223
//
224

225     final public void
226     push_event(org.omg.Components.EventBase evt)
227     throws org.omg.Components.BadEventType
228     {
229         org.omg.Components.EventConsumerBase[] consumers = null;
230         consumers = (org.omg.Components.EventConsumerBase[])_ck2consumers.values().toArray(new org.omg.Components.EventConsumerBase[0]);
231
232         //String opname = "push_event";
233
//String msg = "pushing events to "+consumers.length+" consumers";
234
//TheLogger.debug(_class_name, opname, msg);
235

236         // NOTE: use a worker for each consumer
237
for (int i=0;i<consumers.length;i++) {
238             new HomePushEventWorker(consumers[i], evt).start();
239         }
240     }
241 }
242
243 // NOTE: should use a thread pool
244
class HomePushEventWorker
245 extends java.lang.Thread JavaDoc
246 {
247     //
248
private String JavaDoc _class_name = "ECMPushEventWorker";
249     private org.omg.Components.EventConsumerBase _consumer;
250     private org.omg.Components.EventBase _event;
251
252     // default constructor
253
public
254     HomePushEventWorker(org.omg.Components.EventConsumerBase cons,
255                         org.omg.Components.EventBase evt)
256     {
257         //
258
_consumer = cons;
259         _event = evt;
260     }
261
262     //
263
// java.lang.Thread
264
//
265

266     final public void
267     run()
268     {
269         try {
270             _consumer.push_event(_event);
271         } catch (org.omg.Components.BadEventType ex) {
272             // ignore
273
String JavaDoc opname = "run";
274             TheLogger.debug(_class_name, opname, "IGNORE (bad event type)");
275        }
276     }
277 }
Popular Tags