1 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 35 36 public class ContextLister 37 { 38 public NamingContextExt root_context; 39 private Hashtable contexts = new Hashtable(); 40 private org.omg.CORBA.ORB orb; 41 42 public ContextLister(org.omg.CORBA.ORB orb) 43 { 44 this.orb = orb; 45 try 47 { 48 org.omg.CORBA.Object obj = 49 orb.resolve_initial_references("NameService"); 50 root_context = NamingContextExtHelper.narrow( obj ); 51 } 52 catch( org.omg.CORBA.ORBPackage.InvalidName inex ) { 53 inex.printStackTrace(); 54 } 55 catch(org.omg.CORBA.SystemException 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 orb, String str ) 66 { 67 this.orb = orb; 68 try 70 { 71 org.omg.CORBA.Object obj = orb.string_to_object( str ); 72 root_context = NamingContextExtHelper.narrow( obj ); 73 } 74 catch(org.omg.CORBA.SystemException 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 ps) 95 { 96 list( root_context, " ", ps); 97 } 98 99 private void list( NamingContextExt n, String indent, java.io.PrintStream 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 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 _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 e) 142 { 143 e.printStackTrace(); 144 } 145 } 146 147 148 public static void main(String args[]) 149 { 150 org.omg.CORBA.ORB orb = 151 org.omg.CORBA.ORB.init(args,null); 152 153 ContextLister ctxLister; 154 PrintStream pw = System.out; 155 String 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 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 |