KickJava   Java API By Example, From Geeks To Geeks.

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


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 manages home declarations.
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 HomeDeclImpl
39        extends InterfaceDeclImpl
40        implements HomeRef, HomeDecl
41 {
42     // ==================================================================
43
//
44
// Internal state.
45
//
46
// ==================================================================
47

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

51     private org.omg.CORBA.ComponentIR.HomeDef home_def_;
52
53     /**
54      ** Reference to the base home.
55      **/

56     private HomeDeclImpl base_home_;
57
58     /**
59      ** Reference to the managed component.
60      **/

61     private ComponentDeclImpl managed_component_;
62
63     /**
64      ** Reference to the primary key.
65      **/

66     private ValueDeclImpl primary_key_;
67
68     /**
69      **
70      **/

71     private org.objectweb.ccm.util.Vector supported_interfaces_;
72
73     /**
74      **
75      **/

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

81     private InterfaceDeclImpl local_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 home declaration.
93      **/

94     protected
95     HomeDeclImpl(Repository rep, ScopeImpl parent)
96     {
97         // Call the InterfaceDefImpl constructor.
98
super(rep, parent);
99
100         // Init internal state.
101
home_def_ = null;
102         base_home_ = null;
103         managed_component_ = null;
104         primary_key_ = null;
105         client_mapping_ = null;
106         local_mapping_ = null;
107         supported_interfaces_ = new org.objectweb.ccm.util.Vector();
108         the_declaration_kind_ = DeclarationKind._dk_home;
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 home base.
138      **
139      ** @return The inherited home base.
140      **/

141     protected org.omg.CORBA.ComponentIR.HomeDef
142     getBaseHomeDef()
143     {
144         return (base_home_ != null)
145               ?(base_home_.getHomeDef()) : null;
146     }
147
148     /**
149      **
150      **/

151     protected org.omg.CORBA.ExtValueDef
152     getPrimaryKeyDef()
153     {
154         return (primary_key_ != null)
155               ?(primary_key_.getExtValueDef()) : null;
156     }
157
158     /**
159      ** Loads infos of the CORBA 3.0 HomeDef.
160      **
161      ** @param home_def The HomeDef to load.
162      **/

163     protected void
164     load(org.omg.CORBA.Contained JavaDoc contained)
165     {
166         home_def_ = org.omg.CORBA.ComponentIR.HomeDefHelper.narrow(contained);
167         if (home_def_.base_home()!=null)
168             setBaseHome((HomeRef)getRepository().lookupId(home_def_.base_home().id()));
169
170         setManagedComponent(
171             (ComponentRef)getRepository().lookupId(home_def_.managed_component().id()));
172
173         if (home_def_.primary_key()!=null)
174             setPrimaryKey((ValueRef)getRepository().lookupId(home_def_.primary_key().id()));
175
176         org.omg.CORBA.InterfaceDef JavaDoc[] supported = home_def_.supported_interfaces();
177         for (int i=0;i<supported.length;i++)
178             // WARNING: This will break consistency of array items!!!
179
addSupportedInterface((InterfaceRef)getRepository().lookupId(supported[i].id()));
180
181         // load it's mapped declarations
182
getClientMapping();
183
184         super.load(contained);
185     }
186
187     /**
188      ** Find a declaration into inherited interfaces.
189      **
190      ** @param name The name of the searched declaration.
191      ** @return The declaration that was searched
192      ** or null if it does not exist.
193      **/

194     protected Declaration
195     findInSupportedInterfaces(String JavaDoc name)
196     {
197         for(int i=0; i<supported_interfaces_.size(); i++)
198         {
199             Declaration decl = ((InterfaceDecl)(supported_interfaces_.get(i))).find(name);
200             // If found then returns it.
201
if (decl != null) return decl;
202         }
203
204         // Else not found.
205
return null;
206     }
207
208     // ==================================================================
209
//
210
// Methods for the Declaration interface.
211
//
212
// ==================================================================
213

214     /**
215      ** Find a declaration.
216      **
217      ** Note that the declaration is also recursively searched in the
218      ** parent scope.
219      **
220      ** @param name The name of the searched declaration.
221      **
222      ** @return The declaration that was searched
223      ** or null if it does not exist.
224      **/

225     public Declaration
226     find(String JavaDoc name)
227     {
228         // Finds in the scope.
229
Declaration decl = super.find(name);
230         // If found then returns it.
231
if(decl != null) return decl;
232
233         // If not found then finds in inherited interfaces.
234
if (base_home_ != null)
235         {
236             decl = base_home_.find(name);
237             if(decl != null) return decl;
238         }
239
240         return findInSupportedInterfaces(name);
241     }
242
243     /**
244      ** Obtain the declaration external dependencies.
245      ** Note: for scopes, contained objects are not considered
246      ** as dependencies.
247      **
248      ** @return The list of dependencies as an array of Declaration.
249      **/

250     public Declaration[]
251     getDependencies()
252     {
253         if (dependencies_!=null)
254             return dependencies_;
255
256         org.objectweb.ccm.util.Vector home_depend = new org.objectweb.ccm.util.Vector();
257         Declaration[] depend = null;
258
259         // base home
260
if (getBaseHome()!=null)
261         {
262             home_depend.add(getBaseHome());
263             depend = getBaseHome().getDependencies();
264             for (int j=0;j<depend.length;j++)
265                 home_depend.add(depend[j]);
266         }
267
268         // primary key
269
if (getPrimaryKey()!=null)
270         {
271             home_depend.add(getPrimaryKey());
272             depend = getPrimaryKey().getDependencies();
273             for (int j=0;j<depend.length;j++)
274                 home_depend.add(depend[j]);
275         }
276
277         // managed component
278
home_depend.add(getManagedComponent());
279         depend = getManagedComponent().getDependencies();
280         for (int j=0;j<depend.length;j++)
281             home_depend.add(depend[j]);
282
283         // supported interfaces
284
InterfaceDecl[] supp = getSupportedInterfaces();
285         for (int i=0;i<supp.length;i++)
286         {
287             home_depend.add(supp[i]);
288             depend = supp[i].getDependencies();
289             for (int j=0;j<depend.length;j++)
290             {
291                 if (home_depend.indexOf(depend[j])==-1)
292                     home_depend.add(depend[j]);
293             }
294         }
295
296         // contents
297
Declaration[] decls = getContents(true, org.objectweb.ccm.IDL3.DeclarationKind._dk_all);
298         for (int i=0;i<decls.length;i++)
299         {
300             depend = decls[i].getDependencies();
301             for (int j=0;j<depend.length;j++)
302             {
303                 if ((!containsDecl(depend[j])) &&
304                     (home_depend.indexOf(depend[j])==-1))
305                     home_depend.add(depend[j]);
306             }
307         }
308
309         dependencies_ = (Declaration[])home_depend.toArray(new Declaration[0]);
310         return dependencies_;
311     }
312
313     // ==================================================================
314
//
315
// Methods for the TypeRef interface.
316
//
317
// ==================================================================
318

319     /**
320      ** Obtain its IDLType reference.
321      **
322      ** @return The IDLType associated with the home declaration.
323      **/

324     public org.omg.CORBA.IDLType JavaDoc
325     getIDLType()
326     {
327         return home_def_;
328     }
329
330     /**
331      **
332      **/

333     public int
334     getTypeKind()
335     {
336         return TypeKind._tk_home;
337     }
338
339     // ==================================================================
340
//
341
// Methods for the InterfaceRef interface.
342
//
343
// ==================================================================
344

345     /**
346      ** Obtain its InterfaceDef reference.
347      **
348      ** @return The InterfaceDef object associated with the home declaration.
349      **/

350     public org.omg.CORBA.ExtInterfaceDef
351     getExtInterfaceDef()
352     {
353         return home_def_;
354     }
355
356     /**
357      ** Obtain its InterfaceDef reference.
358      **
359      ** @return The InterfaceDef object associated with the interface
360      ** declaration.
361      **/

362     public org.omg.CORBA.InterfaceDef JavaDoc
363     getInterfaceDef()
364     {
365         return home_def_;
366     }
367
368     // ==================================================================
369
//
370
// Methods for the HomeRef interface.
371
//
372
// ==================================================================
373

374     /**
375      ** Obtain its HomeDef reference.
376      **
377      ** @return The HomeDef object associated with the home declaration.
378      **/

379     public org.omg.CORBA.ComponentIR.HomeDef
380     getHomeDef()
381     {
382         return home_def_;
383     }
384
385     // ==================================================================
386
//
387
// Methods for the HomeDecl interface.
388
//
389
// ==================================================================
390

391     /**
392      ** Add an interface.
393      **
394      ** @param itf The supported interface to add.
395      **/

396     public void
397     addSupportedInterface(InterfaceRef itf)
398     {
399         if(itf != null)
400         {
401             itf.addRef();
402             supported_interfaces_.add(itf);
403         }
404     }
405
406     /**
407      ** Set the base home.
408      **
409      ** @param base_home The base home.
410      **/

411     public void
412     setBaseHome(HomeRef base_home)
413     {
414         if (base_home != null)
415         {
416             base_home_ = (HomeDeclImpl)base_home;
417             base_home_.addRef();
418         }
419     }
420
421     /**
422      ** Set the managed component.
423      **
424      ** @param managed_component The managed component.
425      **/

426     public void
427     setManagedComponent(ComponentRef managed_component)
428     {
429         if (managed_component != null)
430         {
431             managed_component_ = (ComponentDeclImpl)managed_component;
432             managed_component_.addRef();
433         }
434     }
435
436     /**
437      ** Set the primary key.
438      **
439      ** @param primary_key The primary key.
440      **/

441     public void
442     setPrimaryKey(ValueRef primary_key)
443     {
444         if (primary_key != null)
445         {
446             primary_key_ = (ValueDeclImpl)primary_key;
447             primary_key_.addRef();
448         }
449     }
450
451     /**
452      **
453      **/

454     public HomeDecl
455     getBaseHome()
456     {
457         return base_home_;
458     }
459
460     /**
461      **
462      **/

463     public ComponentDecl
464     getManagedComponent()
465     {
466         return managed_component_;
467     }
468
469     /**
470      **
471      **/

472     public ValueDecl
473     getPrimaryKey()
474     {
475         return primary_key_;
476     }
477
478     /**
479      **
480      **/

481     public InterfaceDecl[]
482     getSupportedInterfaces()
483     {
484         InterfaceDecl[] result = new InterfaceDecl[supported_interfaces_.size()];
485         for(int i=0; i<result.length; i++)
486             result[i] = (InterfaceDecl)supported_interfaces_.get(i);
487
488         return result;
489     }
490
491     /**
492      **
493      **/

494     public InterfaceDecl
495     getClientExplicitMapping()
496     {
497         // retrieve client mapping interface.
498
InterfaceDecl client = getClientMapping();
499         InterfaceDecl[] bases = client.getBaseInterfaces();
500         if (bases[0].getName().equals(getName()+"Explicit"))
501             return bases[0];
502         else
503             return bases[1];
504     }
505
506     /**
507      **
508      **/

509     public InterfaceDecl
510     getClientImplicitMapping()
511     {
512         // retrieve client mapping interface.
513
InterfaceDecl client = getClientMapping();
514         InterfaceDecl[] bases = client.getBaseInterfaces();
515         if (bases[0].getName().equals(getName()+"Implicit"))
516             return bases[0];
517         else
518             return bases[1];
519     }
520
521     /**
522      **
523      **/

524     public InterfaceDecl
525     getClientMapping()
526     {
527         if (client_mapping_!=null)
528             return client_mapping_;
529
530         // load the client mapping for this component.
531
client_mapping_ = (InterfaceDeclImpl)getRepository().loadMapping(getParent(), getId());
532
533         return client_mapping_;
534     }
535
536     /**
537      **
538      **/

539     public InterfaceDecl
540     getLocalMapping()
541     {
542         if (local_mapping_!=null)
543             return local_mapping_;
544
545         // load the local mapping for this home.
546
String JavaDoc parent_base_id = the_parent_.getId();
547         int idx = parent_base_id.lastIndexOf(':');
548         parent_base_id = parent_base_id.substring(0, idx);
549         String JavaDoc id = parent_base_id + "/CCM_"+getName()+":"+getVersion();
550
551         local_mapping_ = (InterfaceDeclImpl)getRepository().loadMapping(getParent(), id);
552         return local_mapping_;
553     }
554
555     // ==================================================================
556
//
557
// Methods for the inherited DeclarationImpl class.
558
//
559
// ==================================================================
560

561     /**
562      **
563      **/

564     public void
565     destroy()
566     {
567         if (base_home_!=null)
568             base_home_.removeRef();
569
570         if (managed_component_!=null)
571             managed_component_.removeRef();
572
573         if (primary_key_!=null)
574             primary_key_.removeRef();
575
576         InterfaceDecl[] itfs = getSupportedInterfaces();
577         for (int i=0;i<itfs.length;i++)
578             ((InterfaceRef)itfs[i]).removeRef();
579
580         super.destroy();
581     }
582
583     /**
584      ** Obtain its CORBA 3.0 Contained reference.
585      **
586      ** @return The Contained object associated with the home declaration.
587      **/

588     protected org.omg.CORBA.Contained JavaDoc
589     getContained()
590     {
591        return home_def_;
592     }
593
594     // ==================================================================
595
//
596
// Methods for the inherited ForwardScopeImpl class.
597
//
598
// ==================================================================
599

600     /**
601      ** Create the container object.
602      **/

603     protected void
604     createContainer()
605     {
606         home_def_ = the_parent_.getComponentContainer().create_home(getId(),
607                                                                     getName(),
608                                                                     getVersion(),
609                                                                     getBaseHomeDef(),
610                                                                     managed_component_.getComponentDef(),
611                                                                     getInterfaceDefs(),
612                                                                     getPrimaryKeyDef());
613     }
614
615     /**
616      ** Complete the container object.
617      **/

618     protected void
619     completeContainer()
620     {
621     }
622
623     // ==================================================================
624
//
625
// Methods for the inherited ScopeImpl class.
626
//
627
// ==================================================================
628

629     /**
630      ** Obtain its CORBA 3.0 Container reference.
631      **
632      ** @return The Container object associated with the home declaration.
633      **/

634     protected org.omg.CORBA.Container JavaDoc
635     getContainer()
636     {
637        return home_def_;
638     }
639
640     /**
641      ** To obtain all the contained Declaration objects.
642      **
643      ** @param exclude_inherited If false return also objects contained in inherited scopes.
644      ** @param limited_types A logical combination of DeclarationKind.
645      **
646      ** @return An array of Declaration objects.
647      **/

648     public Declaration[]
649     getContents(boolean exclude_inherited, int limited_types)
650     {
651         if (exclude_inherited)
652             return super.getContents(exclude_inherited, limited_types);
653
654         Declaration[] res = super.getContents(exclude_inherited, limited_types);
655         InterfaceDecl[] itfs = getSupportedInterfaces();
656         for (int i=0;i<itfs.length;i++)
657         {
658             Declaration[] tmp1 = itfs[i].getContents(exclude_inherited, limited_types);
659             Declaration[] tmp2 = res;
660             res = new Declaration[tmp1.length+tmp2.length];
661             System.arraycopy(tmp2, 0, res, 0, tmp2.length);
662             System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
663         }
664
665         if (base_home_==null)
666             return res;
667
668         Declaration[] tmp1 = base_home_.getContents(exclude_inherited, limited_types);
669         Declaration[] tmp2 = res;
670         res = new Declaration[tmp1.length+tmp2.length];
671         System.arraycopy(tmp2, 0, res, 0, tmp2.length);
672         System.arraycopy(tmp1, 0, res, tmp2.length, tmp1.length);
673         return res;
674     }
675 }
676
Popular Tags