KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > util > RepositoryId


1 /*
2  * @(#)RepositoryId.java 1.36 05/01/04
3  *
4  * Copyright 2005 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.util;
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 //d11638 files in the same package, therefore remove their reference
29
//import com.sun.corba.se.impl.util.JDKBridge;
30
//import com.sun.corba.se.impl.util.IdentityHashtable;
31
import com.sun.corba.se.impl.io.ObjectStreamClass;
32
33 import javax.rmi.CORBA.Util JavaDoc;
34 import sun.misc.SoftCache;
35 import java.util.WeakHashMap JavaDoc;
36
37 public class RepositoryId {
38     
39     // Legal IDL Identifier characters (1 = legal). Note
40
// that '.' (2E) is marked as legal even though it is
41
// not legal in IDL. This allows us to treat a fully
42
// qualified Java name with '.' package separators
43
// uniformly, and is safe because that is the only
44
// legal use of '.' in a Java name.
45

46     public static final byte[] IDL_IDENTIFIER_CHARS = {
47         
48     // 0 1 2 3 4 5 6 7 8 9 a b c d e f
49
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 00-0f
50
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 10-1f
51
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, // 20-2f
52
1,1,1,1, 1,1,1,1, 1,1,0,0, 0,0,0,0, // 30-3f
53
0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // 40-4f
54
1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,1, // 50-5f
55
0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // 60-6f
56
1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0, // 70-7f
57
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 80-8f
58
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 90-9f
59
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // a0-af
60
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // b0-bf
61
1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // c0-cf
62
0,1,1,1, 1,1,1,0, 1,1,1,1, 1,0,0,1, // d0-df
63
1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, // e0-ef
64
0,1,1,1, 1,1,1,0, 1,1,1,1, 1,0,0,1, // f0-ff
65
};
66
67     
68     private static final long serialVersionUID = 123456789L;
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 WeakHashMap JavaDoc classToRepStr = new WeakHashMap JavaDoc();
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 SoftCache repStrToClass = new SoftCache();
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 kSequenceKeyword = "seq";
103     private static final String JavaDoc kValuePrefix = "RMI:";
104     private static final String JavaDoc kIDLPrefix = "IDL:";
105     private static final String JavaDoc kIDLNamePrefix = "omg.org/";
106     private static final String JavaDoc kIDLClassnamePrefix = "org.omg.";
107     private static final String JavaDoc kSequencePrefix = "[";
108     private static final String JavaDoc kCORBAPrefix = "CORBA/";
109     private static final String JavaDoc kArrayPrefix = kValuePrefix + kSequencePrefix + kCORBAPrefix;
110     private static final int kValuePrefixLength = kValuePrefix.length();
111     private static final int kIDLPrefixLength = kIDLPrefix.length();
112     private static final int kSequencePrefixLength = kSequencePrefix.length();
113     private static final String JavaDoc kInterfaceHashCode = ":0000000000000000";
114     private static final String JavaDoc kInterfaceOnlyHashStr = "0000000000000000";
115     private static final String JavaDoc kExternalizableHashStr = "0000000000000001";
116
117     // Value tag utility methods and constants
118
public static final int kInitialValueTag= 0x7fffff00;
119     public static final int kNoTypeInfo = 0;
120     public static final int kSingleRepTypeInfo = 0x02;
121     public static final int kPartialListTypeInfo = 0x06;
122     public static final int kChunkedMask = 0x08;
123     public static final int kPreComputed_StandardRMIUnchunked = RepositoryId.computeValueTag(false, RepositoryId.kSingleRepTypeInfo, false);
124     public static final int kPreComputed_CodeBaseRMIUnchunked = RepositoryId.computeValueTag(true, RepositoryId.kSingleRepTypeInfo, false);
125     public static final int kPreComputed_StandardRMIChunked = RepositoryId.computeValueTag(false, RepositoryId.kSingleRepTypeInfo, true);
126     public static final int kPreComputed_CodeBaseRMIChunked = RepositoryId.computeValueTag(true, RepositoryId.kSingleRepTypeInfo, true);
127
128     public static final int kPreComputed_StandardRMIUnchunked_NoRep = RepositoryId.computeValueTag(false, RepositoryId.kNoTypeInfo, false);
129     public static final int kPreComputed_CodeBaseRMIUnchunked_NoRep = RepositoryId.computeValueTag(true, RepositoryId.kNoTypeInfo, false);
130     public static final int kPreComputed_StandardRMIChunked_NoRep = RepositoryId.computeValueTag(false, RepositoryId.kNoTypeInfo, true);
131     public static final int kPreComputed_CodeBaseRMIChunked_NoRep = RepositoryId.computeValueTag(true, RepositoryId.kNoTypeInfo, true);
132
133     // Public, well known repository IDs
134

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

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

255     private static final byte ASCII_HEX[] = {
256     (byte)'0',
257     (byte)'1',
258     (byte)'2',
259     (byte)'3',
260     (byte)'4',
261     (byte)'5',
262     (byte)'6',
263     (byte)'7',
264     (byte)'8',
265     (byte)'9',
266     (byte)'A',
267     (byte)'B',
268     (byte)'C',
269     (byte)'D',
270     (byte)'E',
271     (byte)'F',
272     };
273
274
275     // bug fix for 4328952; to eliminate possibility of overriding this
276
// in a subclass.
277
public static final RepositoryIdCache cache = new RepositoryIdCache();
278
279     // Interface Rep ID Strings
280
public static final String JavaDoc kjava_rmi_Remote = createForAnyType(java.rmi.Remote JavaDoc.class);
281     public static final String JavaDoc korg_omg_CORBA_Object = createForAnyType(org.omg.CORBA.Object JavaDoc.class);
282
283     // Dummy arguments for getIdFromHelper method
284
public static final Class JavaDoc kNoParamTypes[] ={};
285     public static final Object JavaDoc kNoArgs[] = {};
286
287
288     // To create a RepositoryID, use code similar to the following:
289
// RepositoryId.cache.getId( id );
290

291     RepositoryId(){}
292
293     RepositoryId(String JavaDoc aRepId){
294     init(aRepId);
295     }
296
297     RepositoryId init(String JavaDoc aRepId)
298     {
299         this.repId = aRepId;
300         
301     // Special case for remote
302
if (aRepId.length() == 0) {
303         clazz = java.rmi.Remote JavaDoc.class;
304         typeString = "";
305         isRMIValueType = true;
306         suid = kInterfaceOnlyHashStr;
307         return this;
308     } else if (aRepId.equals(kWStringValueRepID)) {
309         clazz = java.lang.String JavaDoc.class;
310         typeString = kWStringTypeStr;
311         isIDLType = true;
312         // fix where Attempting to obtain a FullValueDescription
313
// for an RMI value type with a String field causes an exception.
314
completeClassName = "java.lang.String";
315         versionString = kWStringValueVersion;
316         return this;
317     } else {
318         String JavaDoc repId = convertFromISOLatin1(aRepId);
319
320         int firstIndex = repId.indexOf(':') ;
321         if (firstIndex == -1)
322         throw new IllegalArgumentException JavaDoc( "RepsitoryId must have the form <type>:<body>" ) ;
323         int secondIndex = repId.indexOf( ':', firstIndex + 1 ) ;
324
325         if (secondIndex == -1)
326         versionString = "" ;
327         else
328         versionString = repId.substring(secondIndex) ;
329
330         if (repId.startsWith(kIDLPrefix)) {
331         typeString =
332             repId.substring(kIDLPrefixLength, repId.indexOf(':', kIDLPrefixLength));
333         isIDLType = true;
334
335         if (typeString.startsWith(kIDLNamePrefix))
336             completeClassName = kIDLClassnamePrefix +
337             typeString.substring(kIDLNamePrefix.length()).replace('/','.');
338         else
339             completeClassName = typeString.replace('/','.');
340
341         } else if (repId.startsWith(kValuePrefix)) {
342         typeString =
343             repId.substring(kValuePrefixLength, repId.indexOf(':', kValuePrefixLength));
344         isRMIValueType = true;
345
346         if (versionString.indexOf('.') == -1) {
347             actualSuid = versionString.substring(1);
348             suid = actualSuid; // default if not explicitly specified
349

350             if (actualSuid.indexOf(':') != -1){
351             // we have a declared hash also
352
int pos = actualSuid.indexOf(':')+1;
353             // actualSuid = suid.substring(pos);
354
// suid = suid.substring(0, pos-1);
355
suid = actualSuid.substring(pos);
356             actualSuid = actualSuid.substring(0, pos-1);
357             }
358         } else {
359             // _REVISIT_ : Special case version failure ?
360
}
361         } else {
362         isSupportedFormat = false;
363         typeString = "" ;
364         }
365
366         if (typeString.startsWith(kSequencePrefix)) {
367         isSequence = true;
368         }
369         
370         return this;
371     }
372     }
373
374     public final String JavaDoc getUnqualifiedName() {
375     if (unqualifiedName == null){
376         String JavaDoc className = getClassName();
377         int index = className.lastIndexOf('.');
378         if (index == -1){
379         unqualifiedName = className;
380         definedInId = "IDL::1.0";
381         }
382         else {
383         unqualifiedName = className.substring(index);
384         definedInId = "IDL:" + className.substring(0, index).replace('.','/') + ":1.0";
385         }
386     }
387         
388     return unqualifiedName;
389     }
390
391     public final String JavaDoc getDefinedInId() {
392     if (definedInId == null){
393         getUnqualifiedName();
394     }
395         
396     return definedInId;
397     }
398
399     public final String JavaDoc getTypeString() {
400         return typeString;
401     }
402
403     public final String JavaDoc getVersionString() {
404         return versionString;
405     }
406     
407     public final String JavaDoc getSerialVersionUID() {
408     return suid;
409     }
410
411     public final String JavaDoc getActualSerialVersionUID() {
412     return actualSuid;
413     }
414     public final long getSerialVersionUIDAsLong() {
415     return suidLong;
416     }
417
418     public final long getActualSerialVersionUIDAsLong() {
419     return actualSuidLong;
420     }
421
422     public final boolean isRMIValueType() {
423     return isRMIValueType;
424     }
425     
426     public final boolean isIDLType() {
427     return isIDLType;
428     }
429
430     public final String JavaDoc getRepositoryId() {
431         return repId;
432     }
433
434     public static byte[] getByteArray(String JavaDoc repStr) {
435     synchronized (repStrToByteArray){
436         return (byte[]) repStrToByteArray.get(repStr);
437     }
438     }
439
440     public static void setByteArray(String JavaDoc repStr, byte[] repStrBytes) {
441     synchronized (repStrToByteArray){
442         repStrToByteArray.put(repStr, repStrBytes);
443     }
444     }
445
446     public final boolean isSequence() {
447         return isSequence;
448     }
449
450     public final boolean isSupportedFormat() {
451         return isSupportedFormat;
452     }
453
454     
455     // This method will return the classname from the typestring OR if the classname turns out to be
456
// a special class "pseudo" name, then the matching real classname is returned.
457
public final String JavaDoc getClassName() {
458
459     if (isRMIValueType)
460         return typeString;
461     else if (isIDLType)
462         return completeClassName;
463     else return null;
464
465     }
466
467     // This method calls getClazzFromType() and falls back to the repStrToClass
468
// cache if no class was found. It's used where any class matching the
469
// given repid is an acceptable result.
470
public final Class JavaDoc getAnyClassFromType() throws ClassNotFoundException JavaDoc {
471         try {
472         return getClassFromType();
473     } catch (ClassNotFoundException JavaDoc cnfe) {
474         Class JavaDoc clz = (Class JavaDoc)repStrToClass.get(repId);
475         if (clz != null)
476         return clz;
477         else
478         throw cnfe;
479     }
480     }
481
482     public final Class JavaDoc getClassFromType()
483         throws ClassNotFoundException JavaDoc {
484     if (clazz != null)
485         return clazz;
486         
487     Class JavaDoc specialCase = (Class JavaDoc)kSpecialCasesClasses.get(getClassName());
488
489     if (specialCase != null){
490         clazz = specialCase;
491         return specialCase;
492     }
493     else
494         {
495         try{
496             return Util.loadClass(getClassName(), null, null);
497         }
498         catch(ClassNotFoundException JavaDoc cnfe){
499             if (defaultServerURL != null) {
500             try{
501                 return getClassFromType(defaultServerURL);
502             }
503             catch(MalformedURLException JavaDoc mue){
504                 throw cnfe;
505             }
506             }
507             else throw cnfe;
508         }
509         }
510
511     }
512
513     public final Class JavaDoc getClassFromType(Class JavaDoc expectedType, String JavaDoc codebase)
514         throws ClassNotFoundException JavaDoc {
515     if (clazz != null)
516         return clazz;
517         
518     Class JavaDoc specialCase = (Class JavaDoc)kSpecialCasesClasses.get(getClassName());
519
520     if (specialCase != null){
521         clazz = specialCase;
522         return specialCase;
523     } else {
524             ClassLoader JavaDoc expectedTypeClassLoader = (expectedType == null ? null : expectedType.getClassLoader());
525             return Utility.loadClassOfType(getClassName(),
526                                             codebase,
527                                             expectedTypeClassLoader,
528                                             expectedType,
529                                             expectedTypeClassLoader);
530         }
531
532     }
533
534     public final Class JavaDoc getClassFromType(String JavaDoc url)
535     throws ClassNotFoundException JavaDoc, MalformedURLException JavaDoc {
536     return Util.loadClass(getClassName(), url, null);
537     }
538
539     public final String JavaDoc toString() {
540         return repId;
541     }
542
543     /**
544      * Checks to see if the FullValueDescription should be retrieved.
545      * @exception Throws IOException if suids do not match or if the repositoryID
546      * is not an RMIValueType
547      */

