KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > IDL3 > ComponentDeclImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@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): Philippe Merle, Mathieu Vadet.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.IDL3;
28
29 /**
30  * This class implements a component declaration.
31  *
32  * @author <a=href="Philippe.Merle@lifl.fr">Philippe Merle</a>
33  * <a=href="Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
34  *
35  * @version 0.3
36  */

37
38 public class ComponentDeclImpl
39        extends InterfaceDeclImpl
40        implements ComponentRef, ComponentDecl
41 {
42     // ==================================================================
43
//
44
// Internal state.
45
//
46
// ==================================================================
47

48     /**
49      ** Reference to the CORBA 3.0 ComponentDef.
50      **/

51     private org.omg.CORBA.ComponentIR.ComponentDef component_def_;
52
53     /**
54      ** Its base component.
55      **/

56     private ComponentDeclImpl base_component_;
57
58     /**
59      **
60      **/

61     private org.objectweb.ccm.util.Vector supported_interfaces_;
62
63     /**
64      **
65      **/

66     private InterfaceDeclImpl client_mapping_;
67
68     /**
69      **
70      **/

71     private InterfaceDeclImpl local_context_mapping_;
72
73     /**
74      **
75      **/

76     private InterfaceDeclImpl local_monolithic_mapping_;
77
78     /**
79      **
80      **/

81     private InterfaceDeclImpl local_main_mapping_;
82
83     // ==================================================================
84
//
85
// Constructor.
86
//
87
// ==================================================================
88

89     /**
90      ** The constructor with the parent scope.
91      **
92      ** @param parent The parent scope of the declaration.
93      **/

94     protected
95     ComponentDeclImpl(Repository rep, ScopeImpl parent)
96     {
97         // Call the InterfaceDeclImpl constructor.
98
super(rep, parent);
99
100         // Init internal state.
101
component_def_ = null;
102         base_component_ = null;
103         client_mapping_ = null;
104         local_context_mapping_ = null;
105         local_monolithic_mapping_ = null;
106         local_main_mapping_ = null;
107         supported_interfaces_ = new org.objectweb.ccm.util.Vector();
108         the_declaration_kind_ = DeclarationKind._dk_component;
109     }
110
111     // ==================================================================
112
//
113
// Internal methods.
114
//
115
// ==================================================================
116

117     /**
118      ** Obtain its InterfaceDef[].
119      **
120      ** @return An array containing it's supported InterfaceDef.
121      **/

122     protected org.omg.CORBA.InterfaceDef JavaDoc[]
123     getInterfaceDefs()
124     {
125         org.omg.CORBA.InterfaceDef JavaDoc[] result =
126             new org.omg.CORBA.InterfaceDef JavaDoc[supported_interfaces_.size()];
127
128         for(int i=0; i<result.length; i++)
129         {
130             result[i] = ((InterfaceRef)(supported_interfaces_.get(i))).getExtInterfaceDef();
131         }
132
133         return result;
134     }
135
136     /**
137      ** Obtain the inherited component base.
138      **
139      ** @return The inherited component base.
140      **/

141     protected org.omg.CORBA.ComponentIR.ComponentDef
142     getBaseComponentDef()
143     {
144         return (base_component_ != null)
145          ? (base_component_.getComponentDef()) : null;
146     }
147
148     /**
149      ** Loads infos of the CORBA 3.0 ComponentDef.
150      **
151      ** @param component_def The ComponentDef to load.
152      **/

153     protected void
154     load(org.omg.CORBA.Contained JavaDoc contained)
155     {
156         component_def_ = org.omg.CORBA.ComponentIR.ComponentDefHelper.narrow(contained);
157         if (component_def_.base_component()!=null)
158             setBaseComponent(
159                 (ComponentRef)getRepository().lookupId(component_def_.base_component().id()));
160
161         org.omg.CORBA.InterfaceDef JavaDoc[] supported = component_def_.supported_interfaces();
162         for (int i=0;i<supported.length;i++)
163             // WARNING: This will break consistency of array items!!!
164
addSupportedInterface((InterfaceRef)getRepository().lookupId(supported[i].id()));
165
166         // load it's mapped declarations
167
getClientMapping();
168         getLocalContextMapping();
169
170         super.load(contained);
171     }
172
173     /**
174      ** Find a declaration into inherited interfaces.
175      **
176      ** @param name The name of the searched declaration.
177      ** @return The declaration that was searched
178      ** or null if it does not exist.
179      **/

180     protected Declaration
181     findInSupportedInterfaces(String JavaDoc name)
182     {
183         for(int i=0; i<supported_interfaces_.size(); i++)
184         {
185             Declaration decl = ((InterfaceDecl)(supported_interfaces_.get(i))).find(name);
186             // If found then returns it.
187
if (decl != null) return decl;
188         }
189
190         // Else not found.
191
return null;
192     }
193
194     // ==================================================================
195
//
196
// Methods for the Declaration interface.
197
//
198
// ==================================================================
199

200     /**
201      ** Find a declaration.
202      **
203      ** Note that the declaration is also recursively searched in the
204      ** parent scope.
205      **
206      ** @param name The name of the searched declaration.
207      **
208      ** @return The declaration that was searched
209      ** or null if it does not exist.
210      **/

211     public Declaration
212     find(String JavaDoc name)
213     {
214         // Finds in the scope.
215
Declaration decl = super.find(name);
216         // If found then returns it.
217
if(decl != null) return decl;
218
219         // If not found then finds in base component.
220
if (base_component_ != null)
221         {
222             decl = base_component_.find(name);
223             // If found then returns it.
224
if(decl != null) return decl;
225         }
226         // If not found then finds in supported interfaces.
227
return findInSupportedInterfaces(name);
228     }
229
230     /**
231      ** Obtain the declaration external dependencies.
232      ** Note: for scopes, contained objects are not considered
233      ** as dependencies.
234      **
235      ** @return The list of dependencies as an array of Declaration.
236      **/

237     public Declaration[]
238     getDependencies()
239     {
240         if (dependencies_!=null)
241             return dependencies_;
242
243         dependencies_ = new Declaration[0];
244         org.objectweb.ccm.util.Vector comp_depend = new org.objectweb.ccm.util.Vector();
245         Declaration[] depend = null;
246
247         // base component
248
if (getBaseComponent()!=null)
249         {
250             comp_depend.add(getBaseComponent());
251             depend = getBaseComponent().getDependencies();
252             for (int j=0;j<depend.length;j++)
253             {
254                 if (depend[j]!=this)
255                     comp_depend.add(depend[j]);
256             }
257         }
258
259         // supported interfaces
260
InterfaceDecl[] supp = getSupportedInterfaces();
261         for (int i=0;i<supp.length;i++)
262         {
263             comp_depend.add(supp[i]);
264             depend = supp[i].getDependencies();
265             for (int j=0;j<depend.length;j++)
266             {
267                 if ((depend[j]!=this) &&
268                     (comp_depend.indexOf(depend[j])==-1))
269                     comp_depend.add(depend[j]);
270             }
271         }
272
273         // contents
274
Declaration[] decls = getContents(true, org.objectweb.ccm.IDL3.DeclarationKind._dk_all);
275         for (int i=0;i<decls.length;i++)
276         {
277             depend = decls[i].getDependencies();
278             for (int j=0;j<depend.length;j++)
279             {
280                 if ((!containsDecl(depend[j])) &&
281                     (depend[j]!=this) &&
282                     (comp_depend.indexOf(depend[j])==-1))
283                     comp_depend.add(depend[j]);
284             }
285         }
286
287         dependencies_ = (Declaration[])comp_depend.toArray(new Declaration[0]);
288         return dependencies_;
289     }
290
291     // ==================================================================
292
//
293
// Methods for the TypeRef interface.
294
//
295
// ==================================================================
296

297     /**
298      ** Obtain its IDLType reference.
299      **
300      ** @return The IDLType associated with the component declaration.
301      **/

302     public org.omg.CORBA.IDLType JavaDoc
303     getIDLType()
304     {
305         return component_def_;
306     }
307
308     /**
309      **
310      **/

311     public int
312     getTypeKind()
313     {
314         return TypeKind._tk_component;
315     }
316
317     // ==================================================================
318
//
319
// Methods for the InterfaceRef interface.
320
//
321
// ==================================================================
322

323     /**
324      ** Obtain its InterfaceDef reference.
325      **
326      ** @return The InterfaceDef object associated with the component
327      ** declaration.
328      **/

329     public org.omg.CORBA.ExtInterfaceDef
330     getExtInterfaceDef()
331     {
332         return component_def_;
333     }
334
335     /**
336      ** Obtain its InterfaceDef reference.
337      **
338      ** @return The InterfaceDef object associated with the component
339      ** declaration.
340      **/

341     public org.omg.CORBA.InterfaceDef JavaDoc
342     getInterfaceDef()
343     {
344         return component_def_;
345     }
346
347     // ==================================================================
348
//
349
// Methods for the ComponentRef interface.
350
//
351
// ==================================================================
352

353     /**
354      ** Obtain its ComponentDef reference.
355      **
356      ** @return The ComponentDef object associated with the component
357      ** declaration.
358      **/

359     public org.omg.CORBA.ComponentIR.ComponentDef
360     getComponentDef()
361     {
362         return component_def_;
363     }
364
365     // ==================================================================
366
//
367
// Methods for the InterfaceDecl interface.
368
//
369
// ==================================================================
370

371     // ==================================================================
372
//
373
// Methods for the ComponentDecl interface.
374
//
375
// ==================================================================
376

377     /**
378      ** Add an interface.
379      **
380      ** @param itf The supported interface to add.
381      **/

382     public void
383     addSupportedInterface(InterfaceRef itf)
384     {
385         if(itf != null)
386         {
387             itf.addRef();
388             supported_interfaces_.add(itf);
389         }
390     }
391
392     /**
393      ** Set the base component.
394      **
395      ** @param base_component The base component of the component.
396      ** Note that if more then one base component
397      ** is set, only the first one is considered.
398      **/

399     public void
400     setBaseComponent(ComponentRef base_component)
401     {
402         if (base_component != null)
403         {
404             base_component_ = (ComponentDeclImpl)base_component;
405             base_component_.addRef();
406         }
407     }
408
409     /**
410      **
411      **/

412     public ComponentDecl
413     getBaseComponent()
414     {
415         return base_component_;
416     }
417
418     /**
419      **
420      **/

421     public InterfaceDecl[]
422     getSupportedInterfaces()
423     {
424         InterfaceDecl[] result = new InterfaceDecl[supported_interfaces_.size()];
425
426         for(int i=0; i<result.length; i++)
427             result[i] = (InterfaceDecl)supported_interfaces_.get(i);
428
429         return result;
430     }
431
432     /**
433      **
434      **/

435     public InterfaceDecl
436     getClientMapping()
437     {
438         if (client_mapping_!=null)
439             return client_mapping_;
440
441         // load the client mapping for this component.
442
client_mapping_ = (InterfaceDeclImpl)getRepository().loadMapping(getParent(), getId());
443
444         return client_mapping_;
445     }
446
447     /**
448      **
449      **/

450     public InterfaceDecl
451     getLocalContextMapping()
452     {
453         if (local_context_mapping_!=null)
454             return local_context_mapping_;
455
456         // load the context mapping for this component.
457
String JavaDoc parent_base_id = the_parent_.getId();
458         int idx = parent_base_id.lastIndexOf(':');
459         parent_base_id = parent_base_id.substring(0, idx);
460         String JavaDoc id = parent_base_id + "/CCM_"+getName()+"_Context:"+getVersion();
461
462         local_context_mapping_ = (InterfaceDeclImpl)getRepository().loadMapping(getParent(), id);
463         return local_context_mapping_;
464     }
465
466     /**
467      **
468      **/

469     public InterfaceDecl
470     getLocalMonolithicMapping()
471     {
472         if (local_monolithic_mapping_!=null)
473             return local_monolithic_mapping_;
474
475         // load the monolithic mapping for this component.
476
String JavaDoc parent_base_id = the_parent_.getId();
477         int idx = parent_base_id.lastIndexOf(':');
478         parent_base_id = parent_base_id.substring(0, idx);
479         String JavaDoc id = parent_base_id + "/CCM_"+getName()+":"+getVersion();
480
481         local_monolithic_mapping_ = (InterfaceDeclImpl)getRepository().loadMapping(getParent(), id);
482         return local_monolithic_mapping_;
483     }
484
485     /**
486      **
487      **/

488     public InterfaceDecl
489     getLocalMainMapping()
490     {
491         if (local_main_mapping_!=null)
492             return local_main_mapping_;
493
494         // load the main mapping for this component.
495
String JavaDoc parent_base_id = the_parent_.getId();
496         int idx = parent_base_id.lastIndexOf(':');
497         parent_base_id = parent_base_id.substring(0, idx);
498         String JavaDoc id = parent_base_id + "/CCM_"+getName()+"_Executor:"+getVersion();
499
500         local_main_mapping_ = (InterfaceDeclImpl)getRepository().loadMapping(getParent(), id);
501         return local_main_mapping_;
502     }
503
504     // ==================================================================
505
//
506
// Methods for the inherited DeclarationImpl class.
507
//
508
// ==================================================================
509

510     /**
511      **
512      **/

513     public void
514     destroy()
515     {
516         if (base_component_!=null)
517             base_component_.removeRef();
518
519         InterfaceDecl[] itfs = getSupportedInterfaces();
520         for (int i=0;i<itfs.length;i++)
521             ((InterfaceRef)itfs[i]).removeRef();
522
523         super.destroy();
524     }
525
526     /**
527      ** Obtain its CORBA 3.0 Contained reference.
528      **
529      ** @return The Contained object associated with the component
530      ** declaration.
531      **/

532     protected org.omg.CORBA.Contained JavaDoc
533     getContained()
534     {
535        return component_def_;
536     }
537
538     // ==================================================================
539
//
540
// Methods for the inherited ScopeImpl class.
541
//
542
// ==================================================================
543

544     /**
545      ** Obtain its CORBA 3.0 Container reference.
546      **
547      ** @return The Container object associated with the component
548      ** declaration.
549      **/

550     protected org.omg.CORBA.Container JavaDoc
551     getContainer()
552     {
553        return component_def_;
554     }
555
556     /**
557      ** To obtain all the contained Declaration objects.
558      **
559      ** @param exclude_inherited If false return also objects contained in inherited scopes.
560      ** @param limited_types A logical combination of DeclarationKind.
561      **
562      ** @return An array of Declaration objects.
563      **/

564     public Declaration[]
565     getContents(boolean exclude_inherited, int limited_types)
566     {
567         if (exclude_inherited)
568             return super.getContents(exclude_inherited, limited_types);
569
570         Declaration[] res = super.getContents(exclude_inherited, limited_types);
571         InterfaceDecl[] itfs = getSupportedInterfaces();
572         for (int i=0;i<itfs.length;i++)
573         {
574             Declaration[] tmp1 = itfs[i].getContents(exclude_inherited, limited_types);
575             Declaration[] tmp2 = res;
576             res = new Declaration[tmp1.length+tmp2.length];
577             System.arraycopy(tmp2, 0, res, 0, tmp2.length);
578             System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
579         }
580
581         if (base_component_==null)
582             return res;
583
584         Declaration[] tmp1 = base_component_.getContents(exclude_inherited, limited_types);
585         Declaration[] tmp2 = res;
586         res = new Declaration[tmp1.length+tmp2.length];
587         System.arraycopy(tmp2, 0, res, 0, tmp2.length);
588         System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
589         return res;
590     }
591
592     // ==================================================================
593
//
594
// Methods for the inherited ForwardScopeImpl class.
595
//
596
// ==================================================================
597

598     /**
599      ** Create the container object.
600      **/

601     protected void
602     createContainer()
603     {
604         component_def_ = the_parent_.getComponentContainer().create_component(getId(),
605                                                                               getName(),
606                                                                               getVersion(),
607                                                                               getBaseComponentDef(),
608                                                                               getInterfaceDefs());
609     }
610
611     /**
612      ** Complete the container object.
613      **/

614     protected void
615     completeContainer()
616     {
617         org.omg.CORBA.ComponentIR.ComponentDef base_component = getBaseComponentDef();
618         if (base_component != null)
619             component_def_.base_component(base_component);
620
621         org.omg.CORBA.InterfaceDef JavaDoc[] supported_interfaces = getInterfaceDefs();
622         if (supported_interfaces.length != 0)
623             component_def_.supported_interfaces(supported_interfaces);
624     }
625 }
626
Popular Tags