KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
24 import java.io.*;
25
26 import org.omg.CORBA.INTF_REPOS JavaDoc;
27 import org.omg.PortableServer.POA JavaDoc;
28
29 import org.apache.avalon.framework.logger.Logger;
30
31 public class Container
32     extends IRObject
33     implements org.omg.CORBA.ContainerOperations JavaDoc
34 {
35     protected static char fileSeparator =
36         System.getProperty("file.separator").charAt(0);
37
38     protected IRObject delegator;
39
40     /** CORBA references to contained objects */
41     protected Hashtable contained = new Hashtable();
42
43     /** local references to contained objects */
44     protected Hashtable containedLocals = new Hashtable();
45
46     protected File my_dir = null;
47     protected String JavaDoc path = null;
48     protected String JavaDoc full_name = null;
49
50     /** CORBA reference to this container */
51     protected org.omg.CORBA.Container JavaDoc this_container;
52
53     /** outer container */
54     protected org.omg.CORBA.Container JavaDoc defined_in;
55     protected org.omg.CORBA.Repository JavaDoc containing_repository;
56
57     protected boolean defined = false;
58
59     private ClassLoader JavaDoc loader;
60     private POA JavaDoc poa;
61     private Logger logger;
62
63     /**
64      */

65
66     public Container( IRObject delegator,
67                       String JavaDoc path,
68                       String JavaDoc full_name,
69                       ClassLoader JavaDoc loader,
70                       POA JavaDoc poa,
71                       Logger logger )
72     {
73         this.loader = loader;
74         this.poa = poa;
75         this.logger = logger;
76         this.delegator = delegator;
77         this.path = path;
78         this.full_name = full_name;
79
80         my_dir = new File( path + fileSeparator +
81                            ( full_name != null ? full_name : "" ) );
82
83         if ( ! my_dir.isDirectory())
84         {
85             throw new INTF_REPOS JavaDoc ("no directory : " + path + fileSeparator + full_name);
86         }
87
88         this.name = delegator.getName();
89
90         if (this.logger.isDebugEnabled())
91         {
92             this.logger.debug("New Container full_name " +
93                               full_name + " name : " + name +
94                               " path: " + path);
95         }
96         // else: get reference from delegator, but must be postponed until later
97
}
98
99     /**
100      */

101
102     void loadContents()
103     {
104         this_container =
105             org.omg.CORBA.ContainerHelper.narrow( delegator.getReference());
106         if (this_container == null)
107         {
108             throw new INTF_REPOS JavaDoc ("no container !");
109         }
110
111         if( delegator instanceof Contained )
112         {
113             containing_repository = ((Contained)delegator).containing_repository();
114             defined_in = ((Contained)delegator).defined_in();
115         }
116         else
117         {
118             containing_repository = org.omg.CORBA.
119                 RepositoryHelper.narrow( delegator.getReference());
120             defined_in = containing_repository;
121         }
122
123         if (containing_repository == null)
124         {
125             throw new INTF_REPOS JavaDoc ("no containing repository");
126         }
127
128         String JavaDoc[] classes;
129         String JavaDoc[] dirs;
130
131         // get all files in this directory which either end in ".class" or
132
// do not contain a "." at all
133

134         classes = my_dir.list( new IRFilenameFilter(".class") );
135         dirs = my_dir.list( new IRFilenameFilter( null ) );
136
137         // load class files in this module/package
138
if( classes != null)
139         {
140             String JavaDoc prefix =
141                 ( full_name != null ? full_name + fileSeparator : "");
142
143             for( int j = 0; j< classes.length; j++ )
144             {
145                 try
146                 {
147                     if (this.logger.isDebugEnabled())
148                     {
149                         this.logger.debug("Container " +name+ " tries " +
150                                           prefix +
151                                           classes[j].substring( 0, classes[j].indexOf(".class")));
152                     }
153
154                     Class JavaDoc cl =
155                         this.loader.loadClass(
156                                          ( prefix +
157                                            classes[j].substring( 0, classes[j].indexOf(".class"))
158                                            ).replace( fileSeparator, '.') );
159
160                     Contained containedObject =
161                         Contained.createContained( cl,
162                                                    path,
163                                                    this_container,
164                                                    containing_repository,
165                                                    this.logger,
166                                                    this.loader,
167                                                    this.poa );
168                     if( containedObject == null )
169                     {
170                         if (this.logger.isDebugEnabled())
171                         {
172                             this.logger.debug("Container: nothing created for "
173                                               + cl.getClass().getName());
174                         }
175                         continue;
176                     }
177
178                     org.omg.CORBA.Contained JavaDoc containedRef =
179                         Contained.createContainedReference(containedObject,
180                                                            this.logger,
181                                                            this.poa);
182
183                     containedRef.move( this_container,
184                                        containedRef.name(),
185                                        containedRef.version() );
186
187                     if (this.logger.isDebugEnabled())
188                     {
189                         this.logger.debug("Container " + prefix +
190                                           " loads "+ containedRef.name());
191                     }
192
193                     contained.put( containedRef.name() , containedRef );
194                     containedLocals.put( containedRef.name(), containedObject );
195                     if( containedObject instanceof ContainerType )
196                         ((ContainerType)containedObject).loadContents();
197
198                 }
199                 catch ( java.lang.Throwable JavaDoc e )
200                 {
201                     this.logger.error("Caught exception", e);
202                 }
203             }
204         }
205
206         if( dirs != null)
207         {
208             for( int k = 0; k < dirs.length; k++ )
209             {
210                 if( !dirs[k].endsWith("Package"))
211                 {
212                     File f = new File( my_dir.getAbsolutePath() +
213                                        fileSeparator +
214                                        dirs[k] );
215                     try
216                     {
217                         String JavaDoc [] classList = f.list();
218                         if( classList != null && classList.length > 0)
219                         {
220 // org.jacorb.util.Debug.output(2, "Container " +
221
// full_name + " inspecting " +
222
// dirs[k] + " trying: " +
223
// (( full_name != null ? full_name + "." + dirs[k]: dirs[k] ) +
224
// "." + "_" + dirs[k] + "Module" ).replace( fileSeparator, '.') );
225

226                             Class JavaDoc moduleClass =
227                                 this.loader.loadClass(
228                                   (
229                                    ( full_name != null ? full_name + "." + dirs[k]: dirs[k] ) +
230                                    "." + "_" + dirs[k] + "Module" ).replace( fileSeparator, '.'));
231
232                             ModuleDef m =
233                                 new ModuleDef( path,
234                                                ( full_name != null ?
235                                                  full_name + fileSeparator :
236                                                  ""
237                                                  ) + dirs[k],
238                                                this_container,
239                                                containing_repository,
240                                                this.loader,
241                                                this.poa,
242                                                this.logger);
243
244                             org.omg.CORBA.ModuleDef JavaDoc moduleRef =
245                                 org.omg.CORBA.ModuleDefHelper.narrow(
246                                     this.poa.servant_to_reference(
247                                         new org.omg.CORBA.ModuleDefPOATie( m ) ));
248
249                             m.setReference( moduleRef );
250                             m.loadContents();
251
252                             if (this.logger.isDebugEnabled())
253                             {
254                                 this.logger.debug("Container " +
255                                                   full_name +
256                                                   " puts module " + dirs[k]);
257                             }
258
259                             m.move( this_container, m.name(), m.version() );
260                             contained.put( m.name() , moduleRef );
261                             containedLocals.put( m.name(), m );
262                         }
263                     }
264                     catch( ClassNotFoundException JavaDoc c )
265                     {
266                         this.logger.error("No module meta data: " +
267                                           c.getMessage(), c );
268                     }
269                     catch ( Exception JavaDoc e )
270                     {
271                         this.logger.error("Caught Exception", e);
272                     }
273                 }
274             }
275         }
276     }
277
278     void define()
279     {
280         if (this.logger.isDebugEnabled())
281         {
282             this.logger.debug("Container " + full_name + " defining...");
283         }
284
285         for( Enumeration e = containedLocals.elements();
286              e.hasMoreElements();
287              ((IRObject)e.nextElement()).define())
288             ;
289
290         defined = true;
291
292         if (this.logger.isDebugEnabled())
293         {
294             this.logger.debug("Container " + full_name + " defined");
295         }
296     }
297
298
299     public org.omg.CORBA.Contained JavaDoc[] contents(org.omg.CORBA.DefinitionKind JavaDoc limit_type,
300                                               boolean exclude_inherited)
301     {
302         if ( ! defined)
303         {
304             throw new INTF_REPOS JavaDoc ("contents undefined");
305         }
306
307         Hashtable filtered = new Hashtable();
308
309         if( limit_type.value() == org.omg.CORBA.DefinitionKind._dk_all )
310         {
311             filtered = contained;
312         }
313         else
314         {
315             Enumeration f = contained.keys();
316             while( f.hasMoreElements() )
317             {
318                 Object JavaDoc k = f.nextElement();
319                 org.omg.CORBA.Contained JavaDoc c =
320                     (org.omg.CORBA.Contained JavaDoc)contained.get( k );
321                 if( c.def_kind().value() == limit_type.value() )
322                     filtered.put( k, c );
323             }
324         }
325
326         Enumeration e = filtered.elements();
327         org.omg.CORBA.Contained JavaDoc[] result =
328             new org.omg.CORBA.Contained JavaDoc[ filtered.size() ];
329
330         for( int i = 0; i < filtered.size(); i++ )
331             result[i] = (org.omg.CORBA.Contained JavaDoc)e.nextElement();
332
333         return result;
334     }
335
336     /**
337      * retrieves a contained object given a scoped name
338      */

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

480
481     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 )
482     {
483         return null;
484     }
485
486     /**
487      * not supported
488      */

