KickJava   Java API By Example, From Geeks To Geeks.

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


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 InterfaceDecl. */
39 import org.objectweb.openccm.ast.api.InterfaceDecl;
40
41 /** Used to access AST ComponentDecl. */
42 import org.objectweb.openccm.ast.api.ComponentDecl;
43
44 /** Used to access AST HomeDecl. */
45 import org.objectweb.openccm.ast.api.HomeDecl;
46
47 /** Used to access AST ValueDecl. */
48 import org.objectweb.openccm.ast.api.ValueDecl;
49
50 /** To use CORBA::ComponentIR::HomeDef. */
51 import org.omg.CORBA.ComponentIR.HomeDef;
52 import org.omg.CORBA.ComponentIR.HomeDefHelper;
53
54 /** To use CORBA::ValueDef. */
55 import org.omg.CORBA.ValueDef JavaDoc;
56
57 /**
58  * HomeDeclImpl is a wrapper class for IDL home declarations.
59  *
60  *
61  * Inherits from:
62  *
63  * - ComponentBaseImpl as homes are also interfaces and
64  * have an equivalent client interface mapping.
65  *
66  * - HomeDecl: OMG IDL for home declarations.
67  *
68  *
69  * Provides:
70  *
71  * - The inherited base home,
72  * i.e. the setBaseHome and getBaseHome operations.
73  *
74  * - The managed component,
75  * i.e. the setManagedComponent and getManagedComponent operations.
76  *
77  * - The primary key,
78  * i.e. the setPrimaryKey and getPrimaryKey operations.
79  *
80  * - The client explicit interface mapping,
81  * i.e. the getClientExplicitMapping operation.
82  *
83  * - The client implicit interface mapping,
84  * i.e. the getClientImplicitMapping operation.
85  *
86  * - The local interface mapping,
87  * i.e. the getLocalMapping operation.
88  *
89  *
90  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
91  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
92  *
93  * @version 0.3
94  */

