KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > ast > lib > ComponentDeclImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@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): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.ast.lib;
28
29 /** Used to access AST TypeKind. */
30 import org.objectweb.openccm.ast.api.TypeKind;
31
32 /** Used to access AST DeclarationKind. */
33 import org.objectweb.openccm.ast.api.DeclarationKind;
34
35 /** Used to access AST Declaration. */
36 import org.objectweb.openccm.ast.api.Declaration;
37
38 /** Used to access AST ComponentDecl. */
39 import org.objectweb.openccm.ast.api.ComponentDecl;
40
41 /** Used to access AST InterfaceDecl. */
42 import org.objectweb.openccm.ast.api.InterfaceDecl;
43
44 /** To use CORBA::ComponentIR::ComponentDef. */
45 import org.omg.CORBA.ComponentIR.ComponentDef;
46 import org.omg.CORBA.ComponentIR.ComponentDefHelper;
47
48 /**
49  * ComponentDeclImpl is a wrapper class for IDL component declarations.
50  *
51  *
52  * Inherits from:
53  *
54  * - ComponentBaseImpl as components are also interfaces and
55  * have an equivalent client interface mapping.
56  *
57  * - ComponentDecl: OMG IDL for component declarations.
58  *
59  *
60  * Provides:
61  *
62  * - The 'BaseComponent' property,
63  * i.e. the setBaseComponent and getBaseComponent operations.
64  *
65  * - The local context interface mapping,
66  * i.e. the getLocalContextMapping operation.
67  *
68  * - The local monolithic interface mapping,
69  * i.e. the getLocalMonolithicMapping operation.
70  *
71  * - The local main interface mapping,
72  * i.e. the getLocalMainMapping operation.
73  *
74  *
75  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
76  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
77  *
78  * @version 0.3
79  */

