KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > orbutil > ORBUtility


1 /*
2  * @(#)ORBUtility.java 1.52 07/05/24
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.orbutil;
9
10 import java.lang.Character JavaDoc;
11 import java.lang.reflect.Field JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import java.rmi.NoSuchObjectException JavaDoc;
14 import java.security.AccessController JavaDoc;
15 import java.security.PermissionCollection JavaDoc;
16 import java.security.Policy JavaDoc;
17 import java.security.PrivilegedAction JavaDoc;
18 import java.security.ProtectionDomain JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Map.Entry;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.IdentityHashMap JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36
37 import javax.rmi.CORBA.ValueHandler JavaDoc;
38 import javax.rmi.CORBA.ValueHandlerMultiFormat JavaDoc;
39 import javax.rmi.CORBA.Util JavaDoc;
40
41 import org.omg.CORBA.StructMember JavaDoc ;
42 import org.omg.CORBA.TypeCode JavaDoc ;
43 import org.omg.CORBA.Any JavaDoc ;
44 import org.omg.CORBA.TCKind JavaDoc ;
45 import org.omg.CORBA.SystemException JavaDoc ;
46 import org.omg.CORBA.CompletionStatus JavaDoc ;
47 import org.omg.CORBA.DATA_CONVERSION JavaDoc ;
48 import org.omg.CORBA.BAD_PARAM JavaDoc ;
49 import org.omg.CORBA.BAD_OPERATION JavaDoc ;
50 import org.omg.CORBA.INTERNAL JavaDoc ;
51 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ;
52 import org.omg.CORBA.portable.OutputStream JavaDoc ;
53 import org.omg.CORBA.portable.InputStream JavaDoc ;
54
55 import com.sun.corba.se.pept.transport.ContactInfoList ;
56
57 import com.sun.corba.se.spi.ior.IOR ;
58 import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
59 import com.sun.corba.se.spi.orb.ORB ;
60 import com.sun.corba.se.spi.orb.ORBVersion ;
61 import com.sun.corba.se.spi.orb.ORBVersionFactory ;
62 import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;
63 import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
64 import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
65 import com.sun.corba.se.spi.logging.CORBALogDomains ;
66 import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
67 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate;
68
69 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
70 import com.sun.corba.se.impl.corba.CORBAObjectImpl ;
71 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
72 import com.sun.corba.se.impl.logging.OMGSystemException ;
73 import com.sun.corba.se.impl.ior.iiop.JavaSerializationComponent;
74
75 /**
76  * Handy class full of static functions that don't belong in util.Utility for pure ORB reasons.
77  */