95
96 public class HomeDeclImpl
97        extends ComponentBaseImpl
98        implements HomeDecl
99 {
100     // ==================================================================
101
//
102
// Internal state.
103
//
104
// ==================================================================
105

106     /** Reference to the CORBA 3.0 HomeDef. */
107     private HomeDef home_def_;
108
109     /** Reference to the base home. */
110     private HomeDeclImpl base_home_;
111
112     /** Reference to the managed component. */
113     private ComponentDeclImpl managed_component_;
114
115     /** Reference to the primary key. */
116     private ValueDeclImpl primary_key_;
117
118     /** The local interface mapping. */
119     private InterfaceDeclImpl local_mapping_;
120
121     // ==================================================================
122
//
123
// Constructor.
124
//
125
// ==================================================================
126

127     /**
128      * The constructor with the parent scope.
129      *
130      * @param rep The repository of the declaration.
131      * @param parent The parent scope of the home declaration.
132      */

133     protected
134     HomeDeclImpl(Repository rep,
135                  ScopeImpl parent)
136     {
137         // Call the ComponentBaseImpl constructor.
138
super(rep, parent);
139
140         // Init internal state.
141
home_def_ = null;
142         base_home_ = null;
143         managed_component_ = null;
144         primary_key_ = null;
145         local_mapping_ = null;
146     }
147
148     // ==================================================================
149
//
150
// Internal methods.
151
//
152
// ==================================================================
153

154     // ==================================================================
155
//
156
// Internal methods for DeclarationImpl.
157
//
158
// ==================================================================
159

160     /**
161      * Loads infos of the CORBA 3.0 HomeDef.
162      *
163      * @param contained The HomeDef to load.
164      */

165     protected void
166     load(org.omg.CORBA.Contained JavaDoc contained)
167     {
168         home_def_ = HomeDefHelper.narrow(contained);
169
170         HomeDef baseHome = home_def_.base_home();
171         if (baseHome!=null)
172             setBaseHome((HomeDecl)getRepository().
173                         lookupId(baseHome.id()));
174
175         setManagedComponent(
176             (ComponentDecl)getRepository().
177             lookupId(home_def_.managed_component().id()));
178
179         ValueDef JavaDoc primaryKey = home_def_.primary_key();
180         if (primaryKey!=null)
181             setPrimaryKey((ValueDecl)getRepository().
182                           lookupId(primaryKey.id()));
183
184         addSupportedInterfaces(home_def_.supported_interfaces());
185
186         // load it's mapped declarations
187
getClientMapping();
188
189         super.load(contained);
190     }
191
192     /**
193      * Obtain its CORBA 3.0 Contained reference.
194      *
195      * @return The Contained object associated with the home declaration.
196      */

197     protected org.omg.CORBA.Contained JavaDoc
198     getContained()
199     {
200        return home_def_;
201     }
202
203     // ==================================================================
204
//
205
// Internal methods for ScopeImpl.
206
//
207
// ==================================================================
208

209     /**
210      * Obtain its CORBA 3.0 Container reference.
211      *
212      * @return The Container object associated with the home declaration.
213      */

214     protected org.omg.CORBA.Container JavaDoc
215     getContainer()
216     {
217        return home_def_;
218     }
219
220     // ==================================================================
221
//
222
// Internal methods for ForwardScopeImpl.
223
//
224
// ==================================================================
225

226     /**
227      * Create the container object.
228      */

229     protected void
230     createContainer()
231     {
232         org.omg.CORBA.ComponentIR.ComponentDef managed_component_def =
233             (managed_component_ != null) ? (managed_component_.getComponentDef()) : null;
234
235         home_def_ = the_parent_.getComponentContainer().
236                     create_home(getId(), getName(), getVersion(),
237                                 getBaseHomeDef(),
238                                 managed_component_def,
239                                 supported_interfaces_.getInterfaceDefSeq(),
240                                 getPrimaryKeyDef());
241     }
242
243     /**
244      * Complete the container object.
245      */

246     protected void
247     completeContainer()
248     {
249     }
250
251     // ==================================================================
252
//
253
// Public methods.
254
//
255
// ==================================================================
256

257     /**
258      * Obtain the inherited home base.
259      *
260      * @return The inherited home base.
261      */

262     public HomeDef
263     getBaseHomeDef()
264     {
265         return (base_home_ != null)
266               ?(base_home_.getHomeDef()) : null;
267     }
268
269     /**
270      * Obtain the primary key value.
271      *
272      * @return The primary key value.
273      */

274     public org.omg.CORBA.ExtValueDef
275     getPrimaryKeyDef()
276     {
277         return (primary_key_ != null)
278               ?(primary_key_.getExtValueDef()) : null;
279     }
280
281     /**
282      * Obtain its HomeDef reference.
283      *
284      * @return The HomeDef object associated with the home declaration.
285      */

286     public HomeDef
287     getHomeDef()
288     {
289         return home_def_;
290     }
291
292     /**
293      * Obtain its InterfaceDef reference.
294      *
295      * @return The InterfaceDef object associated with the home declaration.
296      */

297     public org.omg.CORBA.ExtInterfaceDef
298     getExtInterfaceDef()
299     {
300         return home_def_;
301     }
302
303     /**
304      * Obtain its InterfaceDef reference.
305      *
306      * @return The InterfaceDef object associated with the interface
307      * declaration.
308      */

309     public org.omg.CORBA.InterfaceDef JavaDoc
310     getInterfaceDef()
311     {
312         return home_def_;
313     }
314
315     // ==================================================================
316
//
317
// Methods for OMG IDL org.objectweb.openccm.ast.api.WithDependencies
318
//
319
// ==================================================================
320

321     /**
322      * Obtain the declaration external dependencies.
323      *
324      * Note: for scopes, contained objects are not considered
325      * as dependencies.
326      *
327      * @return The list of dependencies as an array of Declaration.
328      */

329     public Declaration[]
330     getDependencies()
331     {
332         if (dependencies_ != null)
333             return dependencies_;
334
335         java.util.List JavaDoc home_depend = new java.util.ArrayList JavaDoc();
336         Declaration[] depend = null;
337
338         // base home
339
if (getBaseHome() != null)
340         {
341             home_depend.add(getBaseHome());
342             depend = getBaseHome().getDependencies();
343             for (int j=0; j<depend.length; j++)
344                 home_depend.add(depend[j]);
345         }
346
347         // primary key
348
if (getPrimaryKey() != null)
349         {
350             home_depend.add(getPrimaryKey());
351             depend = getPrimaryKey().getDependencies();
352             for (int j=0; j<depend.length; j++)
353                 home_depend.add(depend[j]);
354         }
355
356         // managed component
357
home_depend.add(getManagedComponent());
358         depend = getManagedComponent().getDependencies();
359         for (int j=0; j<depend.length; j++)
360             home_depend.add(depend[j]);
361
362         // supported interfaces
363
InterfaceDecl[] supp = supported_interfaces_.getInterfaces();
364         for (int i=0; i<supp.length; i++)
365         {
366             home_depend.add(supp[i]);
367             depend = supp[i].getDependencies();
368             for (int j=0; j<depend.length; j++)
369             {
370                 if (home_depend.indexOf(depend[j])==-1)
371                     home_depend.add(depend[j]);
372             }
373         }
374
375         // contents
376
Declaration[] decls = getContents(true, DeclarationKind.dk_all);
377         for (int i=0; i<decls.length; i++)
378         {
379             depend = decls[i].getDependencies();
380             for (int j=0; j<depend.length; j++)
381             {
382                 if ( (!containsDecl(depend[j])) &&
383                      (home_depend.indexOf(depend[j])==-1) )
384                     home_depend.add(depend[j]);
385             }
386         }
387
388         dependencies_ = (Declaration[])home_depend.toArray(new Declaration[0]);
389         return dependencies_;
390     }
391
392     // ==================================================================
393
//
394
// Methods for OMG IDL org.objectweb.openccm.ast.api.Declaration
395
//
396
// ==================================================================
397

398     /**
399      * Obtain its DeclarationKind.
400      *
401      * @return The DeclarationKind of the object.
402      */

403     public long
404     getDeclKind()
405     {
406         return DeclarationKind.dk_home;
407     }
408
409     // ==================================================================
410
//
411
// Methods for OMG IDL org.objectweb.openccm.ast.api.Scope
412
//
413
// ==================================================================
414

415     /**
416      * Find a declaration.
417      *
418      * Note that the declaration is also recursively searched in the
419      * parent scope.
420      *
421      * @param name The name of the searched declaration.
422      *
423      * @return The declaration that was searched
424      * or null if it does not exist.
425      */

426     public Declaration
427     find(String JavaDoc name)
428     {
429         // Finds in the scope.
430
Declaration decl = super.find(name);
431
432         // If found then returns it.
433
if(decl != null) return decl;
434
435         // If not found then finds in inherited interfaces.
436
if (base_home_ != null)
437         {
438             decl = base_home_.find(name);
439             if(decl != null) return decl;
440         }
441
442         // If not found then finds in supported interfaces.
443
return supported_interfaces_.find(name);
444     }
445
446     /**
447      * To obtain all the contained Declaration objects.
448      *
449      * @param exclude_inherited If false return also objects contained
450      * in inherited scopes.
451      * @param limited_types A logical combination of DeclarationKind.
452      *
453      * @return An array of Declaration objects.
454      */

455     public Declaration[]
456     getContents(boolean exclude_inherited,
457                 long limited_types)
458     {
459         if ((exclude_inherited) || (base_home_==null))
460             return super.getContents(exclude_inherited, limited_types);
461
462         Declaration[] res = super.getContents(exclude_inherited, limited_types);
463         InterfaceDecl[] itfs = supported_interfaces_.getInterfaces();
464         for (int i=0; i<itfs.length; i++)
465         {
466             Declaration[] tmp1 = itfs[i].getContents(exclude_inherited, limited_types);
467             Declaration[] tmp2 = res;
468             res = new Declaration[tmp1.length+tmp2.length];
469             System.arraycopy(tmp2, 0, res, 0, tmp2.length);
470             System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
471         }
472         Declaration[] tmp1 = base_home_.getContents(exclude_inherited, limited_types);
473         Declaration[] tmp2 = res;
474         res = new Declaration[tmp1.length+tmp2.length];
475         System.arraycopy(tmp2, 0, res, 0, tmp2.length);
476         System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
477         return res;
478     }
479
480     // ==================================================================
481
//
482
// Methods for OMG IDL org.objectweb.openccm.ast.api.ForwardScope
483
//
484
// ==================================================================
485

486     // ==================================================================
487
//
488
// Methods for OMG IDL org.objectweb.openccm.ast.api.TypeRef
489
//
490
// ==================================================================
491

492     /**
493      * Obtain its associated TypeKind.
494      *
495      * @return The associated TypeKind.
496      */

497     public TypeKind
498     getTypeKind()
499     {
500         return TypeKind.tk_home;
501     }
502
503     // ==================================================================
504
//
505
// Methods for OMG IDL org.objectweb.openccm.ast.api.InterfaceDecl
506
//
507
// ==================================================================
508

509     // ==================================================================
510
//
511
// Methods for interface IDLTypeWrapper
512
//
513
// ==================================================================
514

515     /**
516      * Obtain its IDLType reference.
517      *
518      * @return The IDLType associated with the home declaration.
519      */

520     public org.omg.CORBA.IDLType JavaDoc
521     getIDLType()
522     {
523         return home_def_;
524     }
525
526     // ==================================================================
527
//
528
// Methods for OMG IDL org.objectweb.openccm.ast.api.ComponentBase
529
//
530
// ==================================================================
531

532     /**
533      * Obtain the client interface mapping.
534      *
535      * @return The client interface mapping.
536      */

537     public InterfaceDecl
538     getClientMapping()
539     {
540         if (client_mapping_ == null)
541         {
542             // load the client mapping for this component.
543
client_mapping_ = (InterfaceDeclImpl)getRepository().
544                               loadMapping(getParent(), getId());
545         }
546         return client_mapping_;
547     }
548
549     // ==================================================================
550
//
551
// Methods for OMG IDL org.objectweb.openccm.ast.api.HomeDecl
552
//
553
// ==================================================================
554

555     /**
556      * Set the base home.
557      *
558      * @param base_home The base home.
559      */

560     public void
561     setBaseHome(HomeDecl base_home)
562     {
563         if (base_home != null)
564         {
565             base_home_ = (HomeDeclImpl)base_home;
566         }
567     }
568
569     /**
570      * Obtain the base home.
571      *
572      * @return The base home.
573      */

574     public HomeDecl
575     getBaseHome()
576     {
577         return base_home_;
578     }
579
580     /**
581      * Set the managed component.
582      *
583      * @param managed_component The managed component.
584      */

585     public void
586     setManagedComponent(ComponentDecl managed_component)
587     {
588         if (managed_component != null)
589         {
590             managed_component_ = (ComponentDeclImpl)managed_component;
591         }
592     }
593
594     /**
595      * Obtain the managed component.
596      *
597      * @return The managed component.
598      */

599     public ComponentDecl
600     getManagedComponent()
601     {
602         return managed_component_;
603     }
604
605     /**
606      * Set the primary key.
607      *
608      * @param primary_key The primary key.
609      */

610     public void
611     setPrimaryKey(ValueDecl primary_key)
612     {
613         if (primary_key != null)
614         {
615             primary_key_ = (ValueDeclImpl)primary_key;
616         }
617     }
618
619     /**
620      * Obtain the primary key.
621      *
622      * @return The primary key.
623      */

624     public ValueDecl
625     getPrimaryKey()
626     {
627         return primary_key_;
628     }
629
630     /**
631      * Obtain the client explicit interface mapping.
632      *
633      * @return The client explicit interface mapping.
634      */

635     public InterfaceDecl
636     getClientExplicitMapping()
637     {
638         // retrieve client mapping interface.
639
InterfaceDecl client = getClientMapping();
640         InterfaceDecl[] bases = client.getInheritedInterfaceList()
641                                       .getInterfaces();
642         if (bases[0].getName().equals(getName()+"Explicit"))
643             return bases[0];
644         else
645             return bases[1];
646     }
647
648     /**
649      * Obtain the client implicit interface mapping.managed_component_
650      *
651      * @return The client implicit interface mapping.
652      */

653     public InterfaceDecl
654     getClientImplicitMapping()
655     {
656         // retrieve client mapping interface.
657
InterfaceDecl client = getClientMapping();
658         InterfaceDecl[] bases = client.getInheritedInterfaceList()
659                                       .getInterfaces();
660         if (bases[0].getName().equals(getName()+"Implicit"))
661             return bases[0];
662         else
663             return bases[1];
664     }
665
666     /**
667      * Obtain the local interface mapping.
668      *
669      * @return The local interface mapping.
670      */

671     public InterfaceDecl
672     getLocalMapping()
673     {
674         if (local_mapping_ == null)
675         {
676             // load the local mapping for this home.
677
String JavaDoc parent_base_id = the_parent_.getId();
678             int idx = parent_base_id.lastIndexOf(':');
679             parent_base_id = parent_base_id.substring(0, idx);
680             String JavaDoc id = parent_base_id + "/CCM_" + getName() +
681                         ':' + getVersion();
682
683             local_mapping_ = (InterfaceDeclImpl)getRepository().
684                              loadMapping(getParent(), id);
685         }
686         return local_mapping_;
687     }
688 }
689
Popular Tags