1. /*
  2. * @(#)ObjectKeyTemplateBase.java 1.16 04/03/01
  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.ior;
  8. import java.util.Iterator ;
  9. import org.omg.CORBA_2_3.portable.InputStream ;
  10. import org.omg.CORBA_2_3.portable.OutputStream ;
  11. import com.sun.corba.se.spi.protocol.CorbaServerRequestDispatcher ;
  12. import com.sun.corba.se.spi.ior.ObjectId ;
  13. import com.sun.corba.se.spi.ior.ObjectAdapterId ;
  14. import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
  15. import com.sun.corba.se.spi.orb.ORB ;
  16. import com.sun.corba.se.spi.orb.ORBVersion ;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  18. import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
  19. import com.sun.corba.se.impl.logging.IORSystemException ;
  20. /**
  21. * @author
  22. */
  23. public abstract class ObjectKeyTemplateBase implements ObjectKeyTemplate
  24. {
  25. // Fixed constants for Java IDL object key template forms
  26. public static final String JIDL_ORB_ID = "" ;
  27. private static final String[] JIDL_OAID_STRINGS = { "TransientObjectAdapter" } ;
  28. public static final ObjectAdapterId JIDL_OAID = new ObjectAdapterIdArray( JIDL_OAID_STRINGS ) ;
  29. private ORB orb ;
  30. protected IORSystemException wrapper ;
  31. private ORBVersion version ;
  32. private int magic ;
  33. private int scid ;
  34. private int serverid ;
  35. private String orbid ;
  36. private ObjectAdapterId oaid ;
  37. private byte[] adapterId ;
  38. public byte[] getAdapterId()
  39. {
  40. return (byte[])(adapterId.clone()) ;
  41. }
  42. private byte[] computeAdapterId()
  43. {
  44. // write out serverid, orbid, oaid
  45. ByteBuffer buff = new ByteBuffer() ;
  46. buff.append( getServerId() ) ;
  47. buff.append( orbid ) ;
  48. buff.append( oaid.getNumLevels() ) ;
  49. Iterator iter = oaid.iterator() ;
  50. while (iter.hasNext()) {
  51. String comp = (String)(iter.next()) ;
  52. buff.append( comp ) ;
  53. }
  54. buff.trimToSize() ;
  55. return buff.toArray() ;
  56. }
  57. public ObjectKeyTemplateBase( ORB orb, int magic, int scid, int serverid,
  58. String orbid, ObjectAdapterId oaid )
  59. {
  60. this.orb = orb ;
  61. this.wrapper = IORSystemException.get( orb,
  62. CORBALogDomains.OA_IOR ) ;
  63. this.magic = magic ;
  64. this.scid = scid ;
  65. this.serverid = serverid ;
  66. this.orbid = orbid ;
  67. this.oaid = oaid ;
  68. adapterId = computeAdapterId() ;
  69. }
  70. public boolean equals( Object obj )
  71. {
  72. if (!(obj instanceof ObjectKeyTemplateBase))
  73. return false ;
  74. ObjectKeyTemplateBase other = (ObjectKeyTemplateBase)obj ;
  75. return (magic == other.magic) && (scid == other.scid) &&
  76. (serverid == other.serverid) && (version.equals( other.version ) &&
  77. orbid.equals( other.orbid ) && oaid.equals( other.oaid )) ;
  78. }
  79. public int hashCode()
  80. {
  81. int result = 17 ;
  82. result = 37*result + magic ;
  83. result = 37*result + scid ;
  84. result = 37*result + serverid ;
  85. result = 37*result + version.hashCode() ;
  86. result = 37*result + orbid.hashCode() ;
  87. result = 37*result + oaid.hashCode() ;
  88. return result ;
  89. }
  90. public int getSubcontractId()
  91. {
  92. return scid ;
  93. }
  94. public int getServerId()
  95. {
  96. return serverid ;
  97. }
  98. public String getORBId()
  99. {
  100. return orbid ;
  101. }
  102. public ObjectAdapterId getObjectAdapterId()
  103. {
  104. return oaid ;
  105. }
  106. public void write(ObjectId objectId, OutputStream os)
  107. {
  108. writeTemplate( os ) ;
  109. objectId.write( os ) ;
  110. }
  111. public void write( OutputStream os )
  112. {
  113. writeTemplate( os ) ;
  114. }
  115. abstract protected void writeTemplate( OutputStream os ) ;
  116. protected int getMagic()
  117. {
  118. return magic ;
  119. }
  120. // All subclasses should set the version in their constructors.
  121. // Public so it can be used in a white-box test.
  122. public void setORBVersion( ORBVersion version )
  123. {
  124. this.version = version ;
  125. }
  126. public ORBVersion getORBVersion()
  127. {
  128. return version ;
  129. }
  130. protected byte[] readObjectKey( InputStream is )
  131. {
  132. int len = is.read_long() ;
  133. byte[] result = new byte[len] ;
  134. is.read_octet_array( result, 0, len ) ;
  135. return result ;
  136. }
  137. public CorbaServerRequestDispatcher getServerRequestDispatcher( ORB orb, ObjectId id )
  138. {
  139. return orb.getRequestDispatcherRegistry().getServerRequestDispatcher( scid ) ;
  140. }
  141. }