78 public final class ORBUtility {
79     private ORBUtility() {}
80
81     private static ORBUtilSystemException wrapper = ORBUtilSystemException.get(
82     CORBALogDomains.UTIL ) ;
83     private static OMGSystemException omgWrapper = OMGSystemException.get(
84     CORBALogDomains.UTIL ) ;
85
86     private static StructMember JavaDoc[] members = null;
87
88     private static StructMember JavaDoc[] systemExceptionMembers (ORB orb) {
89         if (members == null) {
90             members = new StructMember JavaDoc[3];
91             members[0] = new StructMember JavaDoc("id", orb.create_string_tc(0), null);
92             members[1] = new StructMember JavaDoc("minor", orb.get_primitive_tc(TCKind.tk_long), null);
93             members[2] = new StructMember JavaDoc("completed", orb.get_primitive_tc(TCKind.tk_long), null);
94         }
95         return members;
96     }
97
98     private static TypeCode JavaDoc getSystemExceptionTypeCode(ORB orb, String JavaDoc repID, String JavaDoc name) {
99         synchronized (TypeCode JavaDoc.class) {
100             return orb.create_exception_tc(repID, name, systemExceptionMembers(orb));
101         }
102     }
103
104     private static boolean isSystemExceptionTypeCode(TypeCode JavaDoc type, ORB orb) {
105         StructMember JavaDoc[] systemExceptionMembers = systemExceptionMembers(orb);
106         try {
107             return (type.kind().value() == TCKind._tk_except &&
108                     type.member_count() == 3 &&
109                     type.member_type(0).equal(systemExceptionMembers[0].type) &&
110                     type.member_type(1).equal(systemExceptionMembers[1].type) &&
111                     type.member_type(2).equal(systemExceptionMembers[2].type));
112         } catch (BadKind JavaDoc ex) {
113             return false;
114         } catch (org.omg.CORBA.TypeCodePackage.Bounds JavaDoc ex) {
115             return false;
116         }
117     }
118
119     /**
120      * Static method for writing a CORBA standard exception to an Any.
121      * @param any The Any to write the SystemException into.
122      */

123     public static void insertSystemException(SystemException JavaDoc ex, Any JavaDoc any) {
124         OutputStream JavaDoc out = any.create_output_stream();
125         ORB orb = (ORB)(out.orb());
126         String JavaDoc name = ex.getClass().getName();
127         String JavaDoc repID = ORBUtility.repositoryIdOf(name);
128         out.write_string(repID);
129         out.write_long(ex.minor);
130         out.write_long(ex.completed.value());
131         any.read_value(out.create_input_stream(),
132         getSystemExceptionTypeCode(orb, repID, name));
133     }
134
135     public static SystemException JavaDoc extractSystemException(Any JavaDoc any) {
136         InputStream JavaDoc in = any.create_input_stream();
137         ORB orb = (ORB)(in.orb());
138         if ( ! isSystemExceptionTypeCode(any.type(), orb)) {
139         throw wrapper.unknownDsiSysex(CompletionStatus.COMPLETED_MAYBE);
140         }
141         return ORBUtility.readSystemException(in);
142     }
143
144     /**
145      * Creates the correct ValueHandler for the given ORB,
146      * querying ORBVersion information. If the ORB or
147      * ORBVersion is null, gets the ValueHandler from
148      * Util.createValueHandler.
149      */

150     public static ValueHandler JavaDoc createValueHandler(ORB orb) {
151
152         if (orb == null)
153             return Util.createValueHandler();
154
155         ORBVersion version = orb.getORBVersion();
156
157         if (version == null)
158             return Util.createValueHandler();
159
160         if (version.equals(ORBVersionFactory.getOLD()))
161             return new ValueHandlerImpl_1_3();
162         if (version.equals(ORBVersionFactory.getNEW()))
163             return new ValueHandlerImpl_1_3_1();
164
165         return Util.createValueHandler();
166     }
167
168     /**
169      * Returns true if the given ORB could accurately be determined to be a
170      * Kestrel or earlier ORB. Note: If passed the ORBSingleton, this will return
171      * false.
172      */

173     public static boolean isLegacyORB(ORB orb)
174     {
175         try {
176             ORBVersion currentORB = orb.getORBVersion();
177             return currentORB.equals( ORBVersionFactory.getOLD() ) ;
178         } catch (SecurityException JavaDoc se) {
179             return false;
180         }
181     }
182
183     /**
184      * Returns true if it was accurately determined that the remote ORB is
185      * a foreign (non-JavaSoft) ORB. Note: If passed the ORBSingleton, this
186      * will return false.
187      */

188     public static boolean isForeignORB(ORB orb)
189     {
190         if (orb == null)
191             return false;
192
193         try {
194             return orb.getORBVersion().equals(ORBVersionFactory.getFOREIGN());
195         } catch (SecurityException JavaDoc se) {
196             return false;
197         }
198     }
199
200     /** Unmarshal a byte array to an integer.
201         Assume the bytes are in BIGENDIAN order.
202         i.e. array[offset] is the most-significant-byte
203         and array[offset+3] is the least-significant-byte.
204         @param array The array of bytes.
205         @param offset The offset from which to start unmarshalling.
206     */

207     public static int bytesToInt(byte[] array, int offset)
208     {
209         int b1, b2, b3, b4;
210
211         b1 = (array[offset++] << 24) & 0xFF000000;
212         b2 = (array[offset++] << 16) & 0x00FF0000;
213         b3 = (array[offset++] << 8) & 0x0000FF00;
214         b4 = (array[offset++] << 0) & 0x000000FF;
215
216         return (b1 | b2 | b3 | b4);
217     }
218
219     /** Marshal an integer to a byte array.
220         The bytes are in BIGENDIAN order.
221         i.e. array[offset] is the most-significant-byte
222         and array[offset+3] is the least-significant-byte.
223         @param array The array of bytes.
224         @param offset The offset from which to start marshalling.
225     */

226     public static void intToBytes(int value, byte[] array, int offset)
227     {
228         array[offset++] = (byte)((value >>> 24) & 0xFF);
229         array[offset++] = (byte)((value >>> 16) & 0xFF);
230         array[offset++] = (byte)((value >>> 8) & 0xFF);
231         array[offset++] = (byte)((value >>> 0) & 0xFF);
232     }
233
234     /** Converts an Ascii Character into Hexadecimal digit
235      */

236     public static int hexOf( char x )
237     {
238     int val;
239
240         val = x - '0';
241         if (val >=0 && val <= 9)
242             return val;
243
244         val = (x - 'a') + 10;
245         if (val >= 10 && val <= 15)
246             return val;
247
248         val = (x - 'A') + 10;
249         if (val >= 10 && val <= 15)
250             return val;
251
252     throw wrapper.badHexDigit() ;
253     }
254
255     // method moved from util.Utility
256

257     /**
258      * Static method for writing a CORBA standard exception to a stream.
259      * @param strm The OutputStream to use for marshaling.
260      */

261     public static void writeSystemException(SystemException JavaDoc ex, OutputStream JavaDoc strm)
262     {
263     String JavaDoc s;
264
265     s = repositoryIdOf(ex.getClass().getName());
266         strm.write_string(s);
267         strm.write_long(ex.minor);
268         strm.write_long(ex.completed.value());
269     }
270
271     /**
272      * Static method for reading a CORBA standard exception from a stream.
273      * @param strm The InputStream to use for unmarshaling.
274      */

275     public static SystemException JavaDoc readSystemException(InputStream JavaDoc strm)
276     {
277     try {
278         String JavaDoc name = classNameOf(strm.read_string());
279         SystemException JavaDoc ex
280                 = (SystemException JavaDoc)ORBClassLoader.loadClass(name).newInstance();
281         ex.minor = strm.read_long();
282         ex.completed = CompletionStatus.from_int(strm.read_long());
283         return ex;
284     } catch ( Exception JavaDoc ex ) {
285         throw wrapper.unknownSysex( CompletionStatus.COMPLETED_MAYBE, ex );
286     }
287     }
288
289     /**
290      * Get the class name corresponding to a particular repository Id.
291      * This is used by the system to unmarshal (instantiate) the
292      * appropriate exception class for an marshaled as the value of
293      * its repository Id.
294      * @param repositoryId The repository Id for which we want a class name.
295      */

296     public static String JavaDoc classNameOf(String JavaDoc repositoryId)
297     {
298     String JavaDoc className=null;
299
300     className = (String JavaDoc) exceptionClassNames.get(repositoryId);
301     if (className == null)
302         className = "org.omg.CORBA.UNKNOWN";
303
304     return className;
305     }
306
307     /**
308      * Return true if this repositoryId is a SystemException.
309      * @param repositoryId The repository Id to check.
310      */

311     public static boolean isSystemException(String JavaDoc repositoryId)
312     {
313     String JavaDoc className=null;
314
315     className = (String JavaDoc) exceptionClassNames.get(repositoryId);
316     if (className == null)
317         return false;
318     else
319         return true;
320     }
321     
322     /**
323      * @return the Java serialization encoding version.
324      */

325     public static byte getEncodingVersion(ORB orb, IOR ior) {
326
327     // Is Java serialization enabled?
328
// Check the JavaSerializationComponent (tagged component)
329
// in the IIOPProfile. If present, the peer ORB's GIOP is capable
330
// of using Java serialization instead of CDR serialization.
331
// In such a case, use Java serialization, iff the java serialization
332
// versions match.
333

334     if (orb.getORBData().isJavaSerializationEnabled()) {
335         IIOPProfile prof = ior.getProfile();
336         IIOPProfileTemplate profTemp =
337         (IIOPProfileTemplate) prof.getTaggedProfileTemplate();
338         java.util.Iterator JavaDoc iter = profTemp.iteratorById(
339                   ORBConstants.TAG_JAVA_SERIALIZATION_ID);
340         if (iter.hasNext()) {
341         JavaSerializationComponent jc =
342             (JavaSerializationComponent) iter.next();
343         byte jcVersion = jc.javaSerializationVersion();
344         if (jcVersion >= Message.JAVA_ENC_VERSION) {
345             return Message.JAVA_ENC_VERSION;
346         } else if (jcVersion > Message.CDR_ENC_VERSION) {
347             return jc.javaSerializationVersion();
348         } else {
349             // throw error?
350
// Since encodingVersion is <= 0 (CDR_ENC_VERSION).
351
}
352         }
353     }
354     return Message.CDR_ENC_VERSION; // default
355
}
356     
357     /**
358      * Get the repository id corresponding to a particular class.
359      * This is used by the system to write the
360      * appropriate repository id for a system exception.
361      * @param name The class name of the system exception.
362      */

363     public static String JavaDoc repositoryIdOf(String JavaDoc name)
364     {
365     String JavaDoc id;
366
367     id = (String JavaDoc) exceptionRepositoryIds.get(name);
368     if (id == null)
369         id = "IDL:omg.org/CORBA/UNKNOWN:1.0";
370
371     return id;
372     }
373
374     private static final Hashtable JavaDoc exceptionClassNames = new Hashtable JavaDoc();
375     private static final Hashtable JavaDoc exceptionRepositoryIds = new Hashtable JavaDoc();
376
377     static {
378
379     //
380
// construct repositoryId -> className hashtable
381
//
382
exceptionClassNames.put("IDL:omg.org/CORBA/BAD_CONTEXT:1.0",
383                 "org.omg.CORBA.BAD_CONTEXT");
384     exceptionClassNames.put("IDL:omg.org/CORBA/BAD_INV_ORDER:1.0",
385                 "org.omg.CORBA.BAD_INV_ORDER");
386     exceptionClassNames.put("IDL:omg.org/CORBA/BAD_OPERATION:1.0",
387                 "org.omg.CORBA.BAD_OPERATION");
388     exceptionClassNames.put("IDL:omg.org/CORBA/BAD_PARAM:1.0",
389                 "org.omg.CORBA.BAD_PARAM");
390     exceptionClassNames.put("IDL:omg.org/CORBA/BAD_TYPECODE:1.0",
391                 "org.omg.CORBA.BAD_TYPECODE");
392     exceptionClassNames.put("IDL:omg.org/CORBA/COMM_FAILURE:1.0",
393                 "org.omg.CORBA.COMM_FAILURE");
394     exceptionClassNames.put("IDL:omg.org/CORBA/DATA_CONVERSION:1.0",
395                 "org.omg.CORBA.DATA_CONVERSION");
396     exceptionClassNames.put("IDL:omg.org/CORBA/IMP_LIMIT:1.0",
397                 "org.omg.CORBA.IMP_LIMIT");
398     exceptionClassNames.put("IDL:omg.org/CORBA/INTF_REPOS:1.0",
399                 "org.omg.CORBA.INTF_REPOS");
400     exceptionClassNames.put("IDL:omg.org/CORBA/INTERNAL:1.0",
401                 "org.omg.CORBA.INTERNAL");
402     exceptionClassNames.put("IDL:omg.org/CORBA/INV_FLAG:1.0",
403                 "org.omg.CORBA.INV_FLAG");
404     exceptionClassNames.put("IDL:omg.org/CORBA/INV_IDENT:1.0",
405                 "org.omg.CORBA.INV_IDENT");
406     exceptionClassNames.put("IDL:omg.org/CORBA/INV_OBJREF:1.0",
407                 "org.omg.CORBA.INV_OBJREF");
408     exceptionClassNames.put("IDL:omg.org/CORBA/MARSHAL:1.0",
409                 "org.omg.CORBA.MARSHAL");
410     exceptionClassNames.put("IDL:omg.org/CORBA/NO_MEMORY:1.0",
411                 "org.omg.CORBA.NO_MEMORY");
412     exceptionClassNames.put("IDL:omg.org/CORBA/FREE_MEM:1.0",
413                 "org.omg.CORBA.FREE_MEM");
414     exceptionClassNames.put("IDL:omg.org/CORBA/NO_IMPLEMENT:1.0",
415                 "org.omg.CORBA.NO_IMPLEMENT");
416     exceptionClassNames.put("IDL:omg.org/CORBA/NO_PERMISSION:1.0",
417                 "org.omg.CORBA.NO_PERMISSION");
418     exceptionClassNames.put("IDL:omg.org/CORBA/NO_RESOURCES:1.0",
419                 "org.omg.CORBA.NO_RESOURCES");
420     exceptionClassNames.put("IDL:omg.org/CORBA/NO_RESPONSE:1.0",
421                 "org.omg.CORBA.NO_RESPONSE");
422     exceptionClassNames.put("IDL:omg.org/CORBA/OBJ_ADAPTER:1.0",
423                 "org.omg.CORBA.OBJ_ADAPTER");
424     exceptionClassNames.put("IDL:omg.org/CORBA/INITIALIZE:1.0",
425                 "org.omg.CORBA.INITIALIZE");
426     exceptionClassNames.put("IDL:omg.org/CORBA/PERSIST_STORE:1.0",
427                 "org.omg.CORBA.PERSIST_STORE");
428     exceptionClassNames.put("IDL:omg.org/CORBA/TRANSIENT:1.0",
429                 "org.omg.CORBA.TRANSIENT");
430     exceptionClassNames.put("IDL:omg.org/CORBA/UNKNOWN:1.0",
431                 "org.omg.CORBA.UNKNOWN");
432     exceptionClassNames.put("IDL:omg.org/CORBA/OBJECT_NOT_EXIST:1.0",
433                 "org.omg.CORBA.OBJECT_NOT_EXIST");
434
435     // SystemExceptions from OMG Transactions Service Spec
436
exceptionClassNames.put("IDL:omg.org/CORBA/INVALID_TRANSACTION:1.0",
437                 "org.omg.CORBA.INVALID_TRANSACTION");
438     exceptionClassNames.put("IDL:omg.org/CORBA/TRANSACTION_REQUIRED:1.0",
439                 "org.omg.CORBA.TRANSACTION_REQUIRED");
440     exceptionClassNames.put("IDL:omg.org/CORBA/TRANSACTION_ROLLEDBACK:1.0",
441                 "org.omg.CORBA.TRANSACTION_ROLLEDBACK");
442
443     // from portability RTF 98-07-01.txt
444
exceptionClassNames.put("IDL:omg.org/CORBA/INV_POLICY:1.0",
445                 "org.omg.CORBA.INV_POLICY");
446
447     // from orbrev/00-09-01 (CORBA 2.4 Draft Specification)
448
exceptionClassNames.
449         put("IDL:omg.org/CORBA/TRANSACTION_UNAVAILABLE:1.0",
450                 "org.omg.CORBA.TRANSACTION_UNAVAILABLE");
451     exceptionClassNames.put("IDL:omg.org/CORBA/TRANSACTION_MODE:1.0",
452                 "org.omg.CORBA.TRANSACTION_MODE");
453
454         // Exception types introduced between CORBA 2.4 and 3.0
455
exceptionClassNames.put("IDL:omg.org/CORBA/CODESET_INCOMPATIBLE:1.0",
456                                 "org.omg.CORBA.CODESET_INCOMPATIBLE");
457         exceptionClassNames.put("IDL:omg.org/CORBA/REBIND:1.0",
458                                 "org.omg.CORBA.REBIND");
459         exceptionClassNames.put("IDL:omg.org/CORBA/TIMEOUT:1.0",
460                                 "org.omg.CORBA.TIMEOUT");
461         exceptionClassNames.put("IDL:omg.org/CORBA/BAD_QOS:1.0",
462                                 "org.omg.CORBA.BAD_QOS");
463
464         // Exception types introduced in CORBA 3.0
465
exceptionClassNames.put("IDL:omg.org/CORBA/INVALID_ACTIVITY:1.0",
466                 "org.omg.CORBA.INVALID_ACTIVITY");
467         exceptionClassNames.put("IDL:omg.org/CORBA/ACTIVITY_COMPLETED:1.0",
468                 "org.omg.CORBA.ACTIVITY_COMPLETED");
469         exceptionClassNames.put("IDL:omg.org/CORBA/ACTIVITY_REQUIRED:1.0",
470                 "org.omg.CORBA.ACTIVITY_REQUIRED");
471
472     //
473
// construct className -> repositoryId hashtable
474
//
475
Enumeration JavaDoc keys = exceptionClassNames.keys();
476     java.lang.Object JavaDoc s;
477     String JavaDoc rId;
478     String JavaDoc cName;
479
480     try{
481             while (keys.hasMoreElements()) {
482                 s = keys.nextElement();
483             rId = (String JavaDoc) s;
484             cName = (String JavaDoc) exceptionClassNames.get(rId);
485             exceptionRepositoryIds.put (cName, rId);
486         }
487     } catch (NoSuchElementException JavaDoc e) { }
488     }
489
490     /** Parse a version string such as "1.1.6" or "jdk1.2fcs" into
491     a version array of integers {1, 1, 6} or {1, 2}.
492     A string of "n." or "n..m" is equivalent to "n.0" or "n.0.m" respectively.
493     */

494     public static int[] parseVersion(String JavaDoc version) {
495     if (version == null)
496         return new int[0];
497     char[] s = version.toCharArray();
498     //find the maximum span of the string "n.n.n..." where n is an integer
499
int start = 0;
500     for (; start < s.length && (s[start] < '0' || s[start] > '9'); ++start)
501         if (start == s.length) //no digit found
502
return new int[0];
503     int end = start + 1;
504     int size = 1;
505     for (; end < s.length; ++end)
506         if (s[end] == '.')
507         ++size;
508         else if (s[end] < '0' || s[end] > '9')
509         break;
510     int[] val = new int[size];
511     for (int i = 0; i < size; ++i) {
512         int dot = version.indexOf('.', start);
513         if (dot == -1 || dot > end)
514         dot = end;
515         if (start >= dot) //cases like "n." or "n..m"
516
val[i] = 0; //convert equivalent to "n.0" or "n.0.m"
517
else
518         val[i] = Integer.parseInt(version.substring(start, dot));
519         start = dot + 1;
520     }
521     return val;
522     }
523
524     /** Compare two version arrays.
525     Return 1, 0 or -1 if v1 is greater than, equal to, or less than v2.
526     */

527     public static int compareVersion(int[] v1, int[] v2) {
528     if (v1 == null)
529         v1 = new int[0];
530     if (v2 == null)
531         v2 = new int[0];
532     for (int i = 0; i < v1.length; ++i) {
533         if (i >= v2.length || v1[i] > v2[i]) //v1 is longer or greater than v2
534
return 1;
535         if (v1[i] < v2[i])
536         return -1;
537     }
538     return v1.length == v2.length ? 0 : -1;
539     }
540
541     /** Compare two version strings.
542     Return 1, 0 or -1 if v1 is greater than, equal to, or less than v2.
543     */

544     public static synchronized int compareVersion(String JavaDoc v1, String JavaDoc v2) {
545     return compareVersion(parseVersion(v1), parseVersion(v2));
546     }
547
548     private static String JavaDoc compressClassName( String JavaDoc name )
549     {
550     // Note that this must end in . in order to be renamed correctly.
551
String JavaDoc prefix = "com.sun.corba.se." ;
552     if (name.startsWith( prefix ) ) {
553         return "(ORB)." + name.substring( prefix.length() ) ;
554     } else
555         return name ;
556     }
557
558     // Return a compressed representation of the thread name. This is particularly
559
// useful on the server side, where there are many SelectReaderThreads, and
560
// we need a short unambiguous name for such threads.
561
public static String JavaDoc getThreadName( Thread JavaDoc thr )
562     {
563     if (thr == null)
564         return "null" ;
565
566     // This depends on the formatting in SelectReaderThread and CorbaConnectionImpl.
567
// Pattern for SelectReaderThreads:
568
// SelectReaderThread CorbaConnectionImpl[ <host> <post> <state>]
569
// Any other pattern in the Thread's name is just returned.
570
String JavaDoc name = thr.getName() ;
571     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( name ) ;
572     int numTokens = st.countTokens() ;
573     if (numTokens != 5)
574         return name ;
575
576     String JavaDoc[] tokens = new String JavaDoc[numTokens] ;
577     for (int ctr=0; ctr<numTokens; ctr++ )
578         tokens[ctr] = st.nextToken() ;
579
580     if( !tokens[0].equals("SelectReaderThread"))
581         return name ;
582
583     return "SelectReaderThread[" + tokens[2] + ":" + tokens[3] + "]" ;
584     }
585
586     private static String JavaDoc formatStackTraceElement( StackTraceElement JavaDoc ste )
587     {
588         return compressClassName( ste.getClassName() ) + "." + ste.getMethodName() +
589             (ste.isNativeMethod() ? "(Native Method)" :
590              (ste.getFileName() != null && ste.getLineNumber() >= 0 ?
591               "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")" :
592               (ste.getFileName() != null ? "("+ste.getFileName()+")" : "(Unknown Source)")));
593     }
594
595     private static void printStackTrace( StackTraceElement JavaDoc[] trace )
596     {
597     System.out.println( " Stack Trace:" ) ;
598     // print the stack trace, ommitting the zeroth element, which is
599
// always this method.
600
for ( int ctr = 1; ctr < trace.length; ctr++ ) {
601         System.out.print( " >" ) ;
602         System.out.println( formatStackTraceElement( trace[ctr] ) ) ;
603     }
604     }
605
606     //
607
// Implements all dprint calls in this package.
608
//
609
public static synchronized void dprint(java.lang.Object JavaDoc obj, String JavaDoc msg) {
610     System.out.println(
611         compressClassName( obj.getClass().getName() ) + "(" +
612         getThreadName( Thread.currentThread() ) + "): " + msg);
613     }
614
615     public static synchronized void dprint(String JavaDoc className, String JavaDoc msg) {
616     System.out.println(
617         compressClassName( className ) + "(" +
618         getThreadName( Thread.currentThread() ) + "): " + msg);
619     }
620
621     public synchronized void dprint(String JavaDoc msg) {
622     ORBUtility.dprint(this, msg);
623     }
624
625     public static synchronized void dprintTrace(Object JavaDoc obj, String JavaDoc msg) {
626     ORBUtility.dprint(obj, msg);
627
628     Throwable JavaDoc thr = new Throwable JavaDoc() ;
629     printStackTrace( thr.getStackTrace() ) ;
630     }
631
632     public static synchronized void dprint(java.lang.Object JavaDoc caller,
633     String JavaDoc msg, Throwable JavaDoc t)
634     {
635         System.out.println(
636         compressClassName( caller.getClass().getName() ) +
637         '(' + Thread.currentThread() + "): " + msg);
638
639         if (t != null)
640         printStackTrace( t.getStackTrace() ) ;
641     }
642
643     public static String JavaDoc[] concatenateStringArrays( String JavaDoc[] arr1, String JavaDoc[] arr2 )
644     {
645     String JavaDoc[] result = new String JavaDoc[
646         arr1.length + arr2.length ] ;
647
648     for (int ctr = 0; ctr<arr1.length; ctr++)
649         result[ctr] = arr1[ctr] ;
650
651     for (int ctr = 0; ctr<arr2.length; ctr++)
652         result[ctr + arr1.length] = arr2[ctr] ;
653
654     return result ;
655     }
656
657     /**
658      * Throws the CORBA equivalent of a java.io.NotSerializableException
659      *
660      * Duplicated from util/Utility for Pure ORB reasons. There are two
661      * reasons for this:
662      *
663      * 1) We can't introduce dependencies on the util version from outside
664      * of the io/util packages since it will not exist in the pure ORB
665      * build running on JDK 1.3.x.
666      *
667      * 2) We need to pick up the correct minor code from OMGSystemException.
668      */

