1. /*
  2. * @(#)DynAnyCollectionImpl.java 1.8 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.dynamicany;
  8. import org.omg.CORBA.TypeCode;
  9. import org.omg.CORBA.Any;
  10. import org.omg.CORBA.NO_IMPLEMENT;
  11. import org.omg.CORBA.TypeCodePackage.BadKind;
  12. import org.omg.CORBA.TypeCodePackage.Bounds;
  13. import org.omg.DynamicAny.*;
  14. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  15. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  16. import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  17. import com.sun.corba.se.spi.orb.ORB ;
  18. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  19. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  20. abstract class DynAnyCollectionImpl extends DynAnyConstructedImpl
  21. {
  22. //
  23. // Instance variables
  24. //
  25. // Keep in sync with DynAny[] components at all times.
  26. Any[] anys = null;
  27. //
  28. // Constructors
  29. //
  30. private DynAnyCollectionImpl() {
  31. this(null, (Any)null, false);
  32. }
  33. protected DynAnyCollectionImpl(ORB orb, Any any, boolean copyValue) {
  34. super(orb, any, copyValue);
  35. }
  36. protected DynAnyCollectionImpl(ORB orb, TypeCode typeCode) {
  37. super(orb, typeCode);
  38. }
  39. //
  40. // Utility methods
  41. //
  42. protected void createDefaultComponentAt(int i, TypeCode contentType) {
  43. try {
  44. components[i] = DynAnyUtil.createMostDerivedDynAny(contentType, orb);
  45. } catch (InconsistentTypeCode itc) { // impossible
  46. }
  47. // get a hold of the default initialized Any without copying
  48. anys[i] = getAny(components[i]);
  49. }
  50. protected TypeCode getContentType() {
  51. try {
  52. return any.type().content_type();
  53. } catch (BadKind badKind) { // impossible
  54. return null;
  55. }
  56. }
  57. // This method has a different meaning for sequence and array:
  58. // For sequence value of 0 indicates an unbounded sequence,
  59. // values > 0 indicate a bounded sequence.
  60. // For array any value indicates the boundary.
  61. protected int getBound() {
  62. try {
  63. return any.type().length();
  64. } catch (BadKind badKind) { // impossible
  65. return 0;
  66. }
  67. }
  68. //
  69. // DynAny interface methods
  70. //
  71. // _REVISIT_ More efficient copy operation
  72. //
  73. // Collection methods
  74. //
  75. public org.omg.CORBA.Any[] get_elements () {
  76. if (status == STATUS_DESTROYED) {
  77. throw wrapper.dynAnyDestroyed() ;
  78. }
  79. return (checkInitComponents() ? anys : null);
  80. }
  81. protected abstract void checkValue(Object[] value)
  82. throws org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  83. // Initializes the elements of the ordered collection.
  84. // If value does not contain the same number of elements as the array dimension,
  85. // the operation raises InvalidValue.
  86. // If one or more elements have a type that is inconsistent with the collections TypeCode,
  87. // the operation raises TypeMismatch.
  88. // This operation does not change the current position.
  89. public void set_elements (org.omg.CORBA.Any[] value)
  90. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  91. org.omg.DynamicAny.DynAnyPackage.InvalidValue
  92. {
  93. if (status == STATUS_DESTROYED) {
  94. throw wrapper.dynAnyDestroyed() ;
  95. }
  96. checkValue(value);
  97. components = new DynAny[value.length];
  98. anys = value;
  99. // We know that this is of kind tk_sequence or tk_array
  100. TypeCode expectedTypeCode = getContentType();
  101. for (int i=0; i<value.length; i++) {
  102. if (value[i] != null) {
  103. if (! value[i].type().equal(expectedTypeCode)) {
  104. clearData();
  105. // _REVISIT_ More info
  106. throw new TypeMismatch();
  107. }
  108. try {
  109. // Creates the appropriate subtype without copying the Any
  110. components[i] = DynAnyUtil.createMostDerivedDynAny(value[i], orb, false);
  111. //System.out.println(this + " created component " + components[i]);
  112. } catch (InconsistentTypeCode itc) {
  113. throw new InvalidValue();
  114. }
  115. } else {
  116. clearData();
  117. // _REVISIT_ More info
  118. throw new InvalidValue();
  119. }
  120. }
  121. index = (value.length == 0 ? NO_INDEX : 0);
  122. // Other representations are invalidated by this operation
  123. representations = REPRESENTATION_COMPONENTS;
  124. }
  125. public org.omg.DynamicAny.DynAny[] get_elements_as_dyn_any () {
  126. if (status == STATUS_DESTROYED) {
  127. throw wrapper.dynAnyDestroyed() ;
  128. }
  129. return (checkInitComponents() ? components : null);
  130. }
  131. // Same semantics as set_elements(Any[])
  132. public void set_elements_as_dyn_any (org.omg.DynamicAny.DynAny[] value)
  133. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  134. org.omg.DynamicAny.DynAnyPackage.InvalidValue
  135. {
  136. if (status == STATUS_DESTROYED) {
  137. throw wrapper.dynAnyDestroyed() ;
  138. }
  139. checkValue(value);
  140. components = (value == null ? emptyComponents : value);
  141. anys = new Any[value.length];
  142. // We know that this is of kind tk_sequence or tk_array
  143. TypeCode expectedTypeCode = getContentType();
  144. for (int i=0; i<value.length; i++) {
  145. if (value[i] != null) {
  146. if (! value[i].type().equal(expectedTypeCode)) {
  147. clearData();
  148. // _REVISIT_ More info
  149. throw new TypeMismatch();
  150. }
  151. anys[i] = getAny(value[i]);
  152. } else {
  153. clearData();
  154. // _REVISIT_ More info
  155. throw new InvalidValue();
  156. }
  157. }
  158. index = (value.length == 0 ? NO_INDEX : 0);
  159. // Other representations are invalidated by this operation
  160. representations = REPRESENTATION_COMPONENTS;
  161. }
  162. }