KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > util > PrintIOR


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.orb.util;
22
23 import org.apache.avalon.framework.logger.Logger;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.jacorb.orb.CDRInputStream;
31 import org.jacorb.orb.IIOPAddress;
32 import org.jacorb.orb.ORBConstants;
33 import org.jacorb.orb.ParsedIOR;
34 import org.jacorb.orb.TaggedComponentList;
35 import org.jacorb.orb.giop.CodeSet;
36 import org.jacorb.orb.iiop.IIOPProfile;
37
38 import org.omg.CONV_FRAME.CodeSetComponentInfoHelper;
39 import org.omg.CSIIOP.CompoundSecMechList;
40 import org.omg.CSIIOP.CompoundSecMechListHelper;
41 import org.omg.CSIIOP.TAG_CSI_SEC_MECH_LIST;
42 import org.omg.CSIIOP.TAG_NULL_TAG;
43 import org.omg.CSIIOP.TAG_SECIOP_SEC_TRANS;
44 import org.omg.CSIIOP.TAG_TLS_SEC_TRANS;
45 import org.omg.CSIIOP.TLS_SEC_TRANS;
46 import org.omg.CSIIOP.TLS_SEC_TRANSHelper;
47 import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS JavaDoc;
48 import org.omg.IOP.TAG_CODE_SETS JavaDoc;
49 import org.omg.IOP.TAG_JAVA_CODEBASE JavaDoc;
50 import org.omg.IOP.TAG_ORB_TYPE JavaDoc;
51 import org.omg.IOP.TaggedComponent JavaDoc;
52 import org.omg.SSLIOP.TAG_SSL_SEC_TRANS;
53
54 /**
55  * @author Gerald Brose
56  * @version $Id: PrintIOR.java,v 1.35 2004/10/21 14:49:53 francisco Exp $
57  */

58
59 public class PrintIOR
60 {
61     /**
62      * entry point from the command line
63      */

64
65     public static void main(String JavaDoc args[])
66     {
67         org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args,null);
68         Logger logger =
69             ((org.jacorb.orb.ORB)orb).getConfiguration().getNamedLogger("jacorb.print_ior");
70         String JavaDoc line, iorString = null;
71
72         if( args.length < 1 || args.length > 2)
73         {
74             System.err.println("Usage: java PrintIOR [ ior_str | -f filename ]");
75             System.exit( 1 );
76         }
77
78         if( args[0].equals("-f"))
79         {
80             try
81             {
82                 // System.out.println ( "arg.length: " + arg.length );
83
// System.out.println ( "arg[ 0 ]: " + arg[ 0 ] );
84
// System.out.println ( "reading IOR from file: " + arg[ 1 ] );
85
BufferedReader JavaDoc br = new BufferedReader JavaDoc ( new FileReader JavaDoc( args[1] ), 2048 );
86                 line = br.readLine();
87                 // System.out.print ( line );
88
if ( line != null )
89                 {
90                     iorString = line;
91                     while ( line != null )
92                     {
93                         line = br.readLine();
94                         if ( line != null )
95                             iorString = iorString + line;
96                         // System.out.print ( line );
97
}
98                 }
99             }
100             catch ( IOException JavaDoc ioe )
101             {
102                 ioe.printStackTrace();
103                 System.exit(1);
104             }
105         }
106         else
107         {
108             iorString = args[0];
109         }
110
111         if( iorString.startsWith( "IOR:" ))
112         {
113             ParsedIOR pior = new ParsedIOR( iorString, orb, logger );
114             printIOR(pior, orb);
115         }
116         else
117             System.out.println("Sorry, we only unparse IORs in the standard IOR URL scheme");
118
119         orb.shutdown(true);
120     }
121
122
123     /**
124      * top-level
125      */

