1 7 15 16 package com.sun.corba.se.impl.io; 17 18 import com.sun.org.omg.CORBA.ValueDefPackage.FullValueDescription; 19 import com.sun.org.omg.CORBA.OperationDescription; 20 import com.sun.org.omg.CORBA.AttributeDescription; 21 import org.omg.CORBA.ValueMember ; 22 import com.sun.org.omg.CORBA.Initializer; 23 import org.omg.CORBA.IDLType ; 24 import com.sun.org.omg.CORBA._IDLTypeStub; 25 import org.omg.CORBA.ORB ; 26 import org.omg.CORBA.TypeCodePackage.*; 27 import org.omg.CORBA.TypeCode ; 28 import org.omg.CORBA.TCKind ; 29 import java.lang.reflect.*; 30 import com.sun.corba.se.impl.util.RepositoryId; 31 import java.util.*; 32 import javax.rmi.CORBA.Util ; 33 import javax.rmi.CORBA.ValueHandler ; 34 35 39 public class ValueUtility { 40 41 public static final short PRIVATE_MEMBER = 0; 42 public static final short PUBLIC_MEMBER = 1; 43 44 private static final String primitiveConstants[] = { 45 null, null, "S", "I", "S", "I", "F", "D", "Z", "C", "B", null, null, null, null, null, null, null, null, null, null, null, null, "J", "J", "D", "C", null, null, null, null, null, null, }; 79 80 public static String getSignature(ValueMember member) 81 throws ClassNotFoundException { 82 83 88 if (member.type.kind().value() == TCKind._tk_value_box || 89 member.type.kind().value() == TCKind._tk_value || 90 member.type.kind().value() == TCKind._tk_objref) { 91 Class c = RepositoryId.cache.getId(member.id).getClassFromType(); 92 return ObjectStreamClass.getSignature(c); 93 94 } else { 95 96 return primitiveConstants[member.type.kind().value()]; 97 } 98 99 } 100 101 public static FullValueDescription translate(ORB orb, ObjectStreamClass osc, ValueHandler vh){ 102 103 FullValueDescription result = new FullValueDescription(); 105 Class className = osc.forClass(); 106 107 ValueHandlerImpl vhandler = (com.sun.corba.se.impl.io.ValueHandlerImpl) vh; 108 String repId = vhandler.createForAnyType(className); 109 110 result.name = vhandler.getUnqualifiedName(repId); 112 if (result.name == null) 113 result.name = ""; 114 115 result.id = vhandler.getRMIRepositoryID(className); 117 if (result.id == null) 118 result.id = ""; 119 120 result.is_abstract = ObjectStreamClassCorbaExt.isAbstractInterface(className); 122 123 result.is_custom = osc.hasWriteObject() || osc.isExternalizable(); 125 126 result.defined_in = vhandler.getDefinedInId(repId); 128 if (result.defined_in == null) 129 result.defined_in = ""; 130 131 result.version = vhandler.getSerialVersionUID(repId); 133 if (result.version == null) 134 result.version = ""; 135 136 result.operations = new OperationDescription[0]; 138 139 result.attributes = new AttributeDescription[0]; 141 142 IdentityKeyValueStack createdIDs = new IdentityKeyValueStack(); 145 result.members = translateMembers(orb, osc, vh, createdIDs); 147 148 result.initializers = new Initializer[0]; 150 151 Class interfaces[] = osc.forClass().getInterfaces(); 152 int abstractCount = 0; 153 154 result.supported_interfaces = new String [interfaces.length]; 156 for (int interfaceIndex = 0; interfaceIndex < interfaces.length; 157 interfaceIndex++) { 158 result.supported_interfaces[interfaceIndex] = 159 vhandler.createForAnyType(interfaces[interfaceIndex]); 160 161 if ((!(java.rmi.Remote .class.isAssignableFrom(interfaces[interfaceIndex]))) || 162 (!Modifier.isPublic(interfaces[interfaceIndex].getModifiers()))) 163 abstractCount++; 164 } 165 166 result.abstract_base_values = new String [abstractCount]; 168 for (int interfaceIndex = 0; interfaceIndex < interfaces.length; 169 interfaceIndex++) { 170 if ((!(java.rmi.Remote .class.isAssignableFrom(interfaces[interfaceIndex]))) || 171 (!Modifier.isPublic(interfaces[interfaceIndex].getModifiers()))) 172 result.abstract_base_values[interfaceIndex] = 173 vhandler.createForAnyType(interfaces[interfaceIndex]); 174 175 } 176 177 result.is_truncatable = false; 178 179 Class superClass = osc.forClass().getSuperclass(); 181 if (java.io.Serializable .class.isAssignableFrom(superClass)) 182 result.base_value = vhandler.getRMIRepositoryID(superClass); 183 else 184 result.base_value = ""; 185 186 result.type = orb.get_primitive_tc(TCKind.tk_value); 190 return result; 191 192 } 193 194 private static ValueMember [] translateMembers (ORB orb, 195 ObjectStreamClass osc, 196 ValueHandler vh, 197 IdentityKeyValueStack createdIDs) 198 { 199 ValueHandlerImpl vhandler = (com.sun.corba.se.impl.io.ValueHandlerImpl) vh; 200 ObjectStreamField fields[] = osc.getFields(); 201 int fieldsLength = fields.length; 202 ValueMember [] members = new ValueMember [fieldsLength]; 203 for (int i = 0; i < fieldsLength; i++) { 206 String valRepId = vhandler.getRMIRepositoryID(fields[i].getClazz()); 207 members[i] = new ValueMember (); 208 members[i].name = fields[i].getName(); 209 members[i].id = valRepId; members[i].defined_in = vhandler.getDefinedInId(valRepId); members[i].version = "1.0"; 212 members[i].type_def = new _IDLTypeStub(); 214 if (fields[i].getField() == null) { 215 members[i].access = PRIVATE_MEMBER; 221 } else { 222 int m = fields[i].getField().getModifiers(); 223 if (Modifier.isPublic(m)) 224 members[i].access = PUBLIC_MEMBER; 225 else 226 members[i].access = PRIVATE_MEMBER; 227 } 228 229 switch (fields[i].getTypeCode()) { 230 case 'B': 231 members[i].type = orb.get_primitive_tc(TCKind.tk_octet); break; 233 case 'C': 234 members[i].type 235 = orb.get_primitive_tc(vhandler.getJavaCharTCKind()); break; 237 case 'F': 238 members[i].type = orb.get_primitive_tc(TCKind.tk_float); break; 240 case 'D' : 241 members[i].type = orb.get_primitive_tc(TCKind.tk_double); break; 243 case 'I': 244 members[i].type = orb.get_primitive_tc(TCKind.tk_long); break; 246 case 'J': 247 members[i].type = orb.get_primitive_tc(TCKind.tk_longlong); break; 249 case 'S': 250 members[i].type = orb.get_primitive_tc(TCKind.tk_short); break; 252 case 'Z': 253 members[i].type = orb.get_primitive_tc(TCKind.tk_boolean); break; 255 default: 260 members[i].type = createTypeCodeForClassInternal(orb, fields[i].getClazz(), vhandler, 261 createdIDs); 262 members[i].id = vhandler.createForAnyType(fields[i].getType()); 263 break; 264 } 266 } 268 return members; 269 } 270 271 private static boolean exists(String str, String strs[]){ 272 for (int i = 0; i < strs.length; i++) 273 if (str.equals(strs[i])) 274 return true; 275 276 return false; 277 } 278 279 public static boolean isAssignableFrom(String clzRepositoryId, FullValueDescription type, 280 com.sun.org.omg.SendingContext.CodeBase sender){ 281 282 if (exists(clzRepositoryId, type.supported_interfaces)) 283 return true; 284 285 if (clzRepositoryId.equals(type.id)) 286 return true; 287 288 if ((type.base_value != null) && 289 (!type.base_value.equals(""))) { 290 FullValueDescription parent = sender.meta(type.base_value); 291 292 return isAssignableFrom(clzRepositoryId, parent, sender); 293 } 294 295 return false; 296 297 } 298 299 public static TypeCode createTypeCodeForClass (ORB orb, java.lang.Class c, ValueHandler vh) { 300 IdentityKeyValueStack createdIDs = new IdentityKeyValueStack(); 302 TypeCode tc = createTypeCodeForClassInternal(orb, c, vh, createdIDs); 304 return tc; 305 } 306 307 private static TypeCode createTypeCodeForClassInternal (ORB orb, 308 java.lang.Class c, 309 ValueHandler vh, 310 IdentityKeyValueStack createdIDs) 311 { 312 TypeCode tc = null; 314 String id = (String )createdIDs.get(c); 315 if (id != null) { 316 return orb.create_recursive_tc(id); 317 } else { 318 id = vh.getRMIRepositoryID(c); 319 if (id == null) id = ""; 320 createdIDs.push(c, id); 323 tc = createTypeCodeInternal(orb, c, vh, id, createdIDs); 324 createdIDs.pop(); 325 return tc; 326 } 327 } 328 329 private static class IdentityKeyValueStack { 331 private static class KeyValuePair { 332 Object key; 333 Object value; 334 KeyValuePair(Object key, Object value) { 335 this.key = key; 336 this.value = value; 337 } 338 boolean equals(KeyValuePair pair) { 339 return pair.key == this.key; 340 } 341 } 342 343 Stack pairs = null; 344 345 Object get(Object key) { 346 if (pairs == null) { 347 return null; 348 } 349 for (Iterator i = pairs.iterator(); i.hasNext();) { 350 KeyValuePair pair = (KeyValuePair)i.next(); 351 if (pair.key == key) { 352 return pair.value; 353 } 354 } 355 return null; 356 } 357 358 void push(Object key, Object value) { 359 if (pairs == null) { 360 pairs = new Stack(); 361 } 362 pairs.push(new KeyValuePair(key, value)); 363 } 364 365 void pop() { 366 pairs.pop(); 367 } 368 } 369 370 private static TypeCode createTypeCodeInternal (ORB orb, 371 java.lang.Class c, 372 ValueHandler vh, 373 String id, 374 IdentityKeyValueStack createdIDs) 375 { 376 if ( c.isArray() ) { 377 Class componentClass = c.getComponentType(); 379 TypeCode embeddedType; 380 if ( componentClass.isPrimitive() ){ 381 embeddedType 382 = ValueUtility.getPrimitiveTypeCodeForClass(orb, 383 componentClass, 384 vh); 385 } else { 386 embeddedType = createTypeCodeForClassInternal(orb, componentClass, vh, 387 createdIDs); 388 } 389 TypeCode t = orb.create_sequence_tc (0, embeddedType); 390 return orb.create_value_box_tc (id, "Sequence", t); 391 } else if ( c == java.lang.String .class ) { 392 TypeCode t = orb.create_string_tc (0); 394 return orb.create_value_box_tc (id, "StringValue", t); 395 } else if (java.rmi.Remote .class.isAssignableFrom(c)) { 396 return orb.get_primitive_tc(TCKind.tk_objref); 397 } else if (org.omg.CORBA.Object .class.isAssignableFrom(c)) { 398 return orb.get_primitive_tc(TCKind.tk_objref); 399 } 400 401 403 ObjectStreamClass osc = ObjectStreamClass.lookup(c); 404 405 if (osc == null) { 406 return orb.create_value_box_tc (id, "Value", orb.get_primitive_tc (TCKind.tk_value)); 407 } 408 409 short modifier = (osc.isCustomMarshaled() ? org.omg.CORBA.VM_CUSTOM.value : org.omg.CORBA.VM_NONE.value); 412 413 TypeCode base = null; 415 Class superClass = c.getSuperclass(); 416 if (superClass != null && java.io.Serializable .class.isAssignableFrom(superClass)) { 417 base = createTypeCodeForClassInternal(orb, superClass, vh, createdIDs); 418 } 419 420 ValueMember [] members = translateMembers (orb, osc, vh, createdIDs); 422 423 return orb.create_value_tc(id, c.getName(), modifier, base, members); 424 } 425 426 public static TypeCode getPrimitiveTypeCodeForClass (ORB orb, 427 Class c, 428 ValueHandler vh) { 429 430 if (c == Integer.TYPE) { 431 return orb.get_primitive_tc (TCKind.tk_long); 432 } else if (c == Byte.TYPE) { 433 return orb.get_primitive_tc (TCKind.tk_octet); 434 } else if (c == Long.TYPE) { 435 return orb.get_primitive_tc (TCKind.tk_longlong); 436 } else if (c == Float.TYPE) { 437 return orb.get_primitive_tc (TCKind.tk_float); 438 } else if (c == Double.TYPE) { 439 return orb.get_primitive_tc (TCKind.tk_double); 440 } else if (c == Short.TYPE) { 441 return orb.get_primitive_tc (TCKind.tk_short); 442 } else if (c == Character.TYPE) { 443 return orb.get_primitive_tc (((ValueHandlerImpl)vh).getJavaCharTCKind()); 444 } else if (c == Boolean.TYPE) { 445 return orb.get_primitive_tc (TCKind.tk_boolean); 446 } else { 447 return orb.get_primitive_tc (TCKind.tk_any); 449 } 450 } 451 } 452 | Popular Tags |