1 7 8 package com.sun.corba.se.impl.dynamicany; 9 10 import org.omg.CORBA.TypeCode ; 11 import org.omg.CORBA.TCKind ; 12 import org.omg.CORBA.Any ; 13 import org.omg.CORBA.TypeCodePackage.BadKind ; 14 import org.omg.CORBA.TypeCodePackage.Bounds ; 15 import org.omg.CORBA.portable.InputStream ; 16 import org.omg.DynamicAny.*; 17 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch ; 18 import org.omg.DynamicAny.DynAnyPackage.InvalidValue ; 19 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode ; 20 21 import com.sun.corba.se.spi.orb.ORB ; 22 import com.sun.corba.se.spi.logging.CORBALogDomains ; 23 import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 24 25 public class DynUnionImpl extends DynAnyConstructedImpl implements DynUnion 26 { 27 31 DynAny discriminator = null; 32 DynAny currentMember = null; 35 int currentMemberIndex = NO_INDEX; 36 37 41 private DynUnionImpl() { 42 this(null, (Any )null, false); 43 } 44 45 protected DynUnionImpl(ORB orb, Any any, boolean copyValue) { 46 super(orb, any, copyValue); 48 } 49 50 protected DynUnionImpl(ORB orb, TypeCode typeCode) { 51 super(orb, typeCode); 53 } 54 55 protected boolean initializeComponentsFromAny() { 56 try { 57 InputStream input = any.create_input_stream(); 58 Any discriminatorAny = DynAnyUtil.extractAnyFromStream(discriminatorType(), input, orb); 59 discriminator = DynAnyUtil.createMostDerivedDynAny(discriminatorAny, orb, false); 60 currentMemberIndex = currentUnionMemberIndex(discriminatorAny); 61 Any memberAny = DynAnyUtil.extractAnyFromStream(memberType(currentMemberIndex), input, orb); 62 currentMember = DynAnyUtil.createMostDerivedDynAny(memberAny, orb, false); 63 components = new DynAny[] {discriminator, currentMember}; 64 } catch (InconsistentTypeCode ictc) { } 66 return true; 67 } 68 69 protected boolean initializeComponentsFromTypeCode() { 73 try { 75 discriminator = DynAnyUtil.createMostDerivedDynAny(memberLabel(0), orb, false); 77 index = 0; 78 currentMemberIndex = 0; 79 currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(0), orb); 80 components = new DynAny[] {discriminator, currentMember}; 81 } catch (InconsistentTypeCode ictc) { } 83 return true; 84 } 85 86 90 private TypeCode discriminatorType() { 91 TypeCode discriminatorType = null; 92 try { 93 discriminatorType = any.type().discriminator_type(); 94 } catch (BadKind bad) { 95 } 96 return discriminatorType; 97 } 98 99 private int memberCount() { 100 int memberCount = 0; 101 try { 102 memberCount = any.type().member_count(); 103 } catch (BadKind bad) { 104 } 105 return memberCount; 106 } 107 108 private Any memberLabel(int i) { 109 Any memberLabel = null; 110 try { 111 memberLabel = any.type().member_label(i); 112 } catch (BadKind bad) { 113 } catch (Bounds bounds) { 114 } 115 return memberLabel; 116 } 117 118 private TypeCode memberType(int i) { 119 TypeCode memberType = null; 120 try { 121 memberType = any.type().member_type(i); 122 } catch (BadKind bad) { 123 } catch (Bounds bounds) { 124 } 125 return memberType; 126 } 127 128 private String memberName(int i) { 129 String memberName = null; 130 try { 131 memberName = any.type().member_name(i); 132 } catch (BadKind bad) { 133 } catch (Bounds bounds) { 134 } 135 return memberName; 136 } 137 138 private int defaultIndex() { 139 int defaultIndex = -1; 140 try { 141 defaultIndex = any.type().default_index(); 142 } catch (BadKind bad) { 143 } 144 return defaultIndex; 145 } 146 147 private int currentUnionMemberIndex(Any discriminatorValue) { 148 int memberCount = memberCount(); 149 Any memberLabel; 150 for (int i=0; i<memberCount; i++) { 151 memberLabel = memberLabel(i); 152 if (memberLabel.equal(discriminatorValue)) { 153 return i; 154 } 155 } 156 if (defaultIndex() != -1) { 157 return defaultIndex(); 158 } 159 return NO_INDEX; 160 } 161 162 protected void clearData() { 163 super.clearData(); 164 discriminator = null; 165 currentMember.destroy(); 167 currentMember = null; 168 currentMemberIndex = NO_INDEX; 169 } 170 171 175 177 181 184 public org.omg.DynamicAny.DynAny get_discriminator () { 185 if (status == STATUS_DESTROYED) { 186 throw wrapper.dynAnyDestroyed() ; 187 } 188 return (checkInitComponents() ? discriminator : null); 189 } 190 191 public void set_discriminator (org.omg.DynamicAny.DynAny newDiscriminator) 208 throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch 209 { 210 if (status == STATUS_DESTROYED) { 211 throw wrapper.dynAnyDestroyed() ; 212 } 213 if ( ! newDiscriminator.type().equal(discriminatorType())) { 214 throw new TypeMismatch (); 215 } 216 newDiscriminator = DynAnyUtil.convertToNative(newDiscriminator, orb); 217 Any newDiscriminatorAny = getAny(newDiscriminator); 218 int newCurrentMemberIndex = currentUnionMemberIndex(newDiscriminatorAny); 219 if (newCurrentMemberIndex == NO_INDEX) { 220 clearData(); 221 index = 0; 222 } else { 223 checkInitComponents(); 225 if (currentMemberIndex == NO_INDEX || newCurrentMemberIndex != currentMemberIndex) { 226 clearData(); 227 index = 1; 228 currentMemberIndex = newCurrentMemberIndex; 229 try { 230 currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(currentMemberIndex), orb); 231 } catch (InconsistentTypeCode ictc) {} 232 discriminator = newDiscriminator; 233 components = new DynAny[] { discriminator, currentMember }; 234 representations = REPRESENTATION_COMPONENTS; 235 } 236 } 237 } 238 239 public void set_to_default_member () 245 throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch 246 { 247 if (status == STATUS_DESTROYED) { 248 throw wrapper.dynAnyDestroyed() ; 249 } 250 int defaultIndex = defaultIndex(); 251 if (defaultIndex == -1) { 252 throw new TypeMismatch (); 253 } 254 try { 255 clearData(); 256 index = 1; 257 currentMemberIndex = defaultIndex; 258 currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(defaultIndex), orb); 259 components = new DynAny[] {discriminator, currentMember}; 260 Any discriminatorAny = orb.create_any(); 261 discriminatorAny.insert_octet((byte)0); 262 discriminator = DynAnyUtil.createMostDerivedDynAny(discriminatorAny, orb, false); 263 representations = REPRESENTATION_COMPONENTS; 264 } catch (InconsistentTypeCode ictc) {} 265 } 266 267 public void set_to_no_active_member () 274 throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch 275 { 276 if (status == STATUS_DESTROYED) { 277 throw wrapper.dynAnyDestroyed() ; 278 } 279 if (defaultIndex() != -1) { 281 throw new TypeMismatch (); 282 } 283 checkInitComponents(); 284 Any discriminatorAny = getAny(discriminator); 285 discriminatorAny.type(discriminatorAny.type()); 288 index = 0; 289 currentMemberIndex = NO_INDEX; 290 currentMember.destroy(); 292 currentMember = null; 293 components[0] = discriminator; 294 representations = REPRESENTATION_COMPONENTS; 295 } 296 297 public boolean has_no_active_member () { 304 if (status == STATUS_DESTROYED) { 305 throw wrapper.dynAnyDestroyed() ; 306 } 307 if (defaultIndex() != -1) { 309 return false; 310 } 311 checkInitComponents(); 312 return (checkInitComponents() ? (currentMemberIndex == NO_INDEX) : false); 313 } 314 315 public org.omg.CORBA.TCKind discriminator_kind () { 316 if (status == STATUS_DESTROYED) { 317 throw wrapper.dynAnyDestroyed() ; 318 } 319 return discriminatorType().kind(); 320 } 321 322 public org.omg.DynamicAny.DynAny member () 329 throws org.omg.DynamicAny.DynAnyPackage.InvalidValue 330 { 331 if (status == STATUS_DESTROYED) { 332 throw wrapper.dynAnyDestroyed() ; 333 } 334 if ( ! checkInitComponents() || currentMemberIndex == NO_INDEX) 335 throw new InvalidValue (); 336 return currentMember; 337 } 338 339 public String member_name () 344 throws org.omg.DynamicAny.DynAnyPackage.InvalidValue 345 { 346 if (status == STATUS_DESTROYED) { 347 throw wrapper.dynAnyDestroyed() ; 348 } 349 if ( ! checkInitComponents() || currentMemberIndex == NO_INDEX) 350 throw new InvalidValue (); 351 String memberName = memberName(currentMemberIndex); 352 return (memberName == null ? "" : memberName); 353 } 354 355 public org.omg.CORBA.TCKind member_kind () 358 throws org.omg.DynamicAny.DynAnyPackage.InvalidValue 359 { 360 if (status == STATUS_DESTROYED) { 361 throw wrapper.dynAnyDestroyed() ; 362 } 363 if ( ! checkInitComponents() || currentMemberIndex == NO_INDEX) 364 throw new InvalidValue (); 365 return memberType(currentMemberIndex).kind(); 366 } 367 } 368 | Popular Tags |