ValueUtility.java 18.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 */
/*
 * Licensed Materials - Property of IBM
 * RMI-IIOP v1.0
 * Copyright IBM Corp. 1998 1999  All Rights Reserved
 *
 */

package com.sun.corba.se.impl.io;

import com.sun.org.omg.CORBA.ValueDefPackage.FullValueDescription;
import com.sun.org.omg.CORBA.OperationDescription;
import com.sun.org.omg.CORBA.AttributeDescription;
import org.omg.CORBA.ValueMember;
import com.sun.org.omg.CORBA.Initializer;
import org.omg.CORBA.IDLType;
import com.sun.org.omg.CORBA._IDLTypeStub;
import org.omg.CORBA.ORB;
import org.omg.CORBA.TypeCodePackage.*;
import org.omg.CORBA.TypeCode;
import org.omg.CORBA.TCKind;
import java.lang.reflect.*;
import com.sun.corba.se.impl.util.RepositoryId;
import java.util.*;
import javax.rmi.CORBA.Util;
import javax.rmi.CORBA.ValueHandler;

/**
 * Holds utility methods for converting from ObjectStreamClass to
 * FullValueDescription and generating typecodes from ObjectStreamClass.
 **/
public class ValueUtility {

    public static final short PRIVATE_MEMBER = 0;
    public static final short PUBLIC_MEMBER = 1;

    private static final String primitiveConstants[] = {
        null,       // tk_null         0
        null,           // tk_void         1
        "S",            // tk_short        2
        "I",            // tk_long         3
        "S",            // tk_ushort       4
        "I",            // tk_ulong        5
        "F",            // tk_float        6
        "D",            // tk_double       7
        "Z",            // tk_boolean      8
        "C",            // tk_char         9
        "B",            // tk_octet        10
        null,           // tk_any          11
        null,           // tk_typecode     12
        null,           // tk_principal    13
        null,           // tk_objref       14
        null,           // tk_struct       15
        null,           // tk_union        16
        null,           // tk_enum         17
        null,           // tk_string       18
        null,           // tk_sequence     19
        null,           // tk_array        20
        null,           // tk_alias        21
        null,           // tk_except       22
        "J",            // tk_longlong     23
        "J",            // tk_ulonglong    24
        "D",            // tk_longdouble   25
        "C",            // tk_wchar        26
        null,           // tk_wstring      27
        null,       // tk_fixed        28
        null,       // tk_value        29
        null,       // tk_value_box    30
        null,       // tk_native       31
        null,       // tk_abstract_interface 32
    };

96 97 98 99 100 101 102 103
    static {
        sun.corba.SharedSecrets.setJavaCorbaAccess(new sun.corba.JavaCorbaAccess() {
            public ValueHandlerImpl newValueHandlerImpl() {
                return ValueHandlerImpl.getInstance();
            }
        });
    }

D
duke 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    public static String getSignature(ValueMember member)
        throws ClassNotFoundException {

        // REVISIT.  Can the type be something that is
        // non-primitive yet not a value_box, value, or objref?
        // If so, should use ObjectStreamClass or throw
        // exception.

        if (member.type.kind().value() == TCKind._tk_value_box ||
            member.type.kind().value() == TCKind._tk_value ||
            member.type.kind().value() == TCKind._tk_objref) {
            Class c = RepositoryId.cache.getId(member.id).getClassFromType();
            return ObjectStreamClass.getSignature(c);

        } else {

            return primitiveConstants[member.type.kind().value()];
        }

    }

