KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > naming > ContextLister


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

20
21 package org.jacorb.naming;
22
23 import org.omg.CosNaming.*;
24 import org.omg.CosNaming.NamingContextPackage.*;
25 import java.io.*;
26 import java.util.*;
27
28 /**
29  * This class allows listing all bindings in a naming context
30  * to a PrintStream
31  *
32  * @author Gerald Brose
33  * @version $Id: ContextLister.java,v 1.9 2004/08/04 08:41:17 andre.spiegel Exp $
34  */

35
36 public class ContextLister
37 {
38     public NamingContextExt root_context;
39     private Hashtable contexts = new Hashtable();
40     private org.omg.CORBA.ORB JavaDoc orb;
41
42     public ContextLister(org.omg.CORBA.ORB JavaDoc orb)
43     {
44         this.orb = orb;
45         // initialise Naming Service via ORB
46
try
47         {
48             org.omg.CORBA.Object JavaDoc obj =
49                 orb.resolve_initial_references("NameService");
50             root_context = NamingContextExtHelper.narrow( obj );
51         }
52         catch( org.omg.CORBA.ORBPackage.InvalidName JavaDoc inex ) {
53             inex.printStackTrace();
54         }
55         catch(org.omg.CORBA.SystemException JavaDoc corba_exception) {
56             System.err.println(corba_exception);
57         }
58
59         if( root_context == null ) {
60             System.err.println("No Naming Context available, giving up ...");
61             System.exit( 1 );
62         }
63     }
64
65     public ContextLister( org.omg.CORBA.ORB JavaDoc orb, String JavaDoc str )
66     {
67         this.orb = orb;
68         // initialise Naming Service via stringified IOR
69
try
70         {
71             org.omg.CORBA.Object JavaDoc obj = orb.string_to_object( str );
72             root_context = NamingContextExtHelper.narrow( obj );
73         }
74         catch(org.omg.CORBA.SystemException JavaDoc corba_exception) {
75             System.err.println(corba_exception);
76         }
77
78         if( root_context == null ) {
79             System.err.println("No Naming Context available, giving up ...");
80             System.exit( 1 );
81         }
82     }
83
84     private void mark(NamingContextExt nc) {
85         contexts.put( orb.object_to_string(nc), "" );
86     }
87
88     private boolean isMarked(NamingContextExt nc)
89     {
90         return contexts.containsKey(orb.object_to_string(nc));
91     }
92
93
94     public void list(java.io.PrintStream JavaDoc ps)
95     {
96         list( root_context, " ", ps);
97     }
98
99     private void list( NamingContextExt n, String JavaDoc indent, java.io.PrintStream JavaDoc ps )
100     {
101         if( isMarked(n))
102         {
103             return;
104         }
105
106         mark(n);
107
108         try
109         {
110             BindingListHolder blsoh =
111                 new BindingListHolder(new Binding[0]);
112
113             BindingIteratorHolder bioh =
114                 new BindingIteratorHolder();
115
116             n.list( 0, blsoh, bioh );
117
118             BindingHolder bh = new BindingHolder();
119
120             if( bioh.value == null )
121                 return;
122
123             while( bioh.value.next_one( bh ))
124             {
125                 String JavaDoc stringName = root_context.to_string( bh.value.binding_name);
126                 ps.print( indent + stringName );
127                 if( bh.value.binding_type.value() == BindingType._ncontext )
128                 {
129                     String JavaDoc _indent = indent + "\t";
130                     ps.println("/");
131                     
132                     NameComponent [] name = root_context.to_name(stringName);
133                     NamingContextExt sub_context =
134                         NamingContextExtHelper.narrow( n.resolve(name) );
135                     list( sub_context, _indent, ps );
136                 }
137                 else
138                     System.out.println();
139             }
140         }
141         catch (Exception JavaDoc e)
142         {
143             e.printStackTrace();
144         }
145     }
146
147
148     public static void main(String JavaDoc args[])
149     {
150         org.omg.CORBA.ORB JavaDoc orb =
151             org.omg.CORBA.ORB.init(args,null);
152
153         ContextLister ctxLister;
154         PrintStream pw = System.out;
155         String JavaDoc url = null;
156
157         for( int i = 0; i < args.length; i += 2 )
158         {
159             try
160             {
161                 if( args[i].startsWith("-f"))
162                 {
163                     try
164                     {
165                         pw = new PrintStream( new FileOutputStream( args[i+1] ));
166                     }
167                     catch( IOException ioe)
168                     {
169                         System.err.println( ioe.getMessage() );
170                         System.exit(1);
171                     }
172                     continue;
173                 }
174                 if( args[i].startsWith("-url"))
175                 {
176                     url = args[i+1];
177                 }
178             }
179             catch( Exception JavaDoc e )
180             {
181                 System.err.println("Usage: org.jacorb.naming.ContextLister [-url object url] [-f output file]");
182                 System.exit(1);
183
184             }
185         }
186
187         if( url != null )
188         {
189             ctxLister = new ContextLister(orb, url);
190         }
191         else
192         {
193             ctxLister = new ContextLister(orb);
194         }
195
196         ctxLister.list( pw );
197         orb.shutdown(true);
198     }
199 }
200
201
202
Popular Tags