1. /*
  2. * @(#)IOR.java 1.28 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. //Source file: J:/ws/serveractivation/src/share/classes/com.sun.corba.se.internal.ior/IOR.java
  8. package com.sun.corba.se.internal.ior;
  9. import java.util.LinkedList ;
  10. import java.util.ListIterator ;
  11. import java.util.Iterator ;
  12. import java.io.*;
  13. import org.omg.CORBA.ORB;
  14. import org.omg.CORBA_2_3.portable.InputStream ;
  15. import org.omg.CORBA_2_3.portable.OutputStream ;
  16. import org.omg.CORBA.BAD_PARAM ;
  17. import org.omg.CORBA.INTERNAL;
  18. import org.omg.CORBA.CompletionStatus;
  19. import org.omg.IOP.TAG_INTERNET_IOP ;
  20. import com.sun.corba.se.internal.ior.Writeable ;
  21. import com.sun.corba.se.internal.ior.IdEncapsulationContainerBase ;
  22. import com.sun.corba.se.internal.ior.IIOPProfileTemplate ;
  23. import com.sun.corba.se.internal.ior.IIOPProfile ;
  24. import com.sun.corba.se.internal.ior.ObjectIds ;
  25. import com.sun.corba.se.internal.ior.ObjectId ;
  26. import com.sun.corba.se.internal.ior.IdEncapsulationFactory ;
  27. import com.sun.corba.se.internal.ior.TaggedProfileFactoryFinder ;
  28. import com.sun.corba.se.internal.core.MarshalOutputStream;
  29. import com.sun.corba.se.internal.corba.EncapsOutputStream;
  30. import com.sun.corba.se.internal.orbutil.HexOutputStream;
  31. import com.sun.corba.se.internal.orbutil.MinorCodes;
  32. /** An IOR is represented as a linked list of profiles.
  33. * Only objects that extend Profile should be added to an IOR.
  34. * However, enforcing this restriction requires overriding all
  35. * of the addXXX methods inherited from LinkedList, so no check
  36. * is included here.
  37. * @author Ken Cavanaugh
  38. */
  39. public class IOR extends IdEncapsulationContainerBase implements Writeable
  40. {
  41. // Constants used in getIORFromString
  42. public static final String STRINGIFY_PREFIX = "IOR:" ;
  43. protected static final int PREFIX_LENGTH = STRINGIFY_PREFIX.length() ;
  44. private String typeId;
  45. public boolean equals( Object obj )
  46. {
  47. if (obj == null)
  48. return false ;
  49. if (!(obj instanceof IOR))
  50. return false ;
  51. IOR other = (IOR)obj ;
  52. return super.equals( obj ) && typeId.equals( other.typeId ) ;
  53. }
  54. /** Construct an empty IOR. This is needed for null object references.
  55. */
  56. public IOR()
  57. {
  58. this( "" ) ;
  59. }
  60. public IOR( String typeId )
  61. {
  62. this.typeId = typeId ;
  63. }
  64. /** Construct an IOR from a template for a single IIOP profile and id
  65. * info. This is the common case today.
  66. */
  67. public IOR( String typeId, IIOPProfileTemplate template,
  68. ObjectId id )
  69. {
  70. this.typeId = typeId ;
  71. IIOPProfile profile = new IIOPProfile( id, template ) ;
  72. add( profile ) ;
  73. }
  74. /** Construct an IOR from template and id info. Note that this
  75. * IOR will only contain IIOP profiles, since that is all that we currently
  76. * support.
  77. * @param template
  78. * @param ids
  79. * @return
  80. * @exception
  81. * @author
  82. * The template and ids arguments to the IOR constructor must both have the
  83. * same number of elements.
  84. * @roseuid 3910984E01EF
  85. */
  86. public IOR( String typeId, IORTemplate template, ObjectIds ids)
  87. {
  88. this.typeId = typeId ;
  89. Iterator idIterator = ids.iterator() ;
  90. Iterator templateIterator = template.iterator() ;
  91. while (templateIterator.hasNext()) {
  92. TaggedProfileTemplate ptemp = (TaggedProfileTemplate)
  93. (templateIterator.next()) ;
  94. ObjectId oid = null ;
  95. if (idIterator.hasNext())
  96. oid = (ObjectId)(idIterator.next()) ;
  97. else
  98. throw new BAD_PARAM( "Too few ObjectIds in IOR constructor" ) ;
  99. TaggedProfile profile = ptemp.create( oid ) ;
  100. add( profile ) ;
  101. }
  102. if (idIterator.hasNext())
  103. throw new BAD_PARAM( "Too many ObjectIds in IOR constructor" ) ;
  104. }
  105. /** Construct an IOR from an IORTemplate by applying the same
  106. * object id to each IIOPProfileTemplate in the IORTemplate.
  107. */
  108. public IOR( String typeId, IORTemplate template, ObjectId id)
  109. {
  110. this.typeId = typeId ;
  111. Iterator templateIterator = template.iterator() ;
  112. while (templateIterator.hasNext()) {
  113. TaggedProfileTemplate ptemp =
  114. (TaggedProfileTemplate)(templateIterator.next()) ;
  115. TaggedProfile profile = ptemp.create( id ) ;
  116. add( profile ) ;
  117. }
  118. }
  119. /**
  120. * @param is
  121. * @return
  122. * @exception
  123. * @author
  124. * @roseuid 3910984E01FA
  125. */
  126. public IOR(InputStream is)
  127. {
  128. IdEncapsulationFactoryFinder finder =
  129. TaggedProfileFactoryFinder.getFinder() ;
  130. this.typeId = is.read_string() ;
  131. readIdEncapsulationSequence( finder, is ) ;
  132. }
  133. /**
  134. * @return String
  135. * @exception
  136. * @author
  137. * @roseuid 3910984E0204
  138. */
  139. public String getTypeId()
  140. {
  141. return typeId ;
  142. }
  143. /**
  144. * @param arg0
  145. * @return void
  146. * @exception
  147. * @author
  148. * @roseuid 3910984E0205
  149. */
  150. public void write(OutputStream os)
  151. {
  152. os.write_string( typeId ) ;
  153. writeIdEncapsulationSequence( os ) ;
  154. }
  155. public String stringify(com.sun.corba.se.internal.core.ORB orb)
  156. {
  157. StringWriter bs;
  158. MarshalOutputStream s = orb.newOutputStream();
  159. s.putEndian();
  160. write( (OutputStream)s );
  161. bs = new StringWriter();
  162. try {
  163. s.writeTo(new HexOutputStream(bs));
  164. } catch (IOException ex) {
  165. throw new INTERNAL( MinorCodes.STRINGIFY_WRITE_ERROR,
  166. CompletionStatus.COMPLETED_NO );
  167. }
  168. return com.sun.corba.se.internal.core.IOR.STRINGIFY_PREFIX + bs;
  169. }
  170. public void makeImmutable()
  171. {
  172. Iterator iter = iteratorById( TAG_INTERNET_IOP.value ) ;
  173. while (iter.hasNext()) {
  174. Object obj = iter.next() ;
  175. if (obj instanceof IIOPProfile) {
  176. IIOPProfile profile = (IIOPProfile)obj ;
  177. IIOPProfileTemplate temp = profile.getTemplate() ;
  178. temp.makeImmutable() ;
  179. }
  180. }
  181. super.makeImmutable() ;
  182. }
  183. public org.omg.IOP.IOR getIOPIOR(ORB orb) {
  184. EncapsOutputStream os = new EncapsOutputStream(orb);
  185. write(os);
  186. InputStream is = (InputStream) (os.create_input_stream());
  187. return org.omg.IOP.IORHelper.read(is);
  188. }
  189. }