    public static FullValueDescription translate(ORB orb, ObjectStreamClass osc, ValueHandler vh){

        // Create FullValueDescription
        FullValueDescription result = new FullValueDescription();
        Class className = osc.forClass();

        ValueHandlerImpl vhandler = (com.sun.corba.se.impl.io.ValueHandlerImpl) vh;
        String repId = vhandler.createForAnyType(className);

        // Set FVD name
        result.name = vhandler.getUnqualifiedName(repId);
        if (result.name == null)
            result.name = "";

        // Set FVD id _REVISIT_ : Manglings
        result.id = vhandler.getRMIRepositoryID(className);
        if (result.id == null)
            result.id = "";

        // Set FVD is_abstract
        result.is_abstract = ObjectStreamClassCorbaExt.isAbstractInterface(className);

        // Set FVD is_custom
        result.is_custom = osc.hasWriteObject() || osc.isExternalizable();

        // Set FVD defined_in _REVISIT_ : Manglings
        result.defined_in = vhandler.getDefinedInId(repId);
        if (result.defined_in == null)
            result.defined_in = "";

        // Set FVD version
        result.version = vhandler.getSerialVersionUID(repId);
        if (result.version == null)
            result.version = "";

        // Skip FVD operations - N/A
        result.operations = new OperationDescription[0];

        // Skip FVD attributed - N/A
        result.attributes = new AttributeDescription[0];

        // Set FVD members
        // Maps classes to repositoryIDs strings. This is used to detect recursive types.
        IdentityKeyValueStack createdIDs = new IdentityKeyValueStack();
        // Stores all types created for resolving indirect types at the end.
        result.members = translateMembers(orb, osc, vh, createdIDs);

        // Skip FVD initializers - N/A
        result.initializers = new Initializer[0];

        Class interfaces[] = osc.forClass().getInterfaces();
        int abstractCount = 0;

        // Skip FVD supported_interfaces
        result.supported_interfaces =  new String[interfaces.length];
        for (int interfaceIndex = 0; interfaceIndex < interfaces.length;
             interfaceIndex++) {
            result.supported_interfaces[interfaceIndex] =
                vhandler.createForAnyType(interfaces[interfaceIndex]);

            if ((!(java.rmi.Remote.class.isAssignableFrom(interfaces[interfaceIndex]))) ||
                (!Modifier.isPublic(interfaces[interfaceIndex].getModifiers())))
                abstractCount++;
        }

        // Skip FVD abstract_base_values - N/A
        result.abstract_base_values = new String[abstractCount];
        for (int interfaceIndex = 0; interfaceIndex < interfaces.length;
             interfaceIndex++) {
            if ((!(java.rmi.Remote.class.isAssignableFrom(interfaces[interfaceIndex]))) ||
                (!Modifier.isPublic(interfaces[interfaceIndex].getModifiers())))
                result.abstract_base_values[interfaceIndex] =
                    vhandler.createForAnyType(interfaces[interfaceIndex]);

        }

        result.is_truncatable = false;

        // Set FVD base_value
        Class superClass = osc.forClass().getSuperclass();
        if (java.io.Serializable.class.isAssignableFrom(superClass))
            result.base_value = vhandler.getRMIRepositoryID(superClass);
        else
            result.base_value = "";

        // Set FVD type
        //result.type = createTypeCodeForClass(orb, osc.forClass());
        result.type = orb.get_primitive_tc(TCKind.tk_value); //11638

        return result;

    }

