KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RepositoryId_1_3.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.orbutil;
17
18 import java.util.StringTokenizer JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22
23 // Imports for using codebase URL to load class
24
import java.net.MalformedURLException JavaDoc;
25 import org.omg.CORBA.portable.ValueBase JavaDoc;
26 import org.omg.CORBA.portable.IDLEntity JavaDoc;
27
28 import com.sun.corba.se.impl.util.JDKBridge;
29 import com.sun.corba.se.impl.util.Utility;
30 import com.sun.corba.se.impl.util.PackagePrefixChecker;
31 import com.sun.corba.se.impl.util.IdentityHashtable;
32 import com.sun.corba.se.impl.io.ObjectStreamClass;
33
34 import javax.rmi.CORBA.Util JavaDoc;
35
36 // keeping the original RepositoryId class that was shipped in
37
// JDK 1.3. It has interoperability bugs
38

39 public class RepositoryId_1_3 {
40     
41     // Legal IDL Identifier characters (1 = legal). Note
42
// that '.' (2E) is marked as legal even though it is
43
// not legal in IDL. This allows us to treat a fully
44
// qualified Java name with '.' package separators
45
// uniformly, and is safe because that is the only
46
// legal use of '.' in a Java name.
47

48     public static final RepositoryIdCache_1_3 cache = new RepositoryIdCache_1_3();
49     public static final byte[] IDL_IDENTIFIER_CHARS = {
50         
51     // 0 1 2 3 4 5 6 7 8 9 a b c d e f
52
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 00-0f
53
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 10-1f
54
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, // 20-2f
55
1,1,1,1, 1,1,1,1, 1,1,0,0, 0,0,0,0, // 30-3f
56
0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // 40-4f
57
1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,1, // 50-5f
58
0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // 60-6f
59
1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0, // 70-7f
60
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 80-8f
61
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 90-9f
62
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // a0-af
63
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // b0-bf
64
1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // c0-cf
65
0,1,1,1, 1,1,1,0, 1,1,1,1, 1,0,0,1, // d0-df
66
1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // e0-ef
67
0,1,1,1, 1,1,1,0, 1,1,1,1, 1,0,0,1, // f0-ff
68
};
69
70     private static String JavaDoc defaultServerURL = null;
71     private static boolean useCodebaseOnly = false;
72
73     static {
74     if (defaultServerURL == null)
75         defaultServerURL = (String JavaDoc)JDKBridge.getLocalCodebase();
76     useCodebaseOnly = JDKBridge.useCodebaseOnly();
77
78     }
79
80     private static IdentityHashtable classToRepStr = new IdentityHashtable();
81     private static IdentityHashtable classIDLToRepStr = new IdentityHashtable();
82     private static IdentityHashtable classSeqToRepStr = new IdentityHashtable();
83
84     private static IdentityHashtable repStrToByteArray = new IdentityHashtable();
85     private static Hashtable JavaDoc repStrToClass = new Hashtable JavaDoc();
86
87     private String JavaDoc repId = null;
88     private boolean isSupportedFormat = true;
89     private String JavaDoc typeString = null;
90     private String JavaDoc versionString = null;
91     private boolean isSequence = false;
92     private boolean isRMIValueType = false;
93     private boolean isIDLType = false;
94     private String JavaDoc completeClassName = null;
95     private String JavaDoc unqualifiedName = null;
96     private String JavaDoc definedInId = null;
97     private Class JavaDoc clazz = null;
98     private String JavaDoc suid = null, actualSuid = null;
99     private long suidLong = ObjectStreamClass.kDefaultUID, actualSuidLong = ObjectStreamClass.kDefaultUID;
100
101     // Repository ID fragments
102
private static final String JavaDoc kValuePrefix = "RMI:";
103     private static final String JavaDoc kIDLPrefix = "IDL:";
104     private static final String JavaDoc kIDLNamePrefix = "omg.org/";
105     private static final String JavaDoc kIDLClassnamePrefix = "org.omg.";
106     private static final String JavaDoc kSequencePrefix = "[";
107     private static final String JavaDoc kCORBAPrefix = "CORBA/";
108     private static final String JavaDoc kArrayPrefix = kValuePrefix + kSequencePrefix + kCORBAPrefix;
109     private static final int kValuePrefixLength = kValuePrefix.length();
110     private static final int kIDLPrefixLength = kIDLPrefix.length();
111     private static final int kSequencePrefixLength = kSequencePrefix.length();
112     private static final String JavaDoc kInterfaceHashCode = ":0000000000000000";
113     private static final String JavaDoc kInterfaceOnlyHashStr = "0000000000000000";
114     private static final String JavaDoc kExternalizableHashStr = "0000000000000001";
115
116     // Value tag utility methods and constants
117
public static final int kInitialValueTag= 0x7fffff00;
118     public static final int kNoTypeInfo = 0;
119     public static final int kSingleRepTypeInfo = 0x02;
120     public static final int kPartialListTypeInfo = 0x06;
121     public static final int kChunkedMask = 0x08;
122
123     // Public, well known repository IDs
124

125     // _REVISIT_ : A table structure with a good search routine for all of this
126
// would be more efficient and easier to maintain...
127

128     // String
129
public static final String JavaDoc kWStringValueVersion = "1.0";
130     public static final String JavaDoc kWStringValueHash = ":"+kWStringValueVersion;
131     public static final String JavaDoc kWStringStubValue = "WStringValue";
132     public static final String JavaDoc kWStringTypeStr = "omg.org/CORBA/"+kWStringStubValue;
133     public static final String JavaDoc kWStringValueRepID = kIDLPrefix + kWStringTypeStr + kWStringValueHash;
134
135     // Any
136
public static final String JavaDoc kAnyRepID = kIDLPrefix + "omg.org/CORBA/Any";
137
138     // Class
139
public static final String JavaDoc kClassDescValueHash = ":" + Long.toHexString(
140        ObjectStreamClass.getSerialVersionUID(javax.rmi.CORBA.ClassDesc JavaDoc.class));
141     public static final String JavaDoc kClassDescStubValue = "ClassDesc";
142     public static final String JavaDoc kClassDescTypeStr = "javax.rmi.CORBA."+kClassDescStubValue;
143     public static final String JavaDoc kClassDescValueRepID = kValuePrefix + kClassDescTypeStr + kClassDescValueHash;
144
145     // Object
146
public static final String JavaDoc kObjectValueHash = ":1.0";
147     public static final String JavaDoc kObjectStubValue = "Object";
148
149     // Sequence
150
public static final String JavaDoc kSequenceValueHash = ":1.0";
151     public static final String JavaDoc kPrimitiveSequenceValueHash = ":0000000000000000";
152
153     // Serializable
154
public static final String JavaDoc kSerializableValueHash = ":1.0";
155     public static final String JavaDoc kSerializableStubValue = "Serializable";
156
157     // Externalizable
158
public static final String JavaDoc kExternalizableValueHash = ":1.0";
159     public static final String JavaDoc kExternalizableStubValue = "Externalizable";
160
161     // Remote (The empty string is used for java.rmi.Remote)
162
public static final String JavaDoc kRemoteValueHash = "";
163     public static final String JavaDoc kRemoteStubValue = "";
164     public static final String JavaDoc kRemoteTypeStr = "";
165     public static final String JavaDoc kRemoteValueRepID = "";
166
167     public static final Hashtable JavaDoc kSpecialArrayTypeStrings = new Hashtable JavaDoc();
168
169     static {
170     kSpecialArrayTypeStrings.put("CORBA.WStringValue", new StringBuffer JavaDoc(java.lang.String JavaDoc.class.getName()));
171     kSpecialArrayTypeStrings.put("javax.rmi.CORBA.ClassDesc", new StringBuffer JavaDoc(java.lang.Class JavaDoc.class.getName()));
172     kSpecialArrayTypeStrings.put("CORBA.Object", new StringBuffer JavaDoc(java.rmi.Remote JavaDoc.class.getName()));
173         
174     }
175
176     public static final Hashtable JavaDoc kSpecialCasesRepIDs = new Hashtable JavaDoc();
177
178     static {
179     kSpecialCasesRepIDs.put(java.lang.String JavaDoc.class, kWStringValueRepID);
180     kSpecialCasesRepIDs.put(java.lang.Class JavaDoc.class, kClassDescValueRepID);
181     kSpecialCasesRepIDs.put(java.rmi.Remote JavaDoc.class, kRemoteValueRepID);
182     }
183
184     public static final Hashtable JavaDoc kSpecialCasesStubValues = new Hashtable JavaDoc();
185
186     static {
187     kSpecialCasesStubValues.put(java.lang.String JavaDoc.class, kWStringStubValue);
188     kSpecialCasesStubValues.put(java.lang.Class JavaDoc.class, kClassDescStubValue);
189     kSpecialCasesStubValues.put(java.lang.Object JavaDoc.class, kObjectStubValue);
190     kSpecialCasesStubValues.put(java.io.Serializable JavaDoc.class, kSerializableStubValue);
191     kSpecialCasesStubValues.put(java.io.Externalizable JavaDoc.class, kExternalizableStubValue);
192     kSpecialCasesStubValues.put(java.rmi.Remote JavaDoc.class, kRemoteStubValue);
193     }
194
195
196     public static final Hashtable JavaDoc kSpecialCasesVersions = new Hashtable JavaDoc();
197
198     static {
199     kSpecialCasesVersions.put(java.lang.String JavaDoc.class, kWStringValueHash);
200     kSpecialCasesVersions.put(java.lang.Class JavaDoc.class, kClassDescValueHash);
201     kSpecialCasesVersions.put(java.lang.Object JavaDoc.class, kObjectValueHash);
202     kSpecialCasesVersions.put(java.io.Serializable JavaDoc.class, kSerializableValueHash);
203     kSpecialCasesVersions.put(java.io.Externalizable JavaDoc.class, kExternalizableValueHash);
204     kSpecialCasesVersions.put(java.rmi.Remote JavaDoc.class, kRemoteValueHash);
205     }
206
207     public static final Hashtable JavaDoc kSpecialCasesClasses = new Hashtable JavaDoc();
208
209     static {
210     kSpecialCasesClasses.put(kWStringTypeStr, java.lang.String JavaDoc.class);
211     kSpecialCasesClasses.put(kClassDescTypeStr, java.lang.Class JavaDoc.class);
212     kSpecialCasesClasses.put(kRemoteTypeStr, java.rmi.Remote JavaDoc.class);
213
214     kSpecialCasesClasses.put("org.omg.CORBA.WStringValue", java.lang.String JavaDoc.class);
215     kSpecialCasesClasses.put("javax.rmi.CORBA.ClassDesc", java.lang.Class JavaDoc.class);
216     //kSpecialCasesClasses.put(kRemoteTypeStr, java.rmi.Remote.class);
217
}
218
219     public static final Hashtable JavaDoc kSpecialCasesArrayPrefix = new Hashtable JavaDoc();
220
221     static {
222     kSpecialCasesArrayPrefix.put(java.lang.String JavaDoc.class, kValuePrefix + kSequencePrefix + kCORBAPrefix);
223     kSpecialCasesArrayPrefix.put(java.lang.Class JavaDoc.class, kValuePrefix + kSequencePrefix + "javax/rmi/CORBA/");
224     kSpecialCasesArrayPrefix.put(java.lang.Object JavaDoc.class, kValuePrefix + kSequencePrefix + "java/lang/");
225     kSpecialCasesArrayPrefix.put(java.io.Serializable JavaDoc.class, kValuePrefix + kSequencePrefix + "java/io/");
226     kSpecialCasesArrayPrefix.put(java.io.Externalizable JavaDoc.class, kValuePrefix + kSequencePrefix + "java/io/");
227     kSpecialCasesArrayPrefix.put(java.rmi.Remote JavaDoc.class, kValuePrefix + kSequencePrefix + kCORBAPrefix);
228     }
229
230     public static final Hashtable JavaDoc kSpecialPrimitives = new Hashtable JavaDoc();
231
232     static {
233     kSpecialPrimitives.put("int","long");
234     kSpecialPrimitives.put("long","longlong");
235     kSpecialPrimitives.put("byte","octet");
236     }
237
238     /**
239      * Used to convert ascii to hex.
240      */

241     private static final byte ASCII_HEX[] = {
242     (byte)'0',
243     (byte)'1',
244     (byte)'2',
245     (byte)'3',
246     (byte)'4',
247     (byte)'5',
248     (byte)'6',
249     (byte)'7',
250     (byte)'8',
251     (byte)'9',
252     (byte)'A',
253     (byte)'B',
254     (byte)'C',
255     (byte)'D',
256     (byte)'E',
257     (byte)'F',
258     };
259
260
261     // Interface Rep ID Strings
262
public static final String JavaDoc kjava_rmi_Remote = createForAnyType(java.rmi.Remote JavaDoc.class);
263     public static final String JavaDoc korg_omg_CORBA_Object = createForAnyType(org.omg.CORBA.Object JavaDoc.class);
264
265     // Dummy arguments for getIdFromHelper method
266
public static final Class JavaDoc kNoParamTypes[] ={};
267     public static final Object JavaDoc kNoArgs[] = {};
268
269
270     RepositoryId_1_3(){}
271
272     RepositoryId_1_3(String JavaDoc aRepId){
273     init(aRepId);
274     }
275
276     RepositoryId_1_3 init(String JavaDoc aRepId){
277
278         this.repId = aRepId;
279         
280     // Special case for remote
281
if (aRepId.length() == 0) {
282         clazz = java.rmi.Remote JavaDoc.class;
283         typeString = "";
284         isRMIValueType = true;
285         suid = kInterfaceOnlyHashStr;
286         return this;
287     }
288     else if (aRepId.equals(kWStringValueRepID)) {
289         clazz = java.lang.String JavaDoc.class;
290         typeString = kWStringTypeStr;
291         isIDLType = true;
292         versionString = kWStringValueVersion;
293         return this;
294     }
295     else {
296             
297     String JavaDoc repId = convertFromISOLatin1(aRepId);
298
299     versionString = repId.substring(repId.indexOf(':', repId.indexOf(':')+1));
300         if (repId.startsWith(kIDLPrefix)) {
301             typeString =
302                 repId.substring(kIDLPrefixLength, repId.indexOf(':', kIDLPrefixLength));
303         isIDLType = true;
304         if (typeString.startsWith(kIDLNamePrefix))
305         completeClassName = kIDLClassnamePrefix +
306             typeString.substring(kIDLNamePrefix.length()).replace('/','.');
307         else completeClassName = typeString.replace('/','.');
308
309         }
310         else if (repId.startsWith(kValuePrefix)) {
311             typeString =
312                 repId.substring(kValuePrefixLength, repId.indexOf(':', kValuePrefixLength));
313         isRMIValueType = true;
314
315         if (versionString.indexOf('.') == -1) {
316             actualSuid = versionString.substring(1);
317             suid = actualSuid; // default if not explicitly specified
318

319             if (actualSuid.indexOf(':') != -1){
320             // we have a declared hash also
321
int pos = actualSuid.indexOf(':')+1;
322             // actualSuid = suid.substring(pos);
323
// suid = suid.substring(0, pos-1);
324
suid = actualSuid.substring(pos);
325             actualSuid = actualSuid.substring(0, pos-1);
326         }
327             
328         }
329         else {
330             // _REVISIT_ : Special case version failure ?
331
}
332         }
333         else isSupportedFormat = false;
334
335         if (typeString.startsWith(kSequencePrefix)) {
336         isSequence = true;
337         }
338         
339
340         return this;
341     }
342     }
343
344     public final String JavaDoc getUnqualifiedName() {
345     if (unqualifiedName == null){
346         String JavaDoc className = getClassName();
347             int index = (className != null) ? className.lastIndexOf('.') : -1;
348         if (index == -1){
349         unqualifiedName = className;
350         definedInId = "IDL::1.0";
351         }
352         else {
353         unqualifiedName = className.substring(index);
354         definedInId = "IDL:" + className.substring(0, index).replace('.','/') + ":1.0";
355         }
356     }
357         
358     return unqualifiedName;
359     }
360
361     public final String JavaDoc getDefinedInId() {
362     if (definedInId == null){
363         getUnqualifiedName();
364     }
365         
366     return definedInId;
367     }
368
369     public final String JavaDoc getTypeString() {
370         return typeString;
371     }
372
373     public final String JavaDoc getVersionString() {
374         return versionString;
375     }
376     
377     public final String JavaDoc getSerialVersionUID() {
378     return suid;
379     }
380
381     public final String JavaDoc getActualSerialVersionUID() {
382     return actualSuid;
383     }
384     public final long getSerialVersionUIDAsLong() {
385     return suidLong;
386     }
387
388     public final long getActualSerialVersionUIDAsLong() {
389     return actualSuidLong;
390     }
391
392     public final boolean isRMIValueType() {
393     return isRMIValueType;
394     }
395     
396     public final boolean isIDLType() {
397     return isIDLType;
398     }
399
400     public final String JavaDoc getRepositoryId() {
401         return repId;
402     }
403
404     public static byte[] getByteArray(String JavaDoc repStr) {
405     synchronized (repStrToByteArray){
406         return (byte[]) repStrToByteArray.get(repStr);
407     }
408     }
409
410     public static void setByteArray(String JavaDoc repStr, byte[] repStrBytes) {
411     synchronized (repStrToByteArray){
412         repStrToByteArray.put(repStr, repStrBytes);
413     }
414     }
415
416     public final boolean isSequence() {
417         return isSequence;
418     }
419
420     public final boolean isSupportedFormat() {
421         return isSupportedFormat;
422     }
423
424     
425     // This method will return the classname from the typestring OR if the classname turns out to be
426
// a special class "pseudo" name, then the matching real classname is returned.
427
public final String JavaDoc getClassName() {
428
429     if (isRMIValueType)
430         return typeString;
431     else if (isIDLType)
432         return completeClassName;
433     else return null;
434
435     }
436
437     // This method calls getClazzFromType() and falls back to the repStrToClass
438
// cache if no class was found. It's used where any class matching the
439
// given repid is an acceptable result.
440
public final Class JavaDoc getAnyClassFromType() throws ClassNotFoundException JavaDoc {
441         try {
442             return getClassFromType();
443         } catch (ClassNotFoundException JavaDoc cnfe) {
444             Class JavaDoc clz = (Class JavaDoc)repStrToClass.get(repId);
445             if (clz != null)
446                 return clz;
447             else
448                 throw cnfe;
449         }
450     }
451
452     public final Class JavaDoc getClassFromType()
453         throws ClassNotFoundException JavaDoc {
454     if (clazz != null)
455         return clazz;
456         
457     Class JavaDoc specialCase = (Class JavaDoc)kSpecialCasesClasses.get(getClassName());
458
459     if (specialCase != null){
460         clazz = specialCase;
461         return specialCase;
462     }
463     else
464         {
465         try{
466             return Util.loadClass(getClassName(), null, null);
467         }
468         catch(ClassNotFoundException JavaDoc cnfe){
469             if (defaultServerURL != null) {
470             try{
471                 return getClassFromType(defaultServerURL);
472             }
473             catch(MalformedURLException JavaDoc mue){
474                 throw cnfe;
475             }
476             }
477             else throw cnfe;
478         }
479         }
480
481     }
482
483     public final Class JavaDoc getClassFromType(Class JavaDoc expectedType, String JavaDoc codebase)
484         throws ClassNotFoundException JavaDoc {
485     if (clazz != null)
486         return clazz;
487         
488     Class JavaDoc specialCase = (Class JavaDoc)kSpecialCasesClasses.get(getClassName());
489
490     if (specialCase != null){
491         clazz = specialCase;
492         return specialCase;
493     } else {
494             ClassLoader JavaDoc expectedTypeClassLoader = (expectedType == null ? null : expectedType.getClassLoader());
495             return loadClassOfType(getClassName(),
496                                             codebase,
497                                             expectedTypeClassLoader,
498                                             expectedType,
499                                             expectedTypeClassLoader);
500         }
501
502     }
503
504     public final Class JavaDoc getClassFromType(String JavaDoc url)
505     throws ClassNotFoundException JavaDoc, MalformedURLException JavaDoc {
506     return Util.loadClass(getClassName(), url, null);
507     }
508
509     public final String JavaDoc toString() {
510         return repId;
511     }
512
513     private static String JavaDoc createHashString(java.io.Serializable JavaDoc ser) {
514
515     return createHashString(ser.getClass());
516     }
517
518     private static String JavaDoc createHashString(java.lang.Class JavaDoc clazz) {
519
520     if (clazz.isInterface() || !java.io.Serializable JavaDoc.class.isAssignableFrom(clazz))
521         return kInterfaceHashCode;
522
523
524     long actualLong = ObjectStreamClassUtil_1_3.computeStructuralUID(false, clazz);
525     String JavaDoc hash = null;
526     if (actualLong == 0)
527         hash = kInterfaceOnlyHashStr;
528     else if (actualLong == 1)
529         hash = kExternalizableHashStr;
530     else
531         hash = Long.toHexString(actualLong).toUpperCase();
532     while(hash.length() < 16){
533         hash = "0" + hash;
534     }
535
536     long declaredLong = ObjectStreamClassUtil_1_3.computeSerialVersionUID(clazz);
537     String JavaDoc declared = null;
538     if (declaredLong == 0)
539         declared = kInterfaceOnlyHashStr;
540     else if (declaredLong == 1)
541         declared = kExternalizableHashStr;
542     else
543         declared = Long.toHexString(declaredLong).toUpperCase();
544     while (declared.length() < 16){
545         declared = "0" + declared;
546     }
547     hash = hash + ":" + declared;
548
549     return ":" + hash;
550     }
551
552     /**
553      * Creates a repository ID for a sequence. This is for expert users only as
554      * this method assumes the object passed is an array. If passed an object
555      * that is not an array, it will produce a rep id for a sequence of zero
556      * length. This would be an error.
557      * @param ser The Java object to create a repository ID for
558      **/

559     public static String JavaDoc createSequenceRepID(java.lang.Object JavaDoc ser){
560     return createSequenceRepID(ser.getClass());
561     }
562
563     /**
564      * Creates a repository ID for a sequence. This is for expert users only as
565      * this method assumes the object passed is an array. If passed an object
566      * that is not an array, it will produce a malformed rep id.
567      * @param clazz The Java class to create a repository ID for
568      **/

569     public static String JavaDoc createSequenceRepID(java.lang.Class JavaDoc clazz){
570     synchronized (classSeqToRepStr){
571         
572         String JavaDoc repid = (String JavaDoc)classSeqToRepStr.get(clazz);
573     if (repid != null)
574         return repid;
575
576     Class JavaDoc originalClazz = clazz;
577
578         Class JavaDoc type = null;
579     int numOfDims = 0;
580
581         while ((type = clazz.getComponentType()) != null) {
582         numOfDims++;
583             clazz = type;
584         }
585
586     if (clazz.isPrimitive())
587         repid = kValuePrefix + originalClazz.getName() + kPrimitiveSequenceValueHash;
588     else {
589         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
590         buf.append(kValuePrefix);
591         while(numOfDims-- > 0) {
592         buf.append("[");
593         }
594         buf.append("L");
595         buf.append(convertToISOLatin1(clazz.getName()));
596         buf.append(";");
597         buf.append(createHashString(clazz));
598         repid = buf.toString();
599     }
600     classSeqToRepStr.put(originalClazz,repid);
601     return repid;
602     }
603
604     }
605
606
607     public static String JavaDoc createForSpecialCase(java.lang.Class JavaDoc clazz){
608     if (clazz.isArray()){
609         return createSequenceRepID(clazz);
610     }
611     else {
612         return (String JavaDoc)kSpecialCasesRepIDs.get(clazz);
613     }
614     }
615
616     public static String JavaDoc createForSpecialCase(java.io.Serializable JavaDoc ser){
617     Class JavaDoc clazz = ser.getClass();
618     if (clazz.isArray()){
619         return createSequenceRepID(ser);
620     }
621     else
622         return createForSpecialCase(clazz);
623     }
624     
625     /**
626      * Creates a repository ID for a normal Java Type.
627      * @param ser The Java object to create a repository ID for
628      * @exception com.sun.corba.se.impl.io.TypeMismatchException if ser implements the
629      * org.omg.CORBA.portable.IDLEntity interface which indicates it is an IDL Value type.
630      **/

631     public static String JavaDoc createForJavaType(java.io.Serializable JavaDoc ser)
632         throws com.sun.corba.se.impl.io.TypeMismatchException
633     {
634     synchronized (classToRepStr) {
635     String JavaDoc repid = createForSpecialCase(ser);
636     if (repid != null)
637         return repid;
638     Class JavaDoc clazz = ser.getClass();
639     repid = (String JavaDoc)classToRepStr.get(clazz);
640
641     if (repid != null)
642         return repid;
643
644     repid = kValuePrefix + convertToISOLatin1(clazz.getName()) +
645         createHashString(clazz);
646
647     classToRepStr.put(clazz, repid);
648         repStrToClass.put(repid, clazz);
649         return repid;
650     }
651     }
652
653     /**
654      * Creates a repository ID for a normal Java Type.
655      * @param clz The Java class to create a repository ID for
656      * @exception com.sun.corba.se.impl.io.TypeMismatchException if ser implements the
657      * org.omg.CORBA.portable.IDLEntity interface which indicates it is an IDL Value type.
658      **/

659     public static String JavaDoc createForJavaType(Class JavaDoc clz)
660         throws com.sun.corba.se.impl.io.TypeMismatchException
661     {
662     synchronized (classToRepStr){
663     String JavaDoc repid = createForSpecialCase(clz);
664     if (repid != null)
665         return repid;
666
667     repid = (String JavaDoc)classToRepStr.get(clz);
668     if (repid != null)
669         return repid;
670
671         repid = kValuePrefix + convertToISOLatin1(clz.getName()) +
672         createHashString(clz);
673
674     classToRepStr.put(clz, repid);
675         repStrToClass.put(repid, clz);
676         return repid;
677     }
678     }
679
680     /**
681      * Creates a repository ID for an IDL Java Type.
682      * @param ser The IDL Value object to create a repository ID for
683      * @param major The major version number
684      * @param minor The minor version number
685      * @exception com.sun.corba.se.impl.io.TypeMismatchException if ser does not implement the
686      * org.omg.CORBA.portable.IDLEntity interface which indicates it is an IDL Value type.
687      **/

688     public static String JavaDoc createForIDLType(Class JavaDoc ser, int major, int minor)
689         throws com.sun.corba.se.impl.io.TypeMismatchException
690     {
691     synchronized (classIDLToRepStr){
692     String JavaDoc repid = (String JavaDoc)classIDLToRepStr.get(ser);
693     if (repid != null)
694         return repid;
695
696         repid = kIDLPrefix + convertToISOLatin1(ser.getName()).replace('.','/') +
697         ":" + major + "." + minor;
698     classIDLToRepStr.put(ser, repid);
699     return repid;
700     }
701     }
702
703     private static String JavaDoc getIdFromHelper(Class JavaDoc clazz){
704         try {
705         Class JavaDoc helperClazz = Utility.loadClassForClass(clazz.getName()+"Helper", null,
706                                     clazz.getClassLoader(), clazz, clazz.getClassLoader());
707         Method JavaDoc idMethod = helperClazz.getDeclaredMethod("id", kNoParamTypes);
708         return (String JavaDoc)idMethod.invoke(null, kNoArgs);
709         }
710         catch(java.lang.ClassNotFoundException JavaDoc cnfe)
711         {
712             throw new org.omg.CORBA.MARSHAL JavaDoc(cnfe.toString());
713         }
714         catch(java.lang.NoSuchMethodException JavaDoc nsme)
715         {
716             throw new org.omg.CORBA.MARSHAL JavaDoc(nsme.toString());
717         }
718         catch(java.lang.reflect.InvocationTargetException JavaDoc ite)
719         {
720             throw new org.omg.CORBA.MARSHAL JavaDoc(ite.toString());
721         }
722         catch(java.lang.IllegalAccessException JavaDoc iae)
723         {
724             throw new org.omg.CORBA.MARSHAL JavaDoc(iae.toString());
725     }
726     }
727
728     /**
729      * Createa a repository ID for the type if it is either a java type
730      * or an IDL type.
731      * @param type The type to create rep. id for
732      * @return The rep. id.
733      **/

734     public static String JavaDoc createForAnyType(Class JavaDoc type) {
735     try{
736         if (type.isArray())
737         return createSequenceRepID(type);
738         else if (IDLEntity JavaDoc.class.isAssignableFrom(type))
739         {
740             try{
741             return getIdFromHelper(type);
742             }
743             catch(Throwable JavaDoc t) {
744             return createForIDLType(type, 1, 0);
745             }
746         }
747         else return createForJavaType(type);
748     }
749     catch(com.sun.corba.se.impl.io.TypeMismatchException e){
750         return null;
751     }
752
753     }
754
755     public static boolean isAbstractBase(Class JavaDoc clazz) {
756         return (clazz.isInterface() &&
757                 IDLEntity JavaDoc.class.isAssignableFrom(clazz) &&
758                 (!ValueBase JavaDoc.class.isAssignableFrom(clazz)) &&
759                 (!org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(clazz)));
760
761     }
762
763     /**
764      * Convert strings with illegal IDL identifier characters.
765      * <p>
766      * Section 5.5.7 of OBV spec.
767      */

768     private static String JavaDoc convertToISOLatin1 (String JavaDoc name) {
769
770         int length = name.length();
771         if (length == 0) {
772             return name;
773         }
774         StringBuffer JavaDoc buffer = null;
775
776         for (int i = 0; i < length; i++) {
777
778             char c = name.charAt(i);
779
780             if (c > 255 || IDL_IDENTIFIER_CHARS[c] == 0) {
781             
782                 // We gotta convert. Have we already started?
783

784                 if (buffer == null) {
785
786                     // No, so get set up...
787

788                     buffer = new StringBuffer JavaDoc(name.substring(0,i));
789                 }
790
791                 // Convert the character into the IDL escape syntax...
792
buffer.append(
793                   "\\U" +
794                   (char)ASCII_HEX[(c & 0xF000) >>> 12] +
795                   (char)ASCII_HEX[(c & 0x0F00) >>> 8] +
796                   (char)ASCII_HEX[(c & 0x00F0) >>> 4] +
797                   (char)ASCII_HEX[(c & 0x000F)]);
798                 
799             } else {
800                 if (buffer != null) {
801                     buffer.append(c);
802                 }
803             }
804         }
805         
806         if (buffer != null) {
807             name = buffer.toString();
808         }
809  
810         return name;
811     }
812     
813     /**
814      * Convert strings with ISO Latin 1 escape sequences back to original strings.
815      * <p>
816      * Section 5.5.7 of OBV spec.
817      */

818     private static String JavaDoc convertFromISOLatin1 (String JavaDoc name) {
819
820         int index = -1;
821         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(name);
822
823         while ((index = buf.toString().indexOf("\\U")) != -1){
824             String JavaDoc str = "0000" + buf.toString().substring(index+2, index+6);
825
826             // Convert Hexadecimal
827
byte[] buffer = new byte[(str.length() - 4) / 2];
828             for (int i=4, j=0; i < str.length(); i +=2, j++) {
829                 buffer[j] = (byte)((ORBUtility.hexOf(str.charAt(i)) << 4) & 0xF0);
830                 buffer[j] |= (byte)((ORBUtility.hexOf(str.charAt(i+1)) << 0) & 0x0F);
831             }
832             buf = new StringBuffer JavaDoc(delete(buf.toString(), index, index+6));
833             buf.insert(index, (char)buffer[1]);
834         }
835         
836         return buf.toString();
837
838
839     }
840
841     private static String JavaDoc delete(String JavaDoc str, int from, int to)
842     {
843         return str.substring(0, from) + str.substring(to, str.length());
844     }
845
846     private static String JavaDoc replace(String JavaDoc target, String JavaDoc arg, String JavaDoc source)
847     {
848         int i = 0;
849         i = target.indexOf(arg);
850
851         while(i != -1)
852         {
853         String JavaDoc left = target.substring(0, i);
854         String JavaDoc right = target.substring(i+arg.length());
855         target = new String JavaDoc(left+source+right);
856         i = target.indexOf(arg);
857         }
858         return target;
859     }
860
861     /*
862      * Load a class and check that it is assignable to a given type.
863      * @param className the class name.
864      * @param remoteCodebase the codebase to use. May be null.
865      * @param loader the class loader of last resort. May be null.
866      * @param expectedType the expected type. May be null.
867      * @return the loaded class.
868      */

869     private Class JavaDoc loadClassOfType (String JavaDoc className,
870                                   String JavaDoc remoteCodebase,
871                                   ClassLoader JavaDoc loader,
872                                   Class JavaDoc expectedType,
873                                   ClassLoader JavaDoc expectedTypeClassLoader)
874     throws ClassNotFoundException JavaDoc {
875     
876     Class JavaDoc loadedClass = null;
877
878     try {
879             //Sequence finding of the stubs according to spec
880
try{
881                 //If-else is put here for speed up of J2EE.
882
//According to the OMG spec, the if clause is not dead code.
883
//It can occur if some compiler has allowed generation
884
//into org.omg.stub hierarchy for non-offending
885
//classes. This will encourage people to
886
//produce non-offending class stubs in their own hierarchy.
887
if(!PackagePrefixChecker
888                    .hasOffendingPrefix(PackagePrefixChecker
889                                        .withoutPackagePrefix(className))){
890                     loadedClass = Util.loadClass
891                         (PackagePrefixChecker.withoutPackagePrefix(className),
892                          remoteCodebase,
893                          loader);
894                 } else {
895                     loadedClass = Util.loadClass
896                         (className,
897                          remoteCodebase,
898                          loader);
899                 }
900             } catch (ClassNotFoundException JavaDoc cnfe) {
901                 loadedClass = Util.loadClass
902                     (className,
903                      remoteCodebase,
904                      loader);
905             }
906             if (expectedType == null)
907             return loadedClass;
908     } catch (ClassNotFoundException JavaDoc cnfe) {
909         if (expectedType == null)
910             throw cnfe;
911     }
912     
913         // If no class was not loaded, or if the loaded class is not of the
914
// correct type, make a further attempt to load the correct class
915
// using the classloader of the expected type.
916
// _REVISIT_ Is this step necessary, or should the Util,loadClass
917
// algorithm always produce a valid class if the setup is correct?
918
// Does the OMG standard algorithm need to be changed to include
919
// this step?
920
if (loadedClass == null || !expectedType.isAssignableFrom(loadedClass)) {
921             if (expectedType.getClassLoader() != expectedTypeClassLoader)
922                 throw new IllegalArgumentException JavaDoc("expectedTypeClassLoader not class loader of expectedType.");
923
924             if (expectedTypeClassLoader != null)
925         loadedClass = expectedTypeClassLoader.loadClass(className);
926             else
927                 loadedClass = ORBClassLoader.loadClass(className);
928         }
929
930     return loadedClass;
931     }
932
933     /**
934      * Checks to see if the FullValueDescription should be retrieved.
935      * @exception Throws IOException if suids do not match or if the repositoryID
936      * is not an RMIValueType
937      */

938     public static boolean useFullValueDescription(Class JavaDoc clazz, String JavaDoc repositoryID)
939         throws IOException JavaDoc{
940
941         String JavaDoc clazzRepIDStr = createForAnyType(clazz);
942
943         if (clazzRepIDStr.equals(repositoryID))
944             return false;
945
946         RepositoryId_1_3 targetRepid;
947         RepositoryId_1_3 clazzRepid;
948
949         synchronized(cache) {
950         // to avoid race condition where multiple threads could be
951
// accessing this method, and their access to the cache may
952
// be interleaved giving unexpected results
953

954             targetRepid = cache.getId(repositoryID);
955             clazzRepid = cache.getId(clazzRepIDStr);
956         }
957
958         if ((targetRepid.isRMIValueType()) && (clazzRepid.isRMIValueType())){
959             if (!targetRepid.getSerialVersionUID().equals(clazzRepid.getSerialVersionUID())) {
960
961                 String JavaDoc mssg = "Mismatched serialization UIDs : Source (Rep. ID" +
962                     clazzRepid + ") = " +
963                     clazzRepid.getSerialVersionUID() + " whereas Target (Rep. ID " + repositoryID +
964                     ") = " + targetRepid.getSerialVersionUID();
965                 throw new IOException JavaDoc(mssg);
966             } else {
967                 return true;
968             }
969         } else {
970
971             throw new IOException JavaDoc("The repository ID is not of an RMI value type (Expected ID = " + clazzRepIDStr + "; Received ID = " + repositoryID +")");
972         }
973     }
974 }
975
976
Popular Tags