489
490     public org.omg.CORBA.InterfaceDef JavaDoc create_interface(
491                     /*RepositoryId*/ String JavaDoc id,
492                     /*Identifier*/ String JavaDoc name,
493                     /*VersionSpec*/ String JavaDoc version,
494                     /*InterfaceDefSeq*/ org.omg.CORBA.InterfaceDef JavaDoc[] base_interfaces,
495                     boolean is_abstract )
496     {
497         return null;
498     }
499
500     /**
501      * not supported
502      */

503
504     public org.omg.CORBA.ValueBoxDef JavaDoc create_value_box(java.lang.String JavaDoc id,
505                                                       java.lang.String JavaDoc name,
506                                                       java.lang.String JavaDoc version,
507                                                       org.omg.CORBA.IDLType JavaDoc type)
508     {
509         return null;
510     }
511
512
513     /**
514      * not supported
515      */

516
517     public org.omg.CORBA.ValueDef JavaDoc create_value(
518                                      java.lang.String JavaDoc id,
519                                      java.lang.String JavaDoc name,
520                                      java.lang.String JavaDoc version,
521                                      boolean is_custom,
522                                      boolean is_abstract,
523                                      org.omg.CORBA.ValueDef JavaDoc base_value,
524                                      boolean is_truncatable,
525                                      org.omg.CORBA.ValueDef JavaDoc[] abstract_base_values,
526                                      org.omg.CORBA.InterfaceDef JavaDoc[] supported_interfaces,
527                                      org.omg.CORBA.Initializer JavaDoc[] initializers)
528     {
529         return null;
530     }
531
532
533     /**
534      * not supported
535      */

536
537     public org.omg.CORBA.NativeDef JavaDoc create_native(java.lang.String JavaDoc id,
538                                                  java.lang.String JavaDoc name,
539                                                  java.lang.String JavaDoc version)
540     {
541         return null;
542     }
543
544     public void destroy(){}
545
546
547 }
548
Popular Tags