KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.omg.CORBA.INTF_REPOS JavaDoc;
24 import org.omg.PortableServer.POA JavaDoc;
25
26 import java.lang.reflect.*;
27 import java.util.*;
28 import java.io.*;
29
30 import org.apache.avalon.framework.logger.Logger;
31
32 public class StructDef
33     extends TypedefDef
34     implements org.omg.CORBA.StructDefOperations JavaDoc, ContainerType
35 {
36     protected static char fileSeparator =
37         System.getProperty("file.separator").charAt(0);
38
39     private org.omg.CORBA.TypeCode JavaDoc type;
40     private Class JavaDoc myClass;
41     private Class JavaDoc helperClass;
42     private org.omg.CORBA.StructMember JavaDoc[] members;
43
44     /** local references to contained objects */
45     private Hashtable containedLocals = new Hashtable();
46     /** CORBA references to contained objects */
47     private Hashtable contained = new Hashtable();
48
49     private File my_dir;
50     private String JavaDoc path;
51
52     private boolean defined = false;
53
54     private Logger logger;
55     private ClassLoader JavaDoc loader;
56     private POA JavaDoc poa;
57
58     public StructDef(Class JavaDoc c,
59                      String JavaDoc path,
60                      org.omg.CORBA.Container JavaDoc _defined_in,
61                      org.omg.CORBA.Repository JavaDoc ir,
62                      Logger logger,
63                      ClassLoader JavaDoc loader,
64                      POA JavaDoc poa)
65     {
66         this.logger = logger;
67         this.loader = loader;
68         this.poa = poa;
69         def_kind = org.omg.CORBA.DefinitionKind.dk_Struct;
70         containing_repository = ir;
71         defined_in = _defined_in;
72         this.path = path;
73         if (defined_in == null)
74         {
75             throw new INTF_REPOS JavaDoc ("defined_in = null");
76         }
77         if (containing_repository == null)
78         {
79             throw new INTF_REPOS JavaDoc ("containing_repository = null");
80         }
81
82         try
83         {
84             String JavaDoc classId = c.getName();
85             myClass = c;
86             version( "1.0" );
87             full_name = classId.replace('.', '/');
88
89             if( classId.indexOf('.') > 0 )
90             {
91                 name( classId.substring( classId.lastIndexOf('.')+1 ) );
92                 absolute_name =
93                     org.omg.CORBA.ContainedHelper.narrow( defined_in ).absolute_name() +
94                     "::" + name;
95             }
96             else
97             {
98                 name( classId );
99                 absolute_name = "::" + name;
100             }
101
102             helperClass = this.loader.loadClass( classId + "Helper") ;
103             id( (String JavaDoc)helperClass.getDeclaredMethod( "id", (Class JavaDoc[]) null ).invoke( null, (Object JavaDoc[]) null ));
104
105 // type =
106
// TypeCodeUtil.getTypeCode( myClass, this.loader, null, classId );
107

108             type = (org.omg.CORBA.TypeCode JavaDoc)helperClass.getDeclaredMethod(
109                                                    "type",
110                                                    (Class JavaDoc[]) null ).invoke( null, (Object JavaDoc[]) null );
111
112             members = new org.omg.CORBA.StructMember JavaDoc[ type.member_count() ];
113             for( int i = 0; i < members.length; i++ )
114             {
115                 org.omg.CORBA.TypeCode JavaDoc type_code = type.member_type(i);
116                 String JavaDoc member_name = type.member_name(i);
117
118                 if (this.logger.isDebugEnabled())
119                 {
120                     this.logger.debug("StructDef " + absolute_name +
121                                       " member " + member_name);
122                 }
123
124                 members[i] = new org.omg.CORBA.StructMember JavaDoc( member_name,
125                                                              type_code,
126                                                              null );
127             }
128             /* get directory for nested definitions' classes */
129             File f = new File( path + fileSeparator +
130                                classId.replace('.', fileSeparator) + "Package" );
131
132             if( f.exists() && f.isDirectory() )
133                 my_dir = f;
134
135             if (this.logger.isDebugEnabled())
136             {
137                 this.logger.debug("StructDef: " + absolute_name);
138             }
139         }
140         catch ( Exception JavaDoc e )
141         {
142             logger.error("Caught Exception", e);
143             throw new INTF_REPOS JavaDoc( ErrorMsg.IR_Not_Implemented,
144                                   org.omg.CORBA.CompletionStatus.COMPLETED_NO);
145         }
146     }
147
148
149     public void loadContents()
150     {
151         // read from the class (operations and atributes)
152
if (getReference () == null)
153         {
154             throw new INTF_REPOS JavaDoc ("getReference returns null");
155         }
156
157         org.omg.CORBA.StructDef JavaDoc myReference =
158             org.omg.CORBA.StructDefHelper.narrow( getReference());
159
160         if (myReference == null)
161         {
162             throw new INTF_REPOS JavaDoc ("narrow failed for " + getReference() );
163         }
164
165         /* load nested definitions from interfacePackage directory */
166
167         String JavaDoc[] classes = null;
168         if( my_dir != null )
169         {
170             classes = my_dir.list( new IRFilenameFilter(".class") );
171
172             // load class files in this interface's Package directory
173
if( classes != null)
174             {
175                 for( int j = 0; j < classes.length; j++ )
176                 {
177                     try
178                     {
179                         if (this.logger.isDebugEnabled())
180                         {
181                             this.logger.debug("Struct " +name+ " tries " +
182                                               full_name.replace('.', fileSeparator) +
183                                               "Package" + fileSeparator +
184                                               classes[j].substring( 0, classes[j].indexOf(".class")));
185                         }
186
187                         ClassLoader JavaDoc loader = getClass().getClassLoader();
188                         if( loader == null )
189                         {
190                             loader = this.loader;
191                         }
192
193                         Class JavaDoc cl =
194                             loader.loadClass(
195                                              ( full_name.replace('.', fileSeparator) + "Package" + fileSeparator +
196                                                classes[j].substring( 0, classes[j].indexOf(".class"))
197                                                ).replace( fileSeparator, '/') );
198
199
200                         Contained containedObject =
201                             Contained.createContained( cl,
202                                                        path,
203                                                        myReference,
204                                                        containing_repository,
205                                                        this.logger,
206                                                        this.loader,
207                                                        this.poa);
208                         if( containedObject == null )
209                             continue;
210
211                         org.omg.CORBA.Contained JavaDoc containedRef =
212                             Contained.createContainedReference(containedObject,
213                                                                this.logger,
214                                                                this.poa);
215
216                         if( containedObject instanceof ContainerType )
217                             ((ContainerType)containedObject).loadContents();
218
219                         containedRef.move( myReference, containedRef.name(), containedRef.version() );
220
221                         if (this.logger.isDebugEnabled())
222                         {
223                             this.logger.debug("Struct " + full_name +
224                                               " loads "+ containedRef.name() );
225                         }
226
227                         contained.put( containedRef.name() , containedRef );
228                         containedLocals.put( containedRef.name(), containedObject );
229                     }
230                     catch ( Exception JavaDoc e )
231                     {
232                         logger.error("Caught Exception", e);
233                     }
234                 }
235             }
236         }
237     }
238
239
240     /**
241      */

242
243     public void define()
244     {
245         if (this.logger.isDebugEnabled())
246         {
247             this.logger.debug("Struct " + name + " defining...");
248         }
249
250         for( Enumeration e = containedLocals.elements();
251              e.hasMoreElements();
252              ((IRObject)e.nextElement()).define())
253             ;
254
255         for( int i = 0; i < members.length; i++ )
256         {
257             members[i].type_def =
258                 IDLType.create( members[i].type, containing_repository,
259                                 this.logger, this.poa);
260
261             if (members[i].type_def == null)
262             {
263                 throw new INTF_REPOS JavaDoc ("No type_def for member " + members[i].name +
264                                       " in struct " + full_name );
265             }
266         }
267         defined = true;
268
269         if (this.logger.isDebugEnabled())
270         {
271             this.logger.debug("Struct " + name + " defined");
272         }
273     }
274
275     /**
276      */

277
278     org.omg.CORBA.TypeDescription JavaDoc describe_struct()
279     {
280         if ( ! defined)
281         {
282             throw new INTF_REPOS JavaDoc ("Struct " + full_name + " not defined! ");
283         }
284
285         return new org.omg.CORBA.TypeDescription JavaDoc(name(),
286                                                  id(),
287                                                  org.omg.CORBA.ContainedHelper.narrow( defined_in ).id(),
288                                                  version(),
289                                                  type());
290     }
291
292     public org.omg.CORBA.TypeCode JavaDoc type()
293     {
294         if (type == null)
295         {
296             throw new INTF_REPOS JavaDoc ("Struct TypeCode is null");
297         }
298
299         return type;
300     }
301
302     public org.omg.CORBA.Contained JavaDoc lookup( String JavaDoc scopedname )
303     {
304         if (this.logger.isDebugEnabled())
305         {
306             this.logger.debug("Struct " + this.name + " lookup " + scopedname);
307         }
308
309         String JavaDoc top_level_name;
310         String JavaDoc rest_of_name;
311         String JavaDoc name;
312
313         if( scopedname.startsWith("::") )
314         {
315             name = scopedname.substring(2);
316         }
317         else
318             name = scopedname;
319
320         if( name.indexOf("::") > 0 )
321         {
322             top_level_name = name.substring( 0, name.indexOf("::") );
323             rest_of_name = name.substring( name.indexOf("::") + 2);
324         }
325         else
326         {
327             top_level_name = name;
328             rest_of_name = null;
329         }
330
331         try
332         {
333             org.omg.CORBA.Contained JavaDoc top =
334                 (org.omg.CORBA.Contained JavaDoc)contained.get( top_level_name );
335
336             if( top == null )
337             {
338                 if (this.logger.isDebugEnabled())
339                 {
340                     this.logger.debug("Interface " + this.name +
341                                       " top " + top_level_name +
342                                       " not found ");
343                 }
344                 return null;
345             }
346
347             if( rest_of_name == null )
348             {
349                 return top;
350             }
351             else
352             {
353                 if( top instanceof org.omg.CORBA.Container JavaDoc)
354                 {
355                     return ((org.omg.CORBA.Container JavaDoc)top).lookup( rest_of_name );
356                 }
357                 else
358                 {
359                     if (this.logger.isDebugEnabled())
360                     {
361                         this.logger.debug("Interface " + this.name +
362                                           " " + scopedname + " not found");
363                     }
364                     return null;
365                 }
366             }
367         }
368         catch( Exception JavaDoc e )
369         {
370             logger.error("Caught Exception", e);
371             return null;
372         }
373     }
374
375     public org.omg.CORBA.StructMember JavaDoc[] members()
376     {
377         if ( ! defined)
378         {
379             throw new INTF_REPOS JavaDoc ("Struct " + full_name + " not defined! ");
380         }
381
382         return members;
383     }
384
385
386     // write interface not supported!
387

388     public void members(org.omg.CORBA.StructMember JavaDoc[] a)
389     {
390     }
391
392
393     public org.omg.CORBA.ModuleDef JavaDoc create_module( String JavaDoc id, String JavaDoc name, String JavaDoc version)
394     {
395         return null;
396     }
397
398     public org.omg.CORBA.ConstantDef JavaDoc create_constant(java.lang.String JavaDoc id,
399                                                      java.lang.String JavaDoc name,
400                                                      java.lang.String JavaDoc version,
401                                                      org.omg.CORBA.IDLType JavaDoc type,
402                                                      org.omg.CORBA.Any JavaDoc value)
403     {
404         return null;
405     }
406
407     public org.omg.CORBA.StructDef JavaDoc create_struct( String JavaDoc id, String JavaDoc name,
408                                                   String JavaDoc version,
409                                                   org.omg.CORBA.StructMember JavaDoc[] members){
410         return null;
411     }
412
413     public org.omg.CORBA.UnionDef JavaDoc create_union( String JavaDoc id, String JavaDoc name,
414                                                 String JavaDoc version,
415                                                 org.omg.CORBA.IDLType JavaDoc discriminator_type,
416                                                 org.omg.CORBA.UnionMember JavaDoc[] members){
417         return null;
418     }
419
420     public org.omg.CORBA.EnumDef JavaDoc create_enum( String JavaDoc id, String JavaDoc name,
421                                               String JavaDoc version, String JavaDoc[] members){
422         return null;
423     }
424
425     public org.omg.CORBA.AliasDef JavaDoc create_alias( String JavaDoc id, String JavaDoc name,
426                                                 String JavaDoc version,
427                                                 org.omg.CORBA.IDLType JavaDoc original_type){
428         return null;
429     }
430
431     /**
432      * not supported
433      */

434
435     public org.omg.CORBA.ExceptionDef JavaDoc create_exception(java.lang.String JavaDoc id,
436                                                        java.lang.String JavaDoc name ,
437                                                        java.lang.String JavaDoc version,
438                                                        org.omg.CORBA.StructMember JavaDoc[] member )
439     {
440         return null;
441     }
442
443     /**
444      * not supported
445      */

446
447     public org.omg.CORBA.InterfaceDef JavaDoc create_interface(
448                                                        String JavaDoc id,
449                                                        String JavaDoc name,
450                                                        String JavaDoc version,
451                                                        org.omg.CORBA.InterfaceDef JavaDoc[] base_interfaces,
452                                                        boolean is_abstract )
453     {
454         return null;
455     }
456
457     /**
458      * not supported
459      */

460
461     public org.omg.CORBA.ValueBoxDef JavaDoc create_value_box(java.lang.String JavaDoc id,
462                                                       java.lang.String JavaDoc name,
463                                                       java.lang.String JavaDoc version,
464                                                       org.omg.CORBA.IDLType JavaDoc type)
465     {
466         return null;
467     }
468
469
470     /**
471      * not supported
472      */

473
474     public org.omg.CORBA.ValueDef JavaDoc create_value(
475                                                 java.lang.String JavaDoc id,
476                                                 java.lang.String JavaDoc name,
477                                                 java.lang.String JavaDoc version,
478                                                 boolean is_custom,
479                                                 boolean is_abstract,
480                                                 org.omg.CORBA.ValueDef JavaDoc base_value,
481                                                 boolean is_truncatable,
482                                                 org.omg.CORBA.ValueDef JavaDoc[] abstract_base_values,
483                                                 org.omg.CORBA.InterfaceDef JavaDoc[] supported_interfaces,
484                                                 org.omg.CORBA.Initializer JavaDoc[] initializers)
485     {
486         return null;
487     }
488
489
490     /**
491      * not supported
492      */

493
494     public org.omg.CORBA.NativeDef JavaDoc create_native(java.lang.String JavaDoc id,
495                                                  java.lang.String JavaDoc name,
496                                                  java.lang.String JavaDoc version)
497     {
498         return null;
499     }
500
501
502
503
504     public void destroy()
505     {
506         throw new INTF_REPOS JavaDoc(ErrorMsg.IR_Not_Implemented,
507                              org.omg.CORBA.CompletionStatus.COMPLETED_NO);
508     }
509
510
511     public org.omg.CORBA.Contained JavaDoc[] lookup_name( String JavaDoc search_name,
512                                                   int levels_to_search,
513                                                   org.omg.CORBA.DefinitionKind JavaDoc limit_type,
514                                                   boolean exclude_inherited )
515     {
516         if( levels_to_search == 0 )
517             return null;
518
519         org.omg.CORBA.Contained JavaDoc[] c = contents( limit_type, exclude_inherited );
520         Hashtable found = new Hashtable();
521
522         for( int i = 0; i < c.length; i++)
523             if( c[i].name().equals( search_name ) )
524                 found.put( c[i], "" );
525
526         if( levels_to_search > 1 || levels_to_search == -1 )
527         {
528             // search up to a specific depth or undefinitely
529
for( int i = 0; i < c.length; i++)
530             {
531                 if( c[i] instanceof org.omg.CORBA.Container JavaDoc )
532                 {
533                     org.omg.CORBA.Contained JavaDoc[] tmp_seq =
534                         ((org.omg.CORBA.Container JavaDoc)c[i]).lookup_name( search_name,
535                                                                      levels_to_search-1,
536                                                                      limit_type,
537                                                                      exclude_inherited);
538                     if( tmp_seq != null )
539                         for( int j = 0; j < tmp_seq.length; j++)
540                             found.put( tmp_seq[j], "" );
541                 }
542             }
543         }
544
545
546         org.omg.CORBA.Contained JavaDoc[] result = new org.omg.CORBA.Contained JavaDoc[ found.size() ];
547         int idx = 0;
548
549         for( Enumeration e = found.keys(); e.hasMoreElements(); )
550             result[ idx++] = (org.omg.CORBA.Contained JavaDoc)e.nextElement();
551
552         return result;
553     }
554
555     public org.omg.CORBA.ContainerPackage.Description[] describe_contents(
556                                                      org.omg.CORBA.DefinitionKind JavaDoc limit_type,
557                                                      boolean exclude_inherited,
558                                                      int max_returned_objs )
559     {
560         return null;
561     }
562
563
564     public org.omg.CORBA.Contained JavaDoc[] contents(org.omg.CORBA.DefinitionKind JavaDoc limit_type,
565                                               boolean exclude_inherited)
566     {
567         Hashtable limited = new Hashtable();
568
569         // analog constants, exceptions etc.
570

571         for( Enumeration e = contained.elements(); e.hasMoreElements(); )
572         {
573             org.omg.CORBA.Contained JavaDoc c = (org.omg.CORBA.Contained JavaDoc)e.nextElement();
574             if( limit_type == org.omg.CORBA.DefinitionKind.dk_all ||
575                 limit_type == c.def_kind() )
576             {
577                 limited.put( c, "" );
578             }
579         }
580
581         org.omg.CORBA.Contained JavaDoc[] c = new org.omg.CORBA.Contained JavaDoc[limited.size()];
582         int i;
583         Enumeration e;
584         for( e = limited.keys(), i=0 ; e.hasMoreElements(); i++ )
585             c[i] = (org.omg.CORBA.Contained JavaDoc)e.nextElement();
586         return c;
587     }
588
589
590     // from Contained
591

592     public org.omg.CORBA.ContainedPackage.Description describe()
593     {
594         if ( ! defined)
595         {
596             throw new INTF_REPOS JavaDoc ("Struct " + full_name + " not defined! ");
597         }
598
599         org.omg.CORBA.Any JavaDoc a = orb.create_any();
600         org.omg.CORBA.TypeDescription JavaDoc ed = describe_struct();
601         org.omg.CORBA.TypeDescriptionHelper.insert( a, ed );
602         return new org.omg.CORBA.ContainedPackage.Description(
603                                                               org.omg.CORBA.DefinitionKind.dk_Struct, a);
604     }
605
606 }
607
Popular Tags