/* * @(#)DynUnionImpl.java 1.8 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.sun.corba.se.impl.dynamicany; import org.omg.CORBA.TypeCode; import org.omg.CORBA.TCKind; import org.omg.CORBA.Any; import org.omg.CORBA.TypeCodePackage.BadKind; import org.omg.CORBA.TypeCodePackage.Bounds; import org.omg.CORBA.portable.InputStream; import org.omg.DynamicAny.*; import org.omg.DynamicAny.DynAnyPackage.TypeMismatch; import org.omg.DynamicAny.DynAnyPackage.InvalidValue; import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode; import com.sun.corba.se.spi.orb.ORB ; import com.sun.corba.se.spi.logging.CORBALogDomains ; import com.sun.corba.se.impl.logging.ORBUtilSystemException ; public class DynUnionImpl extends DynAnyConstructedImpl implements DynUnion { // // Instance variables // DynAny discriminator = null; // index either points to the discriminator or the named member is it exists. // The currently active member, which is of the same type as the discriminator. DynAny currentMember = null; int currentMemberIndex = NO_INDEX; // // Constructors // private DynUnionImpl() { this(null, (Any)null, false); } protected DynUnionImpl(ORB orb, Any any, boolean copyValue) { // We can be sure that typeCode is of kind tk_union super(orb, any, copyValue); } protected DynUnionImpl(ORB orb, TypeCode typeCode) { // We can be sure that typeCode is of kind tk_union super(orb, typeCode); } protected boolean initializeComponentsFromAny() { try { InputStream input = any.create_input_stream(); Any discriminatorAny = DynAnyUtil.extractAnyFromStream(discriminatorType(), input, orb); discriminator = DynAnyUtil.createMostDerivedDynAny(discriminatorAny, orb, false); currentMemberIndex = currentUnionMemberIndex(discriminatorAny); Any memberAny = DynAnyUtil.extractAnyFromStream(memberType(currentMemberIndex), input, orb); currentMember = DynAnyUtil.createMostDerivedDynAny(memberAny, orb, false); components = new DynAny[] {discriminator, currentMember}; } catch (InconsistentTypeCode ictc) { // impossible } return true; } // Sets the current position to zero. // The discriminator value is set to a value consistent with the first named member // of the union. That member is activated and (recursively) initialized to its default value. protected boolean initializeComponentsFromTypeCode() { //System.out.println(this + " initializeComponentsFromTypeCode"); try { // We can be sure that memberCount() > 0 according to the IDL language spec discriminator = DynAnyUtil.createMostDerivedDynAny(memberLabel(0), orb, false); index = 0; currentMemberIndex = 0; currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(0), orb); components = new DynAny[] {discriminator, currentMember}; } catch (InconsistentTypeCode ictc) { // impossible } return true; } // // Convenience methods // private TypeCode discriminatorType() { TypeCode discriminatorType = null; try { discriminatorType = any.type().discriminator_type(); } catch (BadKind bad) { } return discriminatorType; } private int memberCount() { int memberCount = 0; try { memberCount = any.type().member_count(); } catch (BadKind bad) { } return memberCount; } private Any memberLabel(int i) { Any memberLabel = null; try { memberLabel = any.type().member_label(i); } catch (BadKind bad) { } catch (Bounds bounds) { } return memberLabel; } private TypeCode memberType(int i) { TypeCode memberType = null; try { memberType = any.type().member_type(i); } catch (BadKind bad) { } catch (Bounds bounds) { } return memberType; } private String memberName(int i) { String memberName = null; try { memberName = any.type().member_name(i); } catch (BadKind bad) { } catch (Bounds bounds) { } return memberName; } private int defaultIndex() { int defaultIndex = -1; try { defaultIndex = any.type().default_index(); } catch (BadKind bad) { } return defaultIndex; } private int currentUnionMemberIndex(Any discriminatorValue) { int memberCount = memberCount(); Any memberLabel; for (int i=0; i