126
127     public static void printIOR( ParsedIOR pior, org.omg.CORBA.ORB JavaDoc orb)
128     {
129         org.omg.IOP.IOR JavaDoc ior = pior.getIOR();
130
131         System.out.println("------IOR components-----");
132         System.out.println("TypeId\t:\t" + ior.type_id );
133
134         List JavaDoc profiles = pior.getProfiles();
135
136         System.out.println("TAG_INTERNET_IOP Profiles:");
137         for( int i = 0; i < profiles.size(); i++ )
138         {
139             System.out.print("\tProfile Id: ");
140
141             IIOPProfile p = (IIOPProfile)profiles.get(i);
142             System.out.println("\tIIOP Version : " +
143                                (int)p.version().major + "." +
144                                (int)p.version().minor);
145
146             System.out.println("\tHost\t:\t" + p.getAddress().getOriginalHost());
147             int port = p.getAddress().getPort();
148             if( port < 0 )
149                 port += 65536;
150
151             System.out.println("\tPort\t:\t" + port );
152             try
153             {
154                 System.out.println("\tObject key (URL): " +
155                                    CorbaLoc.parseKey( pior.get_object_key()));
156             }
157             catch( Exception JavaDoc e )
158             {
159                 // ignore, object key not in url format
160
}
161             System.out.print("\tObject key (hex): 0x" );
162             dumpHex( pior.get_object_key() );
163             System.out.println();
164
165             if ( p.version().minor >= ( char ) 1 )
166             {
167                 if( p.getComponents().size() > 0 )
168                     System.out.println("\t-- Found " +
169                                        p.getComponents().size() +
170                                        " Tagged Components--" );
171
172                 printTaggedComponents( p.getComponents().asArray() );
173             }
174             System.out.print("\n");
175         }
176
177         TaggedComponentList multiple_components = pior.getMultipleComponents();
178
179         if( multiple_components.size() > 0 )
180         {
181             System.out.println("Components in MULTIPLE_COMPONENTS profile: " +
182                                multiple_components.size() );
183
184             printTaggedComponents( multiple_components.asArray() );
185         }
186
187     }
188
189     /**
190      * Iterates over a tagged IOP components and prints those that are
191      * recognized.
192      */

