1. /*
  2. * @(#)DynAnyImpl.java 1.10 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.DynamicAny;
  8. import org.omg.CORBA.ORB;
  9. import org.omg.CORBA.Any;
  10. import org.omg.CORBA.TypeCode;
  11. import org.omg.CORBA.TCKind;
  12. import org.omg.CORBA.LocalObject;
  13. import org.omg.CORBA.NO_IMPLEMENT;
  14. import org.omg.CORBA.OBJECT_NOT_EXIST;
  15. import org.omg.CORBA.ORBPackage.InvalidName;
  16. import org.omg.CORBA.portable.OutputStream;
  17. import org.omg.DynamicAny.*;
  18. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  19. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  20. abstract class DynAnyImpl extends org.omg.CORBA.LocalObject implements DynAny
  21. {
  22. protected static final int NO_INDEX = -1;
  23. // A DynAny is destroyable if it is the root of a DynAny hierarchy.
  24. protected static final byte STATUS_DESTROYABLE = 0;
  25. // A DynAny is undestroyable if it is a node in a DynAny hierarchy other than the root.
  26. protected static final byte STATUS_UNDESTROYABLE = 1;
  27. // A DynAny is destroyed if its root has been destroyed.
  28. protected static final byte STATUS_DESTROYED = 2;
  29. //
  30. // Instance variables
  31. //
  32. org.omg.CORBA.ORB orb = null;
  33. // An Any is used internally to implement the basic DynAny.
  34. // It stores the DynAnys TypeCode.
  35. // For primitive types it is the only representation.
  36. // For complex types it is the streamed representation.
  37. Any any = null;
  38. // Destroyable is the default status for free standing DynAnys.
  39. byte status = STATUS_DESTROYABLE;
  40. int index = NO_INDEX;
  41. //
  42. // Constructors
  43. //
  44. private DynAnyImpl() {
  45. }
  46. protected DynAnyImpl(ORB orb, Any any, boolean copyValue) {
  47. this.orb = orb;
  48. if (copyValue)
  49. this.any = DynAnyUtil.copy(any, orb);
  50. else
  51. this.any = any;
  52. // set the current position to 0 if any has components, otherwise to -1.
  53. index = NO_INDEX;
  54. }
  55. protected DynAnyImpl(ORB orb, TypeCode typeCode) {
  56. this.orb = orb;
  57. this.any = DynAnyUtil.createDefaultAnyOfType(typeCode, orb);
  58. }
  59. protected DynAnyFactory factory() {
  60. try {
  61. return (DynAnyFactory)orb.resolve_initial_references("DynAnyFactory");
  62. } catch (InvalidName in) {
  63. throw new RuntimeException("Unable to find DynAnyFactory");
  64. }
  65. }
  66. protected Any getAny() {
  67. return any;
  68. }
  69. // Uses getAny() if this is our implementation, otherwise uses to_any()
  70. // which copies the Any.
  71. protected Any getAny(DynAny dynAny) {
  72. if (dynAny instanceof DynAnyImpl)
  73. return ((DynAnyImpl)dynAny).getAny();
  74. else
  75. // _REVISIT_ Nothing we can do about copying at this point
  76. // if this is not our implementation of DynAny.
  77. // To prevent this we would need another representation,
  78. // one where component DynAnys are initialized but not the component Anys.
  79. return dynAny.to_any();
  80. }
  81. protected void writeAny(OutputStream out) {
  82. //System.out.println(this + " writeAny of type " + type().kind().value());
  83. any.write_value(out);
  84. }
  85. protected void setStatus(byte newStatus) {
  86. status = newStatus;
  87. }
  88. protected void clearData() {
  89. // This clears the data part of the Any while keeping the TypeCode info.
  90. any.type(any.type());
  91. }
  92. //
  93. // DynAny interface methods
  94. //
  95. public org.omg.CORBA.TypeCode type() {
  96. if (status == STATUS_DESTROYED) {
  97. throw new OBJECT_NOT_EXIST();
  98. }
  99. return any.type();
  100. }
  101. // Makes a copy of the Any value inside the parameter
  102. public void assign (org.omg.DynamicAny.DynAny dyn_any)
  103. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch
  104. {
  105. if (status == STATUS_DESTROYED) {
  106. throw new OBJECT_NOT_EXIST();
  107. }
  108. if ((any != null) && (! any.type().equal(dyn_any.type()))) {
  109. throw new TypeMismatch();
  110. }
  111. any = dyn_any.to_any();
  112. }
  113. // Makes a copy of the Any parameter
  114. public void from_any (org.omg.CORBA.Any value)
  115. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  116. org.omg.DynamicAny.DynAnyPackage.InvalidValue
  117. {
  118. if (status == STATUS_DESTROYED) {
  119. throw new OBJECT_NOT_EXIST();
  120. }
  121. if ((any != null) && (! any.type().equal(value.type()))) {
  122. throw new TypeMismatch();
  123. }
  124. // If the passed Any does not contain a legal value
  125. // (such as a null string), the operation raises InvalidValue.
  126. Any tempAny = null;
  127. try {
  128. tempAny = DynAnyUtil.copy(value, orb);
  129. } catch (Exception e) {
  130. throw new InvalidValue();
  131. }
  132. if ( ! DynAnyUtil.isInitialized(tempAny)) {
  133. throw new InvalidValue();
  134. }
  135. any = tempAny;
  136. }
  137. public abstract org.omg.CORBA.Any to_any();
  138. public abstract boolean equal (org.omg.DynamicAny.DynAny dyn_any);
  139. public abstract void destroy();
  140. public abstract org.omg.DynamicAny.DynAny copy();
  141. // Needed for org.omg.CORBA.Object
  142. private String[] __ids = { "IDL:omg.org/DynamicAny/DynAny:1.0" };
  143. public String[] _ids() {
  144. return __ids;
  145. }
  146. }