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