/* * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved. * 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 * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. * * 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. */ 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