548     public static boolean useFullValueDescription(Class JavaDoc clazz, String JavaDoc repositoryID)
549     throws IOException JavaDoc{
550
551     String JavaDoc clazzRepIDStr = createForAnyType(clazz);
552
553     if (clazzRepIDStr.equals(repositoryID))
554         return false;
555
556     RepositoryId targetRepid;
557     RepositoryId clazzRepid;
558
559     synchronized(cache) {
560     // to avoid race condition where multiple threads could be
561
// accessing this method, and their access to the cache may
562
// be interleaved giving unexpected results
563

564         targetRepid = cache.getId(repositoryID);
565         clazzRepid = cache.getId(clazzRepIDStr);
566     }
567     //ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);
568

569     if ((targetRepid.isRMIValueType()) && (clazzRepid.isRMIValueType())){
570         if (!targetRepid.getSerialVersionUID().equals(clazzRepid.getSerialVersionUID())) {
571
572         String JavaDoc mssg = "Mismatched serialization UIDs : Source (Rep. ID" +
573             clazzRepid + ") = " +
574             clazzRepid.getSerialVersionUID() + " whereas Target (Rep. ID " + repositoryID +
575             ") = " + targetRepid.getSerialVersionUID();
576                 //com.sun.corba.se.impl.io.ValueUtility.log("RepositoryId",mssg);
577
throw new IOException JavaDoc(mssg);
578     }
579         else {
580         return true;
581         }
582     }
583     else {
584
585         throw new IOException JavaDoc("The repository ID is not of an RMI value type (Expected ID = " + clazzRepIDStr + "; Received ID = " + repositoryID +")");
586     }
587     }
588
589     private static String JavaDoc createHashString(java.io.Serializable JavaDoc ser) {
590
591     return createHashString(ser.getClass());
592     }
593
594     private static String JavaDoc createHashString(java.lang.Class JavaDoc clazz) {
595
596     if (clazz.isInterface() || !java.io.Serializable JavaDoc.class.isAssignableFrom(clazz))
597         return kInterfaceHashCode;
598
599     //ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);
600

601     long actualLong = ObjectStreamClass.getActualSerialVersionUID(clazz);
602     String JavaDoc hash = null;
603     if (actualLong == 0)
604         hash = kInterfaceOnlyHashStr;
605     else if (actualLong == 1)
606         hash = kExternalizableHashStr;
607     else
608         hash = Long.toHexString(actualLong).toUpperCase();
609     while(hash.length() < 16){
610         hash = "0" + hash;
611     }
612
613     long declaredLong = ObjectStreamClass.getSerialVersionUID(clazz);
614     String JavaDoc declared = null;
615     if (declaredLong == 0)
616         declared = kInterfaceOnlyHashStr;
617     else if (declaredLong == 1)
618         declared = kExternalizableHashStr;
619     else
620         declared = Long.toHexString(declaredLong).toUpperCase();
621     while (declared.length() < 16){
622         declared = "0" + declared;
623     }
624     hash = hash + ":" + declared;
625
626     return ":" + hash;
627     }
628
629     /**
630      * Creates a repository ID for a sequence. This is for expert users only as
631      * this method assumes the object passed is an array. If passed an object
632      * that is not an array, it will produce a rep id for a sequence of zero
633      * length. This would be an error.
634      * @param ser The Java object to create a repository ID for
635      **/