    private static ValueMember[] translateMembers (ORB orb,
                                                   ObjectStreamClass osc,
                                                   ValueHandler vh,
                                                   IdentityKeyValueStack createdIDs)
    {
        ValueHandlerImpl vhandler = (com.sun.corba.se.impl.io.ValueHandlerImpl) vh;
        ObjectStreamField fields[] = osc.getFields();
        int fieldsLength = fields.length;
        ValueMember[] members = new ValueMember[fieldsLength];
        // Note : fields come out of ObjectStreamClass in correct order for
        // writing.  So, we will create the same order in the members array.
        for (int i = 0; i < fieldsLength; i++) {
            String valRepId = vhandler.getRMIRepositoryID(fields[i].getClazz());
            members[i] = new ValueMember();
            members[i].name = fields[i].getName();
            members[i].id = valRepId; // _REVISIT_ : Manglings
            members[i].defined_in = vhandler.getDefinedInId(valRepId);// _REVISIT_ : Manglings
            members[i].version = "1.0";
            members[i].type_def = new _IDLTypeStub(); // _REVISIT_ : IDLType implementation missing

            if (fields[i].getField() == null) {
                // When using serialPersistentFields, the class may
                // no longer have an actual Field that corresponds
                // to one of the items.  The Java to IDL spec
                // ptc-00-01-06 1.3.5.6 says that the IDL field
                // should be private in this case.
                members[i].access = PRIVATE_MEMBER;
            } else {
                int m = fields[i].getField().getModifiers();
                if (Modifier.isPublic(m))
                    members[i].access = PUBLIC_MEMBER;
                else
                    members[i].access = PRIVATE_MEMBER;
            }

            switch (fields[i].getTypeCode()) {
            case 'B':
                members[i].type = orb.get_primitive_tc(TCKind.tk_octet); //11638
                break;
            case 'C':
                members[i].type
                    = orb.get_primitive_tc(vhandler.getJavaCharTCKind()); // 11638
                break;
            case 'F':
                members[i].type = orb.get_primitive_tc(TCKind.tk_float); //11638
                break;
            case 'D' :
                members[i].type = orb.get_primitive_tc(TCKind.tk_double); //11638
                break;
            case 'I':
                members[i].type = orb.get_primitive_tc(TCKind.tk_long); //11638
                break;
            case 'J':
                members[i].type = orb.get_primitive_tc(TCKind.tk_longlong); //11638
                break;
            case 'S':
                members[i].type = orb.get_primitive_tc(TCKind.tk_short); //11638
                break;
            case 'Z':
                members[i].type = orb.get_primitive_tc(TCKind.tk_boolean); //11638
                break;
        // case '[':
        //      members[i].type = orb.get_primitive_tc(TCKind.tk_value_box); //11638
        //      members[i].id = RepositoryId.createForAnyType(fields[i].getType());
        //      break;
            default:
                members[i].type = createTypeCodeForClassInternal(orb, fields[i].getClazz(), vhandler,
                                  createdIDs);
                members[i].id = vhandler.createForAnyType(fields[i].getType());
                break;
            } // end switch

        } // end for loop

        return members;
    }

    private static boolean exists(String str, String strs[]){
        for (int i = 0; i < strs.length; i++)
            if (str.equals(strs[i]))
                return true;

        return false;
    }

    public static boolean isAssignableFrom(String clzRepositoryId, FullValueDescription type,
                                           com.sun.org.omg.SendingContext.CodeBase sender){

        if (exists(clzRepositoryId, type.supported_interfaces))
            return true;

        if (clzRepositoryId.equals(type.id))
            return true;

        if ((type.base_value != null) &&
            (!type.base_value.equals(""))) {
            FullValueDescription parent = sender.meta(type.base_value);

            return isAssignableFrom(clzRepositoryId, parent, sender);
        }

        return false;

    }

    public static TypeCode createTypeCodeForClass (ORB orb, java.lang.Class c, ValueHandler vh) {
        // Maps classes to repositoryIDs strings. This is used to detect recursive types.
        IdentityKeyValueStack createdIDs = new IdentityKeyValueStack();
        // Stores all types created for resolving indirect types at the end.
        TypeCode tc = createTypeCodeForClassInternal(orb, c, vh, createdIDs);
        return tc;
    }

    private static TypeCode createTypeCodeForClassInternal (ORB orb,
                                                            java.lang.Class c,
                                                            ValueHandler vh,
                                                            IdentityKeyValueStack createdIDs)
    {
        // This wrapper method is the protection against infinite recursion.
        TypeCode tc = null;
        String id = (String)createdIDs.get(c);
        if (id != null) {
            return orb.create_recursive_tc(id);
        } else {
            id = vh.getRMIRepositoryID(c);
            if (id == null) id = "";
            // cache the rep id BEFORE creating a new typecode.
            // so that recursive tc can look up the rep id.
            createdIDs.push(c, id);
            tc = createTypeCodeInternal(orb, c, vh, id, createdIDs);
            createdIDs.pop();
            return tc;
        }
    }