80
81 public class ComponentDeclImpl
82      extends ComponentBaseImpl
83   implements ComponentDecl
84 {
85     // ==================================================================
86
//
87
// Internal state.
88
//
89
// ==================================================================
90

91     /** Reference to the CORBA 3.0 ComponentDef. */
92     private ComponentDef component_def_;
93
94     /** Its base component. */
95     private ComponentDeclImpl base_component_;
96
97     /** The local context interface mapping. */
98     private InterfaceDeclImpl local_context_mapping_;
99
100     /** The local monolithic interface mapping. */
101     private InterfaceDeclImpl local_monolithic_mapping_;
102
103     /** The local main interface mapping. */
104     private InterfaceDeclImpl local_main_mapping_;
105
106     // ==================================================================
107
//
108
// Constructor.
109
//
110
// ==================================================================
111

112     /**
113      * The constructor with the parent scope.
114      *
115      * @param rep The repository of the declaration.
116      * @param parent The parent scope of the declaration.
117      */

118     protected
119     ComponentDeclImpl(Repository rep,
120                       ScopeImpl parent)
121     {
122         // Call the ComponentBaselImpl constructor.
123
super(rep, parent);
124
125         // Init internal state.
126
component_def_ = null;
127         base_component_ = null;
128         local_context_mapping_ = null;
129         local_monolithic_mapping_ = null;
130         local_main_mapping_ = null;
131     }
132
133     // ==================================================================
134
//
135
// Internal methods.
136
//
137
// ==================================================================
138

139     // ==================================================================
140
//
141
// Internal methods for DeclarationImpl.
142
//
143
// ==================================================================
144

145     /**
146      * Loads infos of the CORBA 3.0 ComponentDef.
147      *
148      * @param contained The ComponentDef to load.
149      */

150     protected void
151     load(org.omg.CORBA.Contained JavaDoc contained)
152     {
153         component_def_ = ComponentDefHelper.narrow(contained);
154
155         ComponentDef baseComponent = component_def_.base_component();
156         if (baseComponent!=null)
157             setBaseComponent(
158                 (ComponentDecl)getRepository().
159                 lookupId(baseComponent.id()));
160
161         addSupportedInterfaces(component_def_.supported_interfaces());
162
163         // load it's mapped declarations
164
getClientMapping();
165         getLocalContextMapping();
166
167         super.load(contained);
168     }
169
170     /**
171      * Obtain its CORBA 3.0 Contained reference.
172      *
173      * @return The Contained object associated with the component
174      * declaration.
175      */

176     protected org.omg.CORBA.Contained JavaDoc
177     getContained()
178     {
179        return component_def_;
180     }
181
182     // ==================================================================
183
//
184
// Internal methods for ScopeImpl.
185
//
186
// ==================================================================
187

188     /**
189      * Obtain its CORBA 3.0 Container reference.
190      *
191      * @return The Container object associated with the component
192      * declaration.
193      */

194     protected org.omg.CORBA.Container JavaDoc
195     getContainer()
196     {
197        return component_def_;
198     }
199
200     // ==================================================================
201
//
202
// Internal methods for ForwardScopeImpl.
203
//
204
// ==================================================================
205

206     // ==================================================================
207
//
208
// Internal methods for ForwardScopeIDLImpl.
209
//
210
// ==================================================================
211

212     /**
213      * Create the container object.
214      */

215     protected void
216     createContainer()
217     {
218       // System.out.println("create_component on " + the_parent_);
219
// System.out.println(getId());
220

221         component_def_ = the_parent_.getComponentContainer().
222                          create_component(getId(),
223                                           getName(),
224                                           getVersion(),
225                                           getBaseComponentDef(),
226                              supported_interfaces_.getInterfaceDefSeq());
227     }
228
229     /**
230      * Complete the container object.
231      */

232     protected void
233     completeContainer()
234     {
235         ComponentDef baseComponent = getBaseComponentDef();
236         if (baseComponent != null)
237             component_def_.base_component(baseComponent);
238
239         if (supported_interfaces_.getSize() != 0)
240             component_def_.supported_interfaces(
241                 supported_interfaces_.getInterfaceDefSeq());
242     }
243
244     // ==================================================================
245
//
246
// Public methods.
247
//
248
// ==================================================================
249

250     /**
251      * Obtain its ComponentDef reference.
252      *
253      * @return The ComponentDef object associated with the component
254      * declaration.
255      */

256     public ComponentDef
257     getComponentDef()
258     {
259         return component_def_;
260     }
261
262     /**
263      * Obtain the inherited component base.
264      *
265      * @return The inherited component base.
266      */

267     public ComponentDef
268     getBaseComponentDef()
269     {
270         return (base_component_ != null)
271          ? (base_component_.getComponentDef()) : null;
272     }
273
274     /**
275      * Obtain its InterfaceDef reference.
276      *
277      * @return The InterfaceDef object associated with the component
278      * declaration.
279      */

280     public org.omg.CORBA.ExtInterfaceDef
281     getExtInterfaceDef()
282     {
283         return component_def_;
284     }
285
286     /**
287      * Obtain its InterfaceDef reference.
288      *
289      * @return The InterfaceDef object associated with the component
290      * declaration.
291      */

292     public org.omg.CORBA.InterfaceDef JavaDoc
293     getInterfaceDef()
294     {
295         return component_def_;
296     }
297
298     // ==================================================================
299
//
300
// Methods for OMG IDL org.objectweb.openccm.ast.api.WithDependencies
301
//
302
// ==================================================================
303

304     /**
305      * Obtain the declaration external dependencies.
306      *
307      * Note: for scopes, contained objects are not considered
308      * as dependencies.
309      *
310      * @return The list of dependencies as an array of Declaration.
311      */

312     public Declaration[]
313     getDependencies()
314     {
315         if (dependencies_!=null)
316             return dependencies_;
317
318         dependencies_ = new Declaration[0];
319         java.util.List JavaDoc comp_depend = new java.util.ArrayList JavaDoc();
320         Declaration[] depend = null;
321
322         // base component
323
if (getBaseComponent()!=null)
324         {
325             comp_depend.add(getBaseComponent());
326             depend = getBaseComponent().getDependencies();
327             for (int j=0; j<depend.length; j++)
328             {
329                 if (depend[j]!=this)
330                     comp_depend.add(depend[j]);
331             }
332         }
333
334         // supported interfaces
335
InterfaceDecl[] supp = supported_interfaces_.getInterfaces();
336         for (int i=0; i<supp.length; i++)
337         {
338             comp_depend.add(supp[i]);
339             depend = supp[i].getDependencies();
340             for (int j=0; j<depend.length; j++)
341             {
342                 if ( (depend[j]!=this) &&
343                      (comp_depend.indexOf(depend[j])==-1) )
344                     comp_depend.add(depend[j]);
345             }
346         }
347
348         // contents
349
Declaration[] decls = getContents(true, DeclarationKind.dk_all);
350         for (int i=0; i<decls.length; i++)
351         {
352             depend = decls[i].getDependencies();
353             for (int j=0; j<depend.length; j++)
354             {
355                 //boolean tmp =false;
356
//if(depend[j]!=this) tmp=true;
357
if ( (!containsDecl(depend[j])) &&
358                      (depend[j]!=this) &&
359                      (comp_depend.indexOf(depend[j])==-1) )
360                 {
361                     comp_depend.add(depend[j]);
362                 }
363             }
364         }
365
366         dependencies_ = (Declaration[])comp_depend.toArray(new Declaration[0]);
367         return dependencies_;
368     }
369
370     // ==================================================================
371
//
372
// Methods for OMG IDL org.objectweb.openccm.ast.api.Declaration
373
//
374
// ==================================================================
375

376     /**
377      * Obtain its DeclarationKind.
378      *
379      * @return The DeclarationKind of the object.
380      */

381     public long
382     getDeclKind()
383     {
384         return DeclarationKind.dk_component;
385     }
386
387     // ==================================================================
388
//
389
// Methods for OMG IDL org.objectweb.openccm.ast.api.Scope
390
//
391
// ==================================================================
392

393     /**
394      * Find a declaration.
395      *
396      * Note that the declaration is also recursively searched in the
397      * parent scope.
398      *
399      * @param name The name of the searched declaration.
400      *
401      * @return The declaration that was searched
402      * or null if it does not exist.
403      */

404     public Declaration
405     find(String JavaDoc name)
406     {
407         // Finds in the scope.
408
Declaration decl = super.find(name);
409
410         // If found then returns it.
411
if(decl != null) return decl;
412
413         // If not found then finds in base component.
414
if (base_component_ != null)
415         {
416             decl = base_component_.find(name);
417
418             // If found then returns it.
419
if(decl != null) return decl;
420         }
421
422         // If not found then finds in supported interfaces.
423
return supported_interfaces_.find(name);
424     }
425
426     /**
427      * To obtain all the contained Declaration objects.
428      *
429      * @param exclude_inherited If false return also objects contained
430      * in inherited scopes.
431      * @param limited_types A logical combination of DeclarationKind.
432      *
433      * @return An array of Declaration objects.
434      */

435     public Declaration[]
436     getContents(boolean exclude_inherited,
437                 long limited_types)
438     {
439         if (exclude_inherited)
440             return super.getContents(exclude_inherited, limited_types);
441
442         Declaration[] res = super.getContents(exclude_inherited, limited_types);
443         InterfaceDecl[] itfs = supported_interfaces_.getInterfaces();
444         for (int i=0; i<itfs.length; i++)
445         {
446             Declaration[] tmp1 = itfs[i].getContents(exclude_inherited, limited_types);
447             Declaration[] tmp2 = res;
448             res = new Declaration[tmp1.length+tmp2.length];
449             System.arraycopy(tmp2, 0, res, 0, tmp2.length);
450             System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
451         }
452
453         if (base_component_==null)
454             return res;
455
456         Declaration[] tmp1 = base_component_.getContents(exclude_inherited, limited_types);
457         Declaration[] tmp2 = res;
458         res = new Declaration[tmp1.length+tmp2.length];
459         System.arraycopy(tmp2, 0, res, 0, tmp2.length);
460         System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
461         return res;
462     }
463
464     // ==================================================================
465
//
466
// Methods for OMG IDL org.objectweb.openccm.ast.api.ForwardScope
467
//
468
// ==================================================================
469

470     // ==================================================================
471
//
472
// Methods for OMG IDL org.objectweb.openccm.ast.api.TypeRef
473
//
474
// ==================================================================
475

476     /**
477      * Obtain its associated TypeKind.
478      *
479      * @return The associated TypeKind.
480      */

481     public TypeKind
482     getTypeKind()
483     {
484         return TypeKind.tk_component;
485     }
486
487     // ==================================================================
488
//
489
// Methods for OMG IDL org.objectweb.openccm.ast.api.InterfaceDecl
490
//
491
// ==================================================================
492

493     // ==================================================================
494
//
495
// Methods for interface IDLTypeWrapper
496
//
497
// ==================================================================
498

499     /**
500      * Obtain its IDLType reference.
501      *
502      * @return The IDLType associated with the component declaration.
503      */

504     public org.omg.CORBA.IDLType JavaDoc
505     getIDLType()
506     {
507         return component_def_;
508     }
509
510     // ==================================================================
511
//
512
// Methods for OMG IDL org.objectweb.openccm.ast.api.ComponentBase
513
//
514
// ==================================================================
515

516     /**
517      * Obtain the client interface mapping.
518      *
519      * @return The client interface mapping.
520      */

521     public InterfaceDecl
522     getClientMapping()
523     {
524         if (client_mapping_ == null)
525         {
526             // load the client mapping for this component.
527
client_mapping_ = (InterfaceDeclImpl)getRepository().
528                               loadMapping(getParent(), getId());
529         }
530         return client_mapping_;
531     }
532
533     // ==================================================================
534
//
535
// Methods for OMG IDL org.objectweb.openccm.ast.api.ComponentDecl
536
//
537
// ==================================================================
538

539     /**
540      * Set the base component.
541      *
542      * @param base_component The base component of the component.
543      * Note that if more then one base component
544      * is set, only the first one is considered.
545      */

546     public void
547     setBaseComponent(ComponentDecl base_component)
548     {
549         if (base_component != null)
550         {
551             base_component_ = (ComponentDeclImpl)base_component;
552         }
553     }
554
555     /**
556      * Obtain the base component.
557      *
558      * @return The base component.
559      */

560     public ComponentDecl
561     getBaseComponent()
562     {
563         return base_component_;
564     }
565
566     /**
567      * Obtain the local context interface mapping.
568      *
569      * @return The local context interface mapping.
570      */

571     public InterfaceDecl
572     getLocalContextMapping()
573     {
574         if (local_context_mapping_ == null)
575         {
576             // load the context mapping for this component.
577
String JavaDoc parent_base_id = the_parent_.getId();
578             int idx = parent_base_id.lastIndexOf(':');
579             parent_base_id = parent_base_id.substring(0, idx);
580             String JavaDoc id = parent_base_id + "/CCM_" + getName() +
581                         "_Context:" + getVersion();
582             local_context_mapping_ = (InterfaceDeclImpl)getRepository().
583                                      loadMapping(getParent(), id);
584         }
585         return local_context_mapping_;
586     }
587
588     /**
589      * Obtain the local monolithic interface mapping.
590      *
591      * @return The local monolithic interface mapping.
592      */

593     public InterfaceDecl
594     getLocalMonolithicMapping()
595     {
596         if (local_monolithic_mapping_ == null)
597         {
598             // load the monolithic mapping for this component.
599
String JavaDoc parent_base_id = the_parent_.getId();
600             int idx = parent_base_id.lastIndexOf(':');
601             parent_base_id = parent_base_id.substring(0, idx);
602             String JavaDoc id = parent_base_id + "/CCM_" + getName() +
603                         ':' + getVersion();
604
605             local_monolithic_mapping_ = (InterfaceDeclImpl)getRepository().
606                                       loadMapping(getParent(), id);
607         }
608         return local_monolithic_mapping_;
609     }
610
611     /**
612      * Obtain the local main interface mapping.
613      *
614      * @return The local main interface mapping.
615      */

616     public InterfaceDecl
617     getLocalMainMapping()
618     {
619         if (local_main_mapping_ == null)
620         {
621             // load the main mapping for this component.
622
String JavaDoc parent_base_id = the_parent_.getId();
623             int idx = parent_base_id.lastIndexOf(':');
624             parent_base_id = parent_base_id.substring(0, idx);
625             String JavaDoc id = parent_base_id + "/CCM_" + getName() +
626                         "_Executor:" + getVersion();
627
628             local_main_mapping_ = (InterfaceDeclImpl)getRepository().
629                                   loadMapping(getParent(), id);
630         }
631         return local_main_mapping_;
632     }
633 }
634
Popular Tags