636     public static String JavaDoc createSequenceRepID(java.lang.Object JavaDoc ser){
637     return createSequenceRepID(ser.getClass());
638     }
639
640     /**
641      * Creates a repository ID for a sequence. This is for expert users only as
642      * this method assumes the object passed is an array. If passed an object
643      * that is not an array, it will produce a malformed rep id.
644      * @param clazz The Java class to create a repository ID for
645      **/

646     public static String JavaDoc createSequenceRepID(java.lang.Class JavaDoc clazz){
647     synchronized (classSeqToRepStr){
648         
649         String JavaDoc repid = (String JavaDoc)classSeqToRepStr.get(clazz);
650     if (repid != null)
651         return repid;
652
653     Class JavaDoc originalClazz = clazz;
654
655         Class JavaDoc type = null;
656     int numOfDims = 0;
657
658         while ((type = clazz.getComponentType()) != null) {
659         numOfDims++;
660             clazz = type;
661         }
662
663     if (clazz.isPrimitive())
664         repid = kValuePrefix + originalClazz.getName() + kPrimitiveSequenceValueHash;
665     else {
666         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
667         buf.append(kValuePrefix);
668         while(numOfDims-- > 0) {
669         buf.append("[");
670         }
671         buf.append("L");
672         buf.append(convertToISOLatin1(clazz.getName()));
673         buf.append(";");
674         buf.append(createHashString(clazz));
675         repid = buf.toString();
676     }
677     classSeqToRepStr.put(originalClazz,repid);
678     return repid;
679     }
680
681     }
682
683
684     public static String JavaDoc createForSpecialCase(java.lang.Class JavaDoc clazz){
685     if (clazz.isArray()){
686         return createSequenceRepID(clazz);
687     }
688     else {
689         return (String JavaDoc)kSpecialCasesRepIDs.get(clazz);
690     }
691     }
692
693     public static String JavaDoc createForSpecialCase(java.io.Serializable JavaDoc ser){
694     Class JavaDoc clazz = ser.getClass();
695     if (clazz.isArray()){
696         return createSequenceRepID(ser);
697     }
698     else
699         return createForSpecialCase(clazz);
700     }
701     
702     /**
703      * Creates a repository ID for a normal Java Type.
704      * @param ser The Java object to create a repository ID for
705      * @exception com.sun.corba.se.impl.io.TypeMismatchException if ser implements the
706      * org.omg.CORBA.portable.IDLEntity interface which indicates it is an IDL Value type.
707      **/