669     public static void throwNotSerializableForCorba(String JavaDoc className) {
670     throw omgWrapper.notSerializable( CompletionStatus.COMPLETED_MAYBE,
671         className ) ;
672     }
673
674     /**
675      * Returns the maximum stream format version supported by our
676      * ValueHandler.
677      */

678     public static byte getMaxStreamFormatVersion() {
679         ValueHandler JavaDoc vh = Util.createValueHandler();
680
681         if (!(vh instanceof javax.rmi.CORBA.ValueHandlerMultiFormat JavaDoc))
682             return ORBConstants.STREAM_FORMAT_VERSION_1;
683         else
684             return ((ValueHandlerMultiFormat JavaDoc)vh).getMaximumStreamFormatVersion();
685     }
686     
687     public static CorbaClientDelegate makeClientDelegate( IOR ior )
688     {
689     ORB orb = ior.getORB() ;
690     CorbaContactInfoList ccil = orb.getCorbaContactInfoListFactory().create( ior ) ;
691     CorbaClientDelegate del = orb.getClientDelegateFactory().create(ccil);
692     return del ;
693     }
694
695     /** This method is used to create untyped object references.
696     */

697     public static org.omg.CORBA.Object JavaDoc makeObjectReference( IOR ior )
698     {
699     CorbaClientDelegate del = makeClientDelegate( ior ) ;
700     org.omg.CORBA.Object JavaDoc objectImpl = new CORBAObjectImpl() ;
701     StubAdapter.setDelegate( objectImpl, del ) ;
702     return objectImpl ;
703     }
704
705     /** This method obtains an IOR from a CORBA object reference.
706     * It will return null if obj is a local object, a null object,
707     * or an object implemented by a different ORB. It will
708     * throw BAD_OPERATION if obj is an unconnected RMI-IIOP object.
709     * @return IOR the IOR that represents this objref. This will
710     * never be null.
711     * @exception BAD_OPERATION (from oi._get_delegate) if obj is a
712     * normal objref, but does not have a delegate set.
713     * @exception BAD_PARAM if obj is a local object, or else was
714     * created by a foreign ORB.
715     */

