KickJava   Java API By Example, From Geeks To Geeks.

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


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, Cambrigde, MA 02139, USA.
21  */

22
23 import org.omg.CORBA.ORB JavaDoc;
24 import org.omg.PortableServer.*;
25
26 import java.util.*;
27 import java.io.*;
28
29 import org.apache.avalon.framework.logger.Logger;
30 import org.apache.avalon.framework.configuration.*;
31
32 /**
33  * The Interface Repository.
34  * <p>
35  * This class represents the repository itself as
36  * well as the executable server.
37  * <p>
38  * Methods from the "write" interface to the IR
39  * are not supported.
40  * <p>
41  * @author (c) Gerald Brose, FU Berlin 1997
42  * @version 1.0
43  *
44  */

45
46 public class RepositoryImpl
47     extends IRObject
48     implements org.omg.CORBA.RepositoryOperations JavaDoc, Configurable
49
50 {
51     private String JavaDoc classpath;
52     private Hashtable contained = new Hashtable();
53     private Container[] containers ;
54     private Container delegate ;
55
56     private POA poa;
57     private ClassLoader JavaDoc loader;
58
59     public static char fileSeparator =
60         System.getProperty("file.separator").charAt(0);
61  
62     public static String JavaDoc pathSeparator =
63         System.getProperty("path.separator");
64
65     /** the configuration object for this IR instance */
66     private org.jacorb.config.Configuration configuration = null;
67
68     /** the IR logger instance */
69     private Logger logger = null;
70
71     /**
72      * constructor to launch a repository with the contents of <tt>classpath</tt>
73      *
74      * @param classpath a classpath string made up of directories separated by ":"
75      */

76
77     public RepositoryImpl( String JavaDoc classpath,
78                            String JavaDoc outfile,
79                            //#ifjdk 1.2
80
java.net.URLClassLoader JavaDoc loader )
81                            //#else
82
//# ClassLoader loader )
83
//#endif
84
throws Exception JavaDoc
85     {
86         this.classpath = classpath;
87         this.loader = loader;
88         def_kind = org.omg.CORBA.DefinitionKind.dk_Repository;
89         name = "Repository";
90
91         // parse classpath and create a top-level container for
92
// each directory in the path
93

94         StringTokenizer strtok =
95             new StringTokenizer( classpath , java.io.File.pathSeparator );
96         // new StringTokenizer( classpath , ";" );
97

98         String JavaDoc [] paths =
99             new String JavaDoc [ strtok.countTokens() ];
100
101         containers =
102             new Container[ paths.length ];
103
104         org.omg.CORBA.Object JavaDoc obj;
105
106         org.omg.CORBA.ORB JavaDoc orb =
107             org.omg.CORBA.ORB.init( new String JavaDoc[0], null );
108         
109         this.configure(((org.jacorb.orb.ORB) orb).getConfiguration());
110
111         poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
112         
113         org.omg.CORBA.Repository JavaDoc myRef =
114             org.omg.CORBA.RepositoryHelper.narrow(
115                 poa.servant_to_reference( new org.omg.CORBA.RepositoryPOATie( this ) ) );
116         
117        
118         for( int i = 0; strtok.hasMoreTokens(); i++ )
119         {
120             paths[i] = strtok.nextToken();
121             
122             if (this.logger.isDebugEnabled())
123             {
124                 logger.debug("found path: " + paths[i]);
125             }
126             
127             containers[i] = new Container( this, paths[i], null,
128                                            loader, poa, logger );
129         }
130         
131         // dummy
132
delegate = containers[0];
133         
134         PrintWriter out = new PrintWriter( new FileOutputStream( outfile ), true);
135         out.println( orb.object_to_string( myRef ) );
136         setReference( myRef );
137         out.close();
138         poa.the_POAManager().activate();
139         
140         if (this.logger.isInfoEnabled())
141         {
142             //#ifjdk 1.2
143
java.net.URL JavaDoc urls[] = loader.getURLs();
144             //#else
145
//# java.net.URL urls[] = new java.net.URL[0];
146
//#endif
147
StringBuffer JavaDoc sb =
148                 new StringBuffer JavaDoc("IR configured for class path: ");
149             for( int i = 0; i < urls.length; i++ )
150             {
151                 sb.append( urls[i].toString() + "\n");
152             }
153             
154             logger.info(sb.toString());
155         }
156     }
157
158     public void configure(Configuration myConfiguration)
159         throws ConfigurationException
160     {
161         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
162         this.logger = configuration.getNamedLogger("jacorb.ir");
163     }
164
165     // Repository
166

167     /**
168      * convert a repository ID to a scoped name
169      * @param id a string in Repository ID format, e.g. "IDL:myModule/MyInterface:1.0"
170      * @return a scoped name, e.g. "::myModule::MyInterface", or null
171      * if the id argument does not begin with "IDL:"
172      */

173
174     private String JavaDoc idToScopedName( String JavaDoc id )
175     {
176         String JavaDoc scoped = "";
177
178         if( !id.startsWith("IDL:") ||
179             !id.endsWith( ":1.0"))
180             return null;
181
182         // strip "IDL:" and ":1.0")
183

184         String JavaDoc base = id.substring( id.indexOf(':')+1,
185                                     id.lastIndexOf(':')).replace( fileSeparator, '/' );
186
187         if( base.startsWith( "omg.org") )
188             base = "org/omg" + base.substring( 7 );
189
190         StringTokenizer strtok = new StringTokenizer( base, "/" );
191
192         for( int i = 0; strtok.hasMoreTokens(); i++ )
193             scoped = scoped + "::" + strtok.nextToken();
194
195         return scoped;
196
197     }
198
199     /**
200      * lookup a repository ID
201      * @param id a string in Repository ID format,
202      * e.g. "IDL:myModule/MyInterface:1.0"
203      * @return a reference to the object or null, if not found
204      */

205
206     public org.omg.CORBA.Contained JavaDoc lookup_id( String JavaDoc search_id )
207     {
208         if (this.logger.isDebugEnabled())
209         {
210             this.logger.debug("IR lookup_id: " + search_id );
211         }
212
213         String JavaDoc name = idToScopedName( search_id );
214         if( name == null )
215             return null;
216         else
217             return lookup( name );
218     }
219
220     public org.omg.CORBA.PrimitiveDef JavaDoc get_primitive(org.omg.CORBA.PrimitiveKind JavaDoc kind)
221     {
222         try
223         {
224             return org.omg.CORBA.PrimitiveDefHelper.narrow(
225                 poa.servant_to_reference(
226                         new org.omg.CORBA.PrimitiveDefPOATie(
227                             new org.jacorb.ir.PrimitiveDef( kind.value() ))));
228         }
229         catch( Exception JavaDoc e )
230         {
231             e.printStackTrace();
232             return null;
233         }
234     }
235
236     /**
237      * not supported
238      */

239
240     public org.omg.CORBA.StringDef JavaDoc create_string(int bound)
241     {
242         return null;
243     }
244
245     /**
246      * not supported
247      */

248
249     public org.omg.CORBA.WstringDef JavaDoc create_wstring(int bound)
250     {
251         return null;
252     }
253
254     /**
255      * not supported
256      */

257
258     public org.omg.CORBA.FixedDef JavaDoc create_fixed(short digits, short scale)
259     {
260         return null;
261     }
262
263     /**
264      * not supported
265      */

266
267     public org.omg.CORBA.SequenceDef JavaDoc create_sequence(int bound,
268                                                      org.omg.CORBA.IDLType JavaDoc element_type)
269     {
270         return null;
271     }
272
273     /**
274      * not supported
275      */

276
277     public org.omg.CORBA.ArrayDef JavaDoc create_array(int length,
278                                                org.omg.CORBA.IDLType JavaDoc element_type)
279     {
280         return null;
281     }
282
283
284
285     public org.omg.CORBA.TypeCode JavaDoc get_canonical_typecode(org.omg.CORBA.TypeCode JavaDoc tc)
286     {
287         return null;
288     }
289
290
291     // container
292

293     /**
294      * lookup a scoped name in the repository
295      *
296      * @param name the name to look for
297      * @return a reference to the item with the specified name
298      * or null, if not found
299      */

300
301
302     public org.omg.CORBA.Contained JavaDoc lookup( String JavaDoc name )
303     {
304         if (this.logger.isDebugEnabled())
305         {
306             this.logger.debug("IR lookup : " + name );
307         }
308
309         org.omg.CORBA.Contained JavaDoc result = null;
310         for( int i = 0; i < containers.length; i++ )
311         {
312             result = containers[i].lookup( name );
313             if( result != null )
314                 break;
315         }
316         return result;
317     }
318
319     /**
320      * lookup a simple name in the repository
321      * (neither scoped nor ID formatted)
322      *
323      * @param search_name the name to look for
324      * @param levels_to_search if 1, search only this object, if -1, search
325      * all containers contained in this repository, else search
326      * until the specified depth is reached
327      * @param limit_type limit the description to objects of this type
328      * @param exclude_inherited exclude inherited items from the description
329      * @return an array of items with the specified name
330      */

331
332     public org.omg.CORBA.Contained JavaDoc[] lookup_name(
333                 String JavaDoc search_name,
334                 int levels_to_search,
335                 org.omg.CORBA.DefinitionKind JavaDoc limit_type,
336                 boolean exclude_inherited )
337     {
338         if (this.logger.isDebugEnabled())
339         {
340             this.logger.debug("IR lookup_name: " + search_name);
341         }
342
343         org.omg.CORBA.Contained JavaDoc[] result = null;
344         Vector intermediate = new Vector();
345
346         for( int i = 0; i < containers.length; i++ )
347         {
348             intermediate.addElement( containers[i].lookup_name( search_name,
349                                                                 levels_to_search,
350                                                                 limit_type,
351                                                                 exclude_inherited ));
352         }
353         
354         int size = 0;
355         for( int i = 0; i < intermediate.size(); i++ )
356         {
357             size += ((Object JavaDoc[])intermediate.elementAt(i)).length;
358         }
359         result = new org.omg.CORBA.Contained JavaDoc[size];
360         int start = 0;
361
362         for( int i = 0; i < intermediate.size(); i++ )
363         {
364             org.omg.CORBA.Contained JavaDoc[] src =
365                 (org.omg.CORBA.Contained JavaDoc[])intermediate.elementAt(i);
366
367             System.arraycopy( src, 0, result, start, src.length );
368             start += src.length;
369         }
370
371     return result;
372     }
373
374
375     /**
376      * list the contents of the repository
377      * @param limit_type limit the description to objects of this type
378      * @param exclude_inherited exclude inherited items from the description
379      * @return an array of items contained in this repository
380      */

381
382     public org.omg.CORBA.Contained JavaDoc[] contents(org.omg.CORBA.DefinitionKind JavaDoc limit_type,
383                                               boolean exclude_inherited)
384     {
385         org.omg.CORBA.Contained JavaDoc[] result = null;
386         Vector intermediate = new Vector();
387         for( int i = 0; i < containers.length; i++ )
388         {
389             intermediate.addElement( containers[i].contents( limit_type,
390                                                              exclude_inherited ));
391         }
392         
393         int size = 0;
394         for( int i = 0; i < intermediate.size(); i++ )
395         {
396             size += ((Object JavaDoc[])intermediate.elementAt(i)).length;
397         }
398
399         result = new org.omg.CORBA.Contained JavaDoc[size];
400         int start = 0;
401
402         // assemble result array
403
for( int i = 0; i < intermediate.size(); i++ )
404         {
405             org.omg.CORBA.Contained JavaDoc[] src =
406                 (org.omg.CORBA.Contained JavaDoc[])intermediate.elementAt(i);
407
408             System.arraycopy( src, 0, result, start, src.length );
409             start += src.length;
410         }
411
412     return result;
413     }
414
415
416     /**
417      * describe the contents of the repository
418      * @param limit_type limit the description to objects of this type
419      * @param exclude_inherited exclude inherited items from the description
420      * @param max_returned_objs return only so many items
421      * @return an array of descriptions
422      */

423
424     public org.omg.CORBA.ContainerPackage.Description[] describe_contents(
425                            org.omg.CORBA.DefinitionKind JavaDoc limit_type,
426                            boolean exclude_inherited,
427                            int max_returned_objs)
428     {
429         org.omg.CORBA.Contained JavaDoc[] c = contents( limit_type, exclude_inherited );
430         int size;
431         if( max_returned_objs > c.length )
432             size = max_returned_objs;
433         else
434             size = c.length;
435         org.omg.CORBA.ContainerPackage.Description[] result =
436             new org.omg.CORBA.ContainerPackage.Description[size];
437         for( int i = 0; i < size; i++ )
438         {
439             result[i] = new org.omg.CORBA.ContainerPackage.Description();
440             org.omg.CORBA.ContainedPackage.Description cd_descr = c[i].describe();
441             result[i].contained_object = c[i];
442             result[i].kind = cd_descr.kind;
443             result[i].value = cd_descr.value;
444         }
445         return result;
446     }
447
448
449     void define()
450     {
451         // do nothing
452
}
453
454     public void loadContents()
455     {
456         if (this.logger.isInfoEnabled())
457         {
458             this.logger.info("Repository loads contents...");
459         }
460
461         for( int i = 0; i < containers.length; i++ )
462         {
463             containers[i].loadContents();
464         }
465         for( int i = 0; i < containers.length; i++ )
466         {
467             containers[i].define();
468         }
469
470         if (this.logger.isInfoEnabled())
471         {
472             this.logger.info("Repository contents loaded");
473         }
474     }
475
476     public org.omg.CORBA.ModuleDef JavaDoc create_module( String JavaDoc id,
477                                                   String JavaDoc name,
478                                                   String JavaDoc version)
479     {
480         return delegate.create_module( id, name, version);
481     }
482
483     public org.omg.CORBA.ConstantDef JavaDoc create_constant( String JavaDoc id, String JavaDoc name,
484                                                       String JavaDoc version,
485                                                       org.omg.CORBA.IDLType JavaDoc type,
486                                                       org.omg.CORBA.Any JavaDoc value)
487     {
488         return delegate.create_constant( id, name, version, type, value);
489     }
490
491     public org.omg.CORBA.StructDef JavaDoc create_struct( String JavaDoc id,
492                                                   String JavaDoc name,
493                                                   String JavaDoc version,
494                                                   org.omg.CORBA.StructMember JavaDoc[] members)
495     {
496         return delegate.create_struct( id, name, version, members);
497     }
498
499     public org.omg.CORBA.UnionDef JavaDoc create_union( String JavaDoc id,
500                                                 String JavaDoc name,
501                                                 String JavaDoc version,
502                                                 org.omg.CORBA.IDLType JavaDoc
503                                                 discriminator_type,
504                                                 org.omg.CORBA.UnionMember JavaDoc[] members)
505     {
506         return delegate.create_union( id, name, version, discriminator_type, members);
507     }
508
509     public org.omg.CORBA.EnumDef JavaDoc create_enum( String JavaDoc id,
510                                               String JavaDoc name,
511                                               String JavaDoc version,
512                                               String JavaDoc[] members)
513     {
514         return delegate.create_enum( id, name, version, members);
515     }
516
517     public org.omg.CORBA.AliasDef JavaDoc create_alias( String JavaDoc id,
518                                                 String JavaDoc name,
519                                                 String JavaDoc version,
520                                                 org.omg.CORBA.IDLType JavaDoc original_type)
521     {
522         return delegate.create_alias( id, name, version, original_type);
523     }
524
525     public org.omg.CORBA.ExceptionDef JavaDoc create_exception( String JavaDoc id,
526                                                         String JavaDoc name,
527                                                         String JavaDoc version,
528                                                         org.omg.CORBA.StructMember JavaDoc[] member )
529     {
530         return delegate.create_exception(id, name, version, member);
531     }
532
533     /**
534      * not supported
535      */

536
537     public org.omg.CORBA.InterfaceDef JavaDoc create_interface(String JavaDoc id,
538                                                        String JavaDoc name,
539                                                        String JavaDoc version,
540                                                        org.omg.CORBA.InterfaceDef JavaDoc[] base_interfaces,
541                                                        boolean is_abstract )
542     {
543         return delegate.create_interface( id, name, version,
544                     base_interfaces, is_abstract );
545     }
546
547     /**
548      * not supported
549      */

550
551     public org.omg.CORBA.ValueBoxDef JavaDoc create_value_box(String JavaDoc id,
552                                                       String JavaDoc name,
553                                                       String JavaDoc version,
554                                                       org.omg.CORBA.IDLType JavaDoc type)
555     {
556         return delegate.create_value_box(id, name, version, type);
557     }
558
559     /**
560      * not supported
561      */

562
563     public org.omg.CORBA.ValueDef JavaDoc create_value(
564                                      String JavaDoc id,
565                                      String JavaDoc name,
566                                      String JavaDoc version,
567                                      boolean is_custom,
568                                      boolean is_abstract,
569                                      org.omg.CORBA.ValueDef JavaDoc base_value,
570                                      boolean is_truncatable,
571                                      org.omg.CORBA.ValueDef JavaDoc[] abstract_base_values,
572                                      org.omg.CORBA.InterfaceDef JavaDoc[] supported_interfaces,
573                                      org.omg.CORBA.Initializer JavaDoc[] initializers)
574     {
575         return delegate.create_value(id, name, version,is_custom, is_abstract, base_value, is_truncatable,
576                                      abstract_base_values, supported_interfaces, initializers);
577     }
578
579
580     /**
581      * not supported
582      */

583
584     public org.omg.CORBA.NativeDef JavaDoc create_native(String JavaDoc id,
585                                                  String JavaDoc name,
586                                                  String JavaDoc version)
587     {
588         return delegate.create_native( id, name, version);
589     }
590  
591
592     public void destroy()
593     {
594         delegate.destroy();
595     }
596
597
598 }
599
600
601
602
603
604
605
606
607
608
609
Popular Tags