708     public static String JavaDoc createForJavaType(java.io.Serializable JavaDoc ser)
709         throws com.sun.corba.se.impl.io.TypeMismatchException
710     {
711     synchronized (classToRepStr) {
712     String JavaDoc repid = createForSpecialCase(ser);
713     if (repid != null)
714         return repid;
715     Class JavaDoc clazz = ser.getClass();
716     repid = (String JavaDoc)classToRepStr.get(clazz);
717
718     if (repid != null)
719         return repid;
720
721     repid = kValuePrefix + convertToISOLatin1(clazz.getName()) +
722         createHashString(clazz);
723
724     classToRepStr.put(clazz, repid);
725         repStrToClass.put(repid, clazz);
726         return repid;
727     }
728     }
729
730     /**
731      * Creates a repository ID for a normal Java Type.
732      * @param clz The Java class to create a repository ID for
733      * @exception com.sun.corba.se.impl.io.TypeMismatchException if ser implements the
734      * org.omg.CORBA.portable.IDLEntity interface which indicates it is an IDL Value type.
735      **/

736     public static String JavaDoc createForJavaType(Class JavaDoc clz)
737         throws com.sun.corba.se.impl.io.TypeMismatchException
738     {
739     synchronized (classToRepStr){
740     String JavaDoc repid = createForSpecialCase(clz);
741     if (repid != null)
742         return repid;
743
744     repid = (String JavaDoc)classToRepStr.get(clz);
745     if (repid != null)
746         return repid;
747
748         repid = kValuePrefix + convertToISOLatin1(clz.getName()) +
749         createHashString(clz);
750
751     classToRepStr.put(clz, repid);
752         repStrToClass.put(repid, clz);
753         return repid;
754     }
755     }
756
757     /**
758      * Creates a repository ID for an IDL Java Type.
759      * @param ser The IDL Value object to create a repository ID for
760      * @param major The major version number
761      * @param minor The minor version number
762      * @exception com.sun.corba.se.impl.io.TypeMismatchException if ser does not implement the
763      * org.omg.CORBA.portable.IDLEntity interface which indicates it is an IDL Value type.
764      **/