716     public static IOR getIOR( org.omg.CORBA.Object JavaDoc obj )
717     {
718     if (obj == null)
719         throw wrapper.nullObjectReference() ;
720
721     IOR ior = null ;
722     if (StubAdapter.isStub(obj)) {
723         org.omg.CORBA.portable.Delegate JavaDoc del = StubAdapter.getDelegate(
724         obj ) ;
725
726         if (del instanceof CorbaClientDelegate) {
727         CorbaClientDelegate cdel = (CorbaClientDelegate)del ;
728         ContactInfoList cil = cdel.getContactInfoList() ;
729
730         if (cil instanceof CorbaContactInfoList) {
731             CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
732             ior = ccil.getTargetIOR() ;
733             if (ior == null)
734             throw wrapper.nullIor() ;
735
736             return ior ;
737         } else {
738             // This is our code, but the ContactInfoList is not a
739
// CorbaContactInfoList. This should not happen, because
740
// we are in the CORBA application of the DCSA framework.
741
// This is a coding error, and thus an INTERNAL exception
742
// should be thrown.
743
// XXX needs minor code
744
throw new INTERNAL JavaDoc() ;
745         }
746         }
747
748         // obj is implemented by a foreign ORB, because the Delegate is not a
749
// ClientDelegate.
750
// XXX this case could be handled by marshalling and
751
// unmarshalling. However, object_to_string cannot be used
752
// here, as it is implemented with getIOR. Note that this
753
// will require access to an ORB, so that we can create streams
754
// as needed. The ORB is available simply as io._orb().
755
throw wrapper.objrefFromForeignOrb() ;
756     } else
757         throw wrapper.localObjectNotAllowed() ;
758     }
759
760     /** Obtains an IOR for the object reference obj, first connecting it to
761     * the ORB if necessary.
762     * @return IOR the IOR that represents this objref. This will
763     * never be null.
764     * @exception BAD_OPERATION if the object could not be connected,
765     * if a connection attempt was needed.
766     * @exception BAD_PARAM if obj is a local object, or else was
767     * created by a foreign ORB.
768     */

