KickJava   Java API By Example, From Geeks To Geeks.

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


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>Default and base implementation of the <tt>Components::CCMObject</tt> interface.
36  ** It also implements the <tt>ExecutorSegmentBase</tt> interface (as it's the main segment)
37  ** and the <tt>SessionComponent</tt> callback (as only session component are supported at
38  ** present).</p>
39  **/

40 abstract public class CCMObjectBase
41 extends org.omg.CORBA.LocalObject JavaDoc
42 implements org.omg.Components.CCMObjectOperations,
43            org.omg.Components.ExecutorSegmentBase,
44            org.omg.Components.SessionComponent,
45            java.io.Serializable JavaDoc
46 {
47     // executor
48
static private String JavaDoc _class_name = "CCMObjectBase";
49     transient private org.omg.Components.CCM2Context _context;
50     transient private org.omg.Components.Session2Context _mono_ctx;
51     transient private org.omg.Components.SegmentedSession2Context _segmented_ctx;
52     transient private org.coach.ECM.ECMContext _ecm_ctx;
53     private boolean _initialized;
54     // NOTE: should be set by subclasses
55
protected FacetSet _facets;
56     protected ReceptacleSet _receptacles;
57     protected SinkSet _sinks;
58     protected EmitterSet _emitters;
59     protected PublisherSet _publishers;
60
61     // default constructor
62
protected
63     CCMObjectBase()
64     {
65         // executor
66
_context = null;
67         _mono_ctx = null;
68         _segmented_ctx = null;
69         _ecm_ctx = null;
70         _initialized = false;
71         _facets = new FacetSet();
72         _receptacles = new ReceptacleSet();
73         _sinks = new SinkSet();
74         _emitters = new EmitterSet();
75         _publishers = new PublisherSet();
76     }
77
78     //
79
// internal operations
80
//
81

82     private org.omg.CORBA.Object JavaDoc
83     createReference(String JavaDoc tid, short fid, short sid)
84     {
85         final String JavaDoc opname = "createReference";
86         // NOTE:
87
if (_mono_ctx!=null) {
88             try {
89                 return _mono_ctx.create_with_new_target(tid);
90             } catch (org.omg.Components.IllegalState ex) {
91                 // should not happen
92
final String JavaDoc msg = "FAILED (illegal state)";
93                 TheLogger.error(_class_name, opname, msg);
94                 return null;
95             }
96         } else if (_segmented_ctx!=null) {
97             try {
98                 org.omg.Components.ComponentId cid = _segmented_ctx.get_component_id();
99                 org.omg.Components.ComponentId ncid = cid.create_with_new_target(fid, sid);
100                 return _segmented_ctx.create_ref_from_cid(tid, ncid);
101             } catch (org.omg.Components.IllegalState ex) {
102                 // should not happen
103
final String JavaDoc msg = "FAILED (illegal state)";
104                 TheLogger.error(_class_name, opname, msg);
105                 return null;
106             }
107         } else {
108             // should not happen
109
final String JavaDoc msg = "FAILED (no context)";
110             TheLogger.error(_class_name, opname, msg);
111             return null;
112         }
113     }
114
115     //
116
// abstract operations
117
//
118

119     abstract protected void
120     initializePorts();
121
122     //
123
// operation for subclasses
124
//
125

126     final protected org.coach.ECM.ECMContext
127     getECMContext()
128     {
129         return _ecm_ctx;
130     }
131
132     final protected org.omg.Components.CCM2Context
133     getCCM2Context()
134     {
135         return _context;
136     }
137
138     final protected org.omg.Components.Session2Context
139     getSession2Context()
140     {
141         return _mono_ctx;
142     }
143
144     final protected org.omg.Components.SegmentedSession2Context
145     getSegSession2Context()
146     {
147         return _segmented_ctx;
148     }
149
150     //
151
// IDL:omg.org/Components/SessionComponent:1.0
152
//
153

154     final public void
155     set_session_context(org.omg.Components.SessionContext ctx)
156     throws org.omg.Components.CCMException
157     {
158         final String JavaDoc opname = "set_session_context";
159
160         if (ctx instanceof org.omg.Components.CCM2Context) {
161             _context = (org.omg.Components.CCM2Context)ctx;
162         } else {
163             // should not happen
164
final String JavaDoc msg = "FAILED (was expecting CCM2Context: "+ctx.getClass()+")";
165             TheLogger.error(_class_name, opname, msg);
166         }
167
168         if (ctx instanceof org.omg.Components.Session2Context) {
169             _mono_ctx = (org.omg.Components.Session2Context)ctx;
170         } else if (ctx instanceof org.omg.Components.SegmentedSession2Context) {
171             _segmented_ctx = (org.omg.Components.SegmentedSession2Context)ctx;
172         } else {
173             // should not happen
174
final String JavaDoc msg = "FAILED (unsupported context: "+ctx.getClass()+")";
175             TheLogger.error(_class_name, opname, msg);
176         }
177
178         if (ctx instanceof org.coach.ECM.ECMContext) {
179             _ecm_ctx = (org.coach.ECM.ECMContext)ctx;
180         } else {
181             // should not happen
182
final String JavaDoc msg = "FAILED (was expecting ECMContext: "+ctx.getClass()+")";
183             TheLogger.error(_class_name, opname, msg);
184         }
185
186         // initialize ports
187
if (!_initialized) {
188             initializePorts();
189             // NOTE: obtain channels
190
_publishers.initializeChannel(_context);
191             _emitters.initializeChannel(_context);
192             _initialized = true;
193         }
194     }
195
196     // NOTE: may be overriden by subclasses
197
public void
198     ccm_activate()
199     throws org.omg.Components.CCMException
200     {
201     }
202
203     // NOTE: may be overriden by subclasses
204
public void
205     ccm_passivate()
206     throws org.omg.Components.CCMException
207     {
208     }
209
210     // NOTE: may be overriden by subclasses
211
public void
212     ccm_remove()
213     throws org.omg.Components.CCMException
214     {
215     }
216
217     //
218
// IDL:omg.org/Components/Events:1.0
219
//
220

221     final public org.omg.Components.EventConsumerBase
222     get_consumer(String JavaDoc name)
223     throws org.omg.Components.InvalidName
224     {
225         final String JavaDoc opname = "get_consumer";
226         // get info
227
Sink sink = _sinks.get(name);
228         if (sink==null) {
229             throw new org.omg.Components.InvalidName();
230         }
231
232         // create reference
233
org.omg.CORBA.Object JavaDoc obj = createReference(sink.type_id, sink.facet_id, sink.segment_id);
234         return org.omg.Components.EventConsumerBaseHelper.unchecked_narrow(obj);
235     }
236
237     final public org.omg.Components.Cookie
238     subscribe(String JavaDoc name,
239               org.omg.Components.EventConsumerBase consumer)
240     throws org.omg.Components.InvalidName,
241            org.omg.Components.InvalidConnection,
242            org.omg.Components.ExceededConnectionLimit
243     {
244         final String JavaDoc opname = "subscribe";
245         // get info
246
Publisher pub = _publishers.get(name);
247         if (pub==null) {
248             throw new org.omg.Components.InvalidName();
249         }
250
251         // check connection type id
252
if (!consumer._is_a(pub.type_id)) {
253             throw new org.omg.Components.InvalidConnection();
254         }
255
256         // check connection limit
257
// NOTE: how?
258

259         // connect to channel
260
org.omg.Components.Cookie ck = null;
261         try {
262             ck = _context.get_events().subscribe(consumer, pub.channel_id);
263         } catch (org.omg.Components.InvalidChannel ex) {
264             // NOTE: should not happen
265
TheLogger.error(_class_name, opname, "FAILED", ex);
266         }
267
268         return ck;
269     }
270
271     final public org.omg.Components.EventConsumerBase
272     unsubscribe(String JavaDoc name, org.omg.Components.Cookie ck)
273     throws org.omg.Components.InvalidName,
274            org.omg.Components.InvalidConnection
275     {
276         final String JavaDoc opname = "unsubscribe";
277         // get info
278
Publisher pub = _publishers.get(name);
279         if (pub==null) {
280             throw new org.omg.Components.InvalidName();
281         }
282
283         // disconnect from channel
284
org.omg.Components.EventConsumerBase consumer = null;
285         try {
286             consumer = _context.get_events().unsubscribe(ck);
287         } catch (org.omg.Components.InvalidSubscription ex) {
288             throw new org.omg.Components.InvalidConnection();
289         }
290
291         return consumer;
292     }
293
294     final public void
295     connect_consumer(String JavaDoc name,
296                      org.omg.Components.EventConsumerBase consumer)
297     throws org.omg.Components.InvalidName,
298            org.omg.Components.AlreadyConnected,
299            org.omg.Components.InvalidConnection
300     {
301         final String JavaDoc opname = "connect_consumer";
302         // get info
303
Emitter emitter = _emitters.get(name);
304         if (emitter==null) {
305             throw new org.omg.Components.InvalidName();
306         }
307
308         // check connection type id
309
if (!consumer._is_a(emitter.type_id)) {
310             throw new org.omg.Components.InvalidConnection();
311         }
312
313         // check if connected
314
if (emitter.cookie!=null) {
315             throw new org.omg.Components.AlreadyConnected();
316         }
317
318         // obtain channel and connect
319
try {
320             emitter.cookie = _context.get_events().subscribe(consumer, emitter.channel_id);
321         } catch (org.omg.Components.InvalidChannel ex) {
322             // NOTE: should not happen
323
TheLogger.error(_class_name, opname, "FAILED", ex);
324         }
325     }
326
327     final public org.omg.Components.EventConsumerBase
328     disconnect_consumer(String JavaDoc name)
329     throws org.omg.Components.InvalidName,
330            org.omg.Components.NoConnection
331     {
332         final String JavaDoc opname = "disconnect_consumer";
333         // get info
334
Emitter emitter = _emitters.get(name);
335         if (emitter==null) {
336             throw new org.omg.Components.InvalidName();
337         }
338
339         // check cookie validity
340
if (emitter.cookie==null) {
341             throw new org.omg.Components.NoConnection();
342         }
343
344         // disconnect from channel
345
org.omg.Components.EventConsumerBase consumer = null;
346         try {
347             consumer = _context.get_events().unsubscribe(emitter.cookie);
348             emitter.cookie = null;
349         } catch (org.omg.Components.InvalidSubscription ex) {
350             throw new org.omg.Components.NoConnection();
351         }
352
353         return consumer;
354     }
355
356     final public org.omg.Components.ConsumerDescription[]
357     get_all_consumers()
358     {
359         final String JavaDoc opname = "get_all_consumers";
360         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
361         TheLogger.log(_class_name, opname, msg);
362
363         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
364     }
365
366     final public org.omg.Components.ConsumerDescription[]
367     get_named_consumers(String JavaDoc[] names)
368     throws org.omg.Components.InvalidName
369     {
370         final String JavaDoc opname = "get_named_consumers";
371         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
372         TheLogger.log(_class_name, opname, msg);
373
374         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
375     }
376
377     final public org.omg.Components.EmitterDescription[]
378     get_all_emitters()
379     {
380         final String JavaDoc opname = "get_all_emitters";
381         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
382         TheLogger.log(_class_name, opname, msg);
383
384         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
385     }
386
387     final public org.omg.Components.EmitterDescription[]
388     get_named_emitters(String JavaDoc[] names)
389     throws org.omg.Components.InvalidName
390     {
391         final String JavaDoc opname = "get_named_emitters";
392         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
393         TheLogger.log(_class_name, opname, msg);
394
395         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
396     }
397
398     final public org.omg.Components.PublisherDescription[]
399     get_all_publishers()
400     {
401         final String JavaDoc opname = "get_all_publishers";
402         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
403         TheLogger.log(_class_name, opname, msg);
404
405         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
406     }
407
408     final public org.omg.Components.PublisherDescription[]
409     get_named_publishers(String JavaDoc[] names)
410     throws org.omg.Components.InvalidName
411     {
412         final String JavaDoc opname = "get_named_publishers";
413         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
414         TheLogger.log(_class_name, opname, msg);
415
416         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
417     }
418
419     //
420
// IDL:omg.org/Components/Receptacles:1.0
421
//
422

423     final public org.omg.Components.Cookie
424     connect(String JavaDoc name, org.omg.CORBA.Object JavaDoc connection)
425     throws org.omg.Components.InvalidName,
426            org.omg.Components.InvalidConnection,
427            org.omg.Components.AlreadyConnected,
428            org.omg.Components.ExceededConnectionLimit
429     {
430         final String JavaDoc opname = "connect";
431         // get info
432
Receptacle rec = _receptacles.get(name);
433         if (rec==null) {
434             throw new org.omg.Components.InvalidName();
435         }
436
437         // check limit in simple case
438
if (!rec.is_multiple && (rec.cookie!=null)) {
439             throw new org.omg.Components.AlreadyConnected();
440         }
441
442         // check limit in multiple case
443
// TODO: how?
444

445         // check connection type id
446
if (!connection._is_a(rec.type_id)) {
447             throw new org.omg.Components.InvalidConnection();
448         }
449
450         // connect
451
org.omg.Components.Cookie ck = _context.get_receptacles().connect(rec.name, connection);
452
453         // store
454
if (!rec.is_multiple) {
455             rec.cookie = ck;
456         }
457
458         return ck;
459     }
460
461     final public org.omg.CORBA.Object JavaDoc
462     disconnect(String JavaDoc name, org.omg.Components.Cookie ck)
463     throws org.omg.Components.InvalidName,
464            org.omg.Components.InvalidConnection,
465            org.omg.Components.CookieRequired,
466            org.omg.Components.NoConnection
467     {
468         final String JavaDoc opname = "disconnect";
469         // get info
470
Receptacle rec = _receptacles.get(name);
471         if (rec==null) {
472             throw new org.omg.Components.InvalidName();
473         }
474
475         // check if connected in simple case
476
if (!rec.is_multiple && (rec.cookie==null)) {
477             throw new org.omg.Components.NoConnection();
478         }
479
480         // check ck in multiple case
481
if (rec.is_multiple && (ck==null)) {
482             throw new org.omg.Components.CookieRequired();
483         }
484
485         // check cookie validity
486
org.omg.Components.Cookie rck = null;
487         if (!rec.is_multiple) {
488             rck = rec.cookie;
489             rec.cookie = null;
490         } else {
491             rck = ck;
492         }
493
494         if (rck==null) {
495             throw new org.omg.Components.InvalidConnection();
496         }
497
498         // remove connection
499
org.omg.CORBA.Object JavaDoc obj = null;
500         try {
501             obj = _context.get_receptacles().disconnect(rck);
502         } catch (org.omg.Components.InvalidSubscription ex) {
503             // NOTE: shoud not happen
504
TheLogger.error(_class_name, opname, "FAILED", ex);
505         }
506
507         // check validity
508
if (obj==null) {
509             // NOTE: shoud not happen
510
TheLogger.error(_class_name, opname, "FAILED (object is null)");
511         }
512
513         return obj;
514     }
515
516     final public org.omg.Components.ConnectionDescription[]
517     get_connections(String JavaDoc name)
518     throws org.omg.Components.InvalidName
519     {
520         final String JavaDoc opname = "get_connections";
521         // get info
522
Receptacle rec = _receptacles.get(name);
523         if (rec==null) {
524             throw new org.omg.Components.InvalidName();
525         }
526
527         // get connections
528
org.omg.Components.LocalConnection[] cnxs = null;
529         try {
530             cnxs = _context.get_receptacles().get_connections(rec.name);
531         } catch (org.omg.Components.InvalidName ex) {
532             // NOTE: shoud not happen
533
TheLogger.error(_class_name, opname, "FAILED", ex);
534         }
535
536         // check length
537
if (cnxs.length==0) {
538             return new org.omg.Components.ConnectionDescription[0];
539         }
540
541         // map
542
org.omg.Components.ConnectionDescription[] descrs = null;
543         if (!rec.is_multiple) {
544             // NOTE: only one connection at a time for a simple receptacle
545
descrs = new org.omg.Components.ConnectionDescription[1];
546             descrs[0] = org.objectweb.ccm.runtime.ConnectionDescriptionFactoryImpl.create_valuetype();
547             descrs[0].ck = rec.cookie;
548             descrs[0].objref = cnxs[0].ref();
549         } else {
550             descrs = new org.omg.Components.ConnectionDescription[cnxs.length];
551             for (int i=0;i<cnxs.length;i++) {
552                 descrs[i] = org.objectweb.ccm.runtime.ConnectionDescriptionFactoryImpl.create_valuetype();
553                 descrs[i].ck = cnxs[i].ck();
554                 descrs[i].objref = cnxs[i].ref();
555             }
556         }
557
558         return descrs;
559     }
560
561     final public org.omg.Components.ReceptacleDescription[]
562     get_all_receptacles()
563     {
564         final String JavaDoc opname = "get_all_receptacles";
565         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
566         TheLogger.log(_class_name, opname, msg);
567
568         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
569     }
570
571     final public org.omg.Components.ReceptacleDescription[]
572     get_named_receptacles(String JavaDoc[] names)
573     throws org.omg.Components.InvalidName
574     {
575         final String JavaDoc opname = "get_named_receptacles";
576         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
577         TheLogger.log(_class_name, opname, msg);
578
579         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
580     }
581
582     //
583
// IDL:omg.org/Components/Navigation:1.0
584
//
585

586     final public org.omg.CORBA.Object JavaDoc
587     provide_facet(String JavaDoc name)
588     throws org.omg.Components.InvalidName
589     {
590         final String JavaDoc opname = "get_consumer";
591         // get info
592
Facet facet = _facets.get(name);
593         if (facet==null) {
594             throw new org.omg.Components.InvalidName();
595         }
596
597         // create reference
598
return createReference(facet.type_id, facet.facet_id, facet.segment_id);
599     }
600
601     final public org.omg.Components.FacetDescription[]
602     get_all_facets()
603     {
604         final String JavaDoc opname = "get_all_facets";
605         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
606         TheLogger.log(_class_name, opname, msg);
607
608         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
609     }
610
611     final public org.omg.Components.FacetDescription[]
612     get_named_facets(String JavaDoc[] names)
613     throws org.omg.Components.InvalidName
614     {
615         final String JavaDoc opname = "get_named_facets";
616         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
617         TheLogger.log(_class_name, opname, msg);
618
619         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
620     }
621
622     final public boolean
623     same_component(org.omg.CORBA.Object JavaDoc objref)
624     {
625         final String JavaDoc opname = "same_component";
626         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
627         TheLogger.log(_class_name, opname, msg);
628
629         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
630     }
631
632     //
633
// IDL:omg.org/Components/CCMObject:1.0
634
//
635

636     final public org.omg.CORBA.IRObject JavaDoc
637     get_component_def()
638     {
639         final String JavaDoc opname = "get_component_def";
640         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
641         TheLogger.log(_class_name, opname, msg);
642
643         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
644     }
645
646     final public org.omg.Components.CCMHome
647     get_ccm_home()
648     {
649         return _context.get_CCM_home();
650     }
651
652     final public org.omg.Components.PrimaryKeyBase
653     get_primary_key()
654     throws org.omg.Components.NoKeyAvailable
655     {
656         throw new org.omg.Components.NoKeyAvailable();
657     }
658
659     // NOTE: may be overriden by subclasses
660
public void
661     configuration_complete()
662     throws org.omg.Components.InvalidConfiguration
663     {
664     }
665
666     final public void
667     remove()
668     throws org.omg.Components.RemoveFailure
669     {
670         // nothing to do as the destruction of the reference is done in the home
671
}
672
673     final public org.omg.Components.ComponentPortDescription
674     get_all_ports()
675     {
676         final String JavaDoc opname = "get_all_ports";
677         final String JavaDoc msg = "FAILED (op not impl, rethrown)";
678         TheLogger.log(_class_name, opname, msg);
679
680         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
681     }
682 }
683
Popular Tags