765     public static String JavaDoc createForIDLType(Class JavaDoc ser, int major, int minor)
766         throws com.sun.corba.se.impl.io.TypeMismatchException
767     {
768     synchronized (classIDLToRepStr){
769     String JavaDoc repid = (String JavaDoc)classIDLToRepStr.get(ser);
770     if (repid != null)
771         return repid;
772
773         repid = kIDLPrefix + convertToISOLatin1(ser.getName()).replace('.','/') +
774         ":" + major + "." + minor;
775     classIDLToRepStr.put(ser, repid);
776     return repid;
777     }
778     }
779
780     private static String JavaDoc getIdFromHelper(Class JavaDoc clazz){
781         try {
782         Class JavaDoc helperClazz = Utility.loadClassForClass(clazz.getName()+"Helper", null,
783                                     clazz.getClassLoader(), clazz, clazz.getClassLoader());
784         Method JavaDoc idMethod = helperClazz.getDeclaredMethod("id", kNoParamTypes);
785         return (String JavaDoc)idMethod.invoke(null, kNoArgs);
786         }
787         catch(java.lang.ClassNotFoundException JavaDoc cnfe)
788         {
789             throw new org.omg.CORBA.MARSHAL JavaDoc(cnfe.toString());
790         }
791         catch(java.lang.NoSuchMethodException JavaDoc nsme)
792         {
793             throw new org.omg.CORBA.MARSHAL JavaDoc(nsme.toString());
794         }
795         catch(java.lang.reflect.InvocationTargetException JavaDoc ite)
796         {
797             throw new org.omg.CORBA.MARSHAL JavaDoc(ite.toString());
798         }
799         catch(java.lang.IllegalAccessException JavaDoc iae)
800         {
801             throw new org.omg.CORBA.MARSHAL JavaDoc(iae.toString());
802     }
803     }
804
805     /**
806      * Createa a repository ID for the type if it is either a java type
807      * or an IDL type.
808      * @param type The type to create rep. id for
809      * @return The rep. id.
810      **/