193
194     private static void printTaggedComponents( TaggedComponent JavaDoc[] taggedComponents )
195     {
196         for( int i = 0; i < taggedComponents.length; i++ )
197         {
198             switch( taggedComponents[i].tag )
199             {
200                 case TAG_SSL_SEC_TRANS.value:
201                 System.out.println("\t#"+ i + ": TAG_SSL_SEC_TRANS");
202                 printSSLTaggedComponent( taggedComponents[i] );
203                 break;
204                 case TAG_CSI_SEC_MECH_LIST.value:
205                 System.out.println("\t#"+ i + ": TAG_CSI_SEC_MECH_LIST");
206                 printCSIMechComponent( taggedComponents[i] );
207                 break;
208                 case TAG_SECIOP_SEC_TRANS.value:
209                 System.out.println("\t#"+ i + ": TAG_SECIOP_SEC_TRANS");
210                 break;
211                 case TAG_ALTERNATE_IIOP_ADDRESS.value:
212                 System.out.println("\t#"+ i + ": TAG_ALTERNATE_IIOP_ADDRESS");
213                 printAlternateAddress(taggedComponents[i]);
214                 break;
215                 case TAG_CODE_SETS.value:
216                 System.out.println("\t#"+ i + ": TAG_CODE_SETS");
217                 printCodeSetComponent( taggedComponents[i] );
218                 break;
219                 case TAG_JAVA_CODEBASE.value:
220                 System.out.println("\t#"+ i + ": TAG_JAVA_CODEBASE");
221                 printJavaCodebaseComponent( taggedComponents[i] );
222                 break;
223                 case TAG_ORB_TYPE.value:
224                 System.out.println("\t#"+ i + ": TAG_ORB_TYPE");
225                 printOrbTypeComponent( taggedComponents[i] );
226                 break;
227                 case TAG_NULL_TAG.value:
228                 System.out.println("\t#"+ i + ": TAG_NULL_TAG");
229                 break;
230                 default:
231                 System.out.println("\tUnknown tag : " +
232                                    taggedComponents[i].tag);
233             }
234         }
235     }
236
237     private static void printCSIMechComponent( TaggedComponent JavaDoc taggedComponent )
238     {
239         CDRInputStream is =
240         new CDRInputStream( (org.omg.CORBA.ORB JavaDoc)null,
241                             taggedComponent.component_data);
242
243         is.openEncapsulatedArray();
244         CompoundSecMechList csmList = CompoundSecMechListHelper.read( is );
245
246         if( csmList!= null )
247         {
248             System.out.println("\t\tis stateful: " + csmList.stateful );
249             for( int i = 0; i < csmList.mechanism_list.length; i++ )
250             {
251                 System.out.println("\t\tCompoundSecMech #" + i);
252                 System.out.println("\t\t\ttarget_requires: " +
253                                    csmList.mechanism_list[i].target_requires );
254                 System.out.print("\t\t\ttransport mechanism tag: ");
255                 switch( csmList.mechanism_list[i].transport_mech.tag )
256                 {
257                     case TAG_TLS_SEC_TRANS.value:
258                     System.out.println("TAG_TLS_SEC_TRANS");
259                     printTlsSecTrans(csmList.mechanism_list[i].transport_mech.component_data);
260                     break;
261                     case TAG_NULL_TAG.value:
262                     System.out.println("TAG_NULL_TAG");
263                     break;
264                     default:
265                     System.out.println("Unknown tag : " +
266                                        csmList.mechanism_list[i].transport_mech.tag );
267                 }
268                 System.out.println("\t\t\tAS_ContextSec target_supports: " + csmList.mechanism_list[i].as_context_mech.target_supports );
269                 System.out.println("\t\t\tAS_ContextSec target_requires: " + csmList.mechanism_list[i].as_context_mech.target_requires );
270                 System.out.print("\t\t\tAS_ContextSec mech: " );
271                 dumpHex(csmList.mechanism_list[i].as_context_mech.client_authentication_mech);
272                 System.out.println();
273                 System.out.print("\t\t\tAS_ContextSec target_name: " );
274                 printNTExportedName(csmList.mechanism_list[i].as_context_mech.target_name);
275                 //}
276
System.out.println("\t\t\tSAS_ContextSec target_supports: " + csmList.mechanism_list[i].sas_context_mech.target_supports );
277                 System.out.println("\t\t\tSAS_ContextSec target_requires: " + csmList.mechanism_list[i].sas_context_mech.target_requires );
278
279                 for (int j = 0; j < csmList.mechanism_list[i].sas_context_mech.supported_naming_mechanisms.length; j++) {
280                     System.out.print("\t\t\tSAS_ContextSec Naming mech: " );
281                     dumpHex(csmList.mechanism_list[i].sas_context_mech.supported_naming_mechanisms[j]);
282                     System.out.println();
283                 }
284                 System.out.println("\t\t\tSAS_ContextSec Naming types: " + csmList.mechanism_list[i].sas_context_mech.supported_identity_types);
285                 System.out.println();
286             }
287         }
288     }
289     
290     private static void printNTExportedName(byte[] nameData) {
291         // check for token identifier
292
if (nameData.length < 2 || nameData[0] != 0x04 || nameData[1] != 0x01) {
293             dumpHex(nameData);
294             System.out.println();
295             return;
296         }
297         
298         // get mech length
299
int mechLen = (nameData[2] << 8) + nameData[3];
300         if (mechLen > (nameData.length - 8)) {
301             dumpHex(nameData);
302             System.out.println();
303             return;
304         }
305         
306         // get name length
307
int nameLen = (nameData[mechLen + 4] << 24) +
308                       (nameData[mechLen + 5] << 16) +
309                       (nameData[mechLen + 6] << 8) +
310                       (nameData[mechLen + 7]);
311         if ((mechLen + nameLen) > (nameData.length - 8)) {
312             dumpHex(nameData);
313             System.out.println();
314             return;
315         }
316         byte[] name = new byte[nameLen];
317         System.arraycopy(nameData, mechLen + 8, name, 0, nameLen);
318         System.out.println(new String JavaDoc(name));
319     }
320
321     private static void printTlsSecTrans(byte[] tagData) {
322         CDRInputStream in = new CDRInputStream( (org.omg.CORBA.ORB JavaDoc)null, tagData );
323         try
324         {
325             in.openEncapsulatedArray();
326             TLS_SEC_TRANS tls = TLS_SEC_TRANSHelper.read( in );
327             System.out.println("\t\t\tTLS SEC TRANS target requires: " + tls.target_requires);
328             System.out.println("\t\t\tTLS SEC TRANS target supports: " + tls.target_supports);
329             for (int i = 0; i < tls.addresses.length; i++) {
330                 int ssl_port = tls.addresses[i].port;
331                 if( ssl_port < 0 ) ssl_port += 65536;
332                 System.out.println("\t\t\tTLS SEC TRANS address: " + tls.addresses[i].host_name+":"+ssl_port);
333             }
334         }
335         catch ( Exception JavaDoc ex )
336         {
337             System.out.print("\t\t\tTLS SEC TRANS: " );
338             dumpHex(tagData);
339             System.out.println();
340         }
341     }
342
343     private static void printCodeSetComponent( TaggedComponent JavaDoc taggedComponent )
344     {
345         CDRInputStream is =
346         new CDRInputStream( (org.omg.CORBA.ORB JavaDoc)null,
347                             taggedComponent.component_data);
348
349         is.openEncapsulatedArray();
350
351         org.omg.CONV_FRAME.CodeSetComponentInfo codeSet =
352         CodeSetComponentInfoHelper.read( is );
353
354         if( codeSet != null )
355         {
356             System.out.println("\t\tForChar native code set Id: " +
357                                CodeSet.csName(codeSet.ForCharData.native_code_set ));
358             System.out.print("\t\tChar Conversion Code Sets: ");
359             for( int ji = 0; ji < codeSet.ForCharData.conversion_code_sets.length; ji++ )
360             {
361                 System.out.println( CodeSet.csName( codeSet.ForCharData.conversion_code_sets[ji] ) );
362
363                 if( ji < (codeSet.ForCharData.conversion_code_sets.length - 1) )
364                 {
365                     System.out.print( ", " );
366                 }
367             }
368
369             System.out.println("\t\tForWChar native code set Id: " +
370                                CodeSet.csName(codeSet.ForWcharData.native_code_set ));
371             System.out.print("\t\tWChar Conversion Code Sets: ");
372             for( int ji = 0; ji < codeSet.ForWcharData.conversion_code_sets.length; ji++ )
373             {
374                 System.out.println( CodeSet.csName( codeSet.ForWcharData.conversion_code_sets[ji] ));
375
376                 if( ji < (codeSet.ForWcharData.conversion_code_sets.length - 1) )
377                 {
378                     System.out.print( ", " );
379                 }
380             }
381         }
382     }
383
384     private static void printSSLTaggedComponent( TaggedComponent JavaDoc taggedComponent )
385     {
386         org.omg.SSLIOP.SSL ssl = null;
387         if( taggedComponent.tag == 20 )
388         {
389             CDRInputStream in =
390             new CDRInputStream( (org.omg.CORBA.ORB JavaDoc)null,
391                                 taggedComponent.component_data );
392             try
393             {
394                 in.openEncapsulatedArray();
395                 ssl = org.omg.SSLIOP.SSLHelper.read( in );
396             }
397             catch ( Exception JavaDoc ex )
398             {
399                 return;
400             }
401             int ssl_port = ssl.port;
402             if( ssl_port < 0 )
403                 ssl_port += 65536;
404
405             System.out.print ( "\t\ttarget_supports\t:\t" );
406             //dump ( ssl.target_supports );
407
decodeAssociationOption( ssl.target_supports );
408             java.lang.System.out.println();
409             System.out.print ( "\t\ttarget_requires\t:\t" );
410             //dump ( ssl.target_requires );
411
decodeAssociationOption( ssl.target_requires );
412             java.lang.System.out.println();
413             System.out.println ( "\t\tSSL Port\t:\t" + ssl_port );
414
415         }
416     }
417     private static void decodeAssociationOption( int option )
418     {
419         boolean first = true;
420
421         if( (option & org.omg.Security.NoProtection.value) != 0 )
422         {
423             if( ! first )
424             {
425                 System.out.print( ", " );
426             }
427
428             System.out.print( "NoProtection" );
429
430             first = false;
431         }
432
433         if( (option & org.omg.Security.Integrity.value) != 0 )
434         {
435             if( ! first )
436             {
437                 System.out.print( ", " );
438             }
439
440             System.out.print( "Integrity" );
441
442             first = false;
443         }
444
445         if( (option & org.omg.Security.Confidentiality.value) != 0 )
446         {
447             if( ! first )
448             {
449                 System.out.print( ", " );
450             }
451
452             System.out.print( "Confidentiality" );
453
454             first = false;
455         }
456
457         if( (option & org.omg.Security.DetectReplay.value) != 0 )
458         {
459             if( ! first )
460             {
461                 System.out.print( ", " );
462             }
463
464             System.out.print( "DetectReplay" );
465
466             first = false;
467         }
468
469         if( (option & org.omg.Security.DetectMisordering.value) != 0 )
470         {
471             if( ! first )
472             {
473                 System.out.print( ", " );
474             }
475
476             System.out.print( "DetectMisordering" );
477
478             first = false;
479         }
480
481         if( (option & org.omg.Security.EstablishTrustInTarget.value) != 0 )
482         {
483             if( ! first )
484             {
485                 System.out.print( ", " );
486             }
487
488             System.out.print( "EstablishTrustInTarget" );
489
490             first = false;
491         }
492
493         if( (option & org.omg.Security.EstablishTrustInClient.value) != 0 )
494         {
495             if( ! first )
496             {
497                 System.out.print( ", " );
498             }
499
500             System.out.print( "EstablishTrustInClient" );
501
502             first = false;
503         }
504
505         if( (option & org.omg.Security.NoDelegation.value) != 0 )
506         {
507             if( ! first )
508             {
509                 System.out.print( ", " );
510             }
511
512             System.out.print( "NoDelegation" );
513
514             first = false;
515         }
516
517         if( (option & org.omg.Security.SimpleDelegation.value) != 0 )
518         {
519             if( ! first )
520             {
521                 System.out.print( ", " );
522             }
523
524             System.out.print( "SimpleDelegation" );
525
526             first = false;
527         }
528
529         if( (option & org.omg.Security.CompositeDelegation.value) != 0 )
530         {
531             if( ! first )
532             {
533                 System.out.print( ", " );
534             }
535
536             System.out.print( "CompositeDelegation" );
537
538             first = false;
539         }
540
541     }
542
543
544     private static void printJavaCodebaseComponent( TaggedComponent JavaDoc taggedComponent )
545     {
546         CDRInputStream is =
547         new CDRInputStream( (org.omg.CORBA.ORB JavaDoc)null,
548                             taggedComponent.component_data );
549
550         is.openEncapsulatedArray();
551         String JavaDoc codebase = is.read_string();
552
553         System.out.println( "\t\tCodebase: " + codebase );
554     }
555
556     private static void printOrbTypeComponent (TaggedComponent JavaDoc tc)
557     {
558         CDRInputStream is =
559         new CDRInputStream ((org.omg.CORBA.ORB JavaDoc)null, tc.component_data );
560         is.openEncapsulatedArray ();
561         int type = is.read_long ();
562
563         System.out.print ( "\t\tType: " + type);
564         if (type == ORBConstants.JACORB_ORB_ID)
565         {
566             System.out.println (" (JacORB)");
567         }
568         else
569         {
570             System.out.println (" (Foreign)");
571         }
572     }
573
574     private static void printAlternateAddress(TaggedComponent JavaDoc tc)
575     {
576         CDRInputStream is =
577         new CDRInputStream((org.omg.CORBA.ORB JavaDoc)null, tc.component_data);
578         is.openEncapsulatedArray();
579         System.out.println("\t\tAddress: " + IIOPAddress.read(is));
580     }
581
582     public static void dumpHex(byte bs[])
583     {
584         for (int i=0; i<bs.length; i++) {
585             int n1 = (bs[i] & 0xff) / 16;
586             int n2 = (bs[i] & 0xff) % 16;
587             char c1 = (char)(n1>9 ? ('A'+(n1-10)) : ('0'+n1));
588             char c2 = (char)(n2>9 ? ('A'+(n2-10)) : ('0'+n2));
589             System.out.print( c1 + (c2 + " "));
590         }
591     }
592
593     static char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
594                                'A', 'B', 'C', 'D', 'E', 'F'
595     };
596
597     public static void dump ( byte bs[] ) {
598         for ( int i = 0; i < bs.length; i++ ) {
599             dump ( bs[ i ] );
600             System.out.print( " " );
601         }
602     }
603
604     public static void dump ( int is[] ) {
605         for ( int i = 0; i < is.length; i++ ) {
606             dump ( is[ i ] );
607             System.out.print( " " );
608         }
609     }
610
611     public static void dump ( byte b ) {
612         java.lang.System.out.print( ""
613                                     + hexDigit[ ( b >> 4 ) & 0x0f ]
614                                     + hexDigit[ ( b ) & 0x0f ]
615                                   );
616     }
617
618     public static void dump ( short i ) {
619         java.lang.System.out.print( ""
620                                     + hexDigit[ ( i >> 12 ) & 0x0f ]
621                                     + hexDigit[ ( i >> 9 ) & 0x0f ]
622                                     + hexDigit[ ( i >> 4 ) & 0x0f ]
623                                     + hexDigit[ ( i ) & 0x0f ]
624                                   );
625     }
626
627     public static void dump ( int i ) {
628         java.lang.System.out.print( ""
629                                     + hexDigit[ ( i >> 28 ) & 0x0f ]
630                                     + hexDigit[ ( i >> 24 ) & 0x0f ]
631                                     + hexDigit[ ( i >> 20 ) & 0x0f ]
632                                     + hexDigit[ ( i >> 16 ) & 0x0f ]
633                                     + hexDigit[ ( i >> 12 ) & 0x0f ]
634                                     + hexDigit[ ( i >> 8 ) & 0x0f ]
635                                     + hexDigit[ ( i >> 4 ) & 0x0f ]
636                                     + hexDigit[ ( i ) & 0x0f ]
637                                   );
638     }
639
640     public static void dump ( byte bs[], boolean withChar ) {
641         char c;
642         int len = bs.length;
643         for ( int i = 0; i < len; i++ ) {
644             if ( 0 == i % 16 ) java.lang.System.out.println();
645             if ( bs[ i ] > ( byte ) 31 && bs[ i ] < ( byte ) 127 ) c = ( char ) bs[ i ];
646             else c = ' ';
647             java.lang.System.out.print( ":"
648                                         + hexDigit[ ( bs [ i ] >> 4 ) & 0x0f ]
649                                         + hexDigit[ bs [ i ] & 0x0f ]
650                                         + " " + c
651                                       );
652         }
653     }
654 }
655
Popular Tags