769     public static IOR connectAndGetIOR( ORB orb, org.omg.CORBA.Object JavaDoc obj )
770     {
771     IOR result ;
772     try {
773         result = getIOR( obj ) ;
774     } catch (BAD_OPERATION JavaDoc bop) {
775         if (StubAdapter.isStub(obj)) {
776         try {
777             StubAdapter.connect( obj, orb ) ;
778         } catch (java.rmi.RemoteException JavaDoc exc) {
779             throw wrapper.connectingServant( exc ) ;
780         }
781         } else {
782         orb.connect( obj ) ;
783         }
784
785         result = getIOR( obj ) ;
786     }
787     
788     return result ;
789     }
790
791     public static String JavaDoc operationNameAndRequestId(CorbaMessageMediator m)
792     {
793     return "op/" + m.getOperationName() + " id/" + m.getRequestId();
794     }
795
796     public static boolean isPrintable(char c)
797     {
798     if (Character.isJavaIdentifierStart(c)) {
799         // Letters and $ _
800
return true;
801     }
802     if (Character.isDigit(c)) {
803         return true;
804     }
805     switch (Character.getType(c)) {
806     case Character.MODIFIER_SYMBOL : return true; // ` ^
807
case Character.DASH_PUNCTUATION : return true; // -
808
case Character.MATH_SYMBOL : return true; // = ~ + | < >
809
case Character.OTHER_PUNCTUATION : return true; // !@#%&*;':",./?
810
case Character.START_PUNCTUATION : return true; // ( [ {
811
case Character.END_PUNCTUATION : return true; // ) ] }
812
}
813     return false;
814     }
815
816     public static String JavaDoc getClassSecurityInfo(final Class JavaDoc cl)
817     {
818     // Returns a String which looks similar to:
819
// PermissionCollection java.security.Permissions@1053693 ...
820
// (java.io.FilePermission <<ALL FILES>> ....)
821
// (java.io.FilePermission /export0/sunwappserv/lib/- ...)
822
// ... other permissions ...
823
// Domain ProtectionDomain (file:/export0/sunwappserv/lib-)
824
// java.security.Permissions@141fedb (
825
// (java.io.FilePermission <<ALL FILES>> ...)
826
// (java.io.FilePermission /var/tmp//- ...)
827

828     String JavaDoc result =
829         (String JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
830             public java.lang.Object JavaDoc run() {
831             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(500);
832             ProtectionDomain JavaDoc pd = cl.getProtectionDomain();
833                     Policy JavaDoc policy = Policy.getPolicy();
834                 PermissionCollection JavaDoc pc = policy.getPermissions(pd);
835                 sb.append("\nPermissionCollection ");
836                 sb.append(pc.toString());
837             // Don't need to add 'Protection Domain' string, it's
838
// in ProtectionDomain.toString() already.
839
sb.append(pd.toString());
840                     return sb.toString();
841         }
842             });
843     return result;
844     }
845 }
846
847 // End of file.
848
Popular Tags