811     public static String JavaDoc createForAnyType(Class JavaDoc type) {
812     try{
813         if (type.isArray())
814         return createSequenceRepID(type);
815         else if (IDLEntity JavaDoc.class.isAssignableFrom(type))
816         {
817             try{
818             return getIdFromHelper(type);
819             }
820             catch(Throwable JavaDoc t) {
821             return createForIDLType(type, 1, 0);
822             }
823         }
824         else return createForJavaType(type);
825     }
826     catch(com.sun.corba.se.impl.io.TypeMismatchException e){
827         return null;
828     }
829
830     }
831
832     public static boolean isAbstractBase(Class JavaDoc clazz) {
833     return (clazz.isInterface() &&
834         IDLEntity JavaDoc.class.isAssignableFrom(clazz) &&
835         (!ValueBase JavaDoc.class.isAssignableFrom(clazz)) &&
836         (!org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(clazz)));
837                 
838     }
839
840     public static boolean isAnyRequired(Class JavaDoc clazz) {
841     return ((clazz == java.lang.Object JavaDoc.class) ||
842         (clazz == java.io.Serializable JavaDoc.class) ||
843         (clazz == java.io.Externalizable JavaDoc.class));
844     }
845
846     public static long fromHex(String JavaDoc hexNumber) {
847     if (hexNumber.startsWith("0x"))
848         return Long.valueOf(hexNumber.substring(2), 16).longValue();
849     else return Long.valueOf(hexNumber, 16).longValue();
850     }
851
852     /**
853      * Convert strings with illegal IDL identifier characters.
854      * <p>
855      * Section 5.5.7 of OBV spec.
856      */

