KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > UnionDef


1 package org.jacorb.ir;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.lang.reflect.*;
24 import java.util.*;
25 import java.io.*;
26
27 import org.omg.CORBA.INTF_REPOS JavaDoc;
28 import org.omg.CORBA.TypeCode JavaDoc;
29 import org.omg.CORBA.Any JavaDoc;
30 import org.omg.PortableServer.POA JavaDoc;
31
32 import org.apache.avalon.framework.logger.Logger;
33
34 public class UnionDef
35     extends TypedefDef
36     implements org.omg.CORBA.UnionDefOperations JavaDoc, ContainerType
37 {
38     protected static char fileSeparator =
39         System.getProperty("file.separator").charAt(0);
40
41     private org.omg.CORBA.UnionMember JavaDoc [] members;
42     private org.omg.CORBA.TypeCode JavaDoc discriminator_type;
43     private org.omg.CORBA.IDLType JavaDoc discriminator_type_def;
44
45     private Method memberMethods[];
46     private int member_size;
47
48     /* reference to my container as a contained object */
49     private org.omg.CORBA.Contained JavaDoc myContainer;
50     /** local references to contained objects */
51     private Hashtable containedLocals = new Hashtable();
52     /** CORBA references to contained objects */
53     private Hashtable contained = new Hashtable();
54
55     private File my_dir;
56     private String JavaDoc path;
57     private Logger logger;
58     private ClassLoader JavaDoc loader;
59     private POA JavaDoc poa;
60
61     public UnionDef( Class JavaDoc c,
62                      String JavaDoc path,
63                      org.omg.CORBA.Container JavaDoc _defined_in,
64                      org.omg.CORBA.Repository JavaDoc ir,
65                      ClassLoader JavaDoc loader,
66                      Logger logger,
67                      POA JavaDoc poa )
68     {
69         this.loader = loader;
70         this.logger = logger;
71         this.poa = poa;
72
73         def_kind = org.omg.CORBA.DefinitionKind.dk_Union;
74         containing_repository = ir;
75         defined_in = _defined_in;
76         version = "1.0";
77         String JavaDoc classId = c.getName();
78         myContainer = org.omg.CORBA.ContainedHelper.narrow( defined_in );
79
80         if( classId.indexOf('.') > 0 )
81         {
82             name( classId.substring( classId.lastIndexOf('.')+1));
83             absolute_name = myContainer.absolute_name() + "::" + name;
84         }
85         else
86         {
87             name( classId );
88             defined_in = containing_repository;
89             absolute_name = "::" + name;
90         }
91
92         Class JavaDoc helperClass;
93         try
94         {
95             helperClass = this.loader.loadClass(classId + "Helper");
96             id( (String JavaDoc)helperClass.getDeclaredMethod("id", (Class JavaDoc[]) null).invoke( null, (Object JavaDoc[]) null ));
97             type = TypeCodeUtil.getTypeCode( c, this.loader, null, classId, this.logger );
98             members = new org.omg.CORBA.UnionMember JavaDoc[ type.member_count() ];
99             for( int i = 0; i < members.length; i++ )
100             {
101                 members[i] = new org.omg.CORBA.UnionMember JavaDoc( type.member_name(i),
102                                                             type.member_label(i),
103                                                             type.member_type(i),
104                                                             null );
105             }
106             discriminator_type = type.discriminator_type();
107         }
108         catch( Exception JavaDoc e )
109         {
110             this.logger.error("Caught Exception", e);
111         }
112     }
113
114     public void loadContents()
115     {
116         // read from the class (operations and atributes)
117
if (getReference() == null)
118         {
119             throw new INTF_REPOS JavaDoc ("getReference returns null");
120         }
121
122         org.omg.CORBA.UnionDef JavaDoc myReference =
123             org.omg.CORBA.UnionDefHelper.narrow( getReference());
124
125         if (myReference == null)
126         {
127             throw new INTF_REPOS JavaDoc ("narrow failed for " + getReference() );
128         }
129
130         /* load nested definitions from interfacePackage directory */
131
132         String JavaDoc[] classes = null;
133         if( my_dir != null )
134         {
135             classes = my_dir.list( new IRFilenameFilter(".class") );
136
137             // load class files in this interface's Package directory
138
if( classes != null)
139             {
140                 for( int j = 0; j < classes.length; j++ )
141                 {
142                     try
143                     {
144                         if (this.logger.isDebugEnabled())
145                         {
146                             this.logger.debug("Union " +name+ " tries " +
147                                               full_name.replace('.', fileSeparator) +
148                                               "Package" + fileSeparator +
149                                               classes[j].substring( 0, classes[j].indexOf(".class")));
150                         }
151
152                         ClassLoader JavaDoc loader = getClass().getClassLoader();
153                         if( loader == null )
154                         {
155                             loader = this.loader;
156                         }
157
158                         Class JavaDoc cl =
159                             loader.loadClass(
160                                    ( full_name.replace('.', fileSeparator) + "Package" + fileSeparator +
161                                      classes[j].substring( 0, classes[j].indexOf(".class"))
162                                      ).replace( fileSeparator, '/') );
163
164
165                         Contained containedObject =
166                             Contained.createContained( cl,
167                                                        path,
168                                                        myReference,
169                                                        containing_repository,
170                                                        this.logger,
171                                                        this.loader,
172                                                        this.poa);
173                         if( containedObject == null )
174                             continue;
175
176                         org.omg.CORBA.Contained JavaDoc containedRef =
177                             Contained.createContainedReference(containedObject,
178                                                                this.logger,
179                                                                this.poa);
180
181                         if( containedObject instanceof ContainerType )
182                             ((ContainerType)containedObject).loadContents();
183
184                         containedRef.move( myReference, containedRef.name(), containedRef.version() );
185
186                         if (this.logger.isDebugEnabled())
187                         {
188                             this.logger.debug("Union " + full_name +
189                                               " loads "+ containedRef.name());
190                         }
191
192                         contained.put( containedRef.name() , containedRef );
193                         containedLocals.put( containedRef.name(), containedObject );
194                     }
195                     catch ( Exception JavaDoc e )
196                     {
197                         this.logger.error("Caught Exception", e);
198                     }
199                 }
200             }
201         }
202     }
203
204
205     public void define()
206     {
207         if (this.logger.isDebugEnabled())
208         {
209             this.logger.debug("Union " + name + " defining...");
210         }
211
212         discriminator_type_def =
213             IDLType.create( discriminator_type, containing_repository,
214                             this.logger, this.poa);
215
216         for( Enumeration e = containedLocals.elements();
217              e.hasMoreElements();
218              ((IRObject)e.nextElement()).define())
219             ;
220
221         try
222         {
223             for( int i = 0; i < members.length; i++ )
224             {
225                 members[i].type_def =
226                     IDLType.create( members[i].type, containing_repository,
227                                     this.logger, this.poa );
228             }
229         }
230         catch ( Exception JavaDoc e )
231         {
232             this.logger.error("Caught Exception", e);
233         }
234
235         if (this.logger.isDebugEnabled())
236         {
237             this.logger.debug("UnionDef " + name + " defined");
238         }
239     }
240
241     public org.omg.CORBA.UnionMember JavaDoc[] members()
242     {
243         return members;
244     }
245
246     public void members(org.omg.CORBA.UnionMember JavaDoc[] m)
247     {
248         members = m;
249     }
250
251     public org.omg.CORBA.TypeCode JavaDoc discriminator_type()
252     {
253         return discriminator_type;
254     }
255
256     public org.omg.CORBA.IDLType JavaDoc discriminator_type_def()
257     {
258         return discriminator_type_def;
259     }
260
261     public void discriminator_type_def(org.omg.CORBA.IDLType JavaDoc dt)
262     {
263         discriminator_type_def = dt;
264     }
265
266
267     // from Contained
268

269     public org.omg.CORBA.ContainedPackage.Description describe()
270     {
271         org.omg.CORBA.Any JavaDoc a = orb.create_any();
272         String JavaDoc def_in_name;
273
274         if( myContainer != null )
275             def_in_name = myContainer.id();
276         else
277             def_in_name = "IDL:/:1.0";
278
279         org.omg.CORBA.TypeDescriptionHelper.insert( a,
280                                 new org.omg.CORBA.TypeDescription JavaDoc( name(),
281                                                                    id(),
282                                                                    def_in_name,
283                                                                    version(),
284                                                                    type()
285                                                                    ) );
286         return new org.omg.CORBA.ContainedPackage.Description( org.omg.CORBA.DefinitionKind.dk_Union, a);
287     }
288
289     // from IRObject
290

291     public void destroy(){}
292
293
294     public org.omg.CORBA.Contained JavaDoc[] contents(org.omg.CORBA.DefinitionKind JavaDoc limit_type,
295                                               boolean exclude_inherited)
296     {
297         Hashtable filtered = new Hashtable();
298
299         if( limit_type == org.omg.CORBA.DefinitionKind.dk_all )
300         {
301             filtered = contained;
302         }
303         else
304         {
305             Enumeration f = contained.keys();
306             while( f.hasMoreElements() )
307             {
308                 Object JavaDoc k = f.nextElement();
309                 org.omg.CORBA.Contained JavaDoc c = (org.omg.CORBA.Contained JavaDoc)contained.get( k );
310                 if( c.def_kind() == limit_type )
311                     filtered.put( k, c );
312             }
313         }
314
315         Enumeration e = filtered.elements();
316         org.omg.CORBA.Contained JavaDoc[] result = new org.omg.CORBA.Contained JavaDoc[ filtered.size() ];
317
318         for( int i = 0; i < filtered.size(); i++ )
319             result[i] = (org.omg.CORBA.Contained JavaDoc)e.nextElement();
320
321         return result;
322     }
323
324     /**
325      * retrieves a contained object given a scoped name
326      */

327
328     public org.omg.CORBA.Contained JavaDoc lookup( String JavaDoc scopedname )
329     {
330         String JavaDoc top_level_name;
331         String JavaDoc rest_of_name;
332         String JavaDoc name;
333
334         if( scopedname.startsWith("::") )
335         {
336             name = scopedname.substring(2);
337         }
338         else
339             name = scopedname;
340
341         if( name.indexOf("::") > 0 )
342         {
343             top_level_name = name.substring( 0, name.indexOf("::") );
344             rest_of_name = name.substring( name.indexOf("::") + 2);
345         }
346         else
347         {
348             top_level_name = name;
349             rest_of_name = null;
350         }
351
352         org.omg.CORBA.Contained JavaDoc top = (org.omg.CORBA.Contained JavaDoc)contained.get( top_level_name );
353         if( top == null )
354         {
355             if (this.logger.isDebugEnabled())
356             {
357                 this.logger.debug("Container " + this.name + " top " +
358                                   top_level_name + " not found ");
359             }
360
361             return null;
362         }
363
364         if( rest_of_name == null )
365         {
366             return top;
367         }
368         else
369         {
370             org.omg.CORBA.Container JavaDoc topContainer = org.omg.CORBA.ContainerHelper.narrow( top );
371             if( topContainer != null )
372             {
373                 return topContainer.lookup( rest_of_name );
374             }
375             else
376             {
377                 if (this.logger.isDebugEnabled())
378                 {
379                     this.logger.debug("Container " + this.name +" " +
380                                       scopedname + " not found, top " +
381                                       top.getClass().getName());
382                 }
383                 return null;
384             }
385         }
386     }
387
388     public org.omg.CORBA.Contained JavaDoc[] lookup_name( String JavaDoc search_name, /*Identifier*/
389                                                   int levels_to_search,
390                                                   org.omg.CORBA.DefinitionKind JavaDoc limit_type,
391                                                   boolean exclude_inherited)
392     {
393         if( levels_to_search == 0 )
394             return null;
395
396         org.omg.CORBA.Contained JavaDoc[] c = contents( limit_type, exclude_inherited );
397         Hashtable found = new Hashtable();
398
399         for( int i = 0; i < c.length; i++)
400             if( c[i].name().equals( search_name ) )
401                 found.put( c[i], "" );
402
403         if( levels_to_search > 1 || levels_to_search < 0 )
404         {
405             // search up to a specific depth or undefinitely
406
for( int i = 0; i < c.length; i++)
407             {
408                 if( c[i] instanceof org.omg.CORBA.Container JavaDoc )
409                 {
410                     org.omg.CORBA.Contained JavaDoc[] tmp_seq =
411                         ((org.omg.CORBA.Container JavaDoc)c[i]).lookup_name(
412                                                                     search_name, levels_to_search-1, limit_type, exclude_inherited);
413                     if( tmp_seq != null )
414                         for( int j = 0; j < tmp_seq.length; j++)
415                             found.put( tmp_seq[j], "" );
416                 }
417             }
418         }
419
420
421         org.omg.CORBA.Contained JavaDoc[] result = new org.omg.CORBA.Contained JavaDoc[ found.size() ];
422         int idx = 0;
423
424         for( Enumeration e = found.keys(); e.hasMoreElements(); )
425             result[ idx++] = (org.omg.CORBA.Contained JavaDoc)e.nextElement();
426
427         return result;
428     }
429
430     public org.omg.CORBA.ContainerPackage.Description[] describe_contents(org.omg.CORBA.DefinitionKind JavaDoc limit_type, boolean exclude_inherited, int max_returned_objs)
431     {
432         return null;
433     }
434
435     public org.omg.CORBA.ModuleDef JavaDoc create_module(/*RepositoryId*/ String JavaDoc id, /*Identifier*/ String JavaDoc name, /*VersionSpec*/ String JavaDoc version){
436         return null;
437     }
438
439     public org.omg.CORBA.ConstantDef JavaDoc create_constant(/*RepositoryId*/ String JavaDoc id, /*Identifier*/ String JavaDoc name, /*VersionSpec*/ String JavaDoc version, org.omg.CORBA.IDLType JavaDoc type, org.omg.CORBA.Any JavaDoc value){
440         return null;
441     }
442
443     public org.omg.CORBA.StructDef JavaDoc create_struct(/*RepositoryId*/ String JavaDoc id, /*Identifier*/ String JavaDoc name, /*VersionSpec*/ String JavaDoc version, /*StructMemberSeq*/ org.omg.CORBA.StructMember JavaDoc[] members){
444         return null;
445     }
446
447     public org.omg.CORBA.UnionDef JavaDoc create_union(/*RepositoryId*/ String JavaDoc id, /*Identifier*/ String JavaDoc name, /*VersionSpec*/ String JavaDoc version, org.omg.CORBA.IDLType JavaDoc discriminator_type, /*UnionMemberSeq*/ org.omg.CORBA.UnionMember JavaDoc[] members){
448         return null;
449     }
450
451     public org.omg.CORBA.EnumDef JavaDoc create_enum(/*RepositoryId*/ String JavaDoc id, /*Identifier*/ String JavaDoc name, /*VersionSpec*/ String JavaDoc version, /*EnumMemberSeq*/ /*Identifier*/ String JavaDoc[] members){
452         return null;
453     }
454
455     public org.omg.CORBA.AliasDef JavaDoc create_alias(/*RepositoryId*/ String JavaDoc id, /*Identifier*/ String JavaDoc name, /*VersionSpec*/ String JavaDoc version, org.omg.CORBA.IDLType JavaDoc original_type){
456         return null;
457     }
458
459     /**
460      * not supported
461      */

462
463     public org.omg.CORBA.ExceptionDef JavaDoc create_exception(java.lang.String JavaDoc id, java.lang.String JavaDoc name , java.lang.String JavaDoc version, org.omg.CORBA.StructMember JavaDoc[] member )
464     {
465         return null;
466     }
467
468     /**
469      * not supported
470      */

471
472     public org.omg.CORBA.InterfaceDef JavaDoc create_interface(
473                     /*RepositoryId*/ String JavaDoc id,
474                     /*Identifier*/ String JavaDoc name,
475                     /*VersionSpec*/ String JavaDoc version,
476                     /*InterfaceDefSeq*/ org.omg.CORBA.InterfaceDef JavaDoc[] base_interfaces,
477                     boolean is_abstract )
478     {
479         return null;
480     }
481
482     /**
483      * not supported
484      */

485
486     public org.omg.CORBA.ValueBoxDef JavaDoc create_value_box(java.lang.String JavaDoc id,
487                                                       java.lang.String JavaDoc name,
488                                                       java.lang.String JavaDoc version,
489                                                       org.omg.CORBA.IDLType JavaDoc type)
490     {
491         return null;
492     }
493
494
495     /**
496      * not supported
497      */

498
499     public org.omg.CORBA.ValueDef JavaDoc create_value(
500                                      java.lang.String JavaDoc id,
501                                      java.lang.String JavaDoc name,
502                                      java.lang.String JavaDoc version,
503                                      boolean is_custom,
504                                      boolean is_abstract,
505                                      org.omg.CORBA.ValueDef JavaDoc base_value,
506                                      boolean is_truncatable,
507                                      org.omg.CORBA.ValueDef JavaDoc[] abstract_base_values,
508                                      org.omg.CORBA.InterfaceDef JavaDoc[] supported_interfaces,
509                                      org.omg.CORBA.Initializer JavaDoc[] initializers)
510     {
511         return null;
512     }
513
514
515     /**
516      * not supported
517      */

518
519     public org.omg.CORBA.NativeDef JavaDoc create_native(java.lang.String JavaDoc id,
520                                                  java.lang.String JavaDoc name,
521                                                  java.lang.String JavaDoc version)
522     {
523         return null;
524     }
525
526
527
528 }
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
Popular Tags