    // Maintains a stack of key-value pairs. Compares elements using == operator.
    private static class IdentityKeyValueStack {
        private static class KeyValuePair {
            Object key;
            Object value;
            KeyValuePair(Object key, Object value) {
                this.key = key;
                this.value = value;
            }
            boolean equals(KeyValuePair pair) {
                return pair.key == this.key;
            }
        }

        Stack pairs = null;

        Object get(Object key) {
            if (pairs == null) {
                return null;
            }
            for (Iterator i = pairs.iterator(); i.hasNext();) {
                KeyValuePair pair = (KeyValuePair)i.next();
                if (pair.key == key) {
                    return pair.value;
                }
            }
            return null;
        }

        void push(Object key, Object value) {
            if (pairs == null) {
                pairs = new Stack();
            }
            pairs.push(new KeyValuePair(key, value));
        }

        void pop() {
            pairs.pop();
        }
    }

    private static TypeCode createTypeCodeInternal (ORB orb,
                                                    java.lang.Class c,
                                                    ValueHandler vh,
                                                    String id,
                                                    IdentityKeyValueStack createdIDs)
    {
        if ( c.isArray() ) {
            // Arrays - may recurse for multi-dimensional arrays
            Class componentClass = c.getComponentType();
            TypeCode embeddedType;
            if ( componentClass.isPrimitive() ){
                embeddedType
                    = ValueUtility.getPrimitiveTypeCodeForClass(orb,
                                                                componentClass,
                                                                vh);
            } else {
                embeddedType = createTypeCodeForClassInternal(orb, componentClass, vh,
                                                              createdIDs);
            }
            TypeCode t = orb.create_sequence_tc (0, embeddedType);
            return orb.create_value_box_tc (id, "Sequence", t);
        } else if ( c == java.lang.String.class ) {
            // Strings
            TypeCode t = orb.create_string_tc (0);
            return orb.create_value_box_tc (id, "StringValue", t);
        } else if (java.rmi.Remote.class.isAssignableFrom(c)) {
            return orb.get_primitive_tc(TCKind.tk_objref);
        } else if (org.omg.CORBA.Object.class.isAssignableFrom(c)) {
            return orb.get_primitive_tc(TCKind.tk_objref);
        }

        // Anything else

        ObjectStreamClass osc = ObjectStreamClass.lookup(c);

        if (osc == null) {
            return orb.create_value_box_tc (id, "Value", orb.get_primitive_tc (TCKind.tk_value));
        }

        // type modifier
        // REVISIT truncatable and abstract?
        short modifier = (osc.isCustomMarshaled() ? org.omg.CORBA.VM_CUSTOM.value : org.omg.CORBA.VM_NONE.value);

        // concrete base
        TypeCode base = null;
        Class superClass = c.getSuperclass();
        if (superClass != null && java.io.Serializable.class.isAssignableFrom(superClass)) {
            base = createTypeCodeForClassInternal(orb, superClass, vh, createdIDs);
        }

        // members
        ValueMember[] members = translateMembers (orb, osc, vh, createdIDs);

        return orb.create_value_tc(id, c.getName(), modifier, base, members);
    }

    public static TypeCode getPrimitiveTypeCodeForClass (ORB orb,
                                                         Class c,
                                                         ValueHandler vh) {

        if (c == Integer.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_long);
        } else if (c == Byte.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_octet);
        } else if (c == Long.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_longlong);
        } else if (c == Float.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_float);
        } else if (c == Double.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_double);
        } else if (c == Short.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_short);
        } else if (c == Character.TYPE) {
            return orb.get_primitive_tc (((ValueHandlerImpl)vh).getJavaCharTCKind());
        } else if (c == Boolean.TYPE) {
            return orb.get_primitive_tc (TCKind.tk_boolean);
        } else {
            // _REVISIT_ Not sure if this is right.
            return orb.get_primitive_tc (TCKind.tk_any);
        }
    }
}