857     public static String JavaDoc convertToISOLatin1 (String JavaDoc name) {
858
859         int length = name.length();
860         if (length == 0) {
861             return name;
862         }
863         StringBuffer JavaDoc buffer = null;
864
865         for (int i = 0; i < length; i++) {
866
867             char c = name.charAt(i);
868
869             if (c > 255 || IDL_IDENTIFIER_CHARS[c] == 0) {
870             
871                 // We gotta convert. Have we already started?
872

873                 if (buffer == null) {
874
875                     // No, so get set up...
876

877                     buffer = new StringBuffer JavaDoc(name.substring(0,i));
878                 }
879
880                 // Convert the character into the IDL escape syntax...
881
buffer.append(
882                   "\\U" +
883                   (char)ASCII_HEX[(c & 0xF000) >>> 12] +
884                   (char)ASCII_HEX[(c & 0x0F00) >>> 8] +
885                   (char)ASCII_HEX[(c & 0x00F0) >>> 4] +
886                   (char)ASCII_HEX[(c & 0x000F)]);
887                 
888             } else {
889                 if (buffer != null) {
890                     buffer.append(c);
891                 }
892             }
893         }
894         
895         if (buffer != null) {
896             name = buffer.toString();
897         }
898  
899         return name;
900     }
901     
902     /**
903      * Convert strings with ISO Latin 1 escape sequences back to original strings.
904      * <p>
905      * Section 5.5.7 of OBV spec.
906      */

907     private static String JavaDoc convertFromISOLatin1 (String JavaDoc name) {
908
909         int index = -1;
910         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(name);
911
912         while ((index = buf.toString().indexOf("\\U")) != -1){
913             String JavaDoc str = "0000" + buf.toString().substring(index+2, index+6);
914
915             // Convert Hexadecimal
916
byte[] buffer = new byte[(str.length() - 4) / 2];
917             for (int i=4, j=0; i < str.length(); i +=2, j++) {
918                 buffer[j] = (byte)((Utility.hexOf(str.charAt(i)) << 4) & 0xF0);
919                 buffer[j] |= (byte)((Utility.hexOf(str.charAt(i+1)) << 0) & 0x0F);
920             }
921             buf = new StringBuffer JavaDoc(delete(buf.toString(), index, index+6));
922             buf.insert(index, (char)buffer[1]);
923         }
924         
925         return buf.toString();
926
927
928     }
929
930     private static String JavaDoc delete(String JavaDoc str, int from, int to)
931     {
932         return str.substring(0, from) + str.substring(to, str.length());
933     }
934
935     private static String JavaDoc replace(String JavaDoc target, String JavaDoc arg, String JavaDoc source)
936     {
937         int i = 0;
938         i = target.indexOf(arg);
939
940         while(i != -1)
941         {
942         String JavaDoc left = target.substring(0, i);
943         String JavaDoc right = target.substring(i+arg.length());
944         target = new String JavaDoc(left+source+right);
945         i = target.indexOf(arg);
946         }
947         return target;
948     }
949
950     public static int computeValueTag(boolean codeBasePresent, int typeInfo, boolean chunkedEncoding){
951     int value_tag = kInitialValueTag;
952         
953     if (codeBasePresent)
954         value_tag = value_tag | 0x00000001;
955     
956     value_tag = value_tag | typeInfo;
957
958     if (chunkedEncoding)
959         value_tag = value_tag | kChunkedMask;
960
961     return value_tag;
962     }
963
964     public static boolean isCodeBasePresent(int value_tag){
965     return ((value_tag & 0x00000001) == 1);
966     }
967
968     public static int getTypeInfo(int value_tag){
969     return (value_tag & 0x00000006);
970     }
971
972     public static boolean isChunkedEncoding(int value_tag){
973     return ((value_tag & kChunkedMask) != 0);
974     }
975     
976     public static String JavaDoc getServerURL(){
977     return defaultServerURL;
978     }
979 